mmkv bug fix
This commit is contained in:
245
App.tsx
245
App.tsx
@@ -1,4 +1,5 @@
|
||||
// App.tsx
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { NavigationContainer } from '@react-navigation/native';
|
||||
import { createNativeStackNavigator } from '@react-navigation/native-stack';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
@@ -7,18 +8,18 @@ import { navigationRef } from 'components/NavigationRef';
|
||||
import i18n from 'i18n/i18n';
|
||||
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { I18nextProvider } from 'react-i18next';
|
||||
import { Animated, Dimensions, LogBox, StatusBar, View } from 'react-native';
|
||||
import {
|
||||
Animated,
|
||||
Dimensions,
|
||||
LogBox,
|
||||
StatusBar,
|
||||
StyleSheet,
|
||||
View,
|
||||
} from 'react-native';
|
||||
import Toast from 'react-native-toast-message';
|
||||
import SplashScreen from './src/components/SplashScreen';
|
||||
|
||||
// Screens
|
||||
import {
|
||||
authEvents,
|
||||
getLanguage,
|
||||
getToken,
|
||||
loadInitialAuthData,
|
||||
storage,
|
||||
} from 'helpers/event';
|
||||
import Login from 'screens/auth/login/ui';
|
||||
import Confirm from 'screens/auth/login/ui/Confirm';
|
||||
import Register from 'screens/auth/registeration/ui';
|
||||
@@ -46,81 +47,137 @@ import PaymentQrCode from 'screens/wallet/successPayment/ui/PaymentQrCode';
|
||||
import Onboarding from 'screens/welcome/Onboarding';
|
||||
|
||||
LogBox.ignoreLogs([
|
||||
'Non-serializable values were found in the navigation state',
|
||||
'ViewPropTypes will be removed',
|
||||
// NOTE: I recommend NOT ignoring "Non-serializable values were found in the navigation state"
|
||||
// because it hides bugs that can cause crashes. Keep only if you understand the implications.
|
||||
]);
|
||||
|
||||
const Stack = createNativeStackNavigator();
|
||||
const screenWidth = Dimensions.get('window').width;
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
// const saveNotification = async (remoteMessage: any) => {
|
||||
// try {
|
||||
// const stored = await AsyncStorage.getItem('notifications');
|
||||
// const notifications = stored ? JSON.parse(stored) : [];
|
||||
|
||||
// const newNotification = {
|
||||
// id: Date.now(),
|
||||
// title:
|
||||
// remoteMessage.notification?.title ||
|
||||
// remoteMessage.data?.title ||
|
||||
// 'Yangi bildirishnoma',
|
||||
// message:
|
||||
// remoteMessage.notification?.body ||
|
||||
// remoteMessage.data?.body ||
|
||||
// 'Matn yo‘q',
|
||||
// sentTime: remoteMessage.sentTime || Date.now(),
|
||||
// };
|
||||
|
||||
// await AsyncStorage.setItem(
|
||||
// 'notifications',
|
||||
// JSON.stringify([newNotification, ...notifications]),
|
||||
// );
|
||||
// } catch (e) {
|
||||
// console.error('Notification saqlashda xato:', e);
|
||||
// }
|
||||
// };
|
||||
|
||||
// async function onDisplayNotification(remoteMessage: any) {
|
||||
// const channelId = await notifee.createChannel({
|
||||
// id: 'default',
|
||||
// name: 'Umumiy bildirishnomalar',
|
||||
// sound: 'default',
|
||||
// importance: AndroidImportance.HIGH,
|
||||
// });
|
||||
|
||||
// await notifee.displayNotification({
|
||||
// title:
|
||||
// remoteMessage.notification?.title ||
|
||||
// remoteMessage.data?.title ||
|
||||
// 'Yangi xabar',
|
||||
// body:
|
||||
// remoteMessage.notification?.body ||
|
||||
// remoteMessage.data?.body ||
|
||||
// 'Matn yo‘q',
|
||||
// android: {
|
||||
// channelId,
|
||||
// largeIcon: 'ic_launcher_foreground',
|
||||
// sound: 'default',
|
||||
// pressAction: {
|
||||
// id: 'default',
|
||||
// },
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
|
||||
// async function requestNotificationPermission() {
|
||||
// if (Platform.OS === 'android' && Platform.Version >= 33) {
|
||||
// const granted = await PermissionsAndroid.request(
|
||||
// PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
|
||||
// );
|
||||
// console.log('POST_NOTIFICATIONS permission:', granted);
|
||||
// }
|
||||
// }
|
||||
|
||||
export default function App() {
|
||||
const [initialRoute, setInitialRoute] = useState<string | null>(null);
|
||||
const slideAnim = useRef(new Animated.Value(0)).current;
|
||||
const [isSplashVisible, setIsSplashVisible] = useState(true);
|
||||
const isMounted = useRef(false);
|
||||
const currentAnimation = useRef<Animated.CompositeAnimation | null>(null);
|
||||
const token = getToken();
|
||||
|
||||
useEffect(() => {
|
||||
loadInitialAuthData();
|
||||
}, []);
|
||||
// useEffect(() => {
|
||||
// requestNotificationPermission();
|
||||
|
||||
useEffect(() => {
|
||||
const logoutListener = () => {
|
||||
if (navigationRef.isReady()) {
|
||||
navigationRef.reset({
|
||||
index: 0,
|
||||
routes: [{ name: 'Login' }],
|
||||
});
|
||||
}
|
||||
};
|
||||
// const messagingInstance = getMessaging();
|
||||
|
||||
authEvents.on('logout', logoutListener);
|
||||
// const unsubscribe = onMessage(messagingInstance, async remoteMessage => {
|
||||
// console.log('Foreground message:', remoteMessage);
|
||||
// await saveNotification(remoteMessage);
|
||||
// await onDisplayNotification(remoteMessage);
|
||||
// });
|
||||
|
||||
return () => {
|
||||
authEvents.removeListener('logout', logoutListener);
|
||||
};
|
||||
}, []);
|
||||
// const unsubscribeOpened = onNotificationOpenedApp(
|
||||
// messagingInstance,
|
||||
// remoteMessage => {
|
||||
// console.log('Backgrounddan ochildi:', remoteMessage);
|
||||
// saveNotification(remoteMessage);
|
||||
// },
|
||||
// );
|
||||
|
||||
useEffect(() => {
|
||||
isMounted.current = true;
|
||||
return () => {
|
||||
isMounted.current = false;
|
||||
if (currentAnimation.current) {
|
||||
currentAnimation.current.stop();
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
// (async () => {
|
||||
// const remoteMessage = await getInitialNotification(messagingInstance);
|
||||
// if (remoteMessage) {
|
||||
// console.log('Killeddan ochildi:', remoteMessage);
|
||||
// saveNotification(remoteMessage);
|
||||
// }
|
||||
// })();
|
||||
|
||||
// return () => {
|
||||
// unsubscribe();
|
||||
// unsubscribeOpened();
|
||||
// };
|
||||
// }, []);
|
||||
|
||||
useEffect(() => {
|
||||
const initializeApp = async () => {
|
||||
try {
|
||||
const seen = storage.getString('hasSeenOnboarding');
|
||||
const token = getToken();
|
||||
const lang = getLanguage();
|
||||
const [seen, token, lang] = await Promise.all([
|
||||
AsyncStorage.getItem('hasSeenOnboarding'),
|
||||
AsyncStorage.getItem('token'),
|
||||
|
||||
if (lang) {
|
||||
try {
|
||||
await i18n.changeLanguage(lang);
|
||||
} catch (e) {
|
||||
console.warn('i18n.changeLanguage failed:', e);
|
||||
}
|
||||
}
|
||||
AsyncStorage.getItem('language'),
|
||||
]);
|
||||
|
||||
if (lang) await i18n.changeLanguage(lang);
|
||||
|
||||
const initialRouteName = !seen
|
||||
? 'Onboarding'
|
||||
: token
|
||||
? 'Home'
|
||||
: 'select-auth';
|
||||
|
||||
if (isMounted.current) {
|
||||
setInitialRoute(initialRouteName);
|
||||
}
|
||||
setInitialRoute(initialRouteName);
|
||||
} catch (error) {
|
||||
console.error('App initialization error:', error);
|
||||
if (isMounted.current) setInitialRoute('select-auth');
|
||||
setInitialRoute('select-auth');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -129,50 +186,52 @@ export default function App() {
|
||||
|
||||
const handleSplashFinish = useMemo(
|
||||
() => () => {
|
||||
// create animation and keep ref so we can stop it on unmount
|
||||
const animation = Animated.timing(slideAnim, {
|
||||
Animated.timing(slideAnim, {
|
||||
toValue: -screenWidth,
|
||||
duration: 600,
|
||||
useNativeDriver: true,
|
||||
});
|
||||
currentAnimation.current = animation;
|
||||
animation.start(() => {
|
||||
// ensure component still mounted before setting state
|
||||
if (isMounted.current) {
|
||||
setIsSplashVisible(false);
|
||||
}
|
||||
currentAnimation.current = null;
|
||||
});
|
||||
}).start(() => setIsSplashVisible(false));
|
||||
},
|
||||
[slideAnim],
|
||||
);
|
||||
|
||||
const handleOnboardingFinish = useMemo(
|
||||
() => async (navigation: any) => {
|
||||
try {
|
||||
storage.set('hasSeenOnboarding', 'true');
|
||||
} catch (e) {
|
||||
console.warn('Failed to set hasSeenOnboarding', e);
|
||||
}
|
||||
await AsyncStorage.setItem('hasSeenOnboarding', 'true');
|
||||
navigation.replace('select-auth');
|
||||
},
|
||||
[],
|
||||
);
|
||||
// const [firebaseToken, setFirebseToken] = useState<{
|
||||
// fcmToken: string;
|
||||
// deviceId: string;
|
||||
// deviceName: string;
|
||||
// } | null>();
|
||||
// const app = getApp();
|
||||
// const messaging = getMessaging(app);
|
||||
|
||||
if (!initialRoute || isSplashVisible || !currentAnimation) {
|
||||
return (
|
||||
<View
|
||||
style={{
|
||||
flex: 1,
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
backgroundColor: '#ffff',
|
||||
}}
|
||||
>
|
||||
<SplashScreen onFinish={handleSplashFinish} />
|
||||
</View>
|
||||
);
|
||||
}
|
||||
// const getDeviceData = async () => {
|
||||
// try {
|
||||
// const fcmToken = await getToken(messaging);
|
||||
// return {
|
||||
// fcmToken,
|
||||
// deviceId: await DeviceInfo.getUniqueId(),
|
||||
// deviceName: await DeviceInfo.getDeviceName(),
|
||||
// };
|
||||
// } catch (e) {
|
||||
// console.log('Xato:', e);
|
||||
// return null;
|
||||
// }
|
||||
// };
|
||||
// console.log(firebaseToken);
|
||||
|
||||
// useEffect(() => {
|
||||
// getDeviceData().then(data => {
|
||||
// setFirebseToken(data);
|
||||
// });
|
||||
// }, []);
|
||||
|
||||
if (!initialRoute) return null;
|
||||
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
@@ -183,7 +242,7 @@ export default function App() {
|
||||
<Stack.Navigator
|
||||
screenOptions={{
|
||||
headerShown: false,
|
||||
animation: 'slide_from_right',
|
||||
animation: 'none',
|
||||
gestureEnabled: false,
|
||||
}}
|
||||
initialRouteName={initialRoute}
|
||||
@@ -214,6 +273,9 @@ export default function App() {
|
||||
<Stack.Screen name="PaymentQrCode" component={PaymentQrCode} />
|
||||
<Stack.Screen name="Profile" component={Profile} />
|
||||
<Stack.Screen name="Settings" component={Settings} />
|
||||
{/* {Platform.OS === 'android' && (
|
||||
<Stack.Screen name="Notifications" component={Notifications} />
|
||||
)} */}
|
||||
<Stack.Screen name="Warehouses" component={Warehouses} />
|
||||
<Stack.Screen name="Support" component={Support} />
|
||||
<Stack.Screen name="ListBranches" component={ListBranches} />
|
||||
@@ -229,6 +291,19 @@ export default function App() {
|
||||
</Stack.Navigator>
|
||||
</NavigationContainer>
|
||||
|
||||
{/* Splash transition */}
|
||||
{isSplashVisible && (
|
||||
<Animated.View
|
||||
style={{
|
||||
...StyleSheet.absoluteFillObject,
|
||||
transform: [{ translateX: slideAnim }],
|
||||
zIndex: 10,
|
||||
}}
|
||||
>
|
||||
<SplashScreen onFinish={handleSplashFinish} />
|
||||
</Animated.View>
|
||||
)}
|
||||
|
||||
<Toast config={toastConfig} />
|
||||
</View>
|
||||
</I18nextProvider>
|
||||
|
||||
Reference in New Issue
Block a user