1- import Geolocation from '@react-native-community/geolocation' ;
1+ import { request , PERMISSIONS , RESULTS , check , openSettings } from 'react-native-permissions' ;
2+ import { Platform , Alert , ToastAndroid } from 'react-native' ;
23
34// This file was generated by Mendix Studio Pro.
45//
@@ -15,107 +16,62 @@ import Geolocation from '@react-native-community/geolocation';
1516 */
1617async function RequestLocationPermission ( ) {
1718 // BEGIN USER CODE
18- var _a ;
19- let reactNativeModule ;
20- let geolocationModule ;
2119 const hasPermissionIOS = async ( ) => {
2220 const openSetting = ( ) => {
23- reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . Linking . openSettings ( ) . catch ( ( ) => {
24- reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . Alert . alert ( "Unable to open settings." ) ;
21+ openSettings ( ) . catch ( ( ) => {
22+ Alert . alert ( "Unable to open settings." ) ;
2523 } ) ;
2624 } ;
27- return geolocationModule
28- . requestAuthorization ( "whenInUse" )
29- . then ( ( status ) => {
30- if ( status === "granted" ) {
31- return true ;
32- }
33- if ( status === "denied" ) {
34- reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . Alert . alert ( "Location permission denied." ) ;
35- }
36- if ( status === "disabled" ) {
37- reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . Alert . alert ( "Location Services must be enabled to determine your location." , "" , [
38- { text : "Go to Settings" , onPress : openSetting } ,
39- {
40- text : "Don't Use Location"
41- }
42- ] ) ;
43- }
44- return false ;
45- } ) ;
25+ const status = await request ( PERMISSIONS . IOS . LOCATION_WHEN_IN_USE ) ;
26+ if ( status === RESULTS . GRANTED ) {
27+ return true ;
28+ }
29+ if ( status === RESULTS . DENIED || status === RESULTS . BLOCKED ) {
30+ Alert . alert ( "Location permission denied." ) ;
31+ }
32+ if ( status === RESULTS . UNAVAILABLE ) {
33+ Alert . alert ( "Location Services must be enabled to determine your location." , "" , [
34+ { text : "Go to Settings" , onPress : openSetting } ,
35+ {
36+ text : "Don't Use Location"
37+ }
38+ ] ) ;
39+ }
40+ return false ;
4641 } ;
4742 const hasPermissionAndroid = async ( ) => {
48- var _a , _b ;
49- if ( typeof ( ( _a = reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . Platform ) === null || _a === void 0 ? void 0 : _a . Version ) === "number" && ( ( _b = reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . Platform ) === null || _b === void 0 ? void 0 : _b . Version ) < 23 ) {
43+ if ( typeof Platform . Version === "number" && Platform . Version < 23 ) {
5044 return true ;
5145 }
52- const androidLocationPermission = reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . PermissionsAndroid . PERMISSIONS . ACCESS_FINE_LOCATION ;
53- if ( ! androidLocationPermission ) {
54- return false ;
46+ const status = await check ( PERMISSIONS . ANDROID . ACCESS_FINE_LOCATION ) ;
47+ if ( status === RESULTS . GRANTED ) {
48+ return true ;
49+ }
50+ const requestStatus = await request ( PERMISSIONS . ANDROID . ACCESS_FINE_LOCATION ) ;
51+ if ( requestStatus === RESULTS . GRANTED ) {
52+ return true ;
53+ }
54+ if ( requestStatus === RESULTS . DENIED ) {
55+ ToastAndroid . show ( "Location permission denied by user." , ToastAndroid . LONG ) ;
56+ }
57+ else if ( requestStatus === RESULTS . BLOCKED ) {
58+ ToastAndroid . show ( "Location permission revoked by user." , ToastAndroid . LONG ) ;
5559 }
56- return reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . PermissionsAndroid . check ( androidLocationPermission ) . then ( hasPermission => {
57- var _a ;
58- return hasPermission
59- ? true
60- : ( _a = reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . PermissionsAndroid ) === null || _a === void 0 ? void 0 : _a . request ( androidLocationPermission ) . then ( status => {
61- if ( status === ( reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . PermissionsAndroid . RESULTS . GRANTED ) ) {
62- return true ;
63- }
64- if ( status === ( reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . PermissionsAndroid . RESULTS . DENIED ) ) {
65- reactNativeModule . ToastAndroid . show ( "Location permission denied by user." , reactNativeModule . ToastAndroid . LONG ) ;
66- }
67- else if ( status === ( reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . PermissionsAndroid . RESULTS . NEVER_ASK_AGAIN ) ) {
68- reactNativeModule . ToastAndroid . show ( "Location permission revoked by user." , reactNativeModule . ToastAndroid . LONG ) ;
69- }
70- return false ;
71- } ) ;
72- } ) ;
60+ return false ;
7361 } ;
7462 const hasLocationPermission = async ( ) => {
75- if ( ( reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . Platform . OS ) === "ios" ) {
63+ if ( Platform . OS === "ios" ) {
7664 const hasPermission = await hasPermissionIOS ( ) ;
7765 return hasPermission ;
7866 }
79- if ( ( reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . Platform . OS ) === "android" ) {
67+ if ( Platform . OS === "android" ) {
8068 const hasPermission = await hasPermissionAndroid ( ) ;
8169 return hasPermission !== null && hasPermission !== void 0 ? hasPermission : false ;
8270 }
8371 return Promise . reject ( new Error ( "Unsupported platform" ) ) ;
8472 } ;
85- const hasLocationPermissionForOldLibrary = async ( ) => {
86- if ( ( reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . Platform . OS ) === "android" ) {
87- const locationPermission = reactNativeModule . PermissionsAndroid . PERMISSIONS . ACCESS_FINE_LOCATION ;
88- return reactNativeModule . PermissionsAndroid . check ( locationPermission ) . then ( hasPermission => hasPermission
89- ? true
90- : reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . PermissionsAndroid . request ( locationPermission ) . then ( status => status === ( reactNativeModule === null || reactNativeModule === void 0 ? void 0 : reactNativeModule . PermissionsAndroid . RESULTS . GRANTED ) ) ) ;
91- }
92- else if ( geolocationModule ) {
93- return new Promise ( resolve => {
94- geolocationModule . requestAuthorization ( ( ) => {
95- resolve ( true ) ;
96- } , ( ) => {
97- resolve ( false ) ;
98- } ) ;
99- } ) ;
100- }
101- return false ;
102- } ;
10373 if ( navigator && navigator . product === "ReactNative" ) {
104- reactNativeModule = require ( "react-native" ) ;
105- if ( ! reactNativeModule ) {
106- return Promise . reject ( new Error ( "React Native module could not be found" ) ) ;
107- }
108- if ( reactNativeModule . NativeModules . RNFusedLocation ) {
109- geolocationModule = ( await import ( '@react-native-community/geolocation' ) ) . default ;
110- return hasLocationPermission ( ) ;
111- }
112- else if ( reactNativeModule . NativeModules . RNCGeolocation ) {
113- geolocationModule = Geolocation ;
114- return ( _a = ( await hasLocationPermissionForOldLibrary ( ) ) ) !== null && _a !== void 0 ? _a : false ;
115- }
116- else {
117- return Promise . reject ( new Error ( "Geolocation module could not be found" ) ) ;
118- }
74+ return hasLocationPermission ( ) ;
11975 }
12076 else if ( navigator && navigator . geolocation ) {
12177 return Promise . reject ( new Error ( "No permission request for location is required for web/hybrid platform" ) ) ;
0 commit comments