-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
236 lines (228 loc) · 8.28 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
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
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import React, { useEffect, useState } from 'react';
import SplashScreen from 'react-native-splash-screen';
import Toast from 'react-native-toast-message';
import { RecoilRoot } from 'recoil';
import AskIntro from './src/screens/AskIntro';
import AskNotificationTime from './src/screens/AskNotificationTime';
import AskOutro from './src/screens/AskOutro';
import AskTimeZone from './src/screens/AskTimeZone';
import AskWeather from './src/screens/AskWeather';
import EmailLogin from './src/screens/EmailLogin';
import GreetNaming from './src/screens/GreetNaming';
import Greeting from './src/screens/Greeting';
import Login from './src/screens/Login';
import LoginReset from './src/screens/LoginReset';
import PasswordReset from './src/screens/PasswordReset';
import Register from './src/screens/Register';
import { toastConfig } from './src/utils/toastConfig';
import SettingWind from './src/screens/SettingWind';
import { MAIN_COLOR } from './src/styles/color';
import Settings from './src/screens/Settings';
import CompanySetting from './src/screens/CompanySetting';
import MainScreenSetting from './src/screens/MainScreenSetting';
import SettingNotification from './src/screens/SettingNotification';
import UserDataSetting from './src/screens/UserDataSetting';
import PrivacySetting from './src/screens/PrivacySetting';
import MainScreen from './src/screens/MainScreen';
import Notifications from './src/screens/Notifications';
import Report from './src/screens/Report';
import Web from './src/screens/Web';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import BackgroundFetch from 'react-native-background-fetch';
import Geolocation from 'react-native-geolocation-service';
import { Platform } from 'react-native';
import { AppState } from 'react-native';
import { getLocation } from './src/utils/geolocation';
async function requestPermission() {
try {
if (Platform.OS === 'ios') {
return await Geolocation.requestAuthorization('always');
}
} catch (e) {
console.log(e);
}
}
export default function App() {
const queryClient = new QueryClient();
const Stack = createNativeStackNavigator();
const [appState, setAppState] = useState(AppState.currentState);
const [location, setLocation] = useState({
latitude: null,
longitude: null,
});
//-----------------------------------------
useEffect(() => {
if (SplashScreen) {
SplashScreen.hide();
}
//-------------백그라운드 관련---------------------
backgroundStart();
const subscription = AppState.addEventListener(
'change',
handleAppStateChange,
);
return () => {
subscription.remove();
};
}, []);
useEffect(() => {
if (location.latitude && location.longitude) {
console.log('위치 업데이트됨:', location);
}
}, [location]);
// -----------------현재 앱 상태 확인--------------------
const handleAppStateChange = (nextAppState) => {
console.log('현재 앱 상태:', nextAppState);
setAppState(nextAppState);
};
// -----------------------------------------
const backgroundStart = () => {
BackgroundFetch.configure(
{
minimumFetchInterval: 15,
forceAlarmManager: true,
stopOnTerminate: false,
startOnBoot: true,
requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE,
requiresCharging: false, //충전 필요시
requiresDeviceIdle: false,
requiresBatteryNotLow: false, //배터리가 낮으면 백그라운드 실행X
requiresStorageNotLow: false, //저장공간이 낮으면 백그라운드 실행X
},
async (taskId) => {
console.log('[js] 백그라운드 페치 이벤트 수신:', taskId);
//-----------------------------------------
switch (taskId) {
case 'com.transistorsoft.fetch':
requestPermission().then((result) => {
if (result === 'granted') {
getLocation();
}
});
console.log('사용자 위치 전송, 백그라운드 성공');
console.log('스케쥴 성공 후 위치 출력', location);
break;
default:
console.log('기본 페치 작업');
break;
}
BackgroundFetch.finish(taskId);
},
(error) => {
console.log('[js] RNBackgroundFetch를 시작하지 못했습니다:', error);
},
);
//-----------------------------------------
BackgroundFetch.scheduleTask({
taskId: 'com.transistorsoft.fetch',
forceAlarmManager: true,
delay: 1000,
periodic: true,
});
console.log('백그라운드 작업 예약 완료');
//-----------------------------------------
BackgroundFetch.status((status) => {
switch (status) {
case BackgroundFetch.STATUS_RESTRICTED:
console.log('BackgroundFetch가 제한됨');
break;
case BackgroundFetch.STATUS_DENIED:
console.log('BackgroundFetch가 거부됨');
break;
case BackgroundFetch.STATUS_AVAILABLE:
console.log('BackgroundFetch가 활성화되었습니다');
break;
}
});
};
//==========================================================
return (
<QueryClientProvider client={queryClient}>
<RecoilRoot>
<NavigationContainer>
<Stack.Navigator initialRouteName="Login">
<Stack.Screen
name="Login"
component={Login}
options={{ headerShown: false }}
/>
<Stack.Screen name="EmailLogin" component={EmailLogin} />
<Stack.Screen name="Register" component={Register} />
<Stack.Screen name="LoginReset" component={LoginReset} />
<Stack.Screen name="PasswordReset" component={PasswordReset} />
<Stack.Screen
options={{ headerShown: true }}
name="Greeting"
component={Greeting}
/>
<Stack.Screen name="GreetNaming" component={GreetNaming} />
<Stack.Screen name="AskIntro" component={AskIntro} />
<Stack.Screen name="AskWeather" component={AskWeather} />
<Stack.Screen name="AskTimeZone" component={AskTimeZone} />
<Stack.Screen
name="AskNotificationTime"
component={AskNotificationTime}
/>
<Stack.Screen
options={{ headerShown: true }}
name="AskOutro"
component={AskOutro}
/>
<Stack.Screen
name="SettingWind"
options={{
title: '바람 세기 설정',
headerStyle: {
backgroundColor: MAIN_COLOR,
},
headerTintColor: '#fff',
}}
component={SettingWind}
/>
<Stack.Screen name="Settings" component={Settings} />
<Stack.Screen name="CompanySetting" component={CompanySetting} />
<Stack.Screen
name="MainScreenSetting"
component={MainScreenSetting}
/>
<Stack.Screen
name="SettingNotification"
component={SettingNotification}
/>
<Stack.Screen
name="UserDataSetting"
options={{
headerShown: false,
}}
component={UserDataSetting}
/>
<Stack.Screen name="PrivacySetting" component={PrivacySetting} />
<Stack.Screen
options={{ headerShown: false }}
name="MainScreen"
component={MainScreen}
/>
<Stack.Screen name="Notifications" component={Notifications} />
<Stack.Screen
name="Report"
options={{
headerShown: false,
}}
component={Report}
/>
<Stack.Screen
name="Web"
options={{
headerShown: true,
}}
component={Web}
/>
</Stack.Navigator>
</NavigationContainer>
<Toast config={toastConfig} />
</RecoilRoot>
</QueryClientProvider>
);
}