-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
215 lines (201 loc) · 5.65 KB
/
App.js
File metadata and controls
215 lines (201 loc) · 5.65 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
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { useState, useEffect } from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Button,
Alert,
TouchableOpacity,
Image,
AppState,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import dayjs from 'dayjs';
import {Calendar, CalendarList, Agenda} from 'react-native-calendars';
import {read, write} from './src/storage'
import CalendarModel from './src/calendarModel'
import TouchID from 'react-native-touch-id';
const App: () => React$Node = () => {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [calendar, setCalendar] = useState(new CalendarModel());
const [menstruationDays, setMenstruationDays] = useState({});
const [menstruationStats, setMenstruationStats] = useState(null)
const [isLoaded, setIsLoaded] = useState(false);
// Require to re-authenticate after app is inactive more than 10s
useEffect(() => {
const handleStateChange = (nextAppState) => {
if (isAuthenticated && nextAppState !== 'active') {
setTimeout(() => {
if (AppState.currentState !== 'active') {
setIsAuthenticated(false);
}
}, 10000)
}
}
AppState.addEventListener('change', handleStateChange);
return () => {
AppState.removeEventListener('change', handleStateChange);
}
})
// load all data once initially
useEffect(() => {
if (isAuthenticated && !isLoaded) {
read().then(data => {
calendar.load(data);
setMenstruationDays(Object.assign({}, calendar.getMenstruationDays()));
setMenstruationStats(calendar.getIntervalStats());
setIsLoaded(true);
})
}
}, [isAuthenticated, isLoaded]);
// persist data after (but skip if it was never loaded before)
useEffect(() => {
if (isLoaded) {
write(calendar.dump())
setMenstruationStats(calendar.getIntervalStats())
}
}, [menstruationDays]);
let headerText;
if (menstruationStats === null) {
headerText = null;
} else {
headerText = (
<>
<Text style={styles.headerTextFirst}>
Last period was <Text style={styles.headerTextBold}>{menstruationStats.whenWasLastInterval} days ago</Text> lasting {menstruationStats.lengthOfLastInterval} days
</Text>
<Text style={styles.headerTextSecond}>
Usually a period happens every {menstruationStats.p90Interval} days lasting {menstruationStats.p90Length} days
</Text>
</>
)
}
if (!isAuthenticated) {
const handleAuthenticate = () => {
TouchID.authenticate('Authenticate to unlock sensible data', {passcodeFallback: true})
.then(success => {
setIsAuthenticated(true)
})
.catch(error => {
Alert.alert('Authentication Failed. Try Again!');
});
}
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={styles.authenticateContainer}>
<View style={styles.authenticate}>
<TouchableOpacity
onPress={handleAuthenticate}>
<Image
style={{width: 128, height: 128}}
source={require('./resources/logo.png')}
/>
</TouchableOpacity>
<Button
title="Authenticate"
onPress={handleAuthenticate} />
</View>
</SafeAreaView>
</>
)
}
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={styles.main}>
<View style={styles.header}>
{headerText}
</View>
<View style={styles.calendar}>
<CalendarList
firstDay={1}
onDayPress={day => {
if (menstruationDays[day.dateString]) {
calendar.remove(day.dateString);
} else {
calendar.add(day.dateString);
}
setMenstruationDays(Object.assign({}, calendar.getMenstruationDays()))
}}
// Max amount of months allowed to scroll to the past. Default = 50
pastScrollRange={24}
// Max amount of months allowed to scroll to the future. Default = 50
futureScrollRange={1}
// Enable or disable scrolling of calendar list
scrollEnabled={true}
// Enable or disable vertical scroll indicator. Default = false
showScrollIndicator={true}
current={calendar.getToday()}
markedDates={menstruationDays}
// Date marking style [simple/period/multi-dot/custom]. Default = 'simple'
markingType={'period'}
/>
</View>
</SafeAreaView>
</>
);
};
const styles = StyleSheet.create({
authenticateContainer: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
},
authenticate: {
flex: 1,
flexDirection: 'column',
alignItems: 'center',
},
main: {
flex: 1,
},
header: {
flex: 1,
color: Colors.dark,
fontSize: 12,
fontWeight: '300',
padding: 4,
paddingRight: 12,
paddingTop: 30,
borderBottomWidth: 1,
borderBottomColor: 'lightgray'
},
headerTextFirst: {
fontSize: 14,
textAlign: 'center',
fontWeight: '300',
color: Colors.black,
},
headerTextBold: {
fontWeight: '600'
},
headerTextSecond: {
marginTop: 10,
fontSize: 12,
textAlign: 'center',
fontWeight: '300',
color: Colors.black,
},
calendar: {
flex: 10,
backgroundColor: Colors.white,
},
});
export default App;