-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.js
260 lines (242 loc) · 7.83 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {useState, useEffect } from 'react';
import {LogBox} from 'react-native';
import {DefaultTheme, Provider as PaperProvider} from 'react-native-paper';
import Header from './src/components/Header/Header';
import MainScreen from './src/screens/Main/Main';
import AddMedicine from './src/screens/AddMedicine/AddMedicine';
import EditMedicine from './src/screens/EditMedicine/EditMedicine';
import LoginScreen from './src/screens/Login/Login';
import SplashScreen from 'react-native-splash-screen';
import BottomNav from './src/components/BottomNav/BottomNav';
import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import BackgroundFetch from 'react-native-background-fetch';
import Notifications from './Notifications';
import auth from '@react-native-firebase/auth';
import firestore from '@react-native-firebase/firestore';
LogBox.ignoreAllLogs();
const theme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
primary: '#85DEDC',
accent: '#8AB5E6',
},
};
const navTheme = {
dark: false,
colors: {
primary: '#85DEDC',
// background: 'rgb(242, 242, 242)',
background: '#ffffff',
card: 'rgb(255, 255, 255)',
text: 'rgb(28, 28, 30)',
border: 'rgb(199, 199, 204)',
notification: 'rgb(255, 69, 58)',
},
};
const App = () => {
const [loggedIn, setLoggedIn] = useState(false);
const Stack = createNativeStackNavigator();
const initBackgroundFetch = async () => {
let status = await BackgroundFetch.configure(
{
minimumFetchInterval: 15,
stopOnTerminate: false,
enableHeadless: true,
startOnBoot: true,
// Android options
forceAlarmManager: false, // <-- Set true to bypass JobScheduler.
requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE, // Default
requiresCharging: false, // Default
requiresDeviceIdle: false, // Default
requiresBatteryNotLow: false, // Default
requiresStorageNotLow: false,
},
async taskId => {
console.log('[BackgroundFetch] task:', taskId);
// 사용자 복용 약 시간 리스트 불러와서 현재 시간과 비교 후 알림 전송
getTimeLine();
BackgroundFetch.finish(taskId);
},
async taskId => {
console.warn('[BackgroundFetch] TIMEOUT task: ', taskId);
BackgroundFetch.finish(taskId);
},
);
console.log('status:', status);
// BackgroundFetch.scheduleTask({
// taskId: 'test',
// delay: 0,
// forceAlarmManager: true,
// periodic: true
// });
};
// 이 함수가 실행되면 푸시 알림 날아옴
const setNotification = () => {
Notifications.scheduleNotification(new Date(Date.now() + 5 * 1000));
};
function getTimeLine() {
auth().onAuthStateChanged(user => {
if (user) {
setLoggedIn(true);
let tmp = [];
let time_list = [];
auth().onAuthStateChanged(user => {
firestore()
.collection('Users')
.doc(user.uid)
.collection('pillList')
.get()
.then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
tmp.push(documentSnapshot.data());
});
})
.then(() => {
for (let i = 0; i < tmp.length; i++) {
let start = new Date(tmp[i].startDate.seconds * 1000);
let end = new Date(tmp[i].endDate.seconds * 1000);
end.setDate(end.getDate());
let cycle = tmp[i].cycle;
let curr = start;
while (curr <= end) {
if (compareDate(curr, new Date())) {
for (let j = 0; j < tmp[i].periods.length; j++) {
let tmp_date = new Date(curr);
let year = tmp_date.getFullYear();
let month = tmp_date.getMonth();
let date = tmp_date.getDate();
let time = new Date(tmp[i].periods[j].seconds * 1000);
let hour = time.getHours() + 9;
let minutes = time.getMinutes();
time_list.push(
new Date(year, month, date, hour, minutes, 0),
);
}
}
curr.setDate(curr.getDate() + cycle);
}
}
})
.then(() => {
console.log('time list:', time_list);
// 현재시간이랑 time_list에 있는 시간이랑 같으면 알람 보내기
let current;
for (let i=0; i < time_list.length; i++) {
let date = time_list[i];
// console.log('date: ', date, date.getMinutes())
// console.log('now: ', current, current.getMinutes());
current = new Date(Date.now());
current.setHours(current.getHours() + 9);
if (
(date >= current.setMinutes(current.getMinutes() - 15)) &&
(date <= current.setMinutes(current.getMinutes() + 30))
) {
console.log(date);
setNotification();
break;
}
}
});
});
} else {
setLoggedIn(false);
}
});
}
useEffect(() => {
try {
setTimeout(() => {
SplashScreen.hide();
}, 2000); //스플래시 활성화 시간 2초
} catch (e) {
console.log(e.message);
}
initBackgroundFetch();
});
const compareDate = (date1, date2) => {
if (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
)
return true;
else return false;
};
return (
<PaperProvider theme={theme}>
<NavigationContainer theme={navTheme}>
<Stack.Navigator initialRouteName="Main">
<Stack.Screen
name="BottomNav"
component={BottomNav}
options={{
headerTitle: '',
headerRight: () => <Header />,
headerStyle: {
backgroundColor: '#fff',
},
}}
/>
<Stack.Screen
name="Main"
component={MainScreen}
options={{
headerTitle: '',
headerRight: () => <Header />,
headerStyle: {
backgroundColor: '#85DEDC',
},
headerShadowVisible: false,
}}
/>
<Stack.Screen
name="Login"
component={LoginScreen}
options={{
headerTitle: '',
headerRight: () => <Header />,
headerStyle: {
backgroundColor: '#85DEDC',
},
headerShadowVisible: false,
}}
/>
<Stack.Screen
name="AddMedicine"
component={AddMedicine}
options={{
headerTitle: '',
headerRight: () => <Header />,
headerStyle: {
backgroundColor: '#ffffff',
},
headerShadowVisible: false,
}}
/>
<Stack.Screen
name="EditMedicine"
component={EditMedicine}
options={{
headerTitle: '',
headerRight: () => <Header />,
headerStyle: {
backgroundColor: '#ffffff',
},
headerShadowVisible: false,
}}
/>
</Stack.Navigator>
</NavigationContainer>
</PaperProvider>
);
};
export default App;