Skip to content

Commit

Permalink
feat: Separate start and init tracking
Browse files Browse the repository at this point in the history
We used to rely on a single `startTracking` method that was both
initializating the tracking, and started it. Furthermore, the event
handlers were made outside of this function.
This seems to be an anti-pattern, and we suspect that it was causing
tracking failures: we noticed several cases of missing
motion events, which might be the result of a wrong init.
Now, the init is done at each app start, and the tracking is actually
started when we detect the state is not enabled, just like before.
  • Loading branch information
paultranvan committed Feb 26, 2024
1 parent 4a0a7d1 commit 7c913db
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
5 changes: 4 additions & 1 deletion src/app/domain/geolocation/hooks/useGeolocationTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { useClient } from 'cozy-client'

import {
isGeolocationTrackingEnabled,
checkShouldStartTracking
checkShouldStartTracking,
initGeolocationTracking
} from '/app/domain/geolocation/services/tracking'
import { checkGeolocationQuota } from '/app/domain/geolocation/helpers/quota'
import { fetchAndStoreWebhook } from '/app/domain/geolocation/helpers/webhook'
Expand All @@ -16,6 +17,8 @@ export const useGeolocationTracking = (): void => {
const initializeTracking = async (): Promise<void> => {
if (!client) return

// The tracking must be init and enabled to be functional
await initGeolocationTracking()
const trackingEnabled = await isGeolocationTrackingEnabled()

if (trackingEnabled) {
Expand Down
12 changes: 9 additions & 3 deletions src/app/domain/geolocation/services/tracking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import BackgroundGeolocation from 'react-native-background-geolocation'
import BackgroundGeolocation, {
State
} from 'react-native-background-geolocation'

import {
startTracking,
Expand All @@ -8,7 +10,8 @@ import {
getId,
updateId,
stopTrackingAndClearData,
getShouldStartTracking
getShouldStartTracking,
initTracking
} from '/app/domain/geolocation/tracking'
import { isGeolocationQuotaExceeded } from '/app/domain/geolocation/helpers/quota'

Expand Down Expand Up @@ -39,6 +42,10 @@ export const setGeolocationTracking = async (
}
}

export const initGeolocationTracking = async (): Promise<State | null> => {
return await initTracking()
}

export const sendGeolocationTrackingLogs = async (
client?: CozyClient
): Promise<void> => {
Expand All @@ -56,7 +63,6 @@ interface GeolocationTrackingStatus {

export const isGeolocationTrackingEnabled = async (): Promise<boolean> => {
const status = await BackgroundGeolocation.getState()

return status.enabled
}

Expand Down
56 changes: 38 additions & 18 deletions src/app/domain/geolocation/tracking/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,26 @@ export {

const log = Minilog('📍 Geolocation')

export const startTracking = async () => {
export const initTracking = async () => {
try {
const trackingConfig = await getTrackingConfig()
Log('Config : ' + JSON.stringify(trackingConfig))

// Register on activity change
BackgroundGeolocation.onActivityChange(async event => {
return handleActivityChange(event)
})

// Register on motion change
BackgroundGeolocation.onMotionChange(async event => {
return handleMotionChange(event)
})

// Register on conectivty change
BackgroundGeolocation.onConnectivityChange(async event => {
return handleConnectivityChange(event)
})

const state = await BackgroundGeolocation.ready({
// Geolocation Config
desiredAccuracy: trackingConfig.desiredAccuracy || ACCURACY,
Expand Down Expand Up @@ -80,20 +95,39 @@ export const startTracking = async () => {
smallIcon: 'mipmap/ic_stat_ic_notification'
}
})
if (!state.enabled) {
Log('Init with state : ', JSON.stringify(state))
return state
} catch (e) {
Log('Error on tracking init : ', JSON.stringify(e))
log.error(e)

return null
}
}

export const startTracking = async () => {
try {
const state = await BackgroundGeolocation.getState()
if (!state) {
// This case should not happen
log.error('Tracking started without init')
await initTracking()
await BackgroundGeolocation.start()
Log('Tracking init and started')
} else if (!state.enabled) {
await BackgroundGeolocation.start()
Log('Tracking started')
} else {
Log('Tracking already started')
}
await storeData(
CozyPersistedStorageKeys.ShouldBeTrackingFlagStorageAdress,
true
)

return true
} catch (e) {
Log('Error on tracking start : ', JSON.stringify(e))
log.error(e)

return false
}
}
Expand Down Expand Up @@ -259,17 +293,3 @@ export const startOpenPathUploadAndPipeline = async ({
}
return
}

// Register on activity change
BackgroundGeolocation.onActivityChange(async event => {
return handleActivityChange(event)
})

// Register on motion change
BackgroundGeolocation.onMotionChange(async event => {
return handleMotionChange(event)
})

BackgroundGeolocation.onConnectivityChange(async event => {
return handleConnectivityChange(event)
})

0 comments on commit 7c913db

Please sign in to comment.