Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Notification when Permission does not grant #57

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
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
64 changes: 46 additions & 18 deletions app/scan-qr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
import { View, StyleSheet, Dimensions, TouchableOpacity } from "react-native";
import {
View,
StyleSheet,
Dimensions,
Alert,
TouchableOpacity,
} from "react-native";
import { useNavigation, useRouter } from "expo-router";
import { useEffect, useState } from "react";
import { useError } from "@/hooks/useError";
import { ThemedText } from "@/components/ThemedText";
import type { BarcodeScanningResult } from "expo-camera";
import { Camera, CameraView } from "expo-camera";
import { CameraView, Camera } from "expo-camera";
import { isAccessPromptQR, isDownloadQR } from "@/types/accessPrompt";
import * as Linking from "expo-linking";
import { PermissionStatus } from "expo-image-picker";
import { ThemedText } from "@/components/ThemedText";

const { width } = Dimensions.get("window");

Expand All @@ -34,19 +42,38 @@ export default function Logout() {
const { goBack } = useNavigation();
const { showErrorMsg } = useError();
const { replace, navigate } = useRouter();
const [scanned, setScanned] = useState(false);
const [showScanned, setShowScanned] = useState(false);
const handleDeniedPermissions = () => {
Alert.alert(
"Permission Denied",
"Camera access is required. Please enable it in your device settings.",
[
{ text: "Cancel", style: "cancel" },
{
text: "Open Settings",
onPress: () => Linking.openSettings(), // This will open the app settings page
},
],
{ cancelable: false }
);
};

useEffect(() => {
const getCameraPermissions = async () => {
await Camera.requestCameraPermissionsAsync();
const { status } = await Camera.requestCameraPermissionsAsync();
if (status !== PermissionStatus.GRANTED) {
handleDeniedPermissions();
return;
}
setShowScanned(true);
};
getCameraPermissions().catch(() =>
// eslint-disable-next-line no-console
console.log("Don't have camera permission")
);
getCameraPermissions().catch(() => {
console.log("Don't have camera permission");
});
}, []);

const handleBarCodeScanned = ({ data }: BarcodeScanningResult) => {
setScanned(true);
setShowScanned(false);
try {
const resourceInfo = JSON.parse(data);
if (isAccessPromptQR(resourceInfo)) {
Expand Down Expand Up @@ -95,15 +122,16 @@ export default function Logout() {
</TouchableOpacity>
</View>
<View style={styles.cameraContainer}>
<CameraView
onBarcodeScanned={scanned ? undefined : handleBarCodeScanned}
barcodeScannerSettings={{
barcodeTypes: ["qr"],
}}
style={styles.camera}
/>
{showScanned && (
<CameraView
onBarcodeScanned={handleBarCodeScanned}
barcodeScannerSettings={{
barcodeTypes: ["qr"],
}}
style={styles.camera}
/>
)}
</View>

<View style={styles.footer}>
<ThemedText style={styles.footerText}>Scan a QR code</ThemedText>
</View>
Expand Down
20 changes: 19 additions & 1 deletion components/PopupMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.
//
import React, { useRef } from "react";
import { Dimensions, StyleSheet, TouchableOpacity } from "react-native";
import { Dimensions, StyleSheet, TouchableOpacity, Alert } from "react-native";
import * as DocumentPicker from "expo-document-picker";
import * as ImagePicker from "expo-image-picker";
import { PermissionStatus } from "expo-image-picker";
Expand All @@ -26,6 +26,7 @@ import { faImage } from "@fortawesome/free-solid-svg-icons/faImage";
import { faFile } from "@fortawesome/free-solid-svg-icons/faFile";
import { faCamera } from "@fortawesome/free-solid-svg-icons/faCamera";
import { faQrcode } from "@fortawesome/free-solid-svg-icons/faQrcode";
import * as Linking from "expo-linking";
import { ThemedText } from "./ThemedText";

const { width } = Dimensions.get("window");
Expand Down Expand Up @@ -80,9 +81,25 @@ const PopupMenu: React.FC<PopupMenuProps> = ({
break;
}

const handleDeniedPermissions = (note: string) => {
Alert.alert(
"Permission Denied",
`${note}. Please enable it in your device settings.`,
[
{ text: "Cancel", style: "cancel" },
{
text: "Open Settings",
onPress: () => Linking.openSettings(), // This will open the app settings page
},
],
{ cancelable: false }
);
};

const takePicture = async () => {
const { status } = await ImagePicker.requestCameraPermissionsAsync();
if (status !== PermissionStatus.GRANTED) {
handleDeniedPermissions("Camera access is required to take photos");
return;
}
const result = await ImagePicker.launchCameraAsync({
Expand Down Expand Up @@ -111,6 +128,7 @@ const PopupMenu: React.FC<PopupMenuProps> = ({
const { status } =
await ImagePicker.requestMediaLibraryPermissionsAsync();
if (status !== PermissionStatus.GRANTED) {
handleDeniedPermissions("Image Library access is required");
return;
}
const result = await ImagePicker.launchImageLibraryAsync();
Expand Down