diff --git a/index.js b/index.js index 065fd5890..83528c277 100644 --- a/index.js +++ b/index.js @@ -1,32 +1,41 @@ import 'fast-text-encoding' +import dayjs from 'dayjs' import { registerRootComponent } from 'expo' +import * as Location from 'expo-location' import * as TaskManager from 'expo-task-manager' import App from './src' import { useLocationStore } from './src/hooks/useLocationStore' import { locationTaskName } from './src/utils/locationTaskName' -;(async () => { - await TaskManager.unregisterAllTasksAsync() - TaskManager.defineTask(locationTaskName, ({ data, error }) => { - if (error) { - console.error(error) - return - } +TaskManager.defineTask(locationTaskName, ({ data, error }) => { + if (error) { + console.error(error) + return + } + + const curLocation = useLocationStore.getState().location + if ( + curLocation?.coords.latitude === data.locations[0]?.coords.latitude && + curLocation?.coords.longitude === data.locations[0]?.coords.longitude + ) { + return + } - const curLocation = useLocationStore.getState().location - if ( - curLocation?.coords.latitude === data.locations[0]?.coords.latitude && - curLocation?.coords.longitude === data.locations[0]?.coords.longitude - ) { - return - } + const now = dayjs() + const stateTimestamp = curLocation && dayjs(curLocation.timestamp) + if (stateTimestamp && now.diff(stateTimestamp, 'seconds') < 1) { + return + } - useLocationStore.setState((state) => ({ - ...state, - location: data.locations[0], - })) - }) + useLocationStore.setState((state) => ({ + ...state, + location: data.locations[0], + })) +}) +;(async () => { + await TaskManager.unregisterAllTasksAsync() + await Location.stopLocationUpdatesAsync(locationTaskName) })() // registerRootComponent calls AppRegistry.registerComponent('main', () => App); diff --git a/src/constants/location.ts b/src/constants/location.ts index 3c5e4b76e..ce88527b0 100644 --- a/src/constants/location.ts +++ b/src/constants/location.ts @@ -3,6 +3,7 @@ export const LOCATION_TASK_NAME_CANARY = 'trainlcd-canary-background-location-task' // デフォルトの端末上の駅直線距離計算誤差(meter) // 位置情報の処理間隔(meter) -export const DISTANCE_FILTER_LOW = 100 -export const DISTANCE_FILTER_BALANCED = 10 -export const DISTANCE_FILTER_HIGH = 1 + +export const DISTANCE_FILTER_LOW = 300 +export const DISTANCE_FILTER_BALANCED = 150 +export const DISTANCE_FILTER_HIGH = 100 diff --git a/src/hooks/useStartBackgroundLocationUpdates.ts b/src/hooks/useStartBackgroundLocationUpdates.ts index 0121027dd..3c229b1aa 100644 --- a/src/hooks/useStartBackgroundLocationUpdates.ts +++ b/src/hooks/useStartBackgroundLocationUpdates.ts @@ -23,7 +23,6 @@ export const useStartBackgroundLocationUpdates = () => { accuracy: locationServiceAccuracy, activityType: Location.LocationActivityType.OtherNavigation, deferredUpdatesDistance: locationServiceDistanceFilter, - distanceInterval: 1, foregroundService: { notificationTitle: translate('bgAlertTitle'), notificationBody: translate('bgAlertContent'),