Skip to content

Commit 6a2146f

Browse files
author
karahanharunn
committed
feat: migrate to react-native-permissions for location handling
1 parent 307c987 commit 6a2146f

File tree

3 files changed

+43
-101
lines changed

3 files changed

+43
-101
lines changed

packages/jsActions/nanoflow-actions-native/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## [Unreleased]
88

9+
- We've migrated from using @react-native-community/geolocation to react-native-permissions for handling location permissions.
10+
911
## [6.1.1] Nanoflow Commons - 2025-10-7
1012

1113
### Fixed

packages/jsActions/nanoflow-actions-native/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@react-native-community/geolocation": "3.4.0",
2929
"invariant": "^2.2.4",
3030
"js-base64": "~3.7.2",
31+
"react-native-permissions": "5.4.2",
3132
"react-native-geocoder": "0.5.0"
3233
},
3334
"devDependencies": {

packages/jsActions/nanoflow-actions-native/src/geolocation/RequestLocationPermission.ts

Lines changed: 40 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
// - the code between BEGIN USER CODE and END USER CODE
66
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
77
// Other code you write will be lost the next time you deploy the project.
8-
import Geolocation from "@react-native-community/geolocation";
9-
10-
import type { GeolocationServiceStatic, AuthorizationResult } from "../../typings/Geolocation";
8+
import { check, request, PERMISSIONS, RESULTS, openSettings } from "react-native-permissions";
9+
import { Platform, Alert, ToastAndroid } from "react-native";
1110

1211
// BEGIN EXTRA CODE
1312
// END EXTRA CODE
@@ -19,137 +18,77 @@ import type { GeolocationServiceStatic, AuthorizationResult } from "../../typing
1918
export async function RequestLocationPermission(): Promise<boolean> {
2019
// BEGIN USER CODE
2120

22-
let reactNativeModule: typeof import("react-native") | undefined;
23-
let geolocationModule: typeof import("@react-native-community/geolocation").default;
24-
2521
const hasPermissionIOS = async (): Promise<boolean> => {
2622
const openSetting = (): void => {
27-
reactNativeModule?.Linking.openSettings().catch(() => {
28-
reactNativeModule?.Alert.alert("Unable to open settings.");
23+
openSettings().catch(() => {
24+
Alert.alert("Unable to open settings.");
2925
});
3026
};
3127

32-
return (geolocationModule as GeolocationServiceStatic)
33-
.requestAuthorization("whenInUse")
34-
.then((status: AuthorizationResult) => {
35-
if (status === "granted") {
36-
return true;
37-
}
28+
const status = await request(PERMISSIONS.IOS.LOCATION_WHEN_IN_USE);
3829

39-
if (status === "denied") {
40-
reactNativeModule?.Alert.alert("Location permission denied.");
41-
}
30+
if (status === RESULTS.GRANTED) {
31+
return true;
32+
}
33+
34+
if (status === RESULTS.DENIED || status === RESULTS.BLOCKED) {
35+
Alert.alert("Location permission denied.");
36+
}
4237

43-
if (status === "disabled") {
44-
reactNativeModule?.Alert.alert(
45-
"Location Services must be enabled to determine your location.",
46-
"",
47-
[
48-
{ text: "Go to Settings", onPress: openSetting },
49-
{
50-
text: "Don't Use Location"
51-
}
52-
]
53-
);
38+
if (status === RESULTS.UNAVAILABLE) {
39+
Alert.alert("Location Services must be enabled to determine your location.", "", [
40+
{ text: "Go to Settings", onPress: openSetting },
41+
{
42+
text: "Don't Use Location"
5443
}
44+
]);
45+
}
5546

56-
return false;
57-
});
47+
return false;
5848
};
5949

6050
const hasPermissionAndroid = async (): Promise<boolean | undefined> => {
61-
if (typeof reactNativeModule?.Platform?.Version === "number" && reactNativeModule?.Platform?.Version < 23) {
51+
if (typeof Platform.Version === "number" && Platform.Version < 23) {
6252
return true;
6353
}
6454

65-
const androidLocationPermission = reactNativeModule?.PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION;
55+
const status = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);
6656

67-
if (!androidLocationPermission) {
68-
return false;
57+
if (status === RESULTS.GRANTED) {
58+
return true;
6959
}
7060

71-
return reactNativeModule?.PermissionsAndroid.check(androidLocationPermission).then(hasPermission =>
72-
hasPermission
73-
? true
74-
: reactNativeModule?.PermissionsAndroid?.request(androidLocationPermission).then(status => {
75-
if (status === reactNativeModule?.PermissionsAndroid.RESULTS.GRANTED) {
76-
return true;
77-
}
78-
79-
if (status === reactNativeModule?.PermissionsAndroid.RESULTS.DENIED) {
80-
reactNativeModule.ToastAndroid.show(
81-
"Location permission denied by user.",
82-
reactNativeModule.ToastAndroid.LONG
83-
);
84-
} else if (status === reactNativeModule?.PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
85-
reactNativeModule.ToastAndroid.show(
86-
"Location permission revoked by user.",
87-
reactNativeModule.ToastAndroid.LONG
88-
);
89-
}
90-
91-
return false;
92-
})
93-
);
61+
const requestStatus = await request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);
62+
63+
if (requestStatus === RESULTS.GRANTED) {
64+
return true;
65+
}
66+
67+
if (requestStatus === RESULTS.DENIED) {
68+
ToastAndroid.show("Location permission denied by user.", ToastAndroid.LONG);
69+
} else if (requestStatus === RESULTS.BLOCKED) {
70+
ToastAndroid.show("Location permission revoked by user.", ToastAndroid.LONG);
71+
}
72+
73+
return false;
9474
};
9575

9676
const hasLocationPermission = async (): Promise<boolean> => {
97-
if (reactNativeModule?.Platform.OS === "ios") {
77+
if (Platform.OS === "ios") {
9878
const hasPermission = await hasPermissionIOS();
9979
return hasPermission;
10080
}
10181

102-
if (reactNativeModule?.Platform.OS === "android") {
82+
if (Platform.OS === "android") {
10383
const hasPermission = await hasPermissionAndroid();
10484
return hasPermission ?? false;
10585
}
10686

10787
return Promise.reject(new Error("Unsupported platform"));
10888
};
10989

110-
const hasLocationPermissionForOldLibrary = async (): Promise<boolean | undefined> => {
111-
if (reactNativeModule?.Platform.OS === "android") {
112-
const locationPermission = reactNativeModule.PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION;
113-
114-
return reactNativeModule.PermissionsAndroid.check(locationPermission).then(hasPermission =>
115-
hasPermission
116-
? true
117-
: reactNativeModule?.PermissionsAndroid.request(locationPermission).then(
118-
status => status === reactNativeModule?.PermissionsAndroid.RESULTS.GRANTED
119-
)
120-
);
121-
} else if (geolocationModule) {
122-
return new Promise(resolve => {
123-
geolocationModule.requestAuthorization(
124-
() => {
125-
resolve(true);
126-
},
127-
() => {
128-
resolve(false);
129-
}
130-
);
131-
});
132-
}
133-
134-
return false;
135-
};
136-
13790
if (navigator && navigator.product === "ReactNative") {
138-
reactNativeModule = require("react-native");
139-
140-
if (!reactNativeModule) {
141-
return Promise.reject(new Error("React Native module could not be found"));
142-
}
143-
144-
if (reactNativeModule.NativeModules.RNFusedLocation) {
145-
geolocationModule = (await import("@react-native-community/geolocation")).default;
146-
return hasLocationPermission();
147-
} else if (reactNativeModule.NativeModules.RNCGeolocation) {
148-
geolocationModule = Geolocation;
149-
return (await hasLocationPermissionForOldLibrary()) ?? false;
150-
} else {
151-
return Promise.reject(new Error("Geolocation module could not be found"));
152-
}
91+
return hasLocationPermission();
15392
} else if (navigator && navigator.geolocation) {
15493
return Promise.reject(new Error("No permission request for location is required for web/hybrid platform"));
15594
} else {

0 commit comments

Comments
 (0)