-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
164 lines (134 loc) · 4.24 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
import { useEffect } from 'react';
import { PermissionsAndroid, StyleSheet } from 'react-native';
import BootSplash from 'react-native-bootsplash';
import Geolocation from 'react-native-geolocation-service';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { SafeAreaView } from 'react-native-safe-area-context';
import Toast from 'react-native-toast-message';
import { AppNavigator } from '@navigation/AppNavigator';
import ErrorBoundary from '@components/ErrorBoundary';
import { GradientBackground } from '@components/GradientBackground';
import { getCityWeather, getWeather } from '@utils/api';
import { commonValues } from '@utils/commonValues';
import { getFromStorage } from '@utils/storageService';
import { STORAGE_KEYS } from '@utils/storageService/storageKeys';
import { WeatherDataProps, WeatherStore } from '@utils/types';
import { useWeatherStore } from '@store/weatherStore';
const App = () => {
const { setLoading, isGeolocation, setIsGeolocation, setWeatherStoreData } =
useWeatherStore() as WeatherStore;
const requestLocationPermission = async () => {
if (!commonValues.IS_IOS) {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
return granted === PermissionsAndroid.RESULTS.GRANTED;
}
return true;
};
const fetchWeatherByLocation = async (lat: number, lon: number) => {
setLoading(true);
try {
const dataLocation = await getWeather(lat, lon);
setWeatherStoreData(dataLocation);
} catch (error) {
console.error(error);
Toast.show({
type: 'error',
text1: 'Something went wrong.',
position: 'bottom',
});
} finally {
setLoading(false);
}
};
const fetchWeatherByCity = async (city: string) => {
setLoading(true);
try {
const dataCity = await getCityWeather(city);
setWeatherStoreData(dataCity);
} catch (error) {
console.error(error);
Toast.show({
type: 'error',
text1: 'Something went wrong.',
position: 'bottom',
});
} finally {
setLoading(false);
}
};
const getLocation = async () => {
const hasPermission = await requestLocationPermission();
if (!hasPermission) {
return null;
}
const [city, citiesList] = await Promise.all([
getFromStorage<WeatherDataProps>(STORAGE_KEYS.SELECTED_CITY_WEATHER),
getFromStorage<string[]>(STORAGE_KEYS.CITIES_LIST),
]);
if (citiesList?.length && city && !isGeolocation) {
fetchWeatherByCity(city.name);
} else {
Geolocation.getCurrentPosition(
position => {
const coords = {
lat: position.coords.latitude,
lon: position.coords.longitude,
};
fetchWeatherByLocation(coords.lat, coords.lon);
setIsGeolocation(true);
},
error => {
if (error.code === 1) {
Toast.show({
type: 'error',
text1: error.message,
text2:
'Please allow access to your location to provide accurate weather information.',
position: 'bottom',
});
setIsGeolocation(false);
}
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 },
);
}
};
useEffect(() => {
const fetchData = async () => {
await getLocation();
};
fetchData();
}, [isGeolocation]);
useEffect(() => {
const getLocationSettings = async () => {
const isAlwaysUseLocation = await getFromStorage(
STORAGE_KEYS.IS_ALWAYS_USE_GEOLOCATION,
);
setIsGeolocation(!!isAlwaysUseLocation);
};
getLocationSettings();
}, []);
useEffect(() => {
BootSplash.hide({ fade: true });
}, []);
return (
<ErrorBoundary>
<GestureHandlerRootView style={styles.container}>
<GradientBackground>
<SafeAreaView edges={['right', 'left']} style={styles.container}>
<AppNavigator />
<Toast />
</SafeAreaView>
</GradientBackground>
</GestureHandlerRootView>
</ErrorBoundary>
);
};
const styles = StyleSheet.create({
container: {
flex: commonValues.FLEX_1,
},
});
export default App;