-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (69 loc) · 2.12 KB
/
Copy pathindex.js
File metadata and controls
78 lines (69 loc) · 2.12 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
import { AppRegistry, AsyncStorage, View, Text } from 'react-native'
import React from 'react'
import { Provider } from 'react-redux'
import { StackNavigator, TabNavigator } from 'react-navigation'
import throttle from 'lodash/throttle'
import { fromJS } from 'immutable'
import { SERIALIZE_STATE_INTERVAL } from './constants'
import createStore from './client/configs/store'
import App from './main'
import { name as appName } from './app.json'
console.disableYellowBox = true
class Application extends React.Component {
constructor(props) {
super(props)
this.state = {
isLoading: true
}
const init = (initialState) => {
this._store = createStore({ appData: fromJS(initialState) })
const { dispatch } = this._store
this._store.subscribe(throttle(() => {
const data = this._store.getState().appData
if (data) {
const counter = data.get('counter')
const note = data.get('note')
const addItem = data.get('addItem')
const editItem = data.get('editItem')
const enablePin = data.get('enablePin')
const pinCode = data.get('pinCode')
saveState({
counter,
note,
addItem,
editItem,
enablePin,
pinCode
})
}
}, SERIALIZE_STATE_INTERVAL))
this.setState({ isLoading: false })
}
loadState()
.then(state => {
init(state)
})
.catch(e => {
console.log('loadState error - ', e)
init()
})
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, alignSelf: 'stretch', justifyContent: 'center', alignItems: 'center' }}>
<Text style={{ color: 'lightgray' }}>Loading...</Text>
</View>
)
}
return (
<Provider store={this._store}>
<App />
</Provider>
)
}
}
const STATE = 'STATE'
const saveState = (state) => AsyncStorage.setItem(STATE, JSON.stringify(state))
const loadState = () => AsyncStorage.getItem(STATE).then(raw => JSON.parse(raw))
AppRegistry.registerComponent(appName, () => Application)