From 1a0f46aa9b0446ff29209eedcd5e287ff7cb142d Mon Sep 17 00:00:00 2001 From: Tsubasa SEKIGUCHI Date: Tue, 6 Aug 2024 00:36:50 +0900 Subject: [PATCH] =?UTF-8?q?TaskManager.defineTask=E3=82=92Main=E7=94=BB?= =?UTF-8?q?=E9=9D=A2=E3=81=AB=E6=88=BB=E3=81=97&Zustand=E3=81=AE=E3=82=A2?= =?UTF-8?q?=E3=82=AF=E3=82=B7=E3=83=A7=E3=83=B3=E3=81=A7=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 4 +++ src/hooks/useLocationStore.ts | 15 +++++++++ .../useStartBackgroundLocationUpdates.ts | 3 +- src/index.tsx | 31 ------------------- src/screens/Main.tsx | 21 +++++++++++++ 5 files changed, 42 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index 15d4e6816..9f1ebf53e 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,11 @@ import 'fast-text-encoding' import { registerRootComponent } from 'expo' +import * as TaskManager from 'expo-task-manager' import App from './src' +import { locationTaskName } from './src/utils/locationTaskName' + +TaskManager.unregisterTaskAsync(locationTaskName).catch(console.debug) // registerRootComponent calls AppRegistry.registerComponent('main', () => App); // It also ensures that whether you load the app in the Expo client or in a native build, diff --git a/src/hooks/useLocationStore.ts b/src/hooks/useLocationStore.ts index 0c6acfc61..1940b050d 100644 --- a/src/hooks/useLocationStore.ts +++ b/src/hooks/useLocationStore.ts @@ -2,3 +2,18 @@ import { LocationObject } from 'expo-location' import { create } from 'zustand' export const useLocationStore = create(() => null) + +export const setLocation = (location: LocationObject) => + useLocationStore.setState((state) => { + if (!state) { + return location + } + + const { latitude: inputLat, longitude: inputLon } = location.coords + const { latitude: stateLat, longitude: stateLon } = state.coords + if (inputLat === stateLat && inputLon === stateLon) { + return state + } + + return location + }) diff --git a/src/hooks/useStartBackgroundLocationUpdates.ts b/src/hooks/useStartBackgroundLocationUpdates.ts index 483a7f98e..c46717fba 100644 --- a/src/hooks/useStartBackgroundLocationUpdates.ts +++ b/src/hooks/useStartBackgroundLocationUpdates.ts @@ -1,4 +1,5 @@ import * as Location from 'expo-location' +import * as TaskManager from 'expo-task-manager' import { useEffect } from 'react' import { useRecoilValue } from 'recoil' import { autoModeEnabledSelector } from '../store/selectors/autoMode' @@ -23,7 +24,7 @@ export const useStartBackgroundLocationUpdates = () => { }) return () => { - Location.stopLocationUpdatesAsync(locationTaskName) + TaskManager.unregisterTaskAsync(locationTaskName) } // eslint-disable-next-line react-hooks/exhaustive-deps }, []) diff --git a/src/index.tsx b/src/index.tsx index 8097c55fb..6d40ab2f7 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -3,7 +3,6 @@ import remoteConfig from '@react-native-firebase/remote-config' import { NavigationContainer } from '@react-navigation/native' import { createStackNavigator } from '@react-navigation/stack' import * as Location from 'expo-location' -import * as TaskManager from 'expo-task-manager' import React, { ErrorInfo, useCallback, useEffect, useState } from 'react' import { ErrorBoundary } from 'react-error-boundary' import { ActivityIndicator, StatusBar, StyleSheet, Text } from 'react-native' @@ -12,41 +11,11 @@ import ErrorFallback from './components/ErrorBoundary' import FakeStationSettings from './components/FakeStationSettings' import TuningSettings from './components/TuningSettings' import useAnonymousUser from './hooks/useAnonymousUser' -import { useLocationStore } from './hooks/useLocationStore' import useReport from './hooks/useReport' import PrivacyScreen from './screens/Privacy' import SavedRoutesScreen from './screens/SavedRoutesScreen' import MainStack from './stacks/MainStack' import { setI18nConfig } from './translation' -import { locationTaskName } from './utils/locationTaskName' - -TaskManager.defineTask( - locationTaskName, - ({ - data, - error, - }: { - data: { locations: Location.LocationObject[] } - error: TaskManager.TaskManagerError | null - }) => { - if (error) { - console.error(error) - return - } - - const state = useLocationStore.getState() - const stateLat = state?.coords.latitude - const stateLon = state?.coords.longitude - - if ( - stateLat !== data.locations[0]?.coords.latitude && - stateLon !== data.locations[0]?.coords.longitude - ) { - useLocationStore.setState(data.locations[0]) - } - } -) -TaskManager.unregisterTaskAsync(locationTaskName) const Stack = createStackNavigator() diff --git a/src/screens/Main.tsx b/src/screens/Main.tsx index 2c7cc5606..9747299db 100644 --- a/src/screens/Main.tsx +++ b/src/screens/Main.tsx @@ -2,6 +2,8 @@ import AsyncStorage from '@react-native-async-storage/async-storage' import { useNavigation } from '@react-navigation/native' import { useKeepAwake } from 'expo-keep-awake' import * as Linking from 'expo-linking' +import { LocationObject } from 'expo-location' +import * as TaskManager from 'expo-task-manager' import React, { useCallback, useEffect, useMemo, useRef } from 'react' import { Alert, @@ -22,6 +24,7 @@ import { ASYNC_STORAGE_KEYS } from '../constants' import useAutoMode from '../hooks/useAutoMode' import { useCurrentLine } from '../hooks/useCurrentLine' import { useCurrentStation } from '../hooks/useCurrentStation' +import { setLocation } from '../hooks/useLocationStore' import { useLoopLine } from '../hooks/useLoopLine' import { useNextStation } from '../hooks/useNextStation' import useRefreshLeftStations from '../hooks/useRefreshLeftStations' @@ -42,6 +45,24 @@ import { translate } from '../translation' import getCurrentStationIndex from '../utils/currentStationIndex' import { getIsHoliday } from '../utils/isHoliday' import getIsPass from '../utils/isPass' +import { locationTaskName } from '../utils/locationTaskName' + +TaskManager.defineTask( + locationTaskName, + ({ + data, + error, + }: { + data: { locations: LocationObject[] } + error: TaskManager.TaskManagerError | null + }) => { + if (error) { + console.error(error) + return + } + setLocation(data.locations[0]) + } +) const { height: windowHeight } = Dimensions.get('window')