-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
158 lines (140 loc) · 4.49 KB
/
main.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
import Expo from 'expo';
import React, { Component } from 'react';
import { StyleSheet, AsyncStorage, Text, View } from 'react-native';
import { Header, Input, List, ListHeader, Card, TextButton } from './components';
class App extends Component {
state = {
inputValue: '',
data: [],
hasChecked: true,
};
// Generate pseudo-random keys. Good enough, I guess
generateKey() {
return Math.floor(Math.random() * 100000);
}
checkPressed(data) {
hasChecked = false;
data.map((obj) => {
if(obj.checked === true) hasChecked = true;
});
return hasChecked;
}
//****************************************************************************
// ON PRESS METHODS
//****************************************************************************
// onPress method for add button. Adds the new item from input to data array
onAddPress() {
if(this.state.inputValue !== '') {
data = this.state.data === null ? [] : this.state.data.slice(0);
key = this.generateKey();
item = this.state.inputValue;
data.push({ key, item, checked: false });
this.setState({ data, inputValue: '' });
this.setData(data);
}
}
// onPress method for list item. Finds the item in the data array
// and changes it's checked state
onItemPress(item) {
data = this.state.data.slice(0); // get copy of data array
let index;
data.map((obj, i) => { // map through and find index of item
if (obj.key === item.key) {
index = i;
return true
}
});
data[index] = { key: item.key, item: item.item, checked: !item.checked };
hasChecked = this.checkPressed(data);
this.setState({ data, hasChecked }); // update data array in state
this.setData(data); // update data array in storage
}
// onPress method for clear all or clear checked button. Sets data array to empty
onClearPress() {
if(this.state.hasChecked) {
data = this.state.data.slice(0); // get copy of array
data = data.filter((obj) => { // filter out all checked items
return !obj.checked;
})
this.setState({ data, hasChecked: false });
this.setData(data);
} else {
this.setState({ data: [], hasChecked: false });
this.setData([]);
}
}
//****************************************************************************
// LIFE CYCLE METHODS
//****************************************************************************
// Retreive all data on app startup
componentWillMount() {
AsyncStorage.getItem('data')
.then((rawData) => {
data = JSON.parse(rawData);
hasChecked = false;
if (data !== null) hasChecked = this.checkPressed(data);
this.setState({ data, hasChecked });
});
}
// Save data array on device
setData(data) {
rawData = JSON.stringify(data);
AsyncStorage.setItem('data', rawData);
}
//****************************************************************************
// RENDER METHODS
//****************************************************************************
// Renders the list of todo items, as well as Clear All button and
// empty list placeholder text
renderList() {
clearText = this.state.hasChecked ? 'CLEAR CHECKED' : 'CLEAR ALL';
return (
this.state.data === null || this.state.data.length === 0 ?
<View style={styles.placeholderContainer}>
<Text style={styles.placeholderText}>Everything is done!</Text>
</View> :
<View style={{ flex: 1 }}>
<List
data={this.state.data}
onItemPress={this.onItemPress.bind(this)}
/>
<TextButton
text={clearText}
onPress={this.onClearPress.bind(this)}
/>
</View>
);
}
render() {
return (
<View style={styles.container}>
<Header text='Do Stuff' color='#6666ff' />
<Input
value={this.state.inputValue}
placeholder='What should we do today, boss?'
onChangeText={ inputValue => this.setState({ inputValue })}
onPress={this.onAddPress.bind(this)}
/>
<Card>
<ListHeader text='ToDo' />
{this.renderList()}
</Card>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
placeholderText: {
fontSize: 18,
color: '#ddd',
},
placeholderContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
Expo.registerRootComponent(App);