-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
156 lines (144 loc) · 3.82 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
import { StatusBar } from "expo-status-bar";
import React, { useEffect, useState } from "react";
import {
Alert,
Button,
SafeAreaView,
StyleSheet,
Text,
TouchableOpacity,
View,
} from "react-native";
import { Pedometer } from "expo-sensors";
export default function App() {
const [pastStepCount, setPastStepCount] = useState(8340);
const [currentStepCount, setCurrentStepCount] = useState(0);
const [isPedometerAvailable, setIsPedometerAvailable] = useState("Checking");
const [stepGoal, setStepGoal] = useState(10000);
const [goalPercent, setGoalPercent] = useState(0);
const [goalReached, setGoalReached] = useState(false);
const [userName, setUserName] = useState("User");
useEffect(() => {
Pedometer.isAvailableAsync().then(
(result) => {
console.log("isAvailableAsyncResult:", result);
setIsPedometerAvailable(String(result));
},
(error) => {
console.log("isAvailableAsyncError:", error);
}
);
const startDate = new Date();
const endDate = new Date();
startDate.setDate(endDate.getDate() - 1);
Pedometer.getStepCountAsync(startDate, endDate).then(
(result) => {
console.log("getStepCountAsyncResult:", result);
setPastStepCount(result.steps);
},
(error) => {
console.log("getStepCountAsyncError:", error);
}
);
// Sett lytter lytter
const subscription = Pedometer.watchStepCount((result) => {
console.log("Watch:", result);
setCurrentStepCount(result.steps);
});
// Avslutt lytter
return () => {
subscription.remove();
};
}, []);
useEffect(() => {
setGoalPercent((pastStepCount / stepGoal) * 100);
pastStepCount >= stepGoal && setGoalReached(true);
}, [pastStepCount, stepGoal]);
return (
<SafeAreaView style={styles.container}>
<TouchableOpacity
onPress={() =>
Alert.prompt("Username", "Set a new username", (input) =>
setUserName(input)
)
}
>
<Text style={styles.username}>{userName}</Text>
</TouchableOpacity>
<Text style={styles.metadata}>
{new Date(Date.now()).toLocaleDateString()}
</Text>
<Text style={styles.metadata}>Step count today</Text>
<View
style={[
styles.dataContainer,
{ backgroundColor: goalReached ? "lime" : "#e0e0de" },
]}
>
<Text style={styles.count}>
{pastStepCount} / {stepGoal}
</Text>
</View>
<View
style={{
height: 20,
width: "80%",
backgroundColor: "#e0e0de",
borderRadius: 50,
margin: 50,
}}
>
<View
style={{
alignItems: "center",
justifyContent: "center",
height: "100%",
width: goalPercent >= 100 ? "100%" : `${goalPercent}%`,
backgroundColor: goalPercent >= 100 ? "lime" : "darkgrey",
position: "absolute",
borderRadius: 50,
textAlign: "right",
}}
>
<Text>{Math.floor(goalPercent)}%</Text>
</View>
</View>
<Button
title="Set new goal"
onPress={() =>
Alert.prompt("New goal", "Set a new daily step goal", (input) =>
setStepGoal(Number(input))
)
}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
width: "100%",
padding: 20,
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
count: {
fontSize: 20,
fontWeight: "bold",
},
dataContainer: {
padding: 20,
width: "80%",
alignItems: "center",
justifyContent: "center",
borderRadius: 20,
},
metadata: {
margin: 20,
},
username: {
fontSize: 20,
fontWeight: "bold",
},
});