WeatherCard.swift
77 lines · 2290 bytes
1 symbol in this file
1//
2// WeatherCard.swift
3// DayByDay
4//
5
6import SwiftUI
7
8/// A single large, colorful card representing one weather type.
9/// Tapping the card plays a bounce animation and speaks the weather name aloud.
10struct WeatherCard: View {
11 let weather: LearnWeather
12
13 var body: some View {
14 TapCard(color: weather.color, label: weather.name) {
15 SpeechSynthesizer.shared.speak(weather.name)
16 } content: {
17 SymbolLabel(symbol: weather.symbol, name: weather.name)
18 }
19 }
20}
21
22// MARK: - Weather model
23
24nonisolated enum LearnWeather: Int, CaseIterable, Identifiable {
25 case sunny, cloudy, rainy, snowy, windy, stormy, foggy, rainbow
26
27 var id: Int { rawValue }
28
29 var name: String {
30 switch self {
31 case .sunny: "Sunny"
32 case .cloudy: "Cloudy"
33 case .rainy: "Rainy"
34 case .snowy: "Snowy"
35 case .windy: "Windy"
36 case .stormy: "Stormy"
37 case .foggy: "Foggy"
38 case .rainbow: "Rainbow"
39 }
40 }
41
42 var symbol: String {
43 switch self {
44 case .sunny: "sun.max.fill"
45 case .cloudy: "cloud.fill"
46 case .rainy: "cloud.rain.fill"
47 case .snowy: "cloud.snow.fill"
48 case .windy: "wind"
49 case .stormy: "cloud.bolt.rain.fill"
50 case .foggy: "cloud.fog.fill"
51 case .rainbow: "rainbow"
52 }
53 }
54
55 var color: Color {
56 switch self {
57 case .sunny: Color(red: 0.73, green: 0.51, blue: 0.15) // orange
58 case .cloudy: Color(red: 0.55, green: 0.55, blue: 0.6) // gray
59 case .rainy: Color(red: 0.28, green: 0.57, blue: 0.85) // blue
60 case .snowy: Color(red: 0.4, green: 0.57, blue: 0.68) // light blue
61 case .windy: Color(red: 0.21, green: 0.61, blue: 0.61) // teal
62 case .stormy: Color(red: 0.2, green: 0.3, blue: 0.6) // dark blue
63 case .foggy: Color(red: 0.6, green: 0.5, blue: 0.7) // muted purple
64 case .rainbow: Color(red: 0.83, green: 0.42, blue: 0.56) // pink
65 }
66 }
67}
68
69#Preview {
70 HStack(spacing: 24) {
71 WeatherCard(weather: .sunny)
72 .frame(width: 200, height: 260)
73 WeatherCard(weather: .stormy)
74 .frame(width: 200, height: 260)
75 }
76 .padding()
77}