summaryrefslogtreecommitdiff
path: root/WeatherCard.swift
blob: 8b12786b7b4838872c51f7abcb724341aa10e4b2 (plain) (blame)
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
//
//  WeatherCard.swift
//  DayByDay
//

import SwiftUI

/// A single large, colorful card representing one weather type.
/// Tapping the card plays a bounce animation and speaks the weather name aloud.
struct WeatherCard: View {
    let weather: LearnWeather
    @State private var isTapped = false

    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: 32, style: .continuous)
                .fill(weather.color.gradient)
                .shadow(color: weather.color.opacity(0.4), radius: 8, y: 4)

            VStack(spacing: 16) {
                Image(systemName: weather.symbol)
                    .font(.system(size: 64))
                    .foregroundStyle(.white)
                    .symbolRenderingMode(.hierarchical)

                Text(weather.name)
                    .font(.system(size: 32, weight: .bold, design: .rounded))
                    .foregroundStyle(.white)
            }
        }
        .scaleEffect(isTapped ? 1.12 : 1.0)
        .contentShape(RoundedRectangle(cornerRadius: 32, style: .continuous))
        .rotation3DEffect(.degrees(isTapped ? 6 : 0), axis: (x: 1, y: 0, z: 0))
        .animation(.spring(response: 0.35, dampingFraction: 0.5), value: isTapped)
        .onTapGesture {
            isTapped = true
            SpeechSynthesizer.shared.speak(weather.name)
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
                isTapped = false
            }
        }
        .accessibilityLabel(weather.name)
        .accessibilityAddTraits(.isButton)
    }
}

// MARK: - Weather model

enum LearnWeather: Int, CaseIterable, Identifiable {
    case sunny, cloudy, rainy, snowy, windy, stormy, foggy, rainbow

    var id: Int { rawValue }

    var name: String {
        switch self {
        case .sunny:   "Sunny"
        case .cloudy:  "Cloudy"
        case .rainy:   "Rainy"
        case .snowy:   "Snowy"
        case .windy:   "Windy"
        case .stormy:  "Stormy"
        case .foggy:   "Foggy"
        case .rainbow: "Rainbow"
        }
    }

    var symbol: String {
        switch self {
        case .sunny:   "sun.max.fill"
        case .cloudy:  "cloud.fill"
        case .rainy:   "cloud.rain.fill"
        case .snowy:   "cloud.snow.fill"
        case .windy:   "wind"
        case .stormy:  "cloud.bolt.rain.fill"
        case .foggy:   "cloud.fog.fill"
        case .rainbow: "rainbow"
        }
    }

    var color: Color {
        switch self {
        case .sunny:   Color(red: 1.0, green: 0.7, blue: 0.2)    // orange
        case .cloudy:  Color(red: 0.6, green: 0.6, blue: 0.65)   // gray
        case .rainy:   Color(red: 0.3, green: 0.6, blue: 0.9)    // blue
        case .snowy:   Color(red: 0.55, green: 0.8, blue: 0.95)  // light blue
        case .windy:   Color(red: 0.25, green: 0.7, blue: 0.7)   // teal
        case .stormy:  Color(red: 0.2, green: 0.3, blue: 0.6)    // dark blue
        case .foggy:   Color(red: 0.6, green: 0.5, blue: 0.7)    // muted purple
        case .rainbow: Color(red: 0.9, green: 0.45, blue: 0.6)   // pink
        }
    }
}

#Preview {
    HStack(spacing: 24) {
        WeatherCard(weather: .sunny)
            .frame(width: 200, height: 260)
        WeatherCard(weather: .stormy)
            .frame(width: 200, height: 260)
    }
    .padding()
}