Skip to content

Commit 07858e1

Browse files
authored
DevOverlayにバックグラウンド位置情報取得のON/OFF状態表示を追加 (#5402)
1 parent 75c9468 commit 07858e1

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

src/components/DevOverlay.test.tsx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,17 @@ const mockUseNextStation = useNextStation as jest.MockedFunction<
6464
describe('DevOverlay', () => {
6565
beforeEach(() => {
6666
// Default mock implementations for useAtomValue
67-
// locationAtom, accuracyHistoryAtomの順で呼ばれる (useTelemetryEnabledは別途モック済み)
67+
// locationAtom, accuracyHistoryAtom, backgroundLocationTrackingAtomの順で呼ばれる
68+
// (useTelemetryEnabledは別途モック済み)
6869
mockUseAtomValue
6970
.mockReturnValueOnce({
7071
coords: {
7172
speed: 10,
7273
accuracy: 15,
7374
},
7475
}) // locationAtom
75-
.mockReturnValue([10, 15, 20]); // accuracyHistoryAtom
76+
.mockReturnValueOnce([10, 15, 20]) // accuracyHistoryAtom
77+
.mockReturnValue(false); // backgroundLocationTrackingAtom
7678

7779
mockUseDistanceToNextStation.mockReturnValue('500');
7880
mockUseNextStation.mockReturnValue({
@@ -106,6 +108,24 @@ describe('DevOverlay', () => {
106108
const { getByText } = render(<DevOverlay />);
107109
expect(getByText('Telemetry: ON')).toBeTruthy();
108110
});
111+
112+
it('バックグラウンド位置情報のOFF状態を表示する', () => {
113+
const { getByText } = render(<DevOverlay />);
114+
expect(getByText('BG Loc: OFF')).toBeTruthy();
115+
});
116+
117+
it('バックグラウンド位置情報のON状態を表示する', () => {
118+
mockUseAtomValue.mockReset();
119+
mockUseAtomValue
120+
.mockReturnValueOnce({
121+
coords: { speed: 10, accuracy: 15 },
122+
}) // locationAtom
123+
.mockReturnValueOnce([10, 15, 20]) // accuracyHistoryAtom
124+
.mockReturnValue(true); // backgroundLocationTrackingAtom
125+
126+
const { getByText } = render(<DevOverlay />);
127+
expect(getByText('BG Loc: ON')).toBeTruthy();
128+
});
109129
});
110130

111131
describe('位置情報の表示', () => {
@@ -138,7 +158,7 @@ describe('DevOverlay', () => {
138158
mockUseAtomValue
139159
.mockReturnValueOnce(null) // locationAtom
140160
.mockReturnValueOnce([]) // accuracyHistoryAtom
141-
.mockReturnValue({ telemetryEnabled: true });
161+
.mockReturnValue(false); // backgroundLocationTrackingAtom
142162

143163
expect(() => {
144164
render(<DevOverlay />);
@@ -152,7 +172,7 @@ describe('DevOverlay', () => {
152172
coords: { speed: null, accuracy: 15 },
153173
}) // locationAtom
154174
.mockReturnValueOnce([]) // accuracyHistoryAtom
155-
.mockReturnValue({ telemetryEnabled: true });
175+
.mockReturnValue(false); // backgroundLocationTrackingAtom
156176

157177
const { getByText } = render(<DevOverlay />);
158178
expect(getByText('Speed: 0km/h')).toBeTruthy();
@@ -165,7 +185,7 @@ describe('DevOverlay', () => {
165185
coords: { speed: -5, accuracy: 15 },
166186
}) // locationAtom
167187
.mockReturnValueOnce([]) // accuracyHistoryAtom
168-
.mockReturnValue({ telemetryEnabled: true });
188+
.mockReturnValue(false); // backgroundLocationTrackingAtom
169189

170190
const { getByText } = render(<DevOverlay />);
171191
expect(getByText('Speed: 0km/h')).toBeTruthy();
@@ -178,7 +198,7 @@ describe('DevOverlay', () => {
178198
coords: { speed: 10, accuracy: null },
179199
}) // locationAtom
180200
.mockReturnValueOnce([]) // accuracyHistoryAtom
181-
.mockReturnValue({ telemetryEnabled: true });
201+
.mockReturnValue(false); // backgroundLocationTrackingAtom
182202

183203
const { getByText } = render(<DevOverlay />);
184204
expect(getByText('Accuracy: m')).toBeTruthy();
@@ -191,7 +211,7 @@ describe('DevOverlay', () => {
191211
coords: { speed: 10, accuracy: 15 },
192212
}) // locationAtom
193213
.mockReturnValueOnce([]) // accuracyHistoryAtom
194-
.mockReturnValue({ telemetryEnabled: true });
214+
.mockReturnValue(false); // backgroundLocationTrackingAtom
195215

196216
expect(() => {
197217
render(<DevOverlay />);
@@ -242,7 +262,7 @@ describe('DevOverlay', () => {
242262
coords: { speed: 0, accuracy: 15 },
243263
}) // locationAtom
244264
.mockReturnValueOnce([]) // accuracyHistoryAtom
245-
.mockReturnValue({ telemetryEnabled: true });
265+
.mockReturnValue(false); // backgroundLocationTrackingAtom
246266

247267
const { getByText } = render(<DevOverlay />);
248268
expect(getByText('Speed: 0km/h')).toBeTruthy();
@@ -255,7 +275,7 @@ describe('DevOverlay', () => {
255275
coords: { speed: 13.89, accuracy: 15 }, // 約50 km/h
256276
}) // locationAtom
257277
.mockReturnValueOnce([]) // accuracyHistoryAtom
258-
.mockReturnValue({ telemetryEnabled: true });
278+
.mockReturnValue(false); // backgroundLocationTrackingAtom
259279

260280
const { getByText } = render(<DevOverlay />);
261281
expect(getByText('Speed: 50km/h')).toBeTruthy();

src/components/DevOverlay.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import React, { useMemo } from 'react';
44
import { StyleSheet, Text, useWindowDimensions, View } from 'react-native';
55
import { useDistanceToNextStation, useNextStation } from '~/hooks';
66
import { useTelemetryEnabled } from '~/hooks/useTelemetryEnabled';
7-
import { accuracyHistoryAtom, locationAtom } from '~/store/atoms/location';
7+
import {
8+
accuracyHistoryAtom,
9+
backgroundLocationTrackingAtom,
10+
locationAtom,
11+
} from '~/store/atoms/location';
812
import { generateAccuracyChart } from '~/utils/accuracyChart';
913
import Typography from './Typography';
1014

@@ -35,6 +39,9 @@ const DevOverlay: React.FC = () => {
3539
const distanceToNextStation = useDistanceToNextStation();
3640
const nextStation = useNextStation(false);
3741
const isTelemetryEnabled = useTelemetryEnabled();
42+
const isBackgroundLocationTracking = useAtomValue(
43+
backgroundLocationTrackingAtom
44+
);
3845

3946
const coordsSpeed = ((speed ?? 0) < 0 ? 0 : speed) ?? 0;
4047

@@ -88,6 +95,9 @@ const DevOverlay: React.FC = () => {
8895
<Typography style={styles.text}>
8996
Telemetry: {isTelemetryEnabled ? 'ON' : 'OFF'}
9097
</Typography>
98+
<Typography style={styles.text}>
99+
BG Loc: {isBackgroundLocationTracking ? 'ON' : 'OFF'}
100+
</Typography>
91101
</View>
92102
);
93103
};

src/hooks/useStartBackgroundLocationUpdates.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { useStartBackgroundLocationUpdates } from './useStartBackgroundLocationU
1010

1111
jest.mock('expo-location');
1212
jest.mock('./useLocationPermissionsGranted');
13+
jest.mock('~/store', () => ({
14+
store: { set: jest.fn() },
15+
}));
1316
jest.mock('~/store/atoms/location', () => ({
17+
backgroundLocationTrackingAtom: {},
1418
setLocation: jest.fn(),
1519
}));
1620
jest.mock('~/store/atoms/navigation', () => ({

src/hooks/useStartBackgroundLocationUpdates.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import * as Location from 'expo-location';
22
import { useAtomValue } from 'jotai';
33
import { useEffect } from 'react';
4-
import { setLocation } from '~/store/atoms/location';
4+
import { store } from '~/store';
5+
import {
6+
backgroundLocationTrackingAtom,
7+
setLocation,
8+
} from '~/store/atoms/location';
59
import navigationState from '~/store/atoms/navigation';
610
import {
711
LOCATION_START_MAX_RETRIES,
@@ -60,6 +64,8 @@ export const useStartBackgroundLocationUpdates = () => {
6064
stopError
6165
);
6266
}
67+
} else {
68+
store.set(backgroundLocationTrackingAtom, true);
6369
}
6470
return;
6571
} catch (error) {
@@ -82,6 +88,7 @@ export const useStartBackgroundLocationUpdates = () => {
8288

8389
return () => {
8490
cancelled = true;
91+
store.set(backgroundLocationTrackingAtom, false);
8592
Location.stopLocationUpdatesAsync(LOCATION_TASK_NAME).catch((error) => {
8693
console.warn(
8794
'バックグラウンド位置情報の更新停止に失敗しました:',

src/store/atoms/location.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const SMOOTHING_ALPHA = 0.6;
1212

1313
export const locationAtom = atom<Location.LocationObject | null>(null);
1414
export const accuracyHistoryAtom = atom<number[]>([]);
15+
export const backgroundLocationTrackingAtom = atom(false);
1516

1617
export const setLocation = (location: Location.LocationObject) => {
1718
const prev = store.get(locationAtom);

0 commit comments

Comments
 (0)