-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp7.js
More file actions
150 lines (147 loc) · 3.88 KB
/
App7.js
File metadata and controls
150 lines (147 loc) · 3.88 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
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
import { StatusBar } from "expo-status-bar";
import {
StyleSheet,
Text,
View,
TouchableOpacity,
TouchableHighlight,
TouchableWithoutFeedback,
Pressable,
TextInput,
ScrollView,
Alert,
} from "react-native";
import Fontisto from "@expo/vector-icons/Fontisto";
import { theme } from "./colors";
import { useEffect, useState } from "react";
import AsyncStorage from "@react-native-async-storage/async-storage";
const STORAGE_KEY = "@toDos";
export default function App() {
const [working, setWorking] = useState(true);
const [text, setText] = useState("");
const [toDos, setToDos] = useState({});
const travel = () => setWorking(false);
const work = () => setWorking(true);
const onChangeText = (payload) => setText(payload);
const saveToDos = async (toSave) => {
await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(toSave));
};
const loadToDos = async () => {
const s = await AsyncStorage.getItem(STORAGE_KEY);
if (s) {
setToDos(JSON.parse(s));
}
};
useEffect(() => {
loadToDos();
}, []);
const addToDo = async () => {
if (text === " ") {
return;
}
const newToDOs = { ...toDos, [Date.now()]: { text, working } };
setToDos(newToDOs);
await saveToDos(newToDOs);
setText("");
};
const deleteToDo = async (key) => {
Alert.alert("Delete To Do", "Are you sure?", [
{ text: "Cancel" },
{
text: "I'm Sure",
style: "destructive",
onPress: async () => {
const newToDos = { ...toDos };
delete newToDos[key];
setToDos(newToDos);
await saveToDos(newToDos);
},
},
]);
};
return (
<View style={styles.container}>
<StatusBar style="auto" />
<View style={styles.Header}>
<TouchableOpacity onPress={work}>
<Text
style={{ ...styles.btnText, color: working ? "white" : theme.grey }}
>
Work
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={travel}>
<Text
style={{
...styles.btnText,
color: !working ? "white" : theme.grey,
}}
>
Travel
</Text>
</TouchableOpacity>
</View>
<TextInput
onSubmitEditing={addToDo}
returnKeyType="done"
onChangeText={onChangeText}
value={text}
placeholder={working ? "ADD a To DO" : "Where do yout want to go?"}
style={styles.input}
></TextInput>
<ScrollView>
{Object.keys(toDos).map((key) =>
toDos[key].working === working ? (
<View style={styles.toDo} key={key}>
<Text style={styles.toDoText}>{toDos[key].text}</Text>
<TouchableOpacity onPress={() => deleteToDo(key)}>
<Text>
<Fontisto name="trash" size={18} color={theme.grey} />
</Text>
</TouchableOpacity>
</View>
) : null
)}
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: theme.bg,
paddingHorizontal: 20,
},
Header: {
justifyContent: "space-between",
flexDirection: "row",
marginTop: 100,
},
btnText: {
fontSize: 38,
fontWeight: "600",
},
input: {
backgroundColor: "white",
paddingVertical: 15,
paddingHorizontal: 20,
borderRadius: 30,
marginVertical: 20,
fontSize: 18,
},
toDo: {
backgroundColor: theme.toDoBg,
marginBottom: 10,
paddingVertical: 20,
paddingHorizontal: 40,
borderRadius: 15,
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
},
toDoText: {
color: "white",
fontSize: 16,
fontWeight: "500",
},
});