WeatherView.swift
52 lines · 1555 bytes
2 symbols in this file
1//
2// WeatherView.swift
3// DayByDay
4//
5
6import SwiftUI
7
8/// Displays all 8 weather types as large, tappable cards in a scrollable grid.
9/// Shaking the device, or tapping the rain button, makes it rain.
10struct WeatherView: View {
11 @State private var raining = false
12 /// Bumped when the screen goes away, so a pending stop does nothing.
13 @State private var generation = 0
14
15 var body: some View {
16 CardGrid(items: LearnWeather.allCases, column: .narrowCards) {
17 Button(action: rain) {
18 Label("Shake for rain!", systemImage: "cloud.rain.fill")
19 .font(.heading(22))
20 .padding(.horizontal, 24)
21 .padding(.vertical, 14)
22 .background(Capsule().fill(Color(.systemGray6)))
23 }
24 .buttonStyle(.plain)
25 .padding(.top, 16)
26 } card: { weather in
27 WeatherCard(weather: weather)
28 }
29 .overlay { RainOverlay(isActive: raining) }
30 .onShake(rain)
31 .onDisappear {
32 generation += 1
33 raining = false
34 CardVoice.shared.cancel()
35 }
36 }
37
38 private func rain() {
39 guard !raining else { return }
40 raining = true
41 SoundEngine.shared.play(.rain)
42 CardVoice.shared.say(Lines.itsRaining, after: nil)
43 let gen = generation
44 DispatchQueue.main.asyncAfter(deadline: .now() + RainOverlay.duration) {
45 if gen == generation { raining = false }
46 }
47 }
48}
49
50#Preview {
51 WeatherView()
52}