Skip to content

Commit 5eff78b

Browse files
authored
Merge pull request #5227 from HSLdevcom/DT-6447
DT-6447 try restarting frozen location watch
2 parents f19388b + a92f98e commit 5eff78b

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

app/action/PositionActions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,20 @@ export function startLocationWatch(actionContext) {
200200
watchPosition(actionContext);
201201
break;
202202
}
203+
watchPending = false;
203204
});
204205
}
205206
}
206207

208+
/* stops location watch */
209+
export function stopLocationWatch() {
210+
if (typeof geoWatchId !== 'undefined') {
211+
navigator.geolocation.clearWatch(geoWatchId);
212+
geoWatchId = undefined;
213+
}
214+
watchPending = false;
215+
}
216+
207217
export function showGeolocationDeniedMessage(actionContext) {
208218
actionContext.dispatch('GeolocationDenied');
209219
updateGeolocationMessage(actionContext, 'denied');

app/component/itinerary/navigator/NaviContainer.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import PropTypes from 'prop-types';
44
import React, { useEffect, useRef } from 'react';
55
import { legTime } from '../../../util/legUtils';
66
import { legShape, relayShape } from '../../../util/shapes';
7+
import {
8+
startLocationWatch,
9+
stopLocationWatch,
10+
} from '../../../action/PositionActions';
711
import NaviBottom from './NaviBottom';
812
import NaviCardContainer from './NaviCardContainer';
913
import { useRealtimeLegs } from './hooks/useRealtimeLegs';
@@ -23,9 +27,11 @@ function NaviContainer(
2327
mapRef,
2428
mapLayerRef,
2529
},
26-
{ getStore, router },
30+
{ executeAction, getStore, router },
2731
) {
2832
const hasPosition = useRef(false);
33+
const prevPos = useRef(undefined);
34+
const posFrozen = useRef(0);
2935

3036
let position = getStore('PositionStore').getLocationState();
3137
if (!position.hasLocation) {
@@ -49,6 +55,25 @@ function NaviContainer(
4955
mapRef?.enableMapTracking(); // try always, shows annoying notifier
5056
}, [mapRef, hasPosition.current]);
5157

58+
useEffect(() => {
59+
if (position && prevPos.current) {
60+
if (
61+
prevPos.current.lat === position.lat &&
62+
prevPos.current.lon === position.lon
63+
) {
64+
posFrozen.current += 1;
65+
if (posFrozen.current === 3) {
66+
// window.alert('Restarting geolocation watch');
67+
executeAction(stopLocationWatch);
68+
setTimeout(() => executeAction(startLocationWatch), 10);
69+
}
70+
} else {
71+
posFrozen.current = 0;
72+
}
73+
}
74+
prevPos.current = position;
75+
}, [time]);
76+
5277
if (!realTimeLegs?.length) {
5378
return null;
5479
}
@@ -111,6 +136,7 @@ NaviContainer.propTypes = {
111136
};
112137

113138
NaviContainer.contextTypes = {
139+
executeAction: PropTypes.func,
114140
getStore: PropTypes.func.isRequired,
115141
router: routerShape.isRequired,
116142
};

app/component/itinerary/navigator/NaviUtils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ export function summaryString(legs, time, previousLeg, currentLeg, nextLeg) {
2727
color = 'color:pink';
2828
}
2929
colors.push(l.freezeStart ? 'color:lightblue' : color);
30-
msg += `\n%c${legTimeAcc(l.start)}`;
30+
msg += `%c${legTimeAcc(l.start)}`;
3131
colors.push('color:gray');
3232
msg += `%c-`;
3333
colors.push(l.freezeEnd ? 'color:lightblue' : color);
3434
msg += `%c${legTimeAcc(l.end)} `;
3535
});
3636
colors.push('color:gray');
37-
msg += `\n%c${previousLeg?.mode} ${currentLeg?.mode} ${nextLeg?.mode}`;
37+
msg += `%c${previousLeg?.mode} ${currentLeg?.mode} ${nextLeg?.mode}`;
3838
colors.unshift(msg);
3939

4040
return colors;

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-param-reassign */
12
import cloneDeep from 'lodash/cloneDeep';
23
import polyUtil from 'polyline-encoded';
34
import { useCallback, useEffect, useState } from 'react';
@@ -124,8 +125,9 @@ function getLegsOfInterest(legs, now) {
124125
function getInitialState(legs) {
125126
const time = Date.now();
126127
if (legs.length) {
128+
const origin = GeodeticToEcef(legs[0].from.lat, legs[0].from.lon);
127129
return {
128-
origin: GeodeticToEcef(legs[0].from.lat, legs[0].from.lon),
130+
origin,
129131
time,
130132
realTimeLegs: legs.map(leg => {
131133
const geometry = polyUtil.decode(leg.legGeometry.points);
@@ -177,25 +179,15 @@ const useRealtimeLegs = (relayEnvironment, initialLegs) => {
177179

178180
const fetchAndSetRealtimeLegs = useCallback(async () => {
179181
const now = Date.now();
180-
if (
181-
!realTimeLegs?.length ||
182-
now >= legTime(realTimeLegs[realTimeLegs.length - 1].end)
183-
) {
184-
setTimeAndRealTimeLegs(prev => ({
185-
...prev,
186-
time: now,
187-
}));
188-
}
189-
190182
const rtLegMap = await queryAndMapRealtimeLegs(realTimeLegs, now).catch(
191183
err =>
192184
// eslint-disable-next-line no-console
193185
console.error('Failed to query and map real time legs', err),
194186
);
195187

196188
setTimeAndRealTimeLegs(prev => {
197-
// Maps previous legs with fresh real time transit legs. If transit leg start is in the past according to previous state,
198-
// the transit leg is marked as frozen to prevent the start from shifting in UI.
189+
// Maps previous legs with fresh real time transit legs. If transit leg start or end time is in the past according
190+
// to previous state, the time is marked as frozen to stabilize the current navigation state.
199191
// rtLegMap does not contain legs that have ended in the past as they've been filtered before updates are queried
200192
const rtLegs = prev.realTimeLegs.map(l => {
201193
const rtLeg =
@@ -223,13 +215,12 @@ const useRealtimeLegs = (relayEnvironment, initialLegs) => {
223215
matchLegEnds(rtLegs, now);
224216

225217
// Freezes any leg.start|end in the past
226-
const rtLegsWithFreezes = rtLegs.map(l => ({
227-
...l,
228-
freezeStart: l.freezeStart || legTime(l.start) <= now,
229-
freezeEnd: l.freezeEnd || legTime(l.end) <= now,
230-
}));
218+
rtLegs.forEach(l => {
219+
l.freezeStart = legTime(l.start) <= now;
220+
l.freezeEnd = legTime(l.end) <= now;
221+
});
231222

232-
return { ...prev, time: now, realTimeLegs: rtLegsWithFreezes };
223+
return { ...prev, time: now, realTimeLegs: rtLegs };
233224
});
234225
}, [queryAndMapRealtimeLegs]);
235226

0 commit comments

Comments
 (0)