-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
167 lines (152 loc) · 4.53 KB
/
App.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
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
import { StatusBar } from 'expo-status-bar';
import React, { useEffect, useState } from 'react';
import { StyleSheet, Text, View, ActivityIndicator } from 'react-native';
import * as Location from 'expo-location'
import WeatherInfo from './components/WeatherInfo'
import UnitsPicker from './components/UnitsPicker'
import ReloadIcon from './components/ReloadIcon'
import WeatherDetails from './components/WeatherDetails'
import BackgroundImage from './components/BackgroundImage';
import { colors } from './utils/index'
import { weather_API_KEY } from 'react-native-dotenv'
const Thunderstorm = "./assets/backgrounds/Thunderstorm.png"
const Drizzle = "./assets/backgrounds/Drizzle.png"
const Snow = "./assets/backgrounds/Snow.png"
const Rain = "./assets/backgrounds/Rain.png"
const Mist = "./assets/backgrounds/Mist.png"
const Smoke = "./assets/nov0caina.png"
const Haze = "./assets/nov0caina.png"
const Dust = "./assets/nov0caina.png"
const Fog = "./assets/nov0caina.png"
const Sand = "./assets/nov0caina.png"
const Ash = "./assets/nov0caina.png"
const Squall = "./assets/nov0caina.png"
const Tornado = "./assets/nov0caina.png"
const Clear = "./assets/backgrounds/Clear.png"
const Clouds = "./assets/backgrounds/Clouds.png"
const base_WEATHER_URL = 'https://api.openweathermap.org/data/2.5/weather?'
export default function App() {
const [errorMessage, setErrorMessage] = useState(null)
const [currentWeather, setCurrentWeather] = useState(null)
const [unitsSystem, setUnitsSystem] = useState('metric')
useEffect(() => {
load()
}, [unitsSystem])
async function load() {
setCurrentWeather(null)
setErrorMessage(null)
try {
let { status } = await Location.requestPermissionsAsync()
if (status != 'granted') {
setErrorMessage('Access to location is needed in order to run the app')
return
}
const location = await Location.getCurrentPositionAsync();
const { latitude, longitude } = await location.coords;
const weatherUrl = `${base_WEATHER_URL}lat=${latitude}&lon=${longitude}&units=${unitsSystem}&appid=${weather_API_KEY}`;
const response = await fetch(weatherUrl);
const result = await response.json();
if (response.ok) {
setCurrentWeather(result);
}
else {
setErrorMessage(result.message)
}
}
catch (error) {
setErrorMessage(error.message)
}
}
if (currentWeather) {
const {
weather: [details]
} = currentWeather
const { main } = details
let bg
switch (main) {
case 'Thunderstorm':
bg = require(Thunderstorm);
break;
case 'Drizzle':
bg = require(Drizzle);
break;
case 'Rain':
bg = require(Rain);
break;
case 'Snow':
bg = require(Snow);
break;
case 'Mist':
bg = require(Mist);
break;
case 'Smoke':
bg = require(Smoke);
break;
case 'Haze':
bg = require(Haze);
break;
case 'Dust':
bg = require(Dust);
break;
case 'Fog':
bg = require(Fog);
break;
case 'Sand':
bg = require(Sand);
break;
case 'Ash':
bg = require(Ash);
break;
case 'Squall':
bg = require(Squall);
break;
case 'Tornado':
bg = require(Tornado);
break;
case 'Clear':
bg = require(Clear);
break;
case 'Clouds':
bg = require(Clouds);
break;
}
return (
<View style={styles.container}>
<BackgroundImage imageSource={bg} opacity={0.8}>
<StatusBar style="auto" />
<View style={styles.main}>
<UnitsPicker unitsSystem={unitsSystem} setUnitsSystem={setUnitsSystem} />
<ReloadIcon load={load} />
<WeatherInfo currentWeather={currentWeather} º />
</View>
<WeatherDetails currentWeather={currentWeather} unitsSystem={unitsSystem} />
</BackgroundImage>
</View>
)
} else if (errorMessage) {
return (
<View style={styles.container}>
<ReloadIcon load={load} />
<Text style={{ textAlign: 'center' }}>{errorMessage}</Text>
<StatusBar style="auto" />
</View>
)
} else {
return (
<View style={styles.container}>
<ActivityIndicator size="large" color={colors.primaryColor} />
<StatusBar style="auto" />
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
main: {
justifyContent: 'center',
flex: 1,
},
});