-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChap8.js
More file actions
108 lines (104 loc) · 2.81 KB
/
Chap8.js
File metadata and controls
108 lines (104 loc) · 2.81 KB
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
import { StatusBar } from "expo-status-bar";
import { useEffect, useState } from "react";
import { StyleSheet, Text, View, ScrollView, Dimensions } from "react-native";
import * as Location from "expo-location";
import { ActivityIndicator } from "react-native";
const { width: SCREEN_WIDTH } = Dimensions.get("window");
const API_KEY = "615f61618f774aea3e40ad01a004e5f4";
export default function Chap8() {
const [city, setCity] = useState("Loading...");
const [days, setDays] = useState([]);
const [ok, setOk] = useState(true);
const getWeather = async () => {
const { granted } = await Location.requestForegroundPermissionsAsync();
if (!granted) {
setOk(false);
}
const {
coords: { latitude, longitude },
} = await Location.getCurrentPositionAsync({ accuracy: 5 });
const location = await Location.reverseGeocodeAsync(
{ latitude, longitude },
{ useGoogleMaps: false }
);
setCity(location[0].region);
const response = await fetch(
`https://api.openweathermap.org/data/2.5/forecast?lat=${latitude}&lon=${longitude}&appid=${API_KEY}&units=metric`
);
const json = await response.json();
setDays(
json.list.filter((weather) => {
if (weather.dt_txt.includes("00:00:00")) {
return weather;
}
})
);
};
useEffect(() => {
getWeather();
}, []);
return (
<View style={styles.container}>
<View style={styles.city}>
<Text style={styles.cityName}>{city}</Text>
</View>
<ScrollView
horizontal
pagingEnabled
showsHorizontalScrollIndicator={false} //스크롤바 없애기
contentContainerStyle={styles.weather}
>
{days.length === 0 ? (
<View style={styles.day}>
<ActivityIndicator
color="white"
style={{ marginTop: 10 }}
size="large"
/>
</View>
) : (
days.map((day, index) => (
<View key={index} style={styles.day}>
<Text style={styles.temp}>
{parseFloat(day.main.temp).toFixed(1)}
</Text>
<Text style={styles.description}>{day.weather[0].main} </Text>
<Text style={styles.tinyText}>{day.weather[0].description}</Text>
</View>
))
)}
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "tomato",
},
city: {
flex: 1.2,
justifyContent: "center",
alignItems: "center",
},
cityName: {
fontSize: 68,
fontWeight: "500",
},
weather: {},
day: {
width: SCREEN_WIDTH,
alignItems: "center",
},
temp: {
fontSize: 178,
marginTop: 50,
},
description: {
fontSize: 60,
marginTop: -30,
},
tinyText: {
fontSize: 30,
},
});