-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
235 lines (217 loc) · 7.83 KB
/
App.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import React, { Component } from 'react';
import { View, Image, Dimensions, Alert, Linking, Appearance, AppState, } from 'react-native';
// 本地引用
import Nav from './src/Nav';
import RootStore from './src/mobx';
import { COLOR_DIY, isLight, uiStyle, } from './src/utils/uiMap';
import { BASE_HOST } from './src/utils/pathMap';
import { setLanguage, setLocalStorage } from './src/i18n/i18n';
import { checkLocalCourseVersion, } from './src/utils/checkCoursesKits';
import { Provider } from 'mobx-react';
import AnimatedSplash from 'react-native-animated-splash-screen';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { scale } from 'react-native-size-matters';
import Toast, { BaseToast, ErrorToast, } from 'react-native-toast-message';
import RNRestart from 'react-native-restart';
import { t } from 'i18next';
// Initialize Clarity.
// import { initialize } from 'react-native-clarity';
// initialize("il9ynzl9gn");
const { bg_color } = COLOR_DIY;
const { width: PAGE_WIDTH } = Dimensions.get('window');
const LOGO_WIDTH = PAGE_WIDTH * 0.5;
// 自定義Toast外觀
const toastConfig = {
arkToast: (props) => (
<BaseToast
{...props}
style={{
borderLeftColor: COLOR_DIY.themeColor, backgroundColor: COLOR_DIY.white,
width: '80%', height: scale(60),
}}
contentContainerStyle={{ paddingHorizontal: scale(15) }}
text1Style={{
...uiStyle.defaultText,
color: COLOR_DIY.black.main,
fontSize: scale(15),
}}
text2Style={{
...uiStyle.defaultText,
color: COLOR_DIY.black.third,
fontSize: scale(10),
}}
/>
),
error: (props) => (
<ErrorToast
{...props}
style={{
borderLeftColor: COLOR_DIY.unread,
backgroundColor: COLOR_DIY.white,
width: '80%', height: scale(60),
}}
text1Style={{
...uiStyle.defaultText,
color: COLOR_DIY.black.main,
fontSize: scale(15),
}}
text2Style={{
...uiStyle.defaultText,
color: COLOR_DIY.black.main,
fontSize: scale(10),
}}
/>
),
warning: (props) => (
<BaseToast
{...props}
style={{
borderLeftColor: COLOR_DIY.warning, backgroundColor: COLOR_DIY.white,
width: '80%', height: scale(60),
}}
contentContainerStyle={{ paddingHorizontal: scale(15) }}
text1Style={{
...uiStyle.defaultText,
color: COLOR_DIY.black.main,
fontSize: scale(15),
}}
text2Style={{
...uiStyle.defaultText,
color: COLOR_DIY.black.third,
fontSize: scale(10),
}}
/>
),
};
class App extends Component {
state = {
isLoaded: false,
isLogin: false,
versionLock: false,
languageOK: false,
scheme: Appearance.getColorScheme(),
};
async componentDidMount() {
// 開屏動畫
setTimeout(() => {
this.setState({ isLoaded: true });
}, 500);
// 獲取緩存中的用戶數據
try {
const strUserInfo = await AsyncStorage.getItem('userInfo');
const userInfo = strUserInfo ? JSON.parse(strUserInfo) : {};
// 判斷有無登錄token
if (userInfo.stdData || userInfo.clubData) {
// 把緩存中的數據存一份到mobx
// console.log('有登錄token,需存到mobx');
this.setState({ isLogin: true });
RootStore.setUserInfo(userInfo);
} else {
// console.log('無用戶token');
this.setState({ isLogin: false });
}
this.checkLanguage();
// 檢查APP靜態文件的課程更新時間和緩存數據新舊,取最新
checkLocalCourseVersion();
} catch (e) {
console.error('App error', e);
}
this.listener = Appearance.addChangeListener((theme) => {
let scheme = theme.colorScheme;
this.setState({ scheme });
this.schemeChange();
});
this.screenListener = AppState.addEventListener('change', nextAppState => {
this.schemeChange();
})
}
componentWillUnmount() {
this.listener.remove();
this.screenListener.remove();
}
setLock = app_version => {
Alert.alert(
"Please update the app!",
`Latest version: ${app_version}\nYou need the latest version to continue!\n\nDo you want to update it now?
`,
[
// The "Yes" button
{
text: "Yes",
onPress: () => {
// 跳轉到主頁下載新版本
Linking.openURL(BASE_HOST)
},
},
// The "No" button
// Does nothing but dismiss the dialog when tapped
{
text: "No",
},
]
);
this.setState({ versionLock: true });
};
// 啟動檢查語言設置
checkLanguage = async () => {
await AsyncStorage.getItem('language').then(res => {
const lng = JSON.parse(res);
if (!lng) {
Alert.alert('語言設定 / Language Setting', '挑選您的首選語言\nPick your preferred languaeg(English version not fully translated)\n您稍後可以在關於頁再修改!\nYou can modify it later on the About page!', [
{
text: '繁體中文', onPress: () => {
setLocalStorage('tc');
this.setState({ languageOK: true });
}
},
{ text: 'English', onPress: () => setLanguage('en') },
]);
} else {
this.setState({ languageOK: true })
}
})
}
schemeChange = () => {
if (AppState.currentState == 'active' && (isLight != (this.state.scheme == 'light'))) {
Alert.alert(`ARK ALL`, `${t('現在重啟APP切換到')} ${this.state.scheme == 'light' ? t('淺色模式') : t('深色模式')} ?`, [
{
text: 'Yes', onPress: () => {
RNRestart.Restart();
}
},
{ text: 'No', },
])
}
}
render() {
return (
// 開屏動畫
<AnimatedSplash
translucent={true}
isLoaded={this.state.isLoaded}
customComponent={
<Image
source={require('./src/static/img/logo.png')}
style={{
width: LOGO_WIDTH,
height: LOGO_WIDTH,
borderRadius: scale(40)
}}
/>
}
backgroundColor={bg_color}>
<SafeAreaProvider>
{/* 全局變量 */}
{this.state.languageOK ? (
<Provider RootStore={RootStore}>
<Nav lock={this.state.versionLock} setLock={this.setLock} />
<Toast config={toastConfig} />
</Provider>
) : null}
</SafeAreaProvider>
</AnimatedSplash>
);
}
}
export default App;