From dc22dd55879944289f494ea8f542613ae43a9d06 Mon Sep 17 00:00:00 2001 From: nish Date: Tue, 21 Sep 2021 02:15:18 +0530 Subject: [PATCH] location & contacts done --- android/app/src/main/AndroidManifest.xml | 3 + app/navigation/DrawerNav.js | 4 +- .../contacts/ContactsList.android.js | 7 +- .../permissions/location/Location.android.js | 80 +++++++++++++++++++ app/screens/permissions/location/Location.js | 38 --------- app/screens/razorpay/Razorpay.js | 6 +- app/store/lang.store.js | 2 +- 7 files changed, 93 insertions(+), 47 deletions(-) create mode 100644 app/screens/permissions/location/Location.android.js delete mode 100644 app/screens/permissions/location/Location.js diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 6b6bae7..af95d52 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -2,6 +2,9 @@ package="com.nish.reactnativelibs"> + + + { {/* Permission Related Screens */} - {/* */} + ); }; diff --git a/app/screens/permissions/contacts/ContactsList.android.js b/app/screens/permissions/contacts/ContactsList.android.js index c0acfd8..e6205ff 100644 --- a/app/screens/permissions/contacts/ContactsList.android.js +++ b/app/screens/permissions/contacts/ContactsList.android.js @@ -14,7 +14,10 @@ import Header from '_Shared/Header'; import ContactItem from './ContactItem'; /* Read the Guide - https://www.npmjs.com/package/react-native-contacts */ + const ContactsList = () => { + /* Make sure to add relevant permissions in AndroidManifest.xml" */ + const { t } = useTranslation('common'); const [contacts, setContacts] = React.useState([]); @@ -29,11 +32,9 @@ const ContactsList = () => { buttonNegative: 'Cancel', }, ); - console.log('granted: ', granted); if (granted === PermissionsAndroid.RESULTS.GRANTED) { - const contacts = Contacts.getAll(); - console.log(contacts[0]); + const contacts = await Contacts.getAll(); let newContacts = []; contacts.map(contact => { diff --git a/app/screens/permissions/location/Location.android.js b/app/screens/permissions/location/Location.android.js new file mode 100644 index 0000000..e224215 --- /dev/null +++ b/app/screens/permissions/location/Location.android.js @@ -0,0 +1,80 @@ +import React from 'react'; +import { PermissionsAndroid, Button, View } from 'react-native'; +import Geolocation from 'react-native-geolocation-service'; + +import { ThemedContainer, ThemedBody, ThemedText } from '_Shared/Comps.themed'; +import Header from '_Shared/Header'; + +/* + Read about the lib setup here - + https://github.com/Agontuk/react-native-geolocation-service/blob/HEAD/docs/setup.md +*/ + +const Location = () => { + /* Make sure to add relevant permissions in AndroidManifest.xml" */ + const [status, setStatus] = React.useState(''); + const [coords, setCoords] = React.useState({}); + + const getLocationPermission = async () => { + try { + const granted = await PermissionsAndroid.request( + PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, + { + title: 'Location', + message: 'This app would like to get your location.', + buttonPositive: 'Accept', + buttonNegative: 'Cancel', + }, + ); + + if (granted === PermissionsAndroid.RESULTS.GRANTED) { + getCurrentPosition(); + } else { + console.log('Location permission denied'); + } + } catch (err) { + console.warn(err); + } + }; + + const getCurrentPosition = () => { + Geolocation.getCurrentPosition( + position => { + setCoords(position); + }, + error => { + setStatus(error.code, error.message); + }, + { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }, + ); + }; + + getLocationPermission(); + + return ( + +
+ + + Device permission granted to use location. Current status -{' '} + {status} + +