-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
283 lines (263 loc) · 7.64 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
import React, { useEffect, useState } from 'react';
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, TextInput, TouchableOpacity, View, ScrollView } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as Notifications from 'expo-notifications';
import { scheduleNotificationAsync } from 'expo-notifications';
import DateTimePicker from '@react-native-community/datetimepicker';
import { Platform } from 'react-native';
const CrossPlatformDateTimePickerAIO = (props) => {
const [date, setDate] = useState(undefined);
const [time, setTime] = useState(undefined);
const handleDateChange = (event, selectedDate) => {
if (date !== undefined)
return;
const currentDate = selectedDate || date;
setDate(currentDate);
// if (Platform.OS === 'ios') {
// props.onChange(currentDate);
// } else
// {
const combinedDateTime = new Date(
currentDate.getFullYear(),
currentDate.getMonth(),
currentDate.getDate(),
time.getHours(),
time.getMinutes()
);
props.onChange(event, combinedDateTime);
// }
};
const handleTimeChange = (event, selectedTime) => {
if (time !== undefined)
return;
const currentTime = selectedTime || time;
setTime(currentTime);
// if (Platform.OS === 'ios') {
// props.onChange(currentTime);
// }
};
const showDateTimePicker = (
<DateTimePicker
{...props}
mode="datetime"
/>
);
const showDatePicker = (
<DateTimePicker
{...props}
onChange={handleDateChange}
mode="date"
/>
);
const showTimePicker = (
<DateTimePicker
{...props}
onChange={handleTimeChange}
mode="time"
/>
);
return Platform.OS === 'ios' ? showDateTimePicker : <View>{time === undefined ? showTimePicker : showDatePicker}</View>;
};
export default function App() {
const [todos, setTodos] = useState([]);
const [newTodo, setNewTodo] = useState('');
const [date, setDate] = useState(new Date());
const [show, setShow] = useState(false);
useEffect(() => {
loadTodos();
}, []);
const saveTodos = async (updatedTodos) => {
try {
const todosString = JSON.stringify(updatedTodos);
await AsyncStorage.setItem('todos', todosString);
} catch (error) {
console.error('Error saving todos:', error);
}
};
const loadTodos = async () => {
try {
const todosString = await AsyncStorage.getItem('todos');
if (todosString) {
const loadedTodos = JSON.parse(todosString);
setTodos(loadedTodos);
}
} catch (error) {
console.error('Error loading todos:', error);
}
};
const addTodo = () => {
if (newTodo.trim() !== '') {
const updatedTodos = [
...todos,
{ text: newTodo, reminder: date },
];
setTodos(updatedTodos);
saveTodos(updatedTodos);
setNewTodo('');
scheduleNotification(newTodo, date);
}
};
const removeTodo = (index) => {
const updatedTodos = [...todos];
updatedTodos.splice(index, 1);
setTodos(updatedTodos);
saveTodos(updatedTodos);
};
function formatDateTime(dateTimeString) {
const options = { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true };
return new Date(dateTimeString).toLocaleTimeString(navigator.language, options);
}
function hasTimestampPassed(timestamp_str) {
const timestamp = new Date(timestamp_str);
const currentDate = new Date();
return currentDate > timestamp;
}
const onDateChange = (event, selectedDate) => {
if (event?.type === 'dismissed') {
setShow(false);
setDate(date);
return;
}
const currentDate = selectedDate || date;
setShow(false);
setDate(currentDate);
};
const scheduleNotification = async (todo) => {
console.log(date.getSeconds())
await scheduleNotificationAsync({
content: {
title: 'To-Do Reminder',
body: `Don't forget about your to-do: ${todo}`,
},
trigger: date,
});
};
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
return (
<View style={styles.container}>
<Text style={styles.title}>To-Do App</Text>
<View style={styles.inputContainer}>
{!DateTimePicker.toString().includes("DateTimePicker is not supported on:") && Platform.OS === "android" ? <TouchableOpacity style={styles.addButton} onPress={() => setShow(true)}>
<Text style={styles.buttonText}>{date.toString()}</Text>
</TouchableOpacity> : undefined}
{!DateTimePicker.toString().includes("DateTimePicker is not supported on:") ?
(show || Platform.OS === "ios" ? <CrossPlatformDateTimePickerAIO
testID="dateTimePicker"
value={date}
mode="datetime"
is24Hour={true}
display="default"
onChange={onDateChange}
onError={console.error}
onDim
themeVariant="dark"
style={{ color: '#fff' }} // this doesn't work :((
// ill figure it out
/> : undefined)
: <input type="datetime-local" value={date.toISOString().slice(0, 16)} onChange={(ev) => onDateChange(ev, new Date(ev.target.valueAsNumber))}></input>}
<TouchableOpacity style={styles.addButton} onPress={addTodo}>
<Text style={styles.buttonText}>Add</Text>
</TouchableOpacity>
</View>
<View style={styles.inputContainer}>
<TextInput
style={styles.input}
placeholder="Add a new to-do" // this doesn't work anyway.
value={newTodo}
onChangeText={(text) => setNewTodo(text)}
/>
</View>
<ScrollView style={styles.scrollView}>
<View style={styles.todoList}>
{todos.length === 0 ? (
<Text style={styles.todoText}>No todos available</Text>
) : (
todos.map((todo, index) => (
<View key={index} style={styles.todoItem}>
<Text style={[hasTimestampPassed(todo.reminder) ? styles.passedReminderText : styles.todoText]}>
{todo.text} - {formatDateTime(todo.reminder)}
</Text>
<TouchableOpacity onPress={() => removeTodo(index)}>
<Text style={styles.removeButton}>Remove</Text>
</TouchableOpacity>
</View>
))
)}
</View>
</ScrollView>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#1a1a1a',
alignItems: 'center',
justifyContent: 'center',
padding: 16,
},
title: {
fontSize: 24,
fontWeight: 'bold',
color: '#fff',
marginBottom: 16,
marginTop: 30,
},
inputContainer: {
flexDirection: 'row',
marginBottom: 16,
marginTop: 30,
},
passedReminderText: {
color: '#8F8',
},
input: {
flex: 1,
height: 40,
backgroundColor: '#333',
color: '#fff',
borderRadius: 5,
paddingHorizontal: 10,
},
addButton: {
backgroundColor: '#4CAF50',
marginLeft: 10,
padding: 10,
borderRadius: 5,
alignItems: 'center',
justifyContent: 'center',
},
buttonText: {
color: '#fff',
},
todoList: {
width: '100%',
},
todoItem: {
flexDirection: 'row',
justifyContent: 'space-between',
backgroundColor: '#444',
padding: 10,
borderRadius: 5,
marginVertical: 8,
},
todoText: {
color: '#fff',
alignSelf: "center"
},
removeButton: {
color: '#FF4500',
},
scrollView: {
flex: 1,
width: '100%',
},
});