-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapt10.js
More file actions
137 lines (127 loc) · 3.49 KB
/
Chapt10.js
File metadata and controls
137 lines (127 loc) · 3.49 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
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
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";
import { Fontisto } from "@expo/vector-icons"; // 올바르게 import
const { width: SCREEN_WIDTH } = Dimensions.get("window");
const API_KEY = "615f61618f774aea3e40ad01a004e5f4";
const icons = {
Clouds: "cloudy", // 'cloudy' 아이콘을 사용
clear: "day-sunny",
Atomsphere: "",
snow: "snow",
Rain: "rains",
Drizzle: "rain",
Thunderstorm: "lightning",
};
export default function Chapt10() {
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, alignItems: "center" }}>
<ActivityIndicator
color="white"
style={{ marginTop: 10 }}
size="large"
/>
</View>
) : (
days.map((day, index) => (
<View key={index} style={styles.day}>
<View
style={{
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
width: "100%",
}}
>
<Text style={styles.temp}>
{parseFloat(day.main.temp).toFixed(1)}
</Text>
<Fontisto
name={icons[day.weather[0].main]}
size={50}
color="white"
/>
</View>
<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,
},
temp: {
fontSize: 100,
marginTop: 50,
},
description: {
fontSize: 20,
marginTop: -30,
},
tinyText: {
fontSize: 30,
},
});