This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
243 lines (226 loc) · 5.58 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
236
237
238
239
240
241
242
243
import React from "react";
import { View, Platform, Switch, SafeAreaView, StatusBar } from "react-native";
import {
Text,
Button,
Icon,
Slider,
withTheme,
Tooltip,
} from "react-native-elements";
import EStyleSheet from "react-native-extended-stylesheet";
import { Logs } from "expo";
import I18n from "i18n-js";
import Constants from "expo-constants";
import { AppearanceProvider, useColorScheme } from "react-native-appearance";
const PRECISION = 1000;
if (__DEV__ && Platform.OS !== "web") {
Logs.disableExpoCliLogging();
}
EStyleSheet.clearCache();
EStyleSheet.build();
function App({ theme }) {
const colorScheme = useColorScheme();
const [state, setState] = React.useState({
count: 0,
lastTimeout: 0,
running: false,
precision: PRECISION,
darkMode: colorScheme === "dark",
});
React.useEffect(() => {
let timeout;
if (state.running) {
timeout = setTimeout(() => {
setState({
...state,
count: state.count + (Date.now() - state.lastTimeout),
lastTimeout: Date.now(),
});
}, state.precision);
}
return () => {
if (timeout) {
clearTimeout(timeout);
}
};
});
const start = () => {
if (!state.running) {
setState({
...state,
lastTimeout: Date.now(),
running: true,
});
}
};
const stop = () => {
if (state.running) {
setState({ ...state, running: false });
}
};
const reset = () => {
setState({ ...state, count: 0 });
};
const date = new Date(state.count);
const toStr = (value, padding = 3, pattern = "0000") => {
const str = "" + value;
return pattern.substring(0, padding - str.length) + str;
};
const formatDate = (date) => {
return `${toStr(date.getHours() - 19, 2)}:${toStr(
date.getMinutes(),
2
)}:${toStr(date.getSeconds(), 2)}:${toStr(date.getMilliseconds(), 3)}`;
};
const toggleDarkMode = () => {
setState({ ...state, darkMode: !state.darkMode });
};
return (
<AppearanceProvider>
<View style={[styles.statusBar]} />
{/* <StatusBar
barStyle={"light-content"}
backgroundColor={theme.colors.primary}
/>*/}
<View style={styles.container}>
{/*<SafeAreaView style={styles.container}>*/}
<View style={styles.center}>
<View style={styles.switchView}>
<Text>Dark mode</Text>
<Switch
style={styles.switch}
value={state.darkMode}
onValueChange={() => toggleDarkMode()}
/>
</View>
<Text style={styles.counter}>{formatDate(date)}</Text>
<Button
disabled={state.running}
onPress={start}
buttonStyle={styles.buttonStyle}
containerStyle={styles.button}
title="START"
/>
<Button
disabled={!state.running}
onPress={stop}
buttonStyle={styles.buttonStyle}
containerStyle={styles.button}
title="STOP"
/>
<Button
onPress={reset}
buttonStyle={styles.buttonStyle}
containerStyle={styles.button}
title="RESET"
/>
{Platform.OS !== "web" && (
<Slider
thumbTintColor={theme.colors.primary}
style={styles.slider}
minimumValue={10}
maximumValue={1000}
step={10}
value={state.precision}
onValueChange={(value) =>
setState({ ...state, precision: value })
}
/>
)}
<View style={styles.tooltipView}>
<Text style={styles.precision}>
Precision {toStr(state.precision, 4, " ")} ms
</Text>
{Platform.OS !== "web" && (
<View style={styles.tooltip}>
<Tooltip
width={300}
height={50}
popover={<Text>Period between computations of time</Text>}
>
<Icon
iconStyle={[{ color: theme.colors.primary }]}
name="info-circle"
type="font-awesome"
/>
</Tooltip>
</View>
)}
</View>
</View>
</View>
</AppearanceProvider>
);
}
const styles = EStyleSheet.create({
switchView: {
flexDirection: "row",
alignSelf: "flex-end",
alignItems: "center",
justifyContent: "center",
},
switch: {
marginLeft: ".75rem",
},
slider: {
marginTop: "1rem",
},
tooltipView: {
alignItems: "center",
flexDirection: "row",
paddingTop: "1rem",
},
tooltip: {
marginLeft: "1rem",
},
statusBar: {
minHeight: Constants.statusBarHeight,
},
center: {
alignItems: "stretch",
justifyContent: "center",
},
container: {
flex: 1,
justifyContent: "center",
flexDirection: "column",
paddingLeft: "3rem",
paddingRight: "3rem",
...Platform.select({
web: {
width: 500,
margin: "auto",
},
}),
},
counter: {
marginTop: "1rem",
fontSize: 43,
fontWeight: "bold",
alignSelf: "center",
...Platform.select({
web: {
fontSize: 70,
},
}),
},
button: {
marginTop: "1rem",
},
buttonStyle: {
padding: "1rem",
},
precision: {
...Platform.select({
android: {
fontFamily: "monospace",
},
web: {
fontFamily: "monospace",
},
}),
color: "#aaa",
},
});
export default withTheme(App);