-
Notifications
You must be signed in to change notification settings - Fork 2
/
refresh-wrapper.js
131 lines (114 loc) · 3.37 KB
/
refresh-wrapper.js
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
import React, {useState, useEffect, useRef} from 'react';
import {
ScrollView,
RefreshControl,
StyleSheet,
Text,
AppState,
} from 'react-native';
import AppComponents from './app-components';
import RNLocation from 'react-native-location';
import AsyncStorage from '@react-native-community/async-storage';
import {
getLatestLocation,
getCurrentLocationPromise,
getLocationData,
getPermissionPromise,
} from './constants/location-and-weather';
import {openWeatherRequest} from './constants/open-weather';
import {getTemperatureUnitPromise} from './constants/settings';
import {SafeAreaView} from 'react-native-safe-area-context';
const RefreshAppWrapper = ({navigation}) => {
const [savedLocation, setSavedLocation] = useState();
const appState = useRef(AppState.currentState);
const [appStateVisible, setAppStateVisible] = useState(appState.current);
const [weatherConditions, setWeatherConditions] = useState('Clear');
const [weatherResponse, setWeatherResponse] = useState({});
const [temperatureUnit, setTemperatureUnit] = useState('C');
useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
checkPermission();
getTemperatureUnitPromise().then(unit => {
if (unit === 'Metric') {
setTemperatureUnit('C');
} else {
setTemperatureUnit('F');
}
});
});
return unsubscribe;
}, [navigation]);
useEffect(() => {
AppState.addEventListener('change', _handleAppStateChange);
return () => {
AppState.removeEventListener('change', _handleAppStateChange);
};
}, []);
const _handleAppStateChange = nextAppState => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
checkPermission();
}
appState.current = nextAppState;
setAppStateVisible(appState.current);
};
const checkPermission = () => {
RNLocation.checkPermission({
ios: 'whenInUse', // or 'always'
android: {
detail: 'coarse', // or 'fine'
},
}).then(currentPermission => {
if (currentPermission === false) {
return getPermissionPromise().then(permission => {
if ((permission = true)) {
checkWeatherBasedOnLocation();
} else {
// display error modal
}
});
} else {
return checkWeatherBasedOnLocation();
}
});
};
const [refreshing, setRefreshing] = React.useState(false);
const onRefresh = React.useCallback(() => {
setRefreshing(true);
getCurrentLocationPromise().then(() => {
checkWeatherBasedOnLocation();
setRefreshing(false);
});
}, []);
const checkWeatherBasedOnLocation = location => {
getLocationData().then(location => {
openWeatherRequest(40.73, -73.935242, setWeatherConditions).then(
response => {
setWeatherResponse(response);
},
);
});
};
return (
<ScrollView
contentContainerStyle={styles.scrollView}
refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
}>
<AppComponents
onRefresh={onRefresh}
navigation={navigation}
weatherResponse={weatherResponse}
temperatureUnit={temperatureUnit}
/>
</ScrollView>
);
};
const styles = StyleSheet.create({
scrollView: {
flex: 1,
},
});
export default RefreshAppWrapper;