-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
216 lines (213 loc) · 5.11 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
import React from "react";
import {
StyleSheet,
Text,
View,
StatusBar, // 상태바 색상 조절을 위해 import
TextInput,
Dimensions,
Platform,
ScrollView,
AsyncStorage
} from "react-native";
import { AppLoading } from "expo";
import ToDo from "./ToDo";
import uuidv1 from "uuid/v1";
const { height, width } = Dimensions.get("window");
export default class App extends React.Component {
state = {
newToDo: "",
loadedToDos: false, // 최초 리스트 가져오는
toDos: {}
};
componentDidMount = () => {
this._loadToDos();
};
render() {
const { newToDo, loadedToDos, toDos } = this.state; //
if (!loadedToDos) { // 리스트 있는 경우
return <AppLoading />;
}
return (
<View style={styles.container}>
<StatusBar barStyle="light-content" />
<Text style={styles.title}>To Do List</Text>
<Text style={styles.made}>made by kyungsnim</Text>
<View style={styles.card}>
<TextInput
style={styles.input}
placeholder={"New To Do"}
value={newToDo}
onChangeText={this._controllNewToDo}
placeholderTextColor={"#999"}
returnKeyType={"done"} // 완료버튼 타입
autoCorrect={false} // 자동수정 방지
onSubmitEditing={this._addToDo} // 완료버튼 클릭
underlineColorAndroid={"transparent"}
/>
<ScrollView contentContainerStyle={styles.toDos}>
{Object.values(toDos)
.reverse()
.map(toDo => (
<ToDo
key={toDo.id}
deleteToDo={this._deleteToDo}
uncompleteToDo={this._uncompleteToDo}
completeToDo={this._completeToDo}
updateToDo={this._updateToDo}
{...toDo}
/>
))}
</ScrollView>
</View>
</View>
);
}
_controllNewToDo = text => {
this.setState({
newToDo: text
});
};
_loadToDos = async () => {
try {
const toDos = await AsyncStorage.getItem("toDos");
const parsedToDos = JSON.parse(toDos);
this.setState({ loadedToDos: true, toDos: parsedToDos || {} });
} catch (err) {
console.log(err);
}
};
_addToDo = () => {
const { newToDo } = this.state;
if (newToDo !== "") {
this.setState(prevState => {
const ID = uuidv1();
const newToDoObject = {
[ID]: {
id: ID,
isCompleted: false,
text: newToDo,
createdAt: Date.now()
}
};
const newState = {
...prevState,
newToDo: "",
toDos: {
...prevState.toDos,
...newToDoObject
}
};
this._saveToDos(newState.toDos);
return { ...newState };
});
}
};
_deleteToDo = id => {
this.setState(prevState => {
const toDos = prevState.toDos;
delete toDos[id];
const newState = {
...prevState,
...toDos
};
this._saveToDos(newState.toDos);
return { ...newState };
});
};
_uncompleteToDo = id => {
this.setState(prevState => {
const newState = {
...prevState,
toDos: {
...prevState.toDos,
[id]: {
...prevState.toDos[id],
isCompleted: false
}
}
};
this._saveToDos(newState.toDos);
return { ...newState };
});
};
_completeToDo = id => {
this.setState(prevState => {
const newState = {
...prevState,
toDos: {
...prevState.toDos,
[id]: { ...prevState.toDos[id], isCompleted: true }
}
};
this._saveToDos(newState.toDos);
return { ...newState };
});
};
_updateToDo = (id, text) => {
this.setState(prevState => {
const newState = {
...prevState,
toDos: {
...prevState.toDos,
[id]: { ...prevState.toDos[id], text: text }
}
};
this._saveToDos(newState.toDos);
return { ...newState };
});
};
_saveToDos = newToDos => {
const saveToDos = AsyncStorage.setItem("toDos", JSON.stringify(newToDos));
};
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#F23657",
alignItems: "center"
},
title: {
color: "white",
fontSize: 30,
marginTop: 50,
fontWeight: "200",
marginBottom: 5
},
made: {
color: "black",
fontSize: 15,
marginBottom: 5,
alignItems: 'center'
},
card: {
backgroundColor: "white",
flex: 1,
width: width - 25,
borderTopLeftRadius: 10,
borderTopRightRadius: 10,
...Platform.select({
ios: {
shadowColor: "rgb(50, 50, 50)",
shadowOpacity: 0.5,
shadowRadius: 5,
shadowOffset: {
height: -1,
width: 0
}
},
android: {
elevation: 3
}
})
},
input: {
padding: 20,
borderBottomColor: "#bbb",
borderBottomWidth: 1,
fontSize: 25
},
toDos: {
alignItems: "center"
}
});