From 9d7ca552c53996dc9187d1a48ee97ca78b796cb7 Mon Sep 17 00:00:00 2001 From: Steffen Kleinle Date: Mon, 11 Nov 2024 13:41:49 +0100 Subject: [PATCH] 2949: Fix map user location with approximate permission (for degoogled devices?) --- native/src/components/MapView.tsx | 6 ++--- native/src/components/base/IconButton.tsx | 2 +- native/src/hooks/useUserLocation.ts | 29 +++++++++++++---------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/native/src/components/MapView.tsx b/native/src/components/MapView.tsx index 5646a40270..7b17a25091 100644 --- a/native/src/components/MapView.tsx +++ b/native/src/components/MapView.tsx @@ -119,11 +119,11 @@ const MapView = ({ ) const onRequestLocation = useCallback(async () => { - if (userLocation) { - moveTo(userLocation) + const newUserLocation = userLocation ?? (await refreshPermissionAndLocation())?.coordinates + if (newUserLocation) { + moveTo(newUserLocation) setFollowUserLocation(true) } - await refreshPermissionAndLocation() }, [refreshPermissionAndLocation, moveTo, userLocation]) useEffect(() => { diff --git a/native/src/components/base/IconButton.tsx b/native/src/components/base/IconButton.tsx index cc200e8d4d..2f5df73170 100644 --- a/native/src/components/base/IconButton.tsx +++ b/native/src/components/base/IconButton.tsx @@ -16,7 +16,7 @@ const StyledPressable = styled(Pressable)` type IconButtonProps = { accessibilityLabel: string icon: ReactElement - onPress: () => Promise | void + onPress: () => unknown style?: StyleProp } diff --git a/native/src/hooks/useUserLocation.ts b/native/src/hooks/useUserLocation.ts index aa6f10de7e..342ccffea0 100644 --- a/native/src/hooks/useUserLocation.ts +++ b/native/src/hooks/useUserLocation.ts @@ -58,7 +58,7 @@ type RequestPermissionAndLocationOptions = { } type UseUserLocationReturn = LocationStateType & { - refreshPermissionAndLocation: (options?: RequestPermissionAndLocationOptions) => Promise + refreshPermissionAndLocation: (options?: RequestPermissionAndLocationOptions) => Promise } const initialState: LocationStateType = { status: 'loading', message: 'loading', coordinates: undefined } @@ -69,7 +69,10 @@ const useUserLocation = ({ requestPermissionInitially }: UseUserLocationProps): const { t } = useTranslation() const refreshPermissionAndLocation = useCallback( - async ({ showSnackbarIfBlocked = true, requestPermission = true }: RequestPermissionAndLocationOptions = {}) => { + async ({ + showSnackbarIfBlocked = true, + requestPermission = true, + }: RequestPermissionAndLocationOptions = {}): Promise => { setLocationState(initialState) const locationPermissionStatus = await getLocationPermissionStatus(requestPermission) @@ -79,17 +82,19 @@ const useUserLocation = ({ requestPermissionInitially }: UseUserLocationProps): } if (locationPermissionStatus === RESULTS.GRANTED) { - setLocationState(await getUserLocation()) - } else { - setLocationState({ message: 'noPermission', status: 'unavailable', coordinates: undefined }) - - if (requestPermission && showSnackbarIfBlocked && locationPermissionStatus === RESULTS.BLOCKED) { - showSnackbar({ - text: t('landing:noPermission'), - positiveAction: { label: t('layout:settings'), onPress: openSettings }, - }) - } + const location = await getUserLocation() + setLocationState(location) + return location } + + setLocationState({ message: 'noPermission', status: 'unavailable', coordinates: undefined }) + if (requestPermission && showSnackbarIfBlocked && locationPermissionStatus === RESULTS.BLOCKED) { + showSnackbar({ + text: t('landing:noPermission'), + positiveAction: { label: t('layout:settings'), onPress: openSettings }, + }) + } + return null }, [showSnackbar, t], )