-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
212 lines (201 loc) · 5.07 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
import React, { useState } from 'react';
import { Button, Overlay, Icon, Input } from '@rneui/themed';
import { View, Text, StyleSheet, FlatList, TouchableOpacity } from 'react-native';
function ListMaker1000Final () {
// INITIAL VALUES FOR TESTING
const initTodos = [
{ text: 'Get milk', key: 1},
{ text: 'Drop off dry cleaning', key: 2},
{ text: 'Finish 669 homework', key: 3}
];
// STATE VARIABLES AND THEIR UPDATERS
const [todos, setTodos] = useState(initTodos);
const [overlayVisible, setOverlayVisible] = useState(false);
const [inputText, setInputText] = useState('');
const [selectedItem, setSelectedItem] = useState('');
// DATA MODEL FUNCTIONS (CRUD)
const createTodo = (todoText) => {
let newTodo = {
text: todoText,
key: Date.now()
}
setTodos(todos.concat(newTodo));
}
const updateTodo = (todo, newText) => {
let newTodo = {...todo, text: newText};
let newTodos = todos.map(elem=>elem.key===todo.key?newTodo:elem);
setTodos(newTodos);
}
const deleteTodo = (todo) => {
let newTodos = todos.filter((item)=>item.key != todo.key);
setTodos(newTodos);
}
// END DATA MODEL
// CUSTOM COMPONENT FOR DISPLAYING A SINGLE LIST ITEM
function TodoListItem({item}) {
return (
<View style={styles.listItemView}>
<View style={styles.li1}>
<Text style={styles.listItemText}>{item.text}</Text>
</View>
<TouchableOpacity
style={styles.li2}
onPress={()=>{
setSelectedItem(item);
setInputText(item.text);
setOverlayVisible(true);
}}
>
<Icon
name="edit"
type="font-awesome"
color="black"
size={25}
iconStyle={{ marginRight: 10 }}
/>
</TouchableOpacity>
<TouchableOpacity
style={styles.li3}
onPress={()=>{
deleteTodo(item);
}}
>
<Icon
name="trash"
type="font-awesome"
color="black"
size={25}
iconStyle={{ marginRight: 10 }}
/>
</TouchableOpacity>
</View>
);
}
// MAIN UI
return (
<View style={styles.screen}>
<View style={styles.header}>
<Text style={styles.headerText}>
ListMaker 1000
</Text>
</View>
<View style={styles.body}>
<FlatList
data={todos}
renderItem={({item})=>{
return (
<TodoListItem item={item}/>
);
}}
/>
</View>
<View style={styles.footer}>
<Button
size='lg'
color='#AAAACC'
onPress={()=>{setOverlayVisible(true)}}
>
<Icon name='add' color='#444477' size={32}/>
</Button>
</View>
{/* OVERLAY COMPONENT: SHOWN ON TOP WHEN visible==true */}
<Overlay
isVisible={overlayVisible}
onBackdropPress={()=>setOverlayVisible(false)}
overlayStyle={styles.overlayView}
>
<Input
placeholder='New Todo Item'
value={inputText}
onChangeText={(newText)=>setInputText(newText)}
/>
<View style={{flexDirection: 'row', justifyContent: 'space-around', width:'80%'}}>
<Button
title="Cancel"
onPress={()=>{
setSelectedItem(undefined);
setInputText('');
setOverlayVisible(false)
}}
/>
<Button
title={selectedItem ? "Update Todo" : "Add Todo"}
onPress={()=>{
if (selectedItem) {
updateTodo(selectedItem, inputText);
} else {
createTodo(inputText);
}
setSelectedItem(undefined);
setInputText('');
setOverlayVisible(false);
}}
/>
</View>
</Overlay>
</View>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
},
header: {
flex: 0.1,
width: '100%',
justifyContent: 'flex-end',
alignItems: 'center',
paddingTop: '20%',
paddingBottom: '5%',
backgroundColor: '#AAAACC'
},
headerText: {
fontSize: 44,
color: '#444477'
},
body: {
flex: 0.5,
width: '100%',
paddingLeft: '5%',
paddingTop: '5%'
},
listItemView: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
flexDirection: 'row',
padding: '1%',
},
li1: {
flex: 0.8,
paddingRight: '3%'
},
li2: {
flex: 0.1,
backgroundColor: 'white'
},
li3: {
flex: 0.1,
backgroundColor: 'white'
},
listItemText: {
fontSize: 24
},
footer: {
flex: 0.2,
width: '100%',
alignItems: 'center',
justifyContent: 'center'
},
overlayView: {
flex: 0.2,
justifyContent: 'center',
alignItems: 'center',
width: '80%'
}
});
//export default ListMaker1000Start;
// export default ListMaker1000Create;
// export default ListMaker1000Delete;
export default ListMaker1000Final;
//export default ContextDemo;