-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
422 lines (384 loc) · 11.8 KB
/
Copy pathApp.js
File metadata and controls
422 lines (384 loc) · 11.8 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import {
Text,
SafeAreaView,
StyleSheet,
View,
TouchableOpacity,
FlatList,
Modal,
TextInput,
} from 'react-native';
import React from 'react';
import { Feather } from '@expo/vector-icons';
import tempData from './tempData';
import TodoList from './components/TodoList';
import { LinearGradient } from 'expo-linear-gradient';
import SettingsModal from './components/SettingsMenu';
import AsyncStorage from '@react-native-async-storage/async-storage';
import tinycolor from 'tinycolor2';
import { globalStyles } from './styles';
const DEFAULT_BACKGROUND_COLOR = '#4158D0';
export default class App extends React.Component {
textFocus = React.createRef();
state = {
settingsVisible: false,
addingCategory: false,
addingCategoryId: null,
backgroundColor: DEFAULT_BACKGROUND_COLOR,
lists: tempData,
categoryText: '',
};
// Toggles the settings modal
toggleSettingsModal = async () => {
this.setState({ settingsVisible: !this.state.settingsVisible });
if (this.state.settingsVisible == true) {
console.log('Closing modal and loading data');
await this.loadData();
}
}
// Runs the first time the application starts
async componentDidMount() {
this.loadData();
}
// Checks if the previous state is the same as the current state, if so throw an error
async componentDidUpdate(_, prevState) {
if (prevState.lists !== this.state.lists) {
saveList(this.state.lists);
}
}
// Makes the adding category text box appear when "Add Category" is clicked
AddCategory = () => {
this.setState({ addingCategory: true, categoryText: '' }, () => {
this.textFocus.current.focus();
});
};
// Create a new checklist item
createCategory = () => {
const { categoryText, addingCategory, addingCategoryId, lists } = this.state;
// Define updatedLists based on whether adding a new category or editing an existing one
const updatedLists = addingCategory
? [
// Adding a new category
{
id: lists.length + 1,
name: categoryText,
color: '#FFFFFF',
opened: true,
todos: [],
},
...lists,
]
: lists.map((list) =>
// Editing an existing category
list.id === addingCategoryId ? { ...list, name: categoryText } : list
);
// Update the state with new lists and reset category-related states
this.setState({
lists: updatedLists,
addingCategory: false,
addingCategoryId: null,
categoryText: '',
});
};
startCategoryEditing = (id) => {
const list = this.state.lists.find((list) => list.id === id);
this.setState(
{ addingCategoryId: id, categoryText: list.name, addingCategory: false },
() => {
this.textFocus.current.focus(); // Focus the TextInput
}
);
};
saveEditedCategory = async () => {
const { addingCategoryId, categoryText, lists } = this.state;
const updatedLists = lists.map((list) => {
if (list.id === addingCategoryId) {
return { ...list, name: categoryText }; // Update the name, keep other properties
}
return list;
});
this.setState({
lists: updatedLists,
addingCategoryId: null,
categoryText: '',
});
saveList(updatedLists);
};
// Toggles the whole checklist category completed or not
toggleCategoryCompleted = async (listId) => {
const updatedLists = this.state.lists.map((list) => {
if (list.id === listId) {
const completed = !list.completed;
return {
...list,
completed,
todos: list.todos.map((task) => ({ ...task, completed })),
};
}
return list;
});
this.setState({ lists: updatedLists }, () => this.saveList(updatedLists));
};
// Allows you to open/close category lists
toggleListOpened = async (id) => {
const updatedLists = this.state.lists.map((list) =>
list.id === id ? { ...list, opened: !list.opened } : list
);
this.setState({ lists: updatedLists }, () => this.saveList(updatedLists));
};
// Updates background color to the given color
updateBackgroundColor = (color) => {
// If the color is null, use the default background color
// Mostly here for when you reset your data
if (color != null) {
this.setState({ backgroundColor: color });
} else {
this.setState({ backgroundColor: DEFAULT_BACKGROUND_COLOR });
}
};
adjustBrightness(hex, factor) {
// Gets the color based off the hex value using tinycolor2 library
const color = tinycolor(hex);
// Brighten/darken the given color using tinycolor2
const newColor = color.brighten(factor);
// Converts back to a hex value
return newColor.toHexString();
}
changeCategoryText = (text) => {
setCategoryText(text);
};
renderList = (list) => {
return (
<TodoList
textFocus={this.textFocus}
list={list}
lists={this.state.lists}
updateList={this.updateList}
saveList={this.saveList}
loadData={this.loadData}
addingCategoryId={this.state.addingCategoryId}
startCategoryEditing={this.startCategoryEditing}
renderCategory={this.renderCategory}
categoryText={this.state.categoryText}
changeCategoryText={this.changeCategoryText}
/>
);
};
// Adds a new category
addList = (list) => {
this.setState({
lists: [
{ ...list, id: this.state.lists.length + 1, todos: [] },
...this.state.lists,
],
});
};
updateList = (list) => {
this.setState({
lists: this.state.lists.map((item) => {
return item.id === list.id ? list : item;
}),
});
};
loadData = async () => {
try {
// Searches for background color
const color = await AsyncStorage.getItem('backgroundColor');
if (color !== null) {
this.setState({ backgroundColor: color });
}
// Searches for previous list save data
const lists = await AsyncStorage.getItem('lists');
if (lists !== null) {
console.log('Lists from AsyncStorage:', lists);
this.setState({ lists: JSON.parse(lists) });
} else {
console.log('Lists not found');
this.setState({ lists: tempData });
}
} catch (error) {
console.log('Error retrieving data:', error);
}
};
saveList = async (list) => {
try {
await AsyncStorage.setItem('lists', JSON.stringify(list));
} catch (error) {
console.log('Error saving lists:', error);
}
};
renderCategoryInput = () => {
return <Text>Old code</Text>;
};
renderCategory = (list, isInput = false) => {
let categoryColor, boxCheck;
// Set values from list
try {
categoryColor = list.color;
boxCheck = list.completed;
// If it can't find list values set defaults
} catch (error) {
// Checks whether or not you're inputting a new category as to not create weird effect
categoryColor = isInput ? '' : '#FFFFFF';
boxCheck = false;
}
return (
// Open/Close Category Button
<TouchableOpacity
style={[globalStyles.todoContainer, { backgroundColor: categoryColor }]}
onPress={() => this.toggleListOpened(list.id)}>
{/* Check/Uncheck Button */}
<TouchableOpacity onPress={() => this.toggleCategoryCompleted(list.id)}>
<Feather
name={boxCheck ? 'check-square' : 'square'}
style={globalStyles.icon}
/>
</TouchableOpacity>
{isInput === false ? (
<Text style={globalStyles.categoryText} numberOfLines={1}>
{list.name}
</Text>
) : (
<TextInput
ref={this.textFocus}
style={globalStyles.categoryText}
placeholder="Category name..."
placeholderTextColor="black"
value={this.state.categoryText}
maxLength={20}
onChangeText={(text) => this.setState({ categoryText: text })}
onSubmitEditing={this.createCategory}
/>
)}
</TouchableOpacity>
);
};
render() {
const { backgroundColor, addingCategory, addingCategoryId } = this.state;
return (
<SafeAreaView style={styles.container}>
<View style={styles.content}>
{/* Settings Modal */}
<Modal
animationType="slide"
visible={this.state.settingsVisible}
onRequestClose={() => this.toggleSettingsModal}>
<SettingsModal
closeModal={() => this.toggleSettingsModal()}
updateBackgroundColor={this.updateBackgroundColor}
updateList={this.updateList}
saveList={this.saveList}
/>
</Modal>
{/* Header (Contains menu & checklist title) */}
<View style={styles.header}>
<Text style={styles.title}>Checklist</Text>
<TouchableOpacity onPress={() => this.toggleSettingsModal()}>
<Feather name="menu" size={64} color="white" />
</TouchableOpacity>
</View>
{/* Adding new category box */}
{addingCategory && (
<View style={styles.categoryInput}>
{this.renderCategory(null, true)}
</View>
)}
{/* Task container */}
<View style={styles.tasks}>
<FlatList
data={this.state.lists}
keyExtractor={(item) => item.name}
showsVerticalScrollIndicator={false}
renderItem={({ item }) => this.renderList(item)}
keyboardShouldPersistTaps="always"
// Adds a margin below all of the categories, set this to 120 to perfectly fit
ListFooterComponent={<View style={{ height: 360 }} />}
/>
</View>
</View>
{/* Add Category button (Hovers above everything) */}
{!addingCategory && addingCategoryId === null && (
<TouchableOpacity
style={styles.addCategory}
onPress={this.AddCategory}>
<Feather name="plus" size={22} color="black" />
<Text style={styles.addCatText}>Add Category</Text>
</TouchableOpacity>
)}
{/* Gradient Background #7F1DD0 */}
{backgroundColor === '#7F1DD0' ? (
<LinearGradient
colors={['#C40809', '#2503FD', '#632599']}
style={styles.grad}
/>
) : (
<LinearGradient
colors={[
backgroundColor,
this.adjustBrightness(backgroundColor, -5),
this.adjustBrightness(backgroundColor, -30),
]}
style={styles.grad}
/>
)}
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
header: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
content: {
// All content that needs to be placed on top of the gradient
flex: 1,
position: 'relative',
zIndex: 1,
},
container: {
flex: 1,
padding: 8,
},
tasks: {
paddingHorizontal: 16,
height: '100%',
width: '100%',
maxWidth: 600,
alignSelf: 'center',
},
title: {
margin: 24,
fontSize: 48,
fontWeight: 'bold',
textAlign: 'center',
color: '#ffff',
paddingBottom: 5,
},
addCategory: {
display: 'flex',
flexDirection: 'row',
backgroundColor: 'white',
zIndex: 1,
alignItems: 'center',
alignSelf: 'center',
justifyContent: 'center',
borderRadius: 40,
width: 160,
height: 46,
marginBottom: 24,
},
categoryInput: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: 'white',
height: 46,
marginBottom: 5,
borderRadius: 10,
marginHorizontal: 16,
},
grad: {
...StyleSheet.absoluteFillObject, // Position the gradient to cover the entire screen
},
});