forked from UpstreamInno/guardian_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
192 lines (174 loc) · 5.78 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
import {
// AppLoading, temporary remove apploading -> not working on expo sdk
Updates,
} from "expo";
import { Asset } from "expo-asset";
import * as Font from "expo-font";
import React, { useState } from "react";
import {
Platform,
StatusBar,
StyleSheet,
View,
AsyncStorage,
I18nManager as RNI18nManager,
} from "react-native";
import { Ionicons } from "@expo/vector-icons";
import { Provider } from "react-redux";
import GuardianContainer from "Components/GuardianContainer";
import { configureStore } from "Store";
import i18n from 'Lib/i18n';
import Job from "Lib/Job";
import BackgroundFetch from "react-native-background-fetch";
export const scheduleTask = async (name) => {
try {
await BackgroundFetch.scheduleTask({
taskId: name,
stopOnTerminate: false,
startOnBoot: true,
enableHeadless: true,
delay: 60 * 60 * 1000, // milliseconds (5min)
forceAlarmManager: true,
forceReload: true, // more precise timing with AlarmManager vs default JobScheduler
periodic: true, // Fire once only.
});
} catch (e) {
console.warn("[BackgroundFetch] scheduleTask fail", e);
}
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
});
async function loadResourcesAsync() {
await Promise.all([
Asset.loadAsync([
require("./assets/images/robot-dev.png"),
require("./assets/images/robot-prod.png"),
]),
Font.loadAsync({
// This is the font that we are using for our tab bar
...Ionicons.font,
// We include SpaceMono because we use it in HomeScreen.js. Feel free to
// remove this if you are not using it in your app
"space-mono": require("./assets/fonts/SpaceMono-Regular.ttf"),
}),
]);
}
function handleLoadingError(error) {
// In this case, you might want to report the error to your error reporting
// service, for example Sentry
console.warn(error);
}
function handleFinishLoading(setLoadingComplete) {
setLoadingComplete(true);
}
const store = configureStore();
const App = (props) => {
const [isLoadingComplete, setLoadingComplete] = useState(false);
const [isI18nInitialized, setIsI18nInitialized] = useState(false);
const [enabled, setEnabled] = useState(false);
const { skipLoadingScreen } = props;
/// Switch handler in top-toolbar.
///
const onToggleEnabled = async (value) => {
try {
if (value) {
console.log("BackgroundFetch","start");
await BackgroundFetch.start();
} else {
await BackgroundFetch.stop();
console.log("BackgroundFetch","stop");
}
setEnabled(value);
} catch (e) {
console.warn(`[BackgroundFetch] ${value ? "start" : "stop"} falied`, e);
}
};
/// BackgroundFetch event-handler.
/// All events from the plugin arrive here, including #scheduleTask events.
///
const onBackgroundFetchEvent = async (taskId) => {
console.log("[BackgroundFetch] Event received: ", taskId);
// var taskText = await AsyncStorage.getItem("task");
// taskText = taskText + "\n----------s1" +JSON.stringify(new Date());
// var save = await AsyncStorage.setItem("task", taskText);
if (taskId === 'react-native-background-fetch') {
// Test initiating a #scheduleTask when the periodic fetch event is received.
try {
await scheduleTask("com.transistorsoft.customtask");
} catch (e) {
console.warn("[BackgroundFetch] scheduleTask falied", e);
}
}
// Required: Signal completion of your task to native code
// If you fail to do this, the OS can terminate your app
// or assign battery-blame for consuming too much background-time
BackgroundFetch.finish(taskId);
};
/// Configure BackgroundFetch
///
const init = async () => {
// var taskText = await AsyncStorage.getItem("task");
// alert(taskText);
BackgroundFetch.configure({
minimumFetchInterval: 15, // <-- minutes (15 is minimum allowed)
// Android options
forceAlarmManager: false, // <-- Set true to bypass JobScheduler.
stopOnTerminate: false,
enableHeadless: true,
startOnBoot: true,
forceReload:true,
requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE, // Default
requiresCharging: false, // Default
requiresDeviceIdle: false, // Default
requiresBatteryNotLow: false, // Default
requiresStorageNotLow: false, // Default
}, onBackgroundFetchEvent, async (status) => {
// setDefaultStatus(statusToString(status))
Job.executeTasks();
});
// Turn on the enabled switch.
onToggleEnabled(true);
// Load the list with persisted events.
// const events = await loadEvents<Event[]>();
// events && setEvents(events);
};
React.useEffect(() => {
init();
i18n
.init()
.then(() => {
const RNDir = RNI18nManager.isRTL ? "RTL" : "LTR";
if (i18n.dir !== RNDir) {
const isLocaleRTL = i18n.dir === "RTL";
RNI18nManager.forceRTL(isLocaleRTL);
Updates.reloadFromCache();
}
setIsI18nInitialized(true);
})
.catch((error) => console.warn(error));
},[]);
// if (!isLoadingComplete && !skipLoadingScreen && !isI18nInitialized) { // AppLoading, temporary remove apploading -> not working on expo sdk
// return (
// <AppLoading
// startAsync={loadResourcesAsync}
// onError={handleLoadingError}
// onFinish={() => handleFinishLoading(setLoadingComplete)}
// />
// );
// }
return (
<Provider store={store}>
<React.Suspense fallback="loading">
<View style={styles.container}>
{Platform.OS === "ios" && <StatusBar barStyle="default" />}
<GuardianContainer />
</View>
</React.Suspense>
</Provider>
);
};
export default App;