-
Notifications
You must be signed in to change notification settings - Fork 0
/
InitialApp.tsx
231 lines (203 loc) · 7.99 KB
/
InitialApp.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
import { connect } from "react-redux";
import React, { useEffect, useState } from 'react';
import { AppState, BackHandler, Image, Linking, PermissionsAndroid, StyleSheet, Text, TouchableHighlight, View } from "react-native";
import SplashScreen from "react-native-splash-screen";
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { Routes } from "./src/config/routes";
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
import { faCircleUser, faMagnifyingGlass } from '@fortawesome/free-solid-svg-icons';
import * as RootNavigation from "./src/config/rootNavigation";
import KeepAwake from "react-native-keep-awake";
import { ModalConfirm } from "./src/components/modal";
import { useTranslation } from "react-i18next";
import { requestLocationPermission } from "./src/intl/permissions";
import RNExitApp from 'react-native-exit-app';
import DeviceInfo from "react-native-device-info";
import * as layout from './src/components/layout/actionLayout';
interface RootState {
layout: any
}
interface IRoutes {
path: string;
name: string;
component: any;
options: any;
}
const Stack = createNativeStackNavigator();
const HeaderRight = (props: any): JSX.Element => {
const goProfile = (): void => {
RootNavigation.navigate('Profile', {});
}
const goSearch = (): void => {
RootNavigation.navigate('Search', {});
}
return (
<View style={styles.HeaderRight}>
<View>
<TouchableHighlight underlayColor="transparent" onPress={goSearch}>
<FontAwesomeIcon icon={faMagnifyingGlass} color={'white'} size={24} />
</TouchableHighlight>
</View>
<View>
<TouchableHighlight underlayColor="transparent" onPress={goProfile}>
<FontAwesomeIcon icon={faCircleUser} color={'white'} size={24} />
</TouchableHighlight>
</View>
</View>
)
}
const HeaderLogo = (props: any): JSX.Element => {
const goAbout = (): void => {
RootNavigation.navigate('About', {});
}
return (
<View>
<TouchableHighlight underlayColor="transparent" onPress={goAbout}>
<Image
style={styles.debatre}
source={require('./src/assets/png/debatre-name.png')}
/>
</TouchableHighlight>
</View>
)
}
interface IInitialApp {
initDevicesUniqueId: string;
initLocale: string;
onSetDevicesUniqueId: any;
rootTag: number;
setBackground: any;
onSetLocale: any
}
const InitialApp = (props: IInitialApp): JSX.Element => {
const { setBackground, initLocale, initDevicesUniqueId, onSetDevicesUniqueId, onSetLocale } = props;
const { t, i18n } = useTranslation();
const [modalExitApp, setModalExitApp] = useState<boolean>(false);
const [modalLocationPermission, setModalLocationPermission] = useState<boolean>(false);
useEffect(() => {
BackHandler.addEventListener('hardwareBackPress', backPressed);
return () => {
BackHandler.removeEventListener('hardwareBackPress', backPressed);
}
}, []);
useEffect(() => {
if (initLocale) {
i18n.changeLanguage(initLocale);
} else {
i18n.changeLanguage('id');
onSetLocale('id');
}
DeviceInfo.getUniqueId().then((uniqueId) => {
const devicesUniqueId: string = uniqueId;
if (!initDevicesUniqueId) {
onSetDevicesUniqueId(devicesUniqueId);
}
});
}, []);
const handleLocationPermission = async () => {
const result = await requestLocationPermission(t('LOCATION_ACCESS'), t('MESSAGE_LOCATION_ACCESS'), t('YES'));
if (typeof result === 'string' && result === PermissionsAndroid.RESULTS.DENIED) {
setModalLocationPermission(true);
} else if (typeof result === 'string' && result === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
setModalLocationPermission(true);
} else {
console.info('handleLocationPermission-', result)
}
}
useEffect(() => {
const subscription = AppState.addEventListener('focus', () => {
handleLocationPermission();
});
return () => subscription.remove();
}, []);
const onReadyNavigation = (): void => {
SplashScreen.hide();
KeepAwake.activate();
}
const backPressed = (): any => {
const rootState = RootNavigation.navigationRef.getRootState();
const currentState: any = RootNavigation.navigationRef.getCurrentRoute();
if (rootState.routeNames[0] === 'Home' && currentState.name === 'Home') {
setModalExitApp(true);
return true;
}
}
const ContentModalConfirm = (): JSX.Element => {
return (<View>
<Text style={{ color: 'black', fontFamily: 'Sriracha', fontSize: 14 }}>{t('EXIT_APP_MESSAGE')}</Text>
</View>)
}
const ContentModalLocationPermission = (): JSX.Element => {
return (<View>
<Text style={{ color: 'black', fontFamily: 'Sriracha', fontSize: 14 }}>{t('MESSAGE_LOCATION_ACCESS')}</Text>
</View>)
}
const onPressNoModalConfirm = (): void => {
setModalExitApp(false);
}
const onPressNoModalLocationPermission = (): void => {
setModalLocationPermission(false);
RNExitApp.exitApp();
}
const onPressYesModalConfirm = (): void => {
setModalExitApp(false);
RNExitApp.exitApp();
}
const onPressYesModalLocationPermission = (): void => {
setModalLocationPermission(false);
Linking.openSettings();
RNExitApp.exitApp();
}
return (
<NavigationContainer ref={RootNavigation.navigationRef} onReady={onReadyNavigation}>
<Stack.Navigator
initialRouteName='Home'
screenOptions={{
headerShown: true,
headerStyle: {
backgroundColor: 'transparent'
},
headerShadowVisible: false,
headerTintColor: '#FFFFFF',
headerTitleAlign: 'center',
headerTitle: '',
contentStyle: styles.contentStyle,
headerRight: () => (<HeaderRight {...props} />),
headerLeft: () => (<HeaderLogo {...props} />)
}}
>
{
Routes.map((item: IRoutes, index: number) => <Stack.Screen key={index.toString()} name={item.path} component={item.component} options={{
...item.options
}} />)
}
</Stack.Navigator>
<ModalConfirm visible={modalExitApp} title={t('EXIT_APP')} content={<ContentModalConfirm />} onPressNo={onPressNoModalConfirm} onPressYes={onPressYesModalConfirm} />
<ModalConfirm visible={modalLocationPermission} title={t('LOCATION_ACCESS')} labelCancel={t('EXIT_APP')} labelOk={t('OPEN_SETTINGS')} content={<ContentModalLocationPermission />} onPressNo={onPressNoModalLocationPermission} onPressYes={onPressYesModalLocationPermission} />
</NavigationContainer>
)
}
const mapStateToProps = (state: RootState): any => ({
initLocale: state.layout.locale,
initDevicesUniqueId: state.layout.devicesUniqueId,
});
const mapDispatchToProps = (dispatch: any): any => ({
onSetDevicesUniqueId: (value: any) => dispatch(layout.actions.setDevicesUniqueId(value)),
onSetLocale: (value: any) => dispatch(layout.actions.setLocale(value)),
});
export default connect(mapStateToProps, mapDispatchToProps)(InitialApp);
const styles = StyleSheet.create({
debatre: {
width: 80,
height: 50,
resizeMode: 'contain'
},
contentStyle: {
backgroundColor: 'transparent',
},
HeaderRight: {
flexDirection: 'row',
gap: 20
},
});