diff --git a/dev-client/package-lock.json b/dev-client/package-lock.json index ba9beeb77b..467161ec11 100644 --- a/dev-client/package-lock.json +++ b/dev-client/package-lock.json @@ -30,6 +30,7 @@ "expo-dev-client": "~4.0.19", "expo-image-manipulator": "~12.0.5", "expo-image-picker": "~15.0.5", + "expo-location": "~17.0.1", "expo-media-library": "~16.0.3", "expo-screen-orientation": "~7.0.5", "expo-sensors": "~13.0.8", @@ -14944,6 +14945,14 @@ "invariant": "^2.2.4" } }, + "node_modules/expo-location": { + "version": "17.0.1", + "resolved": "https://registry.npmjs.org/expo-location/-/expo-location-17.0.1.tgz", + "integrity": "sha512-m+OzotzlAXO3ZZ1uqW5GC25nXW868zN+ROyBA1V4VF6jGay1ZEs4URPglCVUDzZby2F5wt24cMzqDKw2IX6nRw==", + "peerDependencies": { + "expo": "*" + } + }, "node_modules/expo-manifests": { "version": "0.14.3", "resolved": "https://registry.npmjs.org/expo-manifests/-/expo-manifests-0.14.3.tgz", diff --git a/dev-client/package.json b/dev-client/package.json index 6b042d7851..9c58e12481 100644 --- a/dev-client/package.json +++ b/dev-client/package.json @@ -39,6 +39,7 @@ "expo-dev-client": "~4.0.19", "expo-image-manipulator": "~12.0.5", "expo-image-picker": "~15.0.5", + "expo-location": "~17.0.1", "expo-media-library": "~16.0.3", "expo-screen-orientation": "~7.0.5", "expo-sensors": "~13.0.8", diff --git a/dev-client/src/App.tsx b/dev-client/src/App.tsx index 3b59c2d4bf..108528a314 100644 --- a/dev-client/src/App.tsx +++ b/dev-client/src/App.tsx @@ -47,7 +47,6 @@ import * as Sentry from '@sentry/react-native'; import {APP_CONFIG} from 'terraso-mobile-client/config'; import {GeospatialProvider} from 'terraso-mobile-client/context/GeospatialContext'; import {HomeScreenContextProvider} from 'terraso-mobile-client/context/HomeScreenContext'; -import {checkAndroidPermissions} from 'terraso-mobile-client/native/checkAndroidPermissions'; import {RootNavigator} from 'terraso-mobile-client/navigation/navigators/RootNavigator'; import {Toasts} from 'terraso-mobile-client/screens/Toasts'; import {createStore} from 'terraso-mobile-client/store'; @@ -74,12 +73,6 @@ LogBox.ignoreLogs([ const store = createStore(); function App(): React.JSX.Element { - useEffect(() => - checkAndroidPermissions( - PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, - ), - ); - return ( diff --git a/dev-client/src/native/checkAndroidPermissions.ts b/dev-client/src/native/checkAndroidPermissions.ts deleted file mode 100644 index 013c9c794a..0000000000 --- a/dev-client/src/native/checkAndroidPermissions.ts +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright © 2023 Technology Matters - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see https://www.gnu.org/licenses/. - */ - -import {Permission, PermissionsAndroid, Platform} from 'react-native'; - -// Android requires user to approve certain key permissions manually -// see documentation: https://reactnative.dev/docs/permissionsandroid -export function checkAndroidPermissions(permission: Permission) { - if (Platform.OS !== 'android') { - return; - } - - PermissionsAndroid.request(permission) - .then(granted => { - if (granted) { - console.log(permission, 'granted'); - } else { - // TODO: What to do on rejection - console.log(permission, 'rejected'); - } - }) - .catch(err => { - // TODO: What to do on error? - console.error(err); - }); -} diff --git a/dev-client/src/screens/HomeScreen/HomeScreen.tsx b/dev-client/src/screens/HomeScreen/HomeScreen.tsx index 08cb64553d..b281c82fdc 100644 --- a/dev-client/src/screens/HomeScreen/HomeScreen.tsx +++ b/dev-client/src/screens/HomeScreen/HomeScreen.tsx @@ -26,6 +26,8 @@ import { useState, } from 'react'; +import {useForegroundPermissions} from 'expo-location'; + import BottomSheet, {BottomSheetModal} from '@gorhom/bottom-sheet'; import Mapbox from '@rnmapbox/maps'; @@ -58,6 +60,14 @@ import {ScreenScaffold} from 'terraso-mobile-client/screens/ScreenScaffold'; import {useDispatch, useSelector} from 'terraso-mobile-client/store'; export const HomeScreen = memo(() => { + const [locationPermission, requestLocationPermission] = + useForegroundPermissions(); + useEffect(() => { + if (!locationPermission?.granted) { + requestLocationPermission(); + } + }, []); + const infoBottomSheetRef = useRef(null); const siteListBottomSheetRef = useRef(null); const [mapStyleURL, setMapStyleURL] = useState(Mapbox.StyleURL.Street); diff --git a/dev-client/src/screens/HomeScreen/components/MapSearch.tsx b/dev-client/src/screens/HomeScreen/components/MapSearch.tsx index 5c0c8e1e08..cbe1ae6c12 100644 --- a/dev-client/src/screens/HomeScreen/components/MapSearch.tsx +++ b/dev-client/src/screens/HomeScreen/components/MapSearch.tsx @@ -21,12 +21,15 @@ import {Keyboard} from 'react-native'; import Autocomplete from 'react-native-autocomplete-input'; import {Searchbar} from 'react-native-paper'; +import {useForegroundPermissions} from 'expo-location'; + import {Pressable} from 'native-base'; import {Coords} from 'terraso-client-shared/types'; import {IconButton} from 'terraso-mobile-client/components/icons/IconButton'; import {searchBarStyles} from 'terraso-mobile-client/components/ListFilter'; +import {PermissionsRequestModal} from 'terraso-mobile-client/components/modals/PermissionsRequestModal'; import { Box, Column, @@ -208,14 +211,22 @@ export default function MapSearch({zoomTo, zoomToUser, toggleMapLayer}: Props) { padding={2} onPress={toggleMapLayer} /> - + + {onRequest => ( + + )} +