-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
90 lines (77 loc) · 2.57 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
import React, {Component, ReactNode} from 'react';
import {createAppContainer} from 'react-navigation';
import {StatusBar} from 'react-native';
import {ThemeContext, ThemeType} from './src/components/themeContext/theme-context';
import {observer} from "mobx-react";
import {action, observable} from "mobx";
import {Appearance} from "react-native-appearance";
import {createNavigator} from "./src/components/navigator/create-navigator";
import {AppContext, AppTheme, SettingsManager} from "./src/providers/settings";
import {NotificationManager} from "./src/notifications/NotificationManager";
@observer
export default class App extends Component {
@observable
private navigator;
@observable
private isInitialized: boolean = false;
@observable
private themeType: ThemeType;
private settingsManager: SettingsManager;
private notificationManager: NotificationManager;
constructor(props: object) {
super(props);
this.settingsManager = new SettingsManager();
this.notificationManager = new NotificationManager();
this.settingsManager.load().then(async (settings: AppContext) => {
const theme = settings.theme;
const newThemeType = App.getThemeType(theme);
this.updateThemeType(newThemeType);
this.isInitialized = true;
if (settings.pushNotifications) {
// restart notifications
await this.notificationManager.start();
}
})
}
private static getThemeType(theme: AppTheme) {
let newTheme;
// get theme type from system
if (theme === AppTheme.SYSTEM) {
const colorScheme = Appearance.getColorScheme();
newTheme = (colorScheme === 'light' || colorScheme === 'no-preference') ? 'light' : 'dark';
} else { // user set theme
newTheme = theme; // light or dark
}
return newTheme;
}
@action
private updateThemeType(newThemeType: ThemeType) {
this.themeType = newThemeType;
this.navigator = createNavigator(this.themeType);
}
public async componentWillUnmount() {
}
public componentDidMount() {
// set color for login screen
StatusBar.setBackgroundColor('#496c92');
}
public render(): ReactNode {
if (!this.isInitialized) {
return null;
}
return (
<ThemeContext.Provider value={{
theme: this.themeType,
setTheme: action((newThemeType) => {
if (this.themeType !== newThemeType) {
this.updateThemeType(newThemeType);
}
}),
}}>
{
React.createElement(createAppContainer(this.navigator))
}
</ThemeContext.Provider>
);
}
}