This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
173 lines (156 loc) · 5.07 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* Generated with the TypeScript template
* https://github.com/react-native-community/react-native-template-typescript
*
* @format
*/
import React, {useEffect} from 'react';
import {NavigationContainer, NavigationState} from '@react-navigation/native';
import AppNavigator from 'src/navigators';
import SplashScreen from 'react-native-splash-screen';
import {Provider as PaperProvider} from 'react-native-paper';
import {
Linking,
Platform,
useColorScheme,
InteractionManager,
View,
AppState,
NativeModules,
} from 'react-native';
import {
CombinedDefaultTheme,
CombinedDarkTheme,
appConfig,
appColors,
} from 'src/config';
import {MMKV} from 'react-native-mmkv';
import {storage, PERSISTENCE_KEY} from 'src/utils';
import {GestureHandlerRootView} from 'react-native-gesture-handler';
import codePush from 'react-native-code-push';
import {ApiProvider} from 'src/components';
import {useAppAuth} from 'src/store';
const Service: {
launchService: (arg: string) => void;
closeService: () => void;
} = NativeModules.BluetoothServiceLauncher;
const App = () => {
const isDarkMode = useColorScheme() === 'dark';
const [isReady, setIsReady] = React.useState(false);
const [initialState, setInitialState] = React.useState();
const theme = isDarkMode ? CombinedDarkTheme : CombinedDefaultTheme;
const appState = React.useRef(AppState.currentState);
const {hydrateState, peripheralValue, errorMessage, setErrorMessage} =
useAppAuth(state => ({
hydrateState: state.hydrateState,
peripheralValue: state.peripheralAddress,
errorMessage: state.errorMessage,
setErrorMessage: state.setErrorMessage,
}));
console.log('errrorrr', errorMessage);
const handleStateChange = (state?: NavigationState) => {
if (state) {
// if (
// state.routes[state.index]?.state?.routes[
// state.routes[state?.index]?.state?.index
// ]?.state?.routes[state.routes[state.index]?.state?.routes[
// state.routes[state?.index]?.state?.index
// ]?.state?.index]?.params?.peripheralId
// ) {
// peripheralId.current = state.routes[state.index].params?.peripheralId;
// }
InteractionManager.runAfterInteractions(() => {
storage.set(PERSISTENCE_KEY, JSON.stringify(state));
});
}
};
React.useEffect(() => {
console.log('peripheralValue -> ', peripheralValue);
const subscription = AppState.addEventListener('change', nextAppState => {
if (
appState.current === 'active' &&
nextAppState.match(/inactive|background/)
) {
appState.current = nextAppState;
//call background service
storage.set('APP_AUTH', JSON.stringify(useAppAuth.getState()));
if (peripheralValue) {
console.log('Service running...');
Platform.OS === 'android' && Service.launchService(peripheralValue);
}
}
});
return () => {
subscription.remove();
};
}, [peripheralValue]);
React.useEffect(() => {
const subscription = AppState.addEventListener('change', nextState => {
if (nextState === 'active') {
appState.current = nextState;
Platform.OS === 'android' && Service.closeService();
const stringifiedState = storage.getString('APP_AUTH');
const persistedState:
| {isSignedIn: boolean; token?: string}
| undefined = stringifiedState
? JSON.parse(stringifiedState)
: undefined;
if (persistedState) {
hydrateState(persistedState);
}
}
});
return () => {
subscription.remove();
};
}, [hydrateState]);
const restoreState = React.useCallback(async (mmkv: MMKV) => {
try {
const initialUrl = await Linking.getInitialURL();
if (Platform.OS !== 'web' && initialUrl == null) {
// Only restore state if there's no deep link and we're not on web
const savedStateString = mmkv.getString(PERSISTENCE_KEY);
const state = savedStateString
? JSON.parse(savedStateString)
: undefined;
if (state) {
setInitialState(state);
}
}
} finally {
setIsReady(true);
SplashScreen.hide();
}
}, []);
useEffect(() => {
if (!isReady) {
restoreState(storage);
}
}, [isReady, restoreState]);
if (!isReady && appConfig.IS_ANDROID) {
return <View style={{flex: 1, backgroundColor: appColors.background}} />;
}
return (
<GestureHandlerRootView style={{flex: 1}}>
<PaperProvider theme={theme}>
<NavigationContainer
initialState={initialState}
onStateChange={handleStateChange}
theme={theme}>
<ApiProvider>
<AppNavigator />
</ApiProvider>
</NavigationContainer>
</PaperProvider>
</GestureHandlerRootView>
);
};
// export default codePush({
// checkFrequency: codePush.CheckFrequency.ON_APP_RESUME,
// installMode: codePush.InstallMode.IMMEDIATE,
// })(App);
export default App;
const sub1 = useAppAuth.subscribe(console.log);