Skip to content

Commit

Permalink
Prevent back navigation with back button on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
tsh22 committed Dec 22, 2021
1 parent 29b504e commit 1aa3a08
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 17 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"@react-native-community/datetimepicker": "3.2.0",
"@react-native-community/hooks": "^2.8.1",
"@react-native-community/masked-view": "0.1.10",
"@react-navigation/bottom-tabs": "^5.11.11",
"@react-navigation/native": "^5.9.4",
Expand Down
12 changes: 12 additions & 0 deletions screens/BasicApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import { View, Text } from "react-native";
import firebase from "../database/firebaseDB";
import moment from "moment";
import { Alert, BackHandler } from "react-native";

// Get user's coins
export function getCoins(uid, setCoins) {
Expand Down Expand Up @@ -104,3 +105,14 @@ export function renderAllDates(dates) {
<Text></Text>;
}
}

export const backActionHandler = () => {
Alert.alert("Alert!", "Are you sure you want to exit app?", [
{
text: "Cancel",
onPress: () => null,
},
{ text: "YES", onPress: () => BackHandler.exitApp() },
]);
return true;
};
5 changes: 5 additions & 0 deletions screens/borrow-screens/BorrowSelectionScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import firebase from "../../database/firebaseDB";
import { setStallDetails, setQuotas, getBorrowedNum } from "./BorrowApi";
import { UserContext } from "../../assets/UserContext";
import Constants from "expo-constants";
import { useBackHandler } from "@react-native-community/hooks";
import { backActionHandler } from "../BasicApi";

export default function BorrowSelectionScreen({ navigation, route }) {
// Stall name from scanning QR code
Expand All @@ -41,6 +43,9 @@ export default function BorrowSelectionScreen({ navigation, route }) {
const [numCups, setCupNum] = useState(0);
const [numContainers, setContainerNum] = useState(0);

// Prevent back button action on Android
useBackHandler(backActionHandler);

// Set up stall details / quotas on intial render
useEffect(() => {
setQuotas(setCupQuota, setContainerQuota);
Expand Down
6 changes: 6 additions & 0 deletions screens/borrow-screens/BorrowSuccessfulScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import { globalStyles } from "../../assets/globalStyles";
import FooterText from "../../components/FooterText";
import { UserContext } from "../../assets/UserContext";
import { uploadBorrowData } from "./BorrowApi";
import { useBackHandler } from "@react-native-community/hooks";
import { backActionHandler } from "../BasicApi";

export default function BorrowSuccessfulScreen({ route, navigation }) {
const userData = useContext(UserContext);
const { numCups, numContainers } = route.params;
const [hasError, setError] = useState(false);

// Prevent back button action on Android
useBackHandler(backActionHandler);

// console.log(userData.id);
useEffect(() => {
uploadBorrowData(userData.id, numCups, numContainers, setError);
Expand Down
6 changes: 5 additions & 1 deletion screens/borrow-screens/BorrowUnsuccessfulScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { StyleSheet, Text, ScrollView, Image, Dimensions } from "react-native";
import UnsuccessBox from "../../components/UnsuccessBox";
import colors from "../../assets/colors";
import { globalStyles } from "../../assets/globalStyles";
import { useBackHandler } from "@react-native-community/hooks";
import { backActionHandler } from "../BasicApi";

export default function BorrowUnsuccessfulScreen() {
// Prevent back button action on Android
useBackHandler(backActionHandler);

return (
<ScrollView style={styles.container} showsVerticalScrollIndicator={false}>
Expand All @@ -21,4 +25,4 @@ const styles = StyleSheet.create({
container: {
backgroundColor: colors.white,
},
});
});
22 changes: 11 additions & 11 deletions screens/byo-screens/BYOScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ export default function BYOScreen({ navigation }) {
>
<View style={styles.box}>
{/* <TouchableOpacity
onPress={() => navigation.navigate("BYO Unsuccessful Screen")}
style={styles.imagePlaceholder}
>
<Text>(Unsuccessful Screen)</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => navigation.navigate("BYO Selection Screen")}
style={styles.imagePlaceholder}
>
<Text>(Selection screen)</Text>
</TouchableOpacity> */}
onPress={() => navigation.navigate("BYO Unsuccessful Screen")}
style={styles.imagePlaceholder}
>
<Text>(Unsuccessful Screen)</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => navigation.navigate("BYO Selection Screen")}
style={styles.imagePlaceholder}
>
<Text>(Selection screen)</Text>
</TouchableOpacity> */}
<Text style={styles.text}>Scan the QR code!</Text>
<BarCodeScanner
onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
Expand Down
5 changes: 5 additions & 0 deletions screens/byo-screens/BYOSelectionScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import SelectionComponent from "../../components/SelectionComponent";
import FooterText from "../../components/FooterText";
import Constants from "expo-constants";
import { setStallDetails, setQuotas } from "../borrow-screens/BorrowApi";
import { useBackHandler } from "@react-native-community/hooks";
import { backActionHandler } from "../BasicApi";

export default function BYOSelectionScreen({ navigation, route }) {
//Based on this store name, grab data from firebase
Expand All @@ -36,6 +38,9 @@ export default function BYOSelectionScreen({ navigation, route }) {
const [numCups, setCupNum] = useState(0);
const [numContainers, setContainerNum] = useState(0);

// Prevent back button action on Android
useBackHandler(backActionHandler);

useEffect(() => {
setQuotas(setCupQuota, setContainerQuota);
setStallDetails(
Expand Down
5 changes: 5 additions & 0 deletions screens/byo-screens/BYOSuccessScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { globalStyles } from "../../assets/globalStyles";
import FooterText from "../../components/FooterText";
import { uploadByoData, updateCoins } from "./BYOApi";
import { UserContext } from "../../assets/UserContext";
import { useBackHandler } from "@react-native-community/hooks";
import { backActionHandler } from "../BasicApi";

export default function BYOSuccessScreen({ route }) {
const { numCups, numContainers } = route.params;
Expand All @@ -16,6 +18,9 @@ export default function BYOSuccessScreen({ route }) {
// Can change the value of coins earned accordingly
const coinsEarned = numCups + numContainers;

// Prevent back button action on Android
useBackHandler(backActionHandler);

useEffect(() => {
uploadByoData(uid, numCups, numContainers, setError);
updateCoins(uid, coinsEarned);
Expand Down
22 changes: 17 additions & 5 deletions screens/byo-screens/BYOUnsuccessfulScree.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import React from "react";
import { StyleSheet, Text, View, ScrollView, Image, Dimensions } from "react-native";
import {
StyleSheet,
Text,
View,
ScrollView,
Image,
Dimensions,
} from "react-native";
import UnsuccessBox from "../../components/UnsuccessBox";
import colors from "../../assets/colors";
import { globalStyles } from "../../assets/globalStyles";
import { useBackHandler } from "@react-native-community/hooks";
import { backActionHandler } from "../BasicApi";

export default function BYOUnsuccessfulScreen() {
// Prevent back button action on Android
useBackHandler(backActionHandler);

return (
<ScrollView
style={{ backgroundColor: colors.white }}
showsVerticalScrollIndicator={false}
>
<View style={styles.container}>
<Image
source={require("../../assets/AppImages/byoHeader.png")}
style={{ width: Dimensions.get("window").width, marginBottom: 50 }}
/>
<Image
source={require("../../assets/AppImages/byoHeader.png")}
style={{ width: Dimensions.get("window").width, marginBottom: 50 }}
/>
<UnsuccessBox />
</View>
</ScrollView>
Expand Down

0 comments on commit 1aa3a08

Please sign in to comment.