-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
144 lines (132 loc) · 3.74 KB
/
Copy pathApp.js
File metadata and controls
144 lines (132 loc) · 3.74 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, { Component } from "react";
import {
AppState,
StyleSheet,
ScrollView,
RefreshControl,
Text,
Linking
} from "react-native";
import qs from "qs";
import config from "./config.js";
function OAuth(client_id, cb) {
Linking.addEventListener("url", handleUrl);
function handleUrl(event) {
// console.log(event.url);
Linking.removeEventListener("url", handleUrl);
const [, query_string] = event.url.match(/\#(.*)/);
// console.log(query_string);
const query = qs.parse(query_string);
// console.log(`query: ${JSON.stringify(query)}`);
cb(query.access_token);
}
const oauthurl = `https://www.fitbit.com/oauth2/authorize?${qs.stringify({
client_id,
response_type: "token",
scope: "heartrate activity activity profile sleep",
redirect_uri: "exp://en-mzb.anonymous.awesomeproject.exp.direct:80",
expires_in: "31536000",
})}`;
console.log("url:" + oauthurl);
Linking.openURL(oauthurl).catch(err => console.error("Error processing linking", err));
}
function getData(access_token) {
let now = new Date();
let today = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
console.log("getData");
return fetch("https://api.fitbit.com/1.2/user/-/activities/date/" + today + ".json", {
method: "GET",
headers: {
Authorization: `Bearer ${access_token}`,
},
// body: `root=auto&path=${Math.random()}`
});
}
export default class App extends Component {
componentDidMount() {
console.log("componentDidMount");
OAuth(config.client_id, (token) => {
getData(token)
.then(res => res.json())
.then((res) => {
console.log("res", res);
this.setState({...res, token: token});
}).catch(err => {
console.error("Error: ", err);
})
});
AppState.addEventListener("change", this._handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener("change", this._handleAppStateChange);
}
_handleAppStateChange = (nextAppState) => {
if (
this.state.appState.match(/inactive|background/) &&
nextAppState === "active"
) {
console.log("App has come to the foreground!");
this._onRefresh();
}
this.setState({appState: nextAppState});
};
state = {
appState: AppState.currentState,
refreshing: false
};
static getDerivedStateFromError (error) {
console.log("xxxxxx", error);
return { hasError: true };
}
componentDidCatch (error, info) {
console.log(error, info.componentStack);
}
_onRefresh = () => {
this.setState({refreshing: true});
console.log("_onRefresh", "this.state.token", this.state.token);
getData(this.state.token)
.then(res => res.json())
.then((res) => {
console.log("res", res);
this.setState({...res, refreshing: false});
}).catch(err => {
console.error("Error: ", err);
this.setState({refreshing: false});
});
}
render() {
return (
<ScrollView contentContainerStyle={styles.container} refreshControl={
<RefreshControl
refreshing={this.state.refreshing}
onRefresh={this._onRefresh}
/>
}>
<Text style={styles.welcome}>
{"Howdy there! You have " + (this.state && this.state.summary ? (this.state.goals.steps - (this.state.summary.steps % this.state.goals.steps)) : "?") + " steps till your next goal!"}
</Text>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#00a8b5",
},
welcome: {
fontSize: 25,
textAlign: "center",
color: "#fff",
margin: 10,
},
});