Browse Source

perf: 修复bug优化代码

zengkunsen 4 months ago
parent
commit
4d945d9a72

+ 45 - 40
app/(auth)/(tabs)/(home)/scanQrPage.tsx

@@ -572,6 +572,7 @@ const ScanQrPage = () => {
                     <ActivityIndicator />
                 </View>
             ) : (
+              <View className="flex-1 position-relative">
                 <CameraView
                     style={styles.camera}
                     facing="back"
@@ -581,46 +582,47 @@ const ScanQrPage = () => {
                     onBarcodeScanned={scanned ? undefined : handleBarCodeScanned}
                     responsiveOrientationWhenOrientationLocked={true}
                 >
-                    <View style={styles.overlay}>
-                        <View style={styles.topOverlay}>
-                            <Pressable
-                                className="absolute top-20 left-10 z-10 p-4"
-                                hitSlop={{ top: 20, bottom: 20, left: 20, right: 20 }} // Added hitSlop
-                                onPress={() => {
-                                    if (router.canGoBack()) {
-                                        router.back();
-                                    } else {
-                                        router.push('/mainPage');
-                                    }
-                                }}
-                            >
-                                <View style={{ transform: [{ scale: 1.5 }] }}>
-                                    <CrossLogoWhiteSvg />
-                                </View>
-                            </Pressable>
-                        </View>
-                        <View style={styles.centerRow}>
-                            <View style={styles.leftOverlay}></View>
-                            <View style={styles.transparentArea}></View>
-                            <View style={styles.rightOverlay} />
-                        </View>
-                        <View className="items-center justify-between" style={styles.bottomOverlay}>
-                            <View>
-                                <Text className="text-white text-lg font-bold mt-2 text-center">
-                                    請掃瞄充電座上的二維碼
-                                </Text>
-                            </View>
-                            <View className="flex-row space-x-2 items-center ">
-                                <QuestionSvg />
-
-                                <Pressable onPress={() => router.push('assistancePage')}>
-                                    <Text className="text-white text-base">需要協助?</Text>
-                                </Pressable>
-                            </View>
-                            <View />
-                        </View>
-                    </View>
                 </CameraView>
+                <View style={styles.overlay}>
+                  <View style={styles.topOverlay}>
+                      <Pressable
+                          className="absolute top-20 left-10 z-10 p-4"
+                          hitSlop={{ top: 20, bottom: 20, left: 20, right: 20 }} // Added hitSlop
+                          onPress={() => {
+                              if (router.canGoBack()) {
+                                  router.back();
+                              } else {
+                                  router.push('/mainPage');
+                              }
+                          }}
+                      >
+                          <View style={{ transform: [{ scale: 1.5 }] }}>
+                              <CrossLogoWhiteSvg />
+                          </View>
+                      </Pressable>
+                  </View>
+                  <View style={styles.centerRow}>
+                      <View style={styles.leftOverlay}></View>
+                      <View style={styles.transparentArea}></View>
+                      <View style={styles.rightOverlay} />
+                  </View>
+                  <View className="items-center justify-between" style={styles.bottomOverlay}>
+                      <View>
+                          <Text className="text-white text-lg font-bold mt-2 text-center">
+                              請掃瞄充電座上的二維碼
+                          </Text>
+                      </View>
+                      <View className="flex-row space-x-2 items-center ">
+                          <QuestionSvg />
+
+                          <Pressable onPress={() => router.push('assistancePage')}>
+                              <Text className="text-white text-base">需要協助?</Text>
+                          </Pressable>
+                      </View>
+                      <View />
+                  </View>
+                </View>
+              </View>
             )}
         </View>
     );
@@ -634,7 +636,10 @@ const styles = StyleSheet.create({
         flex: 1
     },
     overlay: {
-        flex: 1
+        flex: 1,
+        width: '100%',
+        height: '100%',
+        position: 'absolute',
     },
     topOverlay: {
         flex: 35,

+ 4 - 4
app/hooks/usePushNotifications.ts

@@ -13,8 +13,8 @@ export const usePushNotifications = (): PushNotificationState => {
   const [expoPushToken, setExpoPushToken] = useState<Notifications.ExpoPushToken | undefined>();
   const [notification, setNotification] = useState<Notifications.Notification | undefined>();
 
-  const notificationListener = useRef<Notifications.Subscription>();
-  const responseListener = useRef<Notifications.Subscription>();
+  const notificationListener = useRef<Notifications.Subscription | undefined>(undefined);
+  const responseListener = useRef<Notifications.Subscription | undefined>(undefined);
 
   useEffect(() => {
     Notifications.setNotificationHandler({
@@ -39,8 +39,8 @@ export const usePushNotifications = (): PushNotificationState => {
     });
 
     return () => {
-      Notifications.removeNotificationSubscription(notificationListener.current!);
-      Notifications.removeNotificationSubscription(responseListener.current!);
+      notificationListener.current?.remove()
+      responseListener.current?.remove()
     };
   }, []);
 

+ 2 - 36
component/accountPages/assistancePageComponent.tsx

@@ -3,43 +3,9 @@ import React, { useContext } from 'react';
 import { SafeAreaView } from 'react-native-safe-area-context';
 import { router } from 'expo-router';
 import { CrossLogoSvg, RightArrowIconSvg } from '../global/SVG';
+import { handleGoWhatsApp } from '../../util/index';
 
 const AssistancePageComponent = () => {
-    // const handlePress = async () => {
-    //     const phoneNumber = 'tel:+85291382129';
-    //     try {
-    //         const supported = await Linking.canOpenURL(phoneNumber);
-    //         if (supported) {
-    //             await Linking.openURL(phoneNumber);
-    //         } else {
-    //             Alert.alert('Error', 'Unable to make a call');
-    //         }
-    //     } catch (error) {
-    //         Alert.alert('Error', 'An unexpected error occurred');
-    //     }
-    // };
-    const handlePress = async () => {
-        const phoneNumber = '85291382139'; // Remove the '+' sign
-        const whatsappUrl = `whatsapp://send?phone=${phoneNumber}`;
-        const webWhatsappUrl = `https://wa.me/${phoneNumber}`;
-
-        try {
-            const canOpenWhatsApp = await Linking.canOpenURL(whatsappUrl);
-            if (canOpenWhatsApp) {
-                await Linking.openURL(whatsappUrl);
-            } else {
-                // If WhatsApp app URL fails, try the web URL
-                await Linking.openURL(webWhatsappUrl);
-            }
-        } catch (error) {
-            console.error('Error opening WhatsApp:', error);
-            // Fallback to showing the number if both methods fail
-            Alert.alert('聯絡我們', `請通過WhatsApp聯繫我們:${phoneNumber}`, [
-                { text: '複製號碼', onPress: () => Clipboard.setString(phoneNumber) },
-                { text: '取消', style: 'cancel' }
-            ]);
-        }
-    };
 
     return (
         <SafeAreaView className="flex-1 bg-white" edges={['top', 'right', 'left']}>
@@ -58,7 +24,7 @@ const AssistancePageComponent = () => {
                     <Text className="text-lg">
                         如果您在使用該應用程式時遇到任何問題,請透過WhatsApp聯絡我們的客戶服務人員:
                     </Text>
-                    <Pressable onPress={handlePress}>
+                    <Pressable onPress={handleGoWhatsApp}>
                         <Text className="text-3xl font-semibold underline text-[#02677D] pt-2">+852 9138 2139</Text>
                     </Pressable>
                 </View>

+ 35 - 34
eas.json

@@ -1,34 +1,35 @@
-{
-    "cli": {
-        "version": ">= 14.2.0",
-        "appVersionSource": "remote"
-    },
-    "build": {
-        "development": {
-            "developmentClient": true,
-            "distribution": "internal",
-            "channel": "development"
-        },
-        "preview": {
-            "ios": {
-                "simulator": false,
-                "enterpriseProvisioning": "adhoc"
-            },
-            "android": {
-                "buildType": "apk",
-                "gradleCommand": ":app:assembleRelease"
-            },
-            "distribution": "internal",
-            "channel": "preview"
-        },
-        "production": {
-            "autoIncrement": true,
-            "android": {
-                "buildType": "apk"
-            }
-        }
-    },
-    "submit": {
-        "production": {}
-    }
-}
+{
+  "cli": {
+    "version": ">= 14.2.0",
+    "appVersionSource": "remote"
+  },
+  "build": {
+    "development": {
+      "developmentClient": true,
+      "distribution": "internal",
+      "channel": "development"
+    },
+    "preview": {
+      "ios": {
+        "simulator": false,
+        "enterpriseProvisioning": "adhoc"
+      },
+      "android": {
+        "buildType": "apk",
+        "gradleCommand": ":app:assembleRelease"
+      },
+      "distribution": "internal",
+      "channel": "preview"
+    },
+    "production": {
+      "autoIncrement": true,
+      "android": {
+        "buildType": "apk"
+      },
+      "channel": "production"
+    }
+  },
+  "submit": {
+    "production": {}
+  }
+}

+ 20 - 8
util/index.ts

@@ -1,15 +1,27 @@
 import { Linking, Alert } from 'react-native';
 
 export const handleGoWhatsApp = async () => {
-  const phoneWithCountryCode = '8528691382139'; // 不带 "+",如 +60 改成 60(马来西亚)
-  const message = 'Hello!';
+  const phoneWithCountryCode = '85291382139'; // 不带 "+",如 +60 改成 60(马来西亚)
+  const message = null;
 
-  const url = `whatsapp://send?phone=${phoneWithCountryCode}&text=${encodeURIComponent(message)}`;
+  const whatsappUrl = `whatsapp://send?phone=${phoneWithCountryCode}&text=${encodeURIComponent(message)}`;
+  const webWhatsappUrl = `https://wa.me/${phoneWithCountryCode}`;
 
-  const supported = await Linking.canOpenURL(url);
-  if (supported) {
-    await Linking.openURL(url);
-  } else {
-    Alert.alert('WhatsApp 未安裝', '請先安裝 WhatsApp 應用');
+  try {
+    const supported = await Linking.canOpenURL(whatsappUrl);
+    if (supported) {
+      await Linking.openURL(whatsappUrl);
+    } else {
+      // If WhatsApp app URL fails, try the web URL
+      await Linking.openURL(webWhatsappUrl);
+      Alert.alert('WhatsApp 未安裝', '請先安裝 WhatsApp 應用');
+    }
+  } catch (error) {
+    console.error('Error opening WhatsApp:', error);
+    // Fallback to showing the number if both methods fail
+    Alert.alert('聯絡我們', `請通過WhatsApp聯繫我們:${phoneWithCountryCode}`, [
+        { text: '複製號碼', onPress: () => Clipboard.setString(phoneWithCountryCode) },
+        { text: '取消', style: 'cancel' }
+    ]);
   }
 };