-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.tsx
147 lines (134 loc) · 4.63 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
import React, { useCallback, useEffect, useState } from 'react';
import { RefreshControl, SafeAreaView, ScrollView, StyleSheet, View } from 'react-native';
import * as SplashScreen from 'expo-splash-screen';
import { fetchForecast } from './src/utils/fetchWeather';
import CurrentWeatherCard from './src/components/CurrentWeatherCard/CurrentWeatherCard';
import { Weather } from './src/types/WeatherTypes';
import { palette } from './src/Styles/Palette';
import HourlyForecast from './src/components/HourlyForecast/HourlyForecast';
import AppHeader from "./src/components/AppHeader/AppHeader";
import DailyForecast from "./src/components/DailyForecast/DailyForecast";
import { AppStateContext } from "./src/utils/AppStateContext";
import { getSelectedTempScale } from "./src/utils/AsyncStorageHelper";
import AppFooter from "./src/components/AppFooter/AppFooter";
import { i18n, translations } from "./src/localization/i18n";
import { uses24HourClock } from "react-native-localize";
import { getLanguage } from "react-native-localization-settings";
import moment from "moment";
import "moment/locale/he";
import "moment/locale/es";
import "moment/locale/ar";
import "moment/locale/fr";
import {
useFonts,
DMSans_400Regular,
DMSans_400Regular_Italic,
DMSans_500Medium,
DMSans_500Medium_Italic,
DMSans_700Bold,
DMSans_700Bold_Italic,
} from "@expo-google-fonts/dm-sans";
// Keep the splash screen visible while we fetch resources
SplashScreen.preventAutoHideAsync();
export default function App() {
const [forecast, setForecast] = useState<Weather>();
const [tempScale, setTempScale] = useState<"C" | "F">("F");
const [location, setLocation] = useState<string>("");
const [refreshing, setRefreshing] = useState<boolean>(false);
const [date, setDate] = useState(moment());
const [appIsReady, setAppIsReady] = useState(false);
let [fontsLoaded] = useFonts({
DMSans_400Regular,
DMSans_400Regular_Italic,
DMSans_500Medium,
DMSans_500Medium_Italic,
DMSans_700Bold,
DMSans_700Bold_Italic,
});
const setLocale = () => {
const clockStyle = uses24HourClock() ? "HH:mm" : "h:mm a";
moment().format(clockStyle);
const userLocale = getLanguage().split("-")[0];
//If locale isn't in the translations object, it'll default to English
const deviceLocal = translations[userLocale] ? userLocale : "en";
i18n.locale = deviceLocal;
moment.locale(deviceLocal);
};
useEffect(() => {
async function prepare() {
console.log(getLanguage());
try {
//Set locale
setLocale();
//Load Forecast data
await loadForecast();
} catch (e) {
console.warn(e);
} finally {
// Tell the application to render
setAppIsReady(true);
}
}
prepare();
}, []);
const onLayoutRootView = useCallback(async () => {
if (appIsReady) {
// This tells the splash screen to hide immediately! If we call this after
// `setAppIsReady`, then we may see a blank screen while the app is
// loading its initial state and rendering its first pixels. So instead,
// we hide the splash screen once we know the root view has already
// performed layout.
await SplashScreen.hideAsync();
}
}, [appIsReady]);
const loadForecast = async () => {
setTempScale(await getSelectedTempScale());
setRefreshing(true);
const fetched = await fetchForecast(i18n.locale);
setForecast(fetched?.data);
setLocation(fetched?.location.city);
setDate(moment());
setRefreshing(false);
};
const onRefresh = React.useCallback(async () => {
loadForecast();
}, []);
if (!fontsLoaded || !appIsReady || !forecast) {
return null;
}
return (
<SafeAreaView style={styles.container}>
<View onLayout={onLayoutRootView}>
<ScrollView
refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
}
>
<AppStateContext.Provider
value={{ forecast, date, tempScale, setTempScale }}
>
<AppHeader location={location} />
<CurrentWeatherCard
temp={forecast.current.temp}
weather={forecast.current.weather[0]}
/>
<HourlyForecast hourlyForecast={forecast.hourly?.slice(0, 24)} />
<DailyForecast dailyForecast={forecast.daily} />
</AppStateContext.Provider>
<AppFooter />
</ScrollView>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: palette.primaryDark
},
scrollView: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
}
});