-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.tsx
More file actions
130 lines (117 loc) · 5.38 KB
/
Main.tsx
File metadata and controls
130 lines (117 loc) · 5.38 KB
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
import React, { useEffect, useRef, useState, useContext } from 'react';
import { StyleSheet, SafeAreaView, AppState, AppStateStatus } from 'react-native';
import RNRestart from 'react-native-restart';
import moment, { Moment } from 'moment';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator, StackNavigationOptions } from '@react-navigation/stack';
import { HomeScreen } from './screens/HomeScreen';
import { NoteMainScreen } from './screens/NoteMainScreen';
import { NewNoteScreen } from './screens/NewNoteScreen';
import { BrowseNoteScreen } from './screens/BrowseNoteScreen';
import { ImportNoteScreen } from './screens/ImportNoteScreen';
import { NoteDetailScreen } from './screens/NoteDetailScreen';
import { SearchExistingNotesScreen } from './screens/SearchExistingNotesScreen';
import { RestoreCloudScreen } from './screens/RestoreCloudScreen';
import { useTranslation } from 'react-i18next';
import { GluestackUIProvider } from '@gluestack-ui/themed';
import { config } from '@gluestack-ui/config';
import theme from './resources/theme.json';
import { MynoteContext } from './context/mynoteContext';
import { MynoteContextType, ScreenType } from './@types/mynote.d';
type RouteParams = {
itemid: string;
notetag: string;
backto: string;
};
type RootStackParamList = {
NoteDetail: RouteParams;
Home: undefined;
NoteMain: undefined;
NewNote: undefined;
BrowseNote: undefined;
SearchExistingNotes: undefined;
ImportNote: undefined;
RestoreCloud: undefined;
};
const Stack = createStackNavigator<RootStackParamList>();
const styles = StyleSheet.create({
container: {
flex: 1,
},
text: {
fontSize: 25,
fontWeight: '500',
},
});
function Main(): JSX.Element {
const appState = useRef<AppStateStatus>(AppState.currentState);
const [offlineAt, setOfflineAt] = useState<Moment | null>(null);
const { mynoteConfig, changeScreen } = useContext(MynoteContext) as MynoteContextType;
const { t } = useTranslation();
useEffect(() => {
const subscription = AppState.addEventListener('change', (nextAppState: AppStateStatus) => {
if (appState.current.match(/inactive|background/) && nextAppState === 'active') {
const now = moment();
const diff = now.diff(offlineAt, 'seconds');
if (diff > theme.session_timeout && mynoteConfig.currentScreen !== ScreenType.HOME) {
RNRestart.Restart();
changeScreen(ScreenType.HOME);
}
} else if (appState.current === 'active' && nextAppState.match(/inactive|background/)) {
const now = moment();
setOfflineAt(now);
}
appState.current = nextAppState;
});
return () => {
subscription.remove();
};
}, [offlineAt, changeScreen, mynoteConfig.currentScreen]);
return (
<SafeAreaView style={styles.container}>
<GluestackUIProvider config={config}>
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} options={{ headerShown: false } as StackNavigationOptions} />
<Stack.Screen
name="NoteMain"
component={NoteMainScreen}
options={{ title: t('note_main'), headerTintColor: theme.major_text_color } as StackNavigationOptions}
/>
<Stack.Screen
name="NewNote"
component={NewNoteScreen}
options={{ title: t('new_note'), headerTintColor: theme.major_text_color } as StackNavigationOptions}
/>
<Stack.Screen
name="BrowseNote"
component={BrowseNoteScreen}
options={{ title: t('browse_note'), headerTintColor: theme.major_text_color } as StackNavigationOptions}
/>
<Stack.Screen
name="NoteDetail"
component={NoteDetailScreen}
options={{ headerShown: false } as StackNavigationOptions}
/>
<Stack.Screen
name="SearchExistingNotes"
component={SearchExistingNotesScreen}
options={{ headerShown: false } as StackNavigationOptions}
/>
<Stack.Screen
name="ImportNote"
component={ImportNoteScreen}
options={{ title: t('import_note'), headerTintColor: theme.major_text_color } as StackNavigationOptions}
/>
<Stack.Screen
name="RestoreCloud"
component={RestoreCloudScreen}
options={{ title: t('restore_cloud'), headerTintColor: theme.major_text_color } as StackNavigationOptions}
/>
</Stack.Navigator>
</NavigationContainer>
</GluestackUIProvider>
</SafeAreaView>
);
}
export default Main;