Skip to content

Commit e7c5224

Browse files
feat: remove location permission handling
1 parent 9ea53f4 commit e7c5224

File tree

2 files changed

+45
-49
lines changed

2 files changed

+45
-49
lines changed

example/src/App.tsx

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,48 @@
1-
import { View, StyleSheet, StatusBar } from 'react-native';
1+
import {
2+
View,
3+
StyleSheet,
4+
StatusBar,
5+
PermissionsAndroid,
6+
Platform,
7+
} from 'react-native';
28

3-
import { useEffect, useState } from 'react';
9+
import { useEffect, useRef, useState } from 'react';
410
import {
511
MapboxTurnByTurnNavigationView,
612
type MapboxTurnByTurnNavigation,
713
} from 'react-native-mapbox-turn-by-turn-navigation';
814

915
export default function App() {
1016
const [hybridRef, setHybridRef] = useState<MapboxTurnByTurnNavigation>();
17+
const isMountedRef = useRef<boolean>(true);
18+
const [ready, setReady] = useState(false);
19+
20+
const checkPermissions = async () => {
21+
try {
22+
const granted = await PermissionsAndroid.request(
23+
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
24+
);
25+
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
26+
console.warn('Location permission not granted');
27+
return;
28+
}
29+
if (isMountedRef.current) setReady(true);
30+
} catch (error) {
31+
console.error('Error checking permissions:', error);
32+
}
33+
};
34+
35+
useEffect(() => {
36+
isMountedRef.current = true;
37+
if (Platform.OS === 'android') {
38+
checkPermissions();
39+
} else {
40+
setReady(true); // Assume permissions are granted on iOS
41+
}
42+
return () => {
43+
isMountedRef.current = false;
44+
};
45+
}, []);
1146

1247
useEffect(() => {
1348
let unsubscribeFromListeners: (() => void)[] = [];
@@ -56,6 +91,10 @@ export default function App() {
5691
};
5792
}, [hybridRef]);
5893

94+
if (!ready) {
95+
return <View style={styles.container} />;
96+
}
97+
5998
return (
6099
<>
61100
<StatusBar barStyle="dark-content" />

src/index.tsx

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ComponentProps } from 'react';
2+
13
const MapboxTurnByTurnNavigationConfig = require('../nitrogen/generated/shared/json/MapboxTurnByTurnNavigationConfig.json');
24
import type {
35
MapboxTurnByTurnNavigationMethods,
@@ -6,62 +8,17 @@ import type {
68

79
export * from './MapboxTurnByTurnNavigation.nitro';
810

9-
import { type ComponentProps, useEffect, useRef, useState } from 'react';
10-
import {
11-
PermissionsAndroid,
12-
Platform,
13-
type StyleProp,
14-
StyleSheet,
15-
View,
16-
type ViewStyle,
17-
} from 'react-native';
11+
import { StyleSheet, View } from 'react-native';
1812
import { getHostComponent } from 'react-native-nitro-modules';
1913

2014
export const NativeView = getHostComponent<
2115
MapboxTurnByTurnNavigationProps,
2216
MapboxTurnByTurnNavigationMethods
2317
>('MapboxTurnByTurnNavigation', () => MapboxTurnByTurnNavigationConfig);
2418

25-
type NativeViewProps = ComponentProps<typeof NativeView>;
26-
interface MapboxTurnByTurnNavigationView extends NativeViewProps {
27-
style?: StyleProp<ViewStyle>;
28-
}
2919
export const MapboxTurnByTurnNavigationView = (
30-
props: MapboxTurnByTurnNavigationView
20+
props: ComponentProps<typeof NativeView>
3121
) => {
32-
const isMountedRef = useRef<boolean>(true);
33-
const [ready, setReady] = useState(false);
34-
35-
const checkPermissions = async () => {
36-
try {
37-
const granted = await PermissionsAndroid.request(
38-
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
39-
);
40-
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
41-
console.warn('Location permission not granted');
42-
return;
43-
}
44-
if (isMountedRef.current) setReady(true);
45-
} catch (error) {
46-
console.error('Error checking permissions:', error);
47-
}
48-
};
49-
50-
useEffect(() => {
51-
isMountedRef.current = true;
52-
if (Platform.OS === 'android') {
53-
checkPermissions();
54-
} else {
55-
setReady(true); // Assume permissions are granted on iOS
56-
}
57-
return () => {
58-
isMountedRef.current = false;
59-
};
60-
}, []);
61-
62-
if (!ready) {
63-
return <View style={styles.container} />;
64-
}
6522
return (
6623
<View style={[styles.container, props.style]}>
6724
<NativeView {...props} style={styles.container} />

0 commit comments

Comments
 (0)