Skip to content

Commit 0f9b4c8

Browse files
committed
fix: use existing positionStore state as tracking permission
Permission api seems to return unknown permission in some devices even when the user has already activated use of own location. Use positionStore to access existing geolocation state.
1 parent 6d1e437 commit 0f9b4c8

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

app/component/itinerary/navigator/NaviContainer.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import PropTypes from 'prop-types';
22
import React, { useEffect, useState } from 'react';
33
import polyUtil from 'polyline-encoded';
44
import { legTime } from '../../../util/legUtils';
5+
import { checkPositioningPermission } from '../../../action/PositionActions';
56
import { GeodeticToEcef, GeodeticToEnu } from '../../../util/geo-utils';
67
import { itineraryShape, relayShape } from '../../../util/shapes';
78
import NaviBottom from './NaviBottom';
@@ -21,6 +22,7 @@ function NaviContainer(
2122
) {
2223
const [planarLegs, setPlanarLegs] = useState([]);
2324
const [origin, setOrigin] = useState();
25+
const [isPositioningAllowed, setPositioningAllowed] = useState(false);
2426

2527
const position = getStore('PositionStore').getLocationState();
2628

@@ -38,7 +40,21 @@ function NaviContainer(
3840
setOrigin(orig);
3941
}, [itinerary]);
4042

41-
const { realTimeLegs, time, isPositioningAllowed } = useRealtimeLegs(
43+
useEffect(async () => {
44+
let permissionGranted;
45+
if (position.hasLocation) {
46+
permissionGranted = true;
47+
} else {
48+
const permission = await checkPositioningPermission();
49+
permissionGranted = permission.state === 'granted';
50+
}
51+
if (permissionGranted) {
52+
mapRef?.enableMapTracking();
53+
}
54+
setPositioningAllowed(permissionGranted);
55+
}, [mapRef]);
56+
57+
const { realTimeLegs, time } = useRealtimeLegs(
4258
planarLegs,
4359
mapRef,
4460
relayEnvironment,

app/component/itinerary/navigator/hooks/useRealtimeLegs.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useCallback, useEffect, useState } from 'react';
22
import { fetchQuery } from 'react-relay';
3-
import { checkPositioningPermission } from '../../../../action/PositionActions';
43
import { legQuery } from '../../queries/LegQuery';
54
import { legTime } from '../../../../util/legUtils';
65
import { epochToIso } from '../../../../util/timeUtils';
@@ -87,19 +86,9 @@ function matchLegEnds(legs) {
8786
}
8887

8988
const useRealtimeLegs = (initialLegs, mapRef, relayEnvironment) => {
90-
const [isPositioningAllowed, setPositioningAllowed] = useState(false);
9189
const [realTimeLegs, setRealTimeLegs] = useState(initialLegs);
9290
const [time, setTime] = useState(Date.now());
9391

94-
const enableMapTracking = useCallback(async () => {
95-
const permission = await checkPositioningPermission();
96-
const isPermissionGranted = permission.state === 'granted' || true;
97-
if (isPermissionGranted) {
98-
mapRef?.enableMapTracking();
99-
}
100-
setPositioningAllowed(isPermissionGranted);
101-
}, [mapRef]);
102-
10392
const queryAndMapRealtimeLegs = useCallback(
10493
async legs => {
10594
if (!legs.length) {
@@ -151,17 +140,16 @@ const useRealtimeLegs = (initialLegs, mapRef, relayEnvironment) => {
151140
}, [initialLegs, queryAndMapRealtimeLegs]);
152141

153142
useEffect(() => {
154-
enableMapTracking();
155143
fetchAndSetRealtimeLegs();
156144
const interval = setInterval(() => {
157145
fetchAndSetRealtimeLegs();
158146
setTime(Date.now());
159147
}, 10000);
160148

161149
return () => clearInterval(interval);
162-
}, [enableMapTracking, fetchAndSetRealtimeLegs]);
150+
}, [fetchAndSetRealtimeLegs]);
163151

164-
return { realTimeLegs, time, isPositioningAllowed };
152+
return { realTimeLegs, time };
165153
};
166154

167155
export { useRealtimeLegs };

0 commit comments

Comments
 (0)