Skip to content

Commit 93a2de0

Browse files
authored
fix: allow zooming to user location on first map view (#2102)
Commits: * fix: allow zooming to user location on first map view by asking for location permissions before trying to update last known location This fixes the bug as written, but there is also an issue when switching permissions outside the app, then returning to the app, and the location button does not register the newly granted permissions * refactor: Remove old console.log * refactor: rename to honest names
1 parent 76b041f commit 93a2de0

3 files changed

Lines changed: 41 additions & 36 deletions

File tree

dev-client/src/components/modals/PermissionsRequestWrapper.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,29 @@ export const PermissionsRequestWrapper = ({
4646
}: Props) => {
4747
const {t} = useTranslation();
4848
const ref = useRef<ModalHandle>(null);
49-
const [permissions, requestPermissions] = usePermissions();
49+
// Issue #1808: These permissions could be outdated if
50+
// a) nobody has done a "get" or "request" on them since they changed, or
51+
// b) a descendant component did the "get" or "request", so there is no reason for the given component to re-render
52+
const [potentiallyOutdatedPermissions, requestPermissions] = usePermissions();
5053

5154
const onRequestAction = useCallback(async () => {
52-
if (permissions === null) {
55+
if (potentiallyOutdatedPermissions === null) {
5356
return;
5457
}
5558

56-
if (permissions.granted) {
59+
if (potentiallyOutdatedPermissions.granted) {
5760
if (permissionedAction !== undefined) {
5861
permissionedAction();
5962
}
60-
} else if (permissions.canAskAgain) {
63+
} else if (potentiallyOutdatedPermissions.canAskAgain) {
6164
const result = await requestPermissions();
6265
if (result.granted && permissionedAction !== undefined) {
6366
permissionedAction();
6467
}
6568
} else {
6669
ref.current?.onOpen();
6770
}
68-
}, [permissionedAction, permissions, requestPermissions]);
71+
}, [permissionedAction, potentiallyOutdatedPermissions, requestPermissions]);
6972

7073
return (
7174
<>

dev-client/src/screens/BottomTabsScreen.tsx

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import {memo, useEffect} from 'react';
1919

20+
import {useForegroundPermissions} from 'expo-location';
21+
2022
import {NavigationHelpers} from '@react-navigation/native';
2123
import {Location, locationManager} from '@rnmapbox/maps';
2224

@@ -36,30 +38,42 @@ import {useDispatch} from 'terraso-mobile-client/store';
3638
export const BottomTabsScreen = memo(() => {
3739
const dispatch = useDispatch();
3840
const {keyboardStatus} = useKeyboardStatus();
41+
const [potentiallyOutdatedLocationPermission, _, requestLocationPermission] =
42+
useForegroundPermissions();
43+
44+
useEffect(() => {
45+
if (!potentiallyOutdatedLocationPermission?.granted) {
46+
requestLocationPermission();
47+
}
48+
// disable depcheck because we only want to run on mount
49+
// eslint-disable-next-line react-hooks/exhaustive-deps
50+
}, []);
3951

4052
useEffect(() => {
41-
locationManager.getLastKnownLocation().then(initCoords => {
42-
if (initCoords !== null) {
53+
if (potentiallyOutdatedLocationPermission?.granted) {
54+
locationManager.getLastKnownLocation().then(initCoords => {
55+
if (initCoords !== null) {
56+
dispatch(
57+
updateLocation({
58+
coords: initCoords.coords,
59+
accuracyM: initCoords.coords.accuracy ?? null,
60+
}),
61+
);
62+
}
63+
});
64+
65+
// add listener to update location on user movement
66+
const listener = ({coords}: Location) => {
4367
dispatch(
44-
updateLocation({
45-
coords: initCoords.coords,
46-
accuracyM: initCoords.coords.accuracy ?? null,
47-
}),
68+
updateLocation({coords: coords, accuracyM: coords.accuracy ?? null}),
4869
);
49-
}
50-
});
51-
52-
// add listener to update location on user movement
53-
const listener = ({coords}: Location) => {
54-
dispatch(
55-
updateLocation({coords: coords, accuracyM: coords.accuracy ?? null}),
56-
);
57-
};
58-
locationManager.setMinDisplacement(USER_DISPLACEMENT_MIN_DISTANCE_M);
59-
locationManager.addListener(listener);
70+
};
71+
locationManager.setMinDisplacement(USER_DISPLACEMENT_MIN_DISTANCE_M);
72+
locationManager.addListener(listener);
6073

61-
return () => locationManager.removeListener(listener);
62-
}, [dispatch]);
74+
return () => locationManager.removeListener(listener);
75+
}
76+
}, [dispatch, potentiallyOutdatedLocationPermission]);
6377

6478
return (
6579
<BottomTabs.Navigator

dev-client/src/screens/HomeScreen/HomeScreen.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import {
2626
useState,
2727
} from 'react';
2828

29-
import {useForegroundPermissions} from 'expo-location';
30-
3129
import BottomSheet from '@gorhom/bottom-sheet';
3230
import Mapbox from '@rnmapbox/maps';
3331

@@ -59,16 +57,6 @@ import {ScreenScaffold} from 'terraso-mobile-client/screens/ScreenScaffold';
5957
import {useDispatch, useSelector} from 'terraso-mobile-client/store';
6058

6159
export const HomeScreen = memo(() => {
62-
const [locationPermission, requestLocationPermission] =
63-
useForegroundPermissions();
64-
useEffect(() => {
65-
if (!locationPermission?.granted) {
66-
requestLocationPermission();
67-
}
68-
// disable depcheck because we only want to run on mount
69-
// eslint-disable-next-line react-hooks/exhaustive-deps
70-
}, []);
71-
7260
const siteListBottomSheetRef = useRef<BottomSheet>(null);
7361
const [mapStyleURL, setMapStyleURL] = useState(Mapbox.StyleURL.Street);
7462
const [calloutState, setCalloutState] = useState<CalloutState>(noneCallout());

0 commit comments

Comments
 (0)