generated from kangkong0805/Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
147 lines (134 loc) · 3.79 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
import * as Location from "expo-location";
import { useEffect, useState } from "react";
import {
ActivityIndicator,
Dimensions,
StyleSheet,
Text,
View,
} from "react-native";
import { ScrollView } from "react-native-web";
import { Fontisto } from "@expo/vector-icons";
const { width: SCREEN_WIDTH } = Dimensions.get("window");
export default function App() {
const [city, setCity] = useState("Loading...");
const [days, setDays] = useState([]);
const [location, setLocation] = useState();
const [ok, setOk] = useState(true);
const API_KEY = "7163a59b0f4ce36adb14bf042e162569";
// 날씨 아이콘
const icons = {
Clouds: "cloudy", // 흐림
Clear: "day-sunny", // 맑음
Atmophere: "cloudy-gusts", // 돌풍
Snow: "snow", // 눈
Drizzle: "rain", // 이슬비, 가랑비
Rain: "rains", // 비
ThunderStorm: "lightning", // 번개
};
const ask = async () => {
// const { granted } = await Location.requestForegroundPermissionsAsync();
// if (!granted) {
// setOk(false);
// }
const latitude = 35.1429503;
const longitude = 126.8005143;
// const {
// coords: { latitude, longitude },
// } = await Location.getCurrentPositionAsync({ accuracy: 5 });
// const location = await Location.reverseGeocodeAsync(
// { latitude, longitude },
// { useGoogleMaps: false }
// );
// Location.setGoogleApiKey();
// setCity(location[0].city);
// 날씨 데이터 조회
const res = await fetch(
`https://api.openweathermap.org/data/2.5/forecast?lat=${latitude}&lon=${longitude}&exclude=alerts&appid=${API_KEY}&units=metric`
);
const json = await res.json();
setDays(json.list);
};
useEffect(() => {
ask();
}, []);
return (
<View style={styles.container}>
<View style={styles.city}>
<Text style={styles.cityContent}>{city}</Text>
</View>
<ScrollView
contentContainerStyle={styles.weather} // ScrollView style 부여
horizontal // 수평 스크롤
pagingEnabled // 수평 스크롤할 때 자식 요소 배수에서 스크롤 뷰가 중지
showsVerticalScrollIndicator={false} // 스크롤 바 안 보이게
>
{days.length === 0 ? (
<View style={{ ...styles.day, alignItems: "center" }}>
<ActivityIndicator color="white" size="large" />
</View>
) : (
days.map((day, index) => (
<View key={index} style={styles.day}>
<View
style={{
flexDirection: "row",
alignItems: "center",
width: "100%",
justifyContent: "space-between",
}}
>
<Text style={styles.temp}>
{parseFloat(day.main.temp).toFixed(1)}
</Text>
<Fontisto
name={icons[day.weather[0].main]}
size={68}
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",
},
cityContent: {
fontSize: 58,
fontWeight: 500,
color: "white",
},
day: {
width: SCREEN_WIDTH,
paddingHorizontal: 20,
},
temp: {
marginTop: 50,
fontWeight: 600,
fontSize: 100,
color: "white",
},
description: {
fontSize: 30,
marginTop: -10,
color: "white",
},
tinyText: {
marginTop: -10,
fontSize: 25,
color: "white",
},
});