-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
95 lines (86 loc) · 3.05 KB
/
App.tsx
File metadata and controls
95 lines (86 loc) · 3.05 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
import React, { useEffect, useRef } from 'react';
import { BackHandler, ToastAndroid, Platform } from 'react-native';
import {
NavigationContainer,
useNavigationContainerRef,
} from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import HomeScreen from './screens/HomeScreen';
import WebViewTestScreen from './screens/WebViewTestScreen';
import {
DefaultPaymentScreen,
TotalPaymentScreen,
SubscriptionScreen,
AuthenticationScreen,
WidgetPaymentScreen,
CommerceScreen,
} from './screens/payments';
const Stack = createNativeStackNavigator();
const BACK_EXIT_INTERVAL_MS = 2000;
// react-navigation 화면을 onBack 패턴 화면과 연결하는 어댑터
function withOnBack<P extends { onBack: () => void }>(
Component: React.ComponentType<P>,
) {
return ({ navigation }: { navigation: { goBack: () => void; popToTop: () => void } }) => {
const onBack = () => {
if (typeof navigation.popToTop === 'function') {
navigation.popToTop();
} else {
navigation.goBack();
}
};
return <Component {...({ onBack } as P)} />;
};
}
const DefaultPayment = withOnBack(DefaultPaymentScreen);
const TotalPayment = withOnBack(TotalPaymentScreen);
const Subscription = withOnBack(SubscriptionScreen);
const Authentication = withOnBack(AuthenticationScreen);
const WidgetPayment = withOnBack(WidgetPaymentScreen);
const Commerce = withOnBack(CommerceScreen);
export default function App() {
const navigationRef = useNavigationContainerRef();
const lastBackPressRef = useRef<number>(0);
useEffect(() => {
if (Platform.OS !== 'android') return;
const onBackPress = () => {
if (navigationRef.canGoBack()) {
navigationRef.goBack();
return true;
}
const now = Date.now();
if (now - lastBackPressRef.current < BACK_EXIT_INTERVAL_MS) {
BackHandler.exitApp();
return true;
}
lastBackPressRef.current = now;
ToastAndroid.show('한 번 더 누르면 종료됩니다', ToastAndroid.SHORT);
return true;
};
const subscription = BackHandler.addEventListener(
'hardwareBackPress',
onBackPress,
);
return () => subscription.remove();
}, [navigationRef]);
return (
<NavigationContainer ref={navigationRef}>
<Stack.Navigator
screenOptions={{ headerShown: false }}
>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="DefaultPayment" component={DefaultPayment} />
<Stack.Screen name="TotalPayment" component={TotalPayment} />
<Stack.Screen name="Subscription" component={Subscription} />
<Stack.Screen name="Authentication" component={Authentication} />
<Stack.Screen name="WidgetPayment" component={WidgetPayment} />
<Stack.Screen name="Commerce" component={Commerce} />
<Stack.Screen
name="WebViewTest"
component={WebViewTestScreen}
options={{ headerShown: true, title: '웹뷰 테스트' }}
/>
</Stack.Navigator>
</NavigationContainer>
);
}