-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
157 lines (149 loc) · 4.41 KB
/
App.tsx
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
import { StatusBar } from "expo-status-bar";
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
import {
Platform,
PlatformColor,
StyleSheet,
FlatList,
useColorScheme,
} from "react-native";
import { useState } from "react";
import { white, black } from "./constants/colors";
import TaskListItem from "./components/TaskListItem";
import TaskCreator from "./components/TaskCreator";
import Task from "./utils/Task";
import { removeFromArray, editInArray } from "./utils/arrayHelpers";
const styles = StyleSheet.create({
container: {
alignItems: "flex-start",
flex: 1,
justifyContent: "flex-start",
marginHorizontal: 10,
},
containerDark: {
...Platform.select({
ios: {
backgroundColor: PlatformColor("systemBackground"),
},
android: {
backgroundColor: PlatformColor(
"@android:color/background_dark"
),
},
default: { backgroundColor: black },
}),
},
containerLight: {
...Platform.select({
ios: {
backgroundColor: PlatformColor("systemBackground"),
},
android: {
backgroundColor: PlatformColor(
"@android:color/background_light"
),
},
default: { backgroundColor: white },
}),
},
creator: {
flex: 1,
},
creatorDark: {
borderColor: white, // PlatformColor crashes the app when used with borderColor
...Platform.select({
ios: {
color: PlatformColor("label"),
},
android: {
color: PlatformColor("@android:color/primary_text_dark"),
},
default: { color: white },
}),
},
creatorLight: {
borderColor: black,
...Platform.select({
ios: {
color: PlatformColor("label"),
},
android: {
color: PlatformColor("@android:color/primary_text_light"),
},
default: { color: black },
}),
},
task: {
marginBottom: 5,
},
taskDark: {
...Platform.select({
ios: {
color: PlatformColor("label"),
},
android: {
color: PlatformColor("@android:color/primary_text_dark"),
},
default: { color: "white" },
}),
},
taskLight: {
...Platform.select({
ios: {
color: PlatformColor("label"),
},
android: {
color: PlatformColor("@android:color/primary_text_light"),
},
default: { color: "black" },
}),
},
taskList: {
flex: 3,
marginTop: 10,
},
});
export default function App(): JSX.Element {
const colourScheme = useColorScheme();
const containerColor =
colourScheme === "dark" ? styles.containerDark : styles.containerLight;
const creatorColor =
colourScheme === "dark" ? styles.creatorDark : styles.creatorLight;
const taskColor =
colourScheme === "dark" ? styles.taskDark : styles.taskLight;
const initialTask = new Task("Welcome to my minimal todo list!");
const [tasks, setTasks] = useState([initialTask]);
const addTask = (newTask: Task) => {
setTasks(tasks.concat(newTask));
};
const removeTask = (task: Task) => {
setTasks(removeFromArray(tasks, task));
};
const updateTask = (task: Task, newName: string) => {
setTasks(editInArray(tasks, task, new Task(newName)));
};
const renderItem = ({ item }: { item: Task }) => (
<TaskListItem
task={item}
onDelete={removeTask}
onEdit={updateTask}
style={[styles.task, taskColor]}
/>
);
return (
<SafeAreaProvider style={containerColor}>
<SafeAreaView style={styles.container}>
<TaskCreator
onAdd={addTask}
style={[styles.creator, creatorColor]}
/>
<FlatList
data={tasks}
renderItem={renderItem}
style={styles.taskList}
/>
<StatusBar />
</SafeAreaView>
</SafeAreaProvider>
);
}