-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
141 lines (129 loc) · 3.92 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
import React from 'react';
import {StyleSheet, Dimensions, View} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
//library for time/date/...
import Moment from 'moment';
//vasern db
import VasernDB, {ToDoDB} from 'db_vasern';
//where screens get loaded
import Main from './src';
//redux
import {Provider} from 'react-redux';
import store from './src/redux/store';
import {refreshAllLists, startRefreshing} from './src/redux/actions';
//interfaces
import {SavedToDoI} from 'res';
import {ActivityIndicator} from 'react-native';
//format for moment
const FORMAT = 'DD-MM-YYYY';
const LATEST_DATE = 'latestDate';
interface StateI {
latestDate: string;
loadedData: boolean;
}
class App extends React.Component<{}, StateI> {
state = {
latestDate: Moment().format(FORMAT),
loadedData: false,
};
componentDidMount() {
//first call on db and set to-dos
VasernDB.onLoaded(() => {
//loaded vasern data
this.setState({loadedData: true});
//make calculations
this.makeCalculations();
});
}
/* look if date changed and refresh ToDos if necessary */
/* refresh ^= refresh all views and set all-day to-dos done to false plus
refresh streaks if necessary (reset currentStreak to 0 if not done that day) */
makeCalculations = async () => {
store.dispatch(startRefreshing());
try {
//get last saved date
let latestDate = await AsyncStorage.getItem(LATEST_DATE);
if (latestDate !== null) {
if (!Moment(latestDate, FORMAT).isSame(Moment(), 'day')) {
/*
date has changed
*/
//update date in AsyncStorage and state
const newLatestDate = Moment().format(FORMAT);
await AsyncStorage.setItem(LATEST_DATE, newLatestDate);
//change all done of daily events to undone
let toDos = ToDoDB.data() as SavedToDoI[];
for (const toDo of toDos) {
if (toDo.recurrence_id && toDo.done === true) {
await ToDoDB.update(toDo.id, {done: false});
}
}
//refresh views
store.dispatch(refreshAllLists());
} else {
/*
date has not changed
*/
//get and set to-dos
store.dispatch(refreshAllLists());
}
} else {
/*
LATEST_DATE not set => first launch of application
*/
const newLatestDate = Moment().format(FORMAT);
await AsyncStorage.setItem(LATEST_DATE, newLatestDate);
}
} catch (error) {
alert('Sorry, an error occured!');
}
};
//have to build a button to refresh views and date
refreshAndCheckDate = async () => {
let latestDate = await AsyncStorage.getItem(LATEST_DATE);
//check date
if (!Moment(latestDate, FORMAT).isSame(Moment(), 'day')) {
/*
date has changed
*/
//update date in AsyncStorage and state
const newLatestDate = Moment().format(FORMAT);
await AsyncStorage.setItem(LATEST_DATE, newLatestDate);
//change all done of daily events to undone
let toDos = ToDoDB.data() as SavedToDoI[];
for (const toDo of toDos) {
if (toDo.recurrence_id && toDo.done === true) {
await ToDoDB.update(toDo.id, {done: false});
}
}
//refresh views
store.dispatch(refreshAllLists());
} else {
/*
date has not changed
*/
//get and set to-dos
store.dispatch(refreshAllLists());
}
};
render() {
return (
<Provider store={store}>
{store.getState().toDos.refreshing && (
<View style={styles.activityIndicatorContainer}>
<ActivityIndicator />
</View>
)}
{this.state.loadedData && <Main />}
</Provider>
);
}
}
const styles = StyleSheet.create({
activityIndicatorContainer: {
position: 'absolute',
left: Dimensions.get('screen').width / 2 - 10,
top: 100,
},
});
export default App;