-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
313 lines (297 loc) · 10.1 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import "react-native-gesture-handler";
import { useState, useEffect } from "react";
import { StyleSheet, Text, View, SafeAreaView, Alert, TouchableOpacity } from "react-native";
import { StatusBar } from "expo-status-bar";
import { AntDesign } from "@expo/vector-icons";
import { Entypo } from "@expo/vector-icons";
import { MaterialCommunityIcons } from "@expo/vector-icons";
import { Drawer } from "react-native-drawer-layout";
import AsyncStorage from "@react-native-async-storage/async-storage";
import Todo from "./components/Todo";
import Goals from "./components/Goals";
const STORAGE_TODOS_KEY = "@todos";
const STORAGE_GOALS_KEY = "@goals";
export default function App() {
const [open, setOpen] = useState(false);
const [today, setToday] = useState("");
const [todos, setTodos] = useState(new Map());
const [goals, setGoals] = useState({});
const [todayTodo, setTodayTodo] = useState({});
const [dayCursor, setDayCursor] = useState();
const [dayKey, setDayKey] = useState("");
const [topColor, setTopColor] = useState(false);
//input으로 들어온 map 형식의 data를 json으로 바꾼 후 string으로 바꾼 후 저장
//map -> json 객체 -> json string
const saveTodos = async (data) => {
try {
await AsyncStorage.setItem(STORAGE_TODOS_KEY, JSON.stringify(Object.fromEntries(data)));
} catch (e) {
console.log(e);
}
};
//string형식의 데이터를 다시 가져옴
//json string -> json 객체 -> map
const loadTodos = async () => {
try {
const data = await AsyncStorage.getItem(STORAGE_TODOS_KEY);
if (data) {
setTodos(new Map(Object.entries(JSON.parse(data))));
}
} catch (e) {
console.log(e);
}
};
const saveGoals = async (data) => {
try {
await AsyncStorage.setItem(STORAGE_GOALS_KEY, JSON.stringify(data));
} catch (e) {
console.log(e);
}
};
const loadGoals = async () => {
try {
const data = await AsyncStorage.getItem(STORAGE_GOALS_KEY);
if (data) {
setGoals(JSON.parse(data));
}
} catch (e) {
console.log(e);
}
};
//새로 할 일 추가 하기
const addTodo = async (newTodo, when) => {
if (newTodo === "") {
return;
} else {
const newMemo = { ...todos.get(dayKey), [Date.now()]: { text: newTodo, when: when, clear: false } };
setTodos((prev) => new Map(prev).set(dayKey, newMemo));
const newMap = new Map(todos).set(dayKey, newMemo);
await saveTodos(newMap);
}
};
//새로 목표 추가 하기
const addGoal = async (newGoal, when) => {
if (newGoal === "") {
return;
} else {
const newGoals = { ...goals, [Date.now()]: { text: newGoal, when: when, clear: false } };
setGoals(newGoals);
await saveGoals(newGoals);
}
};
const deleteTodo = (id) => {
if (!id) {
return;
} else {
Alert.alert("삭제할까요?", "정말로요?", [
{
text: "네",
onPress: () => {
const newMemo = todos.get(dayKey);
delete newMemo[id];
setTodos((prev) => new Map(prev).set(dayKey, newMemo));
saveTodos(todos);
},
},
{
text: "아니요",
},
]);
}
};
//목표 삭제
const deleteGoal = (id) => {
Alert.alert("삭제할까요?", "정말로요?", [
{
text: "네",
onPress: () => {
const newGoals = { ...goals };
delete newGoals[id];
setGoals(newGoals);
saveGoals(newGoals);
},
},
{
text: "아니요",
},
]);
};
const updateTodo = (id, newText) => {
const newMemo = todos.get(dayKey);
newMemo[id].text = newText;
setTodos((prev) => new Map(prev).set(dayKey, newMemo));
saveTodos(todos);
};
const updateGoal = (id, newText) => {
const newGoals = { ...goals };
newGoals[id].text = newText;
setGoals(newGoals);
saveGoals(newGoals);
};
//할 일 클리어
const clearTodo = (id) => {
const newMemo = todos.get(dayKey);
newMemo[id].clear = !newMemo[id].clear;
setTodos((prev) => new Map(prev).set(dayKey, newMemo));
saveTodos(todos);
};
//목표 클리어
const clearGoal = (id) => {
const newGoals = { ...goals };
newGoals[id].clear = !newGoals[id].clear;
setGoals(newGoals);
saveGoals(newGoals);
};
//화면 상단에 표시할 날짜와 input된 날짜로 기반한 스토리지 조회에 사용될 키 이름 생성
const getDay = (today) => {
if (!today) {
today = new Date();
}
const week = ["일", "월", "화", "수", "목", "금", "토"];
const todayYear = today.getFullYear();
const todayMonth = today.getMonth() + 1;
const todayDate = today.getDate();
const todayWeek = week[today.getDay()];
setToday(todayYear + "년 " + todayMonth + "월 " + todayDate + "일 " + todayWeek + "요일");
setDayKey("" + todayYear + todayMonth + todayDate);
};
//오늘 할일 데이터 가져오기
const getTodayTodo = () => {
//오늘 날짜로 데이터가 있다면 가져오기 없다면 새로 생성
if (dayKey === "") {
return;
}
if (!todos.has(dayKey)) {
todos.set(dayKey, {});
}
setTodayTodo(todos.get(dayKey));
};
//현재 날짜에서 다음날로 이동
const goTomorrow = () => {
setDayCursor(new Date(dayCursor.setDate(dayCursor.getDate() + 1)));
getDay(dayCursor);
setTopColor((prev) => !prev);
};
//현재 날짜에서 이전날로 이동
const goYesterday = () => {
setDayCursor(new Date(dayCursor.setDate(dayCursor.getDate() - 1)));
getDay(dayCursor);
setTopColor((prev) => !prev);
};
//로컬데이터 모두 삭제
const deleteAll = () => {
Alert.alert("모든 데이터 삭제할까요?", "되돌릴 수 없습니다.", [
{
text: "네",
onPress: () => {
AsyncStorage.clear();
setTodos(new Map());
setGoals({});
},
},
{
text: "아니요",
},
]);
};
useEffect(() => {
setDayCursor(new Date());
loadTodos();
loadGoals();
getDay(dayCursor);
}, []);
useEffect(() => {
getTodayTodo();
}, [dayCursor, todos]);
return (
<Drawer
open={open}
onOpen={() => setOpen(true)}
onClose={() => setOpen(false)}
drawerPosition="right"
renderDrawerContent={() => {
return (
<SafeAreaView style={styles.container}>
<Text style={styles.title}>목 표</Text>
<Goals
goals={goals}
addGoal={addGoal}
deleteGoal={deleteGoal}
clearGoal={clearGoal}
updateGoal={updateGoal}
></Goals>
</SafeAreaView>
);
}}
>
<StatusBar style="black" />
<SafeAreaView style={{ ...styles.topBar, backgroundColor: topColor ? "#F06B6E" : "#99DBA0" }}>
<View style={styles.row}>
<Text style={styles.title}>{today}</Text>
<TouchableOpacity>
<MaterialCommunityIcons
style={{ marginLeft: 5 }}
onPress={deleteAll}
name="delete"
size={24}
color="#3D4A3E"
/>
</TouchableOpacity>
</View>
<TouchableOpacity>
<AntDesign
style={styles.sideButton}
onPress={() => setOpen((prevOpen) => !prevOpen)}
name="menu-unfold"
size={26}
color="black"
/>
</TouchableOpacity>
</SafeAreaView>
<View style={styles.todoWrapper}>
<TouchableOpacity style={styles.arrowButton} onPress={goYesterday}>
<Entypo style={styles.arrowButton} name="chevron-thin-left" size={28} color="black" />
</TouchableOpacity>
<Todo todo={todayTodo} addTodo={addTodo} deleteTodo={deleteTodo} clearTodo={clearTodo} updateTodo={updateTodo} />
<TouchableOpacity style={styles.arrowButton} onPress={goTomorrow}>
<Entypo style={styles.arrowButton} name="chevron-thin-right" size={28} color="black" />
</TouchableOpacity>
</View>
</Drawer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#E8EAD6",
},
todoWrapper: {
flex: 1,
flexDirection: "row",
backgroundColor: "#E8EAD6",
},
topBar: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
borderBottomColor: "black",
borderBottomWidth: 1.5,
},
row: {
flexDirection: "row",
},
title: {
paddingLeft: 15,
fontSize: 20,
fontWeight: 700,
paddingBottom: 5,
},
sideButton: {
paddingRight: 10,
paddingBottom: 10,
},
arrowButton: {
padding: 3,
justifyContent: "center",
},
});