Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

位置情報取得処理改善 #3656

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ 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)
TaskManager.unregisterAllTasksAsync().catch(console.error)

// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
// It also ensures that whether you load the app in the Expo client or in a native build,
Expand Down
9 changes: 7 additions & 2 deletions src/components/Permitted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import RNFS from 'react-native-fs'
import { LongPressGestureHandler, State } from 'react-native-gesture-handler'
import Share from 'react-native-share'
import ViewShot from 'react-native-view-shot'
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'
import { useRecoilValue, useSetRecoilState } from 'recoil'
import {
ALL_AVAILABLE_LANGUAGES,
ASYNC_STORAGE_KEYS,
Expand All @@ -18,6 +18,7 @@ import {
} from '../constants'
import useAndroidWearable from '../hooks/useAndroidWearable'
import useAppleWatch from '../hooks/useAppleWatch'
import { useApplicationFlagStore } from '../hooks/useApplicationFlagStore'
import { useBadAccuracy } from '../hooks/useBadAccuracy'
import useCachedInitAnonymousUser from '../hooks/useCachedAnonymousUser'
import useCheckStoreVersion from '../hooks/useCheckStoreVersion'
Expand Down Expand Up @@ -69,14 +70,18 @@ const PermittedLayout: React.FC<Props> = ({ children }: Props) => {
const [longPressNoticeDismissed, setLongPressNoticeDismissed] = useState(true)

const { selectedBound } = useRecoilValue(stationState)
const [{ autoModeEnabled }, setNavigation] = useRecoilState(navigationState)
const setNavigation = useSetRecoilState(navigationState)
const setSpeech = useSetRecoilState(speechState)
const [reportModalShow, setReportModalShow] = useState(false)
const [sendingReport, setSendingReport] = useState(false)
const [reportDescription, setReportDescription] = useState('')
const [screenShotBase64, setScreenShotBase64] = useState('')
const [screenshotTaken, setScreenshotTaken] = useState(false)

const autoModeEnabled = useApplicationFlagStore(
(state) => state.autoModeEnabled
)

useCheckStoreVersion()
useAppleWatch()
useAndroidWearable()
Expand Down
6 changes: 0 additions & 6 deletions src/constants/location.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export const LOCATION_TASK_NAME_PROD = 'trainlcd-background-location-task'
export const LOCATION_TASK_NAME_CANARY =
'trainlcd-canary-background-location-task'
// デフォルトの端末上の駅直線距離計算誤差(meter)
// 位置情報の処理間隔(meter)

export const DISTANCE_FILTER_LOW = 300
export const DISTANCE_FILTER_BALANCED = 150
export const DISTANCE_FILTER_HIGH = 100
1 change: 0 additions & 1 deletion src/constants/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ export const RECOIL_STATES = {

// selectors
isEnSelector: 'isEnSelector',
autoModeEnabledSelector: 'autoModeEnabledSelector',
}
12 changes: 12 additions & 0 deletions src/hooks/useApplicationFlagStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { create } from 'zustand'

type State = {
autoModeEnabled: boolean
toggleAutoModeEnabled: () => void
}

export const useApplicationFlagStore = create<State>((set) => ({
autoModeEnabled: false,
toggleAutoModeEnabled: () =>
set((state) => ({ autoModeEnabled: !state.autoModeEnabled })),
}))
14 changes: 1 addition & 13 deletions src/hooks/useLocationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,4 @@ import { create } from 'zustand'
export const useLocationStore = create<LocationObject | null>(() => 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
})
useLocationStore.setState(location, true)
10 changes: 3 additions & 7 deletions src/hooks/useStartBackgroundLocationUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
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'
import { translate } from '../translation'
import { locationTaskName } from '../utils/locationTaskName'
import { useApplicationFlagStore } from './useApplicationFlagStore'

export const useStartBackgroundLocationUpdates = () => {
const autoModeEnabled = useRecoilValue(autoModeEnabledSelector)

useEffect(() => {
const autoModeEnabled = useApplicationFlagStore.getState()?.autoModeEnabled
if (autoModeEnabled) {
return
}
Expand All @@ -24,8 +21,7 @@ export const useStartBackgroundLocationUpdates = () => {
})

return () => {
TaskManager.unregisterTaskAsync(locationTaskName)
Location.stopLocationUpdatesAsync(locationTaskName).catch(console.debug)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
}
8 changes: 7 additions & 1 deletion src/screens/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Transfers from '../components/Transfers'
import TransfersYamanote from '../components/TransfersYamanote'
import TypeChangeNotify from '../components/TypeChangeNotify'
import { ASYNC_STORAGE_KEYS } from '../constants'
import { useApplicationFlagStore } from '../hooks/useApplicationFlagStore'
import useAutoMode from '../hooks/useAutoMode'
import { useCurrentLine } from '../hooks/useCurrentLine'
import { useCurrentStation } from '../hooks/useCurrentStation'
Expand Down Expand Up @@ -60,6 +61,7 @@ TaskManager.defineTask(
console.error(error)
return
}

setLocation(data.locations[0])
}
)
Expand All @@ -77,11 +79,15 @@ const MainScreen: React.FC = () => {
const isLEDTheme = theme === APP_THEME.LED

const { stations, selectedDirection, arrived } = useRecoilValue(stationState)
const [{ leftStations, bottomState, autoModeEnabled }, setNavigation] =
const [{ leftStations, bottomState }, setNavigation] =
useRecoilState(navigationState)
const currentLine = useCurrentLine()
const currentStation = useCurrentStation()

const autoModeEnabled = useApplicationFlagStore(
(state) => state.autoModeEnabled
)

const nextStation = useNextStation()
useAutoMode(autoModeEnabled)
const { isYamanoteLine, isOsakaLoopLine, isMeijoLine } = useLoopLine()
Expand Down
25 changes: 11 additions & 14 deletions src/screens/SelectBound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Heading from '../components/Heading'
import Typography from '../components/Typography'
import { TOEI_OEDO_LINE_ID } from '../constants'
import { TOEI_OEDO_LINE_TOCHOMAE_STATION_ID } from '../constants/station'
import { useApplicationFlagStore } from '../hooks/useApplicationFlagStore'
import useBounds from '../hooks/useBounds'
import { useLoopLine } from '../hooks/useLoopLine'
import { useStationList } from '../hooks/useStationList'
Expand Down Expand Up @@ -73,10 +74,8 @@ const SelectBoundScreen: React.FC = () => {
const navigation = useNavigation()
const [{ station, stations, wantedDestination }, setStationState] =
useRecoilState(stationState)
const [
{ trainType, fetchedTrainTypes, autoModeEnabled, fromBuilder },
setNavigationState,
] = useRecoilState(navigationState)
const [{ trainType, fetchedTrainTypes, fromBuilder }, setNavigationState] =
useRecoilState(navigationState)
const [{ selectedLine }, setLineState] = useRecoilState(lineState)

const { loading, error, mutateStations } = useStationList()
Expand All @@ -85,6 +84,13 @@ const SelectBoundScreen: React.FC = () => {
bounds: [inboundStations, outboundStations],
} = useBounds()

const autoModeEnabled = useApplicationFlagStore(
(state) => state.autoModeEnabled
)
const toggleAutoModeEnabled = useApplicationFlagStore(
(state) => state.toggleAutoModeEnabled
)

// 種別選択ボタンを表示するかのフラグ
const withTrainTypes = useMemo(
(): boolean => fetchedTrainTypes.length > 1,
Expand Down Expand Up @@ -141,13 +147,6 @@ const SelectBoundScreen: React.FC = () => {
navigation.navigate('TrainType')
}

const handleAutoModeButtonPress = useCallback(async () => {
setNavigationState((prev) => ({
...prev,
autoModeEnabled: !prev.autoModeEnabled,
}))
}, [setNavigationState])

const handleAllStopsButtonPress = useCallback(() => {
const stopStations = stations.filter(
(s) => s.stopCondition !== StopCondition.Not
Expand Down Expand Up @@ -399,9 +398,7 @@ const SelectBoundScreen: React.FC = () => {
{translate('selectBoundSettings')}
</Button>
) : null}
<Button onPress={handleAutoModeButtonPress}>
{autoModeButtonText}
</Button>
<Button onPress={toggleAutoModeEnabled}>{autoModeButtonText}</Button>
</View>
</View>
</ScrollView>
Expand Down
2 changes: 0 additions & 2 deletions src/store/atoms/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export interface NavigationState {
// 実際の次の停車駅を保持している
stationForHeader: Station | null
enabledLanguages: AvailableLanguage[]
autoModeEnabled: boolean
fetchedTrainTypes: TrainType[]
fromBuilder: boolean
}
Expand All @@ -30,7 +29,6 @@ export const initialNavigationState: NavigationState = {
leftStations: [],
stationForHeader: null,
enabledLanguages: ALL_AVAILABLE_LANGUAGES,
autoModeEnabled: false,
fetchedTrainTypes: [],
fromBuilder: false,
}
Expand Down
8 changes: 0 additions & 8 deletions src/store/selectors/autoMode.ts

This file was deleted.

Loading