-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
150 lines (138 loc) · 4.36 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* Generated with the TypeScript template
* https://github.com/react-native-community/react-native-template-typescript
*
* @format
*/
import React, {useEffect, useState} from 'react';
import {firebaseConfig} from './src/firebaseConfig';
import {initializeApp} from 'firebase/app';
import {getStorage} from 'firebase/storage';
// import {
// Colors,
// DebugInstructions,
// Header,
// LearnMoreLinks,
// ReloadInstructions,
// } from 'react-native/Libraries/NewAppScreen';
import auth from '@react-native-firebase/auth';
import {GOOGLE_SIGN_IN_WED_CLIENT_ID} from '@env';
import {GoogleSignin} from '@react-native-google-signin/google-signin';
import {NavigationContainer} from '@react-navigation/native';
import {navigationRef} from './src/utils/RouteNavigation';
import AuthStack from './src/stacks/Auth/AuthStack';
import useUserStore from './src/store/useUserStore';
import {NativeBaseProvider} from 'native-base';
import TabScreen from './src/TabScreen';
import {WithSplashScreen} from './src/screens/SplashScreen';
import {requestLocationPermission} from './src/utils';
import Geolocation, {GeoPosition} from 'react-native-geolocation-service';
const app = initializeApp(firebaseConfig);
export const appStorage = getStorage(app);
GoogleSignin.configure({
webClientId: GOOGLE_SIGN_IN_WED_CLIENT_ID,
// iosClientId:
// '969588173065-69otq7c42vr68uor6eqfjaj607nke2i6.apps.googleusercontent.com',
});
const App = () => {
// const isDarkMode = useColorScheme() === 'dark';
const {setUser, user, setUserLocation} = useUserStore();
const [initializing, setInitializing] = useState(true);
const [isAppReady, setIsAppReady] = useState(false);
// const backgroundStyle = {
// backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
// };
useEffect(() => {
setIsAppReady(true);
let watchID: number = 0;
const getLocation = async () => {
await requestLocationPermission();
Geolocation.getCurrentPosition(
(position: GeoPosition) => {
setUserLocation([
position.coords.latitude,
position.coords.longitude,
]);
},
error => {
// See error code charts below.
console.log(error.code, error.message);
return [0, 0];
},
{
accuracy: {
android: 'high',
ios: 'best',
},
enableHighAccuracy: true,
timeout: 15000,
maximumAge: 10000,
},
);
try {
watchID = Geolocation.watchPosition(
(position: GeoPosition) => {
console.log('Watch Location:', position);
setUserLocation([
position.coords.latitude,
position.coords.longitude,
]);
return;
},
error => {
// See error code charts below.
console.log(error.code, error.message);
setUserLocation([]);
return;
},
{
accuracy: {
android: 'high',
ios: 'best',
},
enableHighAccuracy: true,
interval: 3000,
fastestInterval: 3000,
distanceFilter: 5,
},
);
// console.log('WATCH ID', watchID);
} catch (error) {
console.log('Location Watch Error', error);
}
};
getLocation();
// console.log('WATCH ID', watchID);
return () => {
// FIXME: watchID not being returned in above function
// stopObserving works somwtimes but throws yellow box error
Geolocation.clearWatch(watchID);
Geolocation.stopObserving();
};
}, [setUserLocation]);
// Handle user state changes
function onAuthStateChanged(currentUser: any) {
setUser(currentUser);
if (initializing) {
setInitializing(false);
}
}
useEffect(() => {
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
return subscriber; // unsubscribe on unmount
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<WithSplashScreen isAppReady={isAppReady}>
<NavigationContainer ref={navigationRef}>
<NativeBaseProvider>
{!user ? <AuthStack /> : <TabScreen />}
</NativeBaseProvider>
</NavigationContainer>
</WithSplashScreen>
);
};
export default App;