krz/daybyday

iOS learning games for young children: days, months, seasons, numbers, letters, animals and more. education ios kids swift https://krz.sh/apps/daybyday/

DayByDay/Quiz/MovesView.swift

062549ad26607ce9e973ed2a9a7ef6f853885f83
daybyday/DayByDay/Quiz/MovesView.swift history · blame · raw

84 lines · 3012 bytes

2 symbols in this file
 1//
 2//  MovesView.swift
 3//  DayByDay
 4//
 5
 6import SwiftUI
 7
 8/// "Touch your nose!": the app says a move, waits while the child does it,
 9/// then cheers.
10struct MovesView: View {
11    static let wait = 3.0
12
13    @State private var round: MovesRound
14    @State private var praise = PraisePhrases()
15    @State private var celebrating = false
16    @State private var mascot = MascotState()
17    /// Bumped by each new move and when the screen goes away.
18    @State private var generation = 0
19
20    init() {
21        var rng = SystemRandomNumberGenerator()
22        _round = State(initialValue: MovesRound(using: &rng))
23    }
24
25    var body: some View {
26        ZStack {
27            if let part = round.current {
28                CenteredScrollView {
29                    VStack(spacing: 32) {
30                        PromptBar(mascot: $mascot) { start() }
31                        Image(systemName: part.symbol)
32                            .font(.system(size: 140))
33                            .foregroundStyle(.white)
34                            .frame(width: 240, height: 240)
35                            .background(RoundedRectangle(cornerRadius: 40, style: .continuous).fill(Category.bodyParts.color))
36                            .accessibilityLabel(part.name)
37                        Text(part.command)
38                            .font(.heading(30, weight: .heavy))
39                            .multilineTextAlignment(.center)
40                    }
41                    .padding(24)
42                }
43                .overlay { CelebrationOverlay(isActive: celebrating) }
44            } else {
45                FinishedView {
46                    var rng = SystemRandomNumberGenerator()
47                    round = MovesRound(using: &rng)
48                    start()
49                }
50            }
51        }
52        .onAppear { start() }
53        .onDisappear {
54            generation += 1
55            SpeechSynthesizer.shared.stop()
56        }
57    }
58
59    /// Says the move, waits, cheers, then goes on to the next move.
60    private func start() {
61        guard let part = round.current else { return }
62        generation += 1
63        let gen = generation
64        celebrating = false
65        mascot.handle(.prompt(part.command))
66        SpeechSynthesizer.shared.speak(part.command) {
67            DispatchQueue.main.asyncAfter(deadline: .now() + Self.wait) {
68                guard gen == generation else { return }
69                SoundEngine.shared.play(.success)
70                mascot.handle(.correct)
71                celebrating = true
72                var rng = SystemRandomNumberGenerator()
73                DispatchQueue.main.asyncAfter(deadline: .now() + RoundStyle.speechDelay) {
74                    guard gen == generation else { return }
75                    SpeechSynthesizer.shared.speak(praise.next(using: &rng)) {
76                        guard gen == generation else { return }
77                        round.next()
78                        start()
79                    }
80                }
81            }
82        }
83    }
84}