Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 75 additions & 6 deletions apps/mobile/src/screens/ScanScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
Share,
Platform,
PermissionsAndroid,
Linking,
AppState,
type AppStateStatus,
NativeModules,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useFocusEffect } from '@react-navigation/native';
Expand Down Expand Up @@ -65,6 +69,33 @@ export default function ScanScreen({ navigation }: Props) {
}
};

const checkInitialCameraPermission = useCallback(async () => {
if (Platform.OS === 'android') {
const hasPerm = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA);
setHasPermission(hasPerm);
} else if (Platform.OS === 'ios') {
try {
const RNCameraKitModule = NativeModules.RNCameraKitModule;
if (RNCameraKitModule) {
const status = await RNCameraKitModule.checkDeviceCameraAuthorizationStatus();
if (status === true) {
setHasPermission(true);
} else if (status === -1) {
const granted = await RNCameraKitModule.requestDeviceCameraAuthorization();
setHasPermission(granted === true);
} else {
setHasPermission(false);
}
} else {
setHasPermission(true);
}
} catch (err) {
console.warn(err);
setHasPermission(false);
}
}
}, []);

const requestCameraPermission = async () => {
if (Platform.OS === 'android') {
try {
Expand All @@ -82,10 +113,34 @@ export default function ScanScreen({ navigation }: Props) {
} catch (err) {
console.warn(err);
}
} else {
// iOS permissions would typically be handled via react-native-permissions
// For this demo, assume true if not Android
setHasPermission(true);
} else if (Platform.OS === 'ios') {
try {
const RNCameraKitModule = NativeModules.RNCameraKitModule;
if (RNCameraKitModule) {
const status = await RNCameraKitModule.checkDeviceCameraAuthorizationStatus();
if (status === true) {
setHasPermission(true);
} else if (status === -1) {
const granted = await RNCameraKitModule.requestDeviceCameraAuthorization();
setHasPermission(granted === true);
} else {
setHasPermission(false);
Alert.alert(
'Camera Access Required',
'Please enable camera access in your device settings to scan QR codes.',
[
{ text: 'Cancel', style: 'cancel' },
{ text: 'Settings', onPress: () => Linking.openURL('app-settings:') },
],
);
}
} else {
setHasPermission(true);
}
} catch (err) {
console.warn(err);
setHasPermission(false);
}
}
};

Expand All @@ -104,7 +159,7 @@ export default function ScanScreen({ navigation }: Props) {
title: 'My DevCard QR',
url: uri,
});
} catch (err) {
} catch {
Alert.alert('Error', 'Failed to save QR code');
}
}
Expand All @@ -126,7 +181,8 @@ export default function ScanScreen({ navigation }: Props) {
useFocusEffect(
useCallback(() => {
fetchCards();
}, [fetchCards])
checkInitialCameraPermission();
}, [fetchCards, checkInitialCameraPermission])
);

useEffect(() => {
Expand All @@ -144,6 +200,19 @@ export default function ScanScreen({ navigation }: Props) {
loadStoredCardId();
}, []);

useEffect(() => {
const handleAppStateChange = (nextAppState: AppStateStatus) => {
if (nextAppState === 'active') {
checkInitialCameraPermission();
}
};

const subscription = AppState.addEventListener('change', handleAppStateChange);
return () => {
subscription.remove();
};
}, [checkInitialCameraPermission]);

useEffect(() => {
if (!hasLoadedStoredCard) return;

Expand Down