|
1 | | -/** |
2 | | - * Sample React Native App |
3 | | - * https://github.com/facebook/react-native |
4 | | - * @flow |
5 | | - */ |
| 1 | +// @flow |
6 | 2 |
|
7 | 3 | import React, { Component } from 'react'; |
8 | 4 | import { |
| 5 | + Alert, |
9 | 6 | AppRegistry, |
| 7 | + AsyncStorage, |
| 8 | + Button, |
| 9 | + ScrollView, |
10 | 10 | StyleSheet, |
11 | 11 | Text, |
12 | 12 | View |
13 | 13 | } from 'react-native'; |
| 14 | +import BackgroundTask from 'react-native-background-task' |
| 15 | + |
| 16 | +function currentTimestamp(): string { |
| 17 | + const d = new Date() |
| 18 | + const z = n => n.toString().length == 1 ? `0${n}` : n // Zero pad |
| 19 | + return `${d.getFullYear()}-${z(d.getMonth()+1)}-${z(d.getDate())} ${z(d.getHours())}:${z(d.getMinutes())}` |
| 20 | +} |
| 21 | + |
| 22 | +BackgroundTask.define( |
| 23 | + async () => { |
| 24 | + console.log('Hello from a background task') |
| 25 | + |
| 26 | + const value = await AsyncStorage.getItem('@MySuperStore:times') |
| 27 | + await AsyncStorage.setItem('@MySuperStore:times', `${value || ''}\n${currentTimestamp()}`) |
| 28 | + |
| 29 | + // Or, instead of just setting a timestamp, do an http request |
| 30 | + /* const response = await fetch('http://worldclockapi.com/api/json/utc/now') |
| 31 | + const text = await response.text() |
| 32 | + await AsyncStorage.setItem('@MySuperStore:times', text) */ |
| 33 | + |
| 34 | + BackgroundTask.finish() |
| 35 | + }, |
| 36 | +) |
14 | 37 |
|
15 | 38 | export default class AsyncStorageTimestamp extends Component { |
| 39 | + constructor() { |
| 40 | + super() |
| 41 | + this.state = { |
| 42 | + text: '', |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + componentDidMount() { |
| 47 | + BackgroundTask.schedule() |
| 48 | + this.checkStatus() |
| 49 | + } |
| 50 | + |
| 51 | + async checkStatus() { |
| 52 | + const status = await BackgroundTask.statusAsync() |
| 53 | + console.log(status.available) |
| 54 | + } |
| 55 | + |
16 | 56 | render() { |
17 | 57 | return ( |
18 | | - <View style={styles.container}> |
| 58 | + <ScrollView style={styles.container}> |
19 | 59 | <Text style={styles.welcome}> |
20 | | - Welcome to React Native! |
| 60 | + react-native-background-task! |
21 | 61 | </Text> |
22 | 62 | <Text style={styles.instructions}> |
23 | | - To get started, edit index.android.js |
| 63 | + The defined task (storing the current timestamp in AsyncStorage) |
| 64 | + should run while the app is in the background every 15 minutes. |
24 | 65 | </Text> |
25 | 66 | <Text style={styles.instructions}> |
26 | | - Double tap R on your keyboard to reload,{'\n'} |
27 | | - Shake or press menu button for dev menu |
| 67 | + Press the button below to see the current value in storage. |
28 | 68 | </Text> |
29 | | - </View> |
| 69 | + <Button |
| 70 | + onPress={async () => { |
| 71 | + const value = await AsyncStorage.getItem('@MySuperStore:times') |
| 72 | + console.log('value', value) |
| 73 | + this.setState({ text: value }) |
| 74 | + }} |
| 75 | + title="Get" |
| 76 | + /> |
| 77 | + <Text>{this.state.text}</Text> |
| 78 | + </ScrollView> |
30 | 79 | ); |
31 | 80 | } |
32 | 81 | } |
33 | 82 |
|
34 | 83 | const styles = StyleSheet.create({ |
35 | 84 | container: { |
36 | 85 | flex: 1, |
37 | | - justifyContent: 'center', |
38 | | - alignItems: 'center', |
39 | 86 | backgroundColor: '#F5FCFF', |
| 87 | + padding: 15, |
40 | 88 | }, |
41 | 89 | welcome: { |
42 | 90 | fontSize: 20, |
|
0 commit comments