-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
157 lines (144 loc) · 4.58 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 React, {useEffect, useState, useCallback} from 'react';
import {View, StyleSheet, FlatList, ActivityIndicator} from 'react-native';
import Header from './src/components/Header';
import FloatButton from './src/components/FloatButton';
import COLORS from './src/assets/Colors';
import {EmptyListContainer, EmptyListMessage} from './src/components/AppStyles';
import Icon from 'react-native-vector-icons/MaterialIcons';
import ModalAddReminder from './src/components/ModalAddReminder';
import ReminderItem from './src/components/ReminderItem';
import useLayoutAnimation from './src/hooks/useLayoutAnimation';
import Notification from './src/services/Notification';
import {getReminders, storeReminders} from './src/services/storage';
import {isAfter} from 'date-fns';
export interface INotification {
id: number;
description: string;
date: number;
ongoing: boolean;
displayed: boolean;
}
Notification.configure((notification) => {
console.log('Notification received!');
console.log(notification);
});
export default function App() {
const [modalVisible, setModalVisible] = useState(false);
const [reminders, setReminders] = useState<INotification[] | null>(null);
const [loading, setLoading] = useState(true);
const configureNextAnimation = useLayoutAnimation();
useEffect(() => {
async function prepareReminders() {
const remindersFromStorage = await getReminders();
const remindersDisplayed: INotification[] = [];
const remindersNotDisplayed: INotification[] = [];
remindersFromStorage.forEach((reminder) => {
if (isAfter(Date.now(), reminder.date)) {
remindersDisplayed.push(reminder);
} else {
remindersNotDisplayed.push(reminder);
}
});
setReminders([...remindersNotDisplayed, ...remindersDisplayed]);
configureNextAnimation('easeInEaseOut');
setLoading(false);
}
prepareReminders();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
if (reminders !== null) {
storeReminders(reminders);
}
}, [reminders]);
const renderListEmpty = useCallback(() => {
return (
<EmptyListContainer>
{loading ? (
<ActivityIndicator size={40} color={COLORS.primaryLight} />
) : (
<>
<Icon name="notifications" size={80} color="#aaa" />
<EmptyListMessage>Nenhum lembrete</EmptyListMessage>
</>
)}
</EmptyListContainer>
);
}, [loading]);
const handleInsertNotification = useCallback(
(data) => {
const notification: INotification = {
id: Math.floor(Math.random() * 1000),
description: data.description,
date: data.date,
ongoing: data.ongoing,
displayed: false,
};
Notification.notifySchedule(notification);
configureNextAnimation('easeInEaseOut');
setReminders((prev) => [...(prev || []), notification]);
},
[configureNextAnimation],
);
const handleReminderDeletion = useCallback(
(reminderId: number) => {
const filtered = reminders?.filter((reminder) => reminder.id !== reminderId) || [];
Notification.cancelNofication(reminderId.toString());
configureNextAnimation('easeInEaseOut');
setReminders([...filtered]);
},
[configureNextAnimation, reminders],
);
const renderItemList = useCallback(
({item}) => {
return <ReminderItem data={item} onDeletePress={() => handleReminderDeletion(item.id)} />;
},
[handleReminderDeletion],
);
return (
<View style={styles.container}>
<Header amountReminders={reminders?.length || 0} loading={loading} />
<FlatList
data={reminders}
contentContainerStyle={styles.listContent}
style={styles.list}
keyExtractor={(item) => item.id.toString()}
renderItem={renderItemList}
ListEmptyComponent={renderListEmpty}
ItemSeparatorComponent={() => <View style={styles.separator} />}
/>
<ModalAddReminder
visible={modalVisible}
onClose={() => {
setModalVisible(false);
}}
onInsertPress={handleInsertNotification}
/>
<FloatButton
size={50}
color={COLORS.primaryLight}
onPress={() => {
setModalVisible(true);
}}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
list: {
borderTopColor: '#bbb',
borderTopWidth: 1,
},
listContent: {
flexGrow: 1,
},
separator: {
backgroundColor: '#ddd',
width: '100%',
height: StyleSheet.hairlineWidth,
},
});