/
App.js
268 lines (259 loc) · 6.84 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import React, { Component } from 'react'
import {
View,
StyleSheet,
Modal,
TouchableHighlight,
Image,
AsyncStorage,
} from 'react-native'
import { Constants } from 'expo'
import DatePicker from 'react-native-datepicker'
import {
Text,
Header,
FormLabel,
FormInput,
Card,
Icon,
Button,
} from 'react-native-elements'
import Storage from 'react-native-storage'
import moment from 'moment'
import '@expo/vector-icons'
const storage = new Storage({
size: 1000,
storageBackend: AsyncStorage,
defaultExpires: null,
enableCache: true,
})
export default class App extends Component {
state = {
payables: 10,
balance: 100,
nextPayDay: moment().add(30, 'days'),
modalVisible: false,
allowance: 0,
daysToNextPayDay: 30,
}
componentDidMount() {
storage
.load({
key: 'state',
})
.then(state => {
if (state) this.setState(state)
})
.catch(err => {
console.warn(err)
})
setInterval(() => {
this.calculateAllowance()
this.calculateDaysToNextPayDay()
}, 1000)
}
persistStateToStorage = () => {
setTimeout(
() =>
storage.save({
key: 'state',
data: this.state,
expires: null,
}),
100,
)
}
calculateAllowance = () => {
let allowance = Math.floor(
(this.state.balance - this.state.payables) /
moment(this.state.nextPayDay).diff(moment(), 'days', false),
)
this.setState({ allowance })
}
calculateDaysToNextPayDay = () => {
this.setState({
daysToNextPayDay: moment(this.state.nextPayDay).diff(
moment(),
'days',
false,
),
})
}
setModalVisible = visible => {
this.setState({ modalVisible: visible })
this.persistStateToStorage()
}
handleBalanceChange = balance => {
this.setState({ balance })
this.persistStateToStorage()
}
handleNextPayDayChange = nextPayDay => {
this.setState({ nextPayDay: moment(nextPayDay) })
this.persistStateToStorage()
}
handlePayablesChange = payables => {
this.setState({ payables })
this.persistStateToStorage()
}
render() {
return (
<View style={styles.container}>
<Header>
<Icon
name="help"
color="#ffffff"
onPress={() => this.setModalVisible(true)}
/>
<Text style={{ color: '#ffffff' }}>Bonobo Budget </Text>
<Text>.</Text>
</Header>
<FormLabel>Balance €</FormLabel>
<FormInput
onChangeText={this.handleBalanceChange}
placeholder="How much money do you have?"
keyboardType="numeric"
value={this.state.balance.toString()}
inputStyle={{
height: 44,
padding: 8,
alignSelf: 'stretch',
color: 'black',
}}
/>
<FormLabel>Payables (sum of all the bills and etc.)</FormLabel>
<FormInput
value={this.state.payables.toString()}
onChangeText={this.handlePayablesChange}
keyboardType="numeric"
inputStyle={{
height: 44,
padding: 8,
alignSelf: 'stretch',
color: 'black',
}}
/>
<FormLabel labelStyle={{ marginBottom: 10 }}>Next pay day:</FormLabel>
<DatePicker
style={{ width: 200 }}
date={moment(this.state.nextPayDay).format('YYYY-MM-DD')}
mode="date"
placeholder="select date"
format="YYYY-MM-DD"
minDate={moment().format('YYYY-MM-DD')}
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateIcon: {
position: 'absolute',
left: 20,
top: 4,
marginLeft: 0,
},
dateInput: {
marginLeft: 56,
},
}}
onDateChange={date => {
this.setState({ nextPayDay: moment(date) })
this.persistStateToStorage()
}}
/>
<Card containerStyle={styles.results}>
<Text style={styles.title}>Each day you can spend: </Text>
<Text style={styles.allowance}>
{this.state.allowance} €
</Text>
<Text style={styles.title}>
Next pay day is in {this.state.daysToNextPayDay} days
</Text>
</Card>
<Modal
animationType="slide"
transparent={false}
visible={this.state.modalVisible}
onRequestClose={() => {}}
>
<View style={styles.helpContainer}>
<Button
onPress={() => this.setModalVisible(false)}
title="BACK TO MAIN VIEW"
backgroundColor="#397af8"
buttonStyle={{ marginLeft: -15, marginBottom: 5 }}
icon={{ name: 'arrow-back' }}
/>
<Text h4>About</Text>
<Text>
The bonobo is an endangered species. Here are two bonobos! This
photo is from Wikipedia.
</Text>
<Image
style={{
width: 150,
height: 188,
margin: 10,
alignSelf: 'center',
}}
source={require('./images/bonobo.jpg')}
/>
<Text h4>How it works</Text>
<Text>
Bonobo Budget is a simple budget manager. Find out how much money
you can spend each day, until your next paycheck arrives.
</Text>
<Text>
1. Enter how much money you got into Balance field. 2. Enter sum
of all your bills until your next pay day in the Payables field
and 3. Set your next pay day.
</Text>
<Text h4>Credits</Text>
<Text>
Logo Icon: https://commons.wikimedia.org/wiki/File:Monkey.svg
</Text>
<Text>
Bonobo Budget is open source, help us improve it:
https://github.com/omidfi/bonobo-budget/
</Text>
</View>
</Modal>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
alignItems: 'flex-start',
justifyContent: 'flex-start',
paddingTop: Constants.statusBarHeight,
},
helpContainer: {
margin: 10,
marginTop: 0,
flex: 1,
flexDirection: 'column',
alignItems: 'flex-start',
justifyContent: 'flex-start',
paddingTop: Constants.statusBarHeight,
},
title: {
marginLeft: 20,
marginTop: 20,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'left',
color: '#34495e',
},
allowance: {
marginBottom: 0,
fontSize: 90,
fontWeight: 'bold',
textAlign: 'center',
color: 'green',
marginLeft: 20,
},
results: {
marginTop: 30,
alignSelf: 'center',
},
})