Commit edfb9a609e
Unsigned
Layout: unified · split
AlphabetView.swift +1
| @@ -22,6 +22,7 @@ struct AlphabetView: View { | ||
| 22 | 22 | } |
| 23 | 23 | .padding(32) |
| 24 | 24 | } |
| 25 | .onDisappear { CardVoice.shared.cancel() } | |
| 25 | 26 | } |
| 26 | 27 | } |
| 27 | 28 | |
AnimalCard.swift +33 −13
| @@ -14,7 +14,7 @@ struct AnimalCard: View { | ||
| 14 | 14 | var body: some View { |
| 15 | 15 | ZStack { |
| 16 | 16 | RoundedRectangle(cornerRadius: 32, style: .continuous) |
| 17 | .fill(animal.color.gradient) | |
| 17 | .fill(animal.color) | |
| 18 | 18 | .shadow(color: animal.color.opacity(0.4), radius: 8, y: 4) |
| 19 | 19 | |
| 20 | 20 | VStack(spacing: 16) { |
| @@ -34,7 +34,7 @@ struct AnimalCard: View { | ||
| 34 | 34 | .animation(.spring(response: 0.35, dampingFraction: 0.5), value: isTapped) |
| 35 | 35 | .onTapGesture { |
| 36 | 36 | isTapped = true |
| 37 | SpeechSynthesizer.shared.speak(animal.name) | |
| 37 | CardVoice.shared.say(animal.cardLine, after: animal.sound) | |
| 38 | 38 | DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) { |
| 39 | 39 | isTapped = false |
| 40 | 40 | } |
| @@ -81,22 +81,42 @@ nonisolated enum LearnAnimal: Int, CaseIterable, Identifiable { | ||
| 81 | 81 | } |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | /// At least 3:1 against the white icon and name. | |
| 84 | 85 | var color: Color { |
| 85 | 86 | switch self { |
| 86 | case .cat: Color(red: 0.9, green: 0.55, blue: 0.2) // orange | |
| 87 | case .dog: Color(red: 0.6, green: 0.45, blue: 0.3) // brown | |
| 88 | case .bird: Color(red: 0.3, green: 0.65, blue: 0.9) // sky blue | |
| 89 | case .fish: Color(red: 0.2, green: 0.75, blue: 0.8) // teal | |
| 90 | case .rabbit: Color(red: 0.75, green: 0.6, blue: 0.8) // lavender | |
| 91 | case .turtle: Color(red: 0.35, green: 0.7, blue: 0.4) // green | |
| 92 | case .ladybug: Color(red: 0.9, green: 0.25, blue: 0.25) // red | |
| 93 | case .cow: Color(red: 0.35, green: 0.38, blue: 0.45) // slate | |
| 94 | case .duck: Color(red: 0.95, green: 0.75, blue: 0.2) // yellow | |
| 95 | case .sheep: Color(red: 0.55, green: 0.6, blue: 0.7) // blue gray | |
| 96 | case .pig: Color(red: 0.95, green: 0.5, blue: 0.65) // pink | |
| 87 | case .cat: Color(red: 0.80, green: 0.42, blue: 0.08) // orange | |
| 88 | case .dog: Color(red: 0.55, green: 0.38, blue: 0.22) // brown | |
| 89 | case .bird: Color(red: 0.10, green: 0.48, blue: 0.78) // sky blue | |
| 90 | case .fish: Color(red: 0.0, green: 0.53, blue: 0.56) // teal | |
| 91 | case .rabbit: Color(red: 0.55, green: 0.40, blue: 0.70) // lavender | |
| 92 | case .turtle: Color(red: 0.18, green: 0.56, blue: 0.28) // green | |
| 93 | case .ladybug: Color(red: 0.86, green: 0.20, blue: 0.22) // red | |
| 94 | case .cow: Color(red: 0.35, green: 0.38, blue: 0.45) // slate | |
| 95 | case .duck: Color(red: 0.66, green: 0.50, blue: 0.0) // mustard | |
| 96 | case .sheep: Color(red: 0.40, green: 0.45, blue: 0.55) // blue gray | |
| 97 | case .pig: Color(red: 0.82, green: 0.25, blue: 0.52) // pink | |
| 97 | 98 | } |
| 98 | 99 | } |
| 99 | 100 | |
| 101 | var fact: String { | |
| 102 | switch self { | |
| 103 | case .cat: "Cats say meow." | |
| 104 | case .dog: "Dogs say woof." | |
| 105 | case .bird: "Birds can fly." | |
| 106 | case .fish: "Fish swim in water." | |
| 107 | case .rabbit: "Rabbits hop." | |
| 108 | case .turtle: "Turtles carry their shell." | |
| 109 | case .ladybug: "Ladybugs have spots." | |
| 110 | case .cow: "Cows say moo." | |
| 111 | case .duck: "Ducks say quack." | |
| 112 | case .sheep: "Sheep say baa." | |
| 113 | case .pig: "Pigs say oink." | |
| 114 | } | |
| 115 | } | |
| 116 | ||
| 117 | /// Said when the card is tapped, after the animal's sound. | |
| 118 | var cardLine: String { "\(name)! \(fact)" } | |
| 119 | ||
| 100 | 120 | var canFly: Bool { |
| 101 | 121 | switch self { |
| 102 | 122 | case .bird, .ladybug, .duck: true |
AnimalsView.swift +1
| @@ -22,6 +22,7 @@ struct AnimalsView: View { | ||
| 22 | 22 | } |
| 23 | 23 | .padding(32) |
| 24 | 24 | } |
| 25 | .onDisappear { CardVoice.shared.cancel() } | |
| 25 | 26 | } |
| 26 | 27 | } |
| 27 | 28 | |
DayByDay/Quiz/CardVoice.swift added +53
| @@ -0,0 +1,53 @@ | ||
| 1 | // | |
| 2 | // CardVoice.swift | |
| 3 | // DayByDay | |
| 4 | // | |
| 5 | ||
| 6 | import Foundation | |
| 7 | ||
| 8 | /// Tokens for spoken lines; only the latest one may still be said. | |
| 9 | nonisolated struct LineQueue { | |
| 10 | private var latest = 0 | |
| 11 | ||
| 12 | mutating func next() -> Int { | |
| 13 | latest += 1 | |
| 14 | return latest | |
| 15 | } | |
| 16 | ||
| 17 | func isCurrent(_ token: Int) -> Bool { token == latest } | |
| 18 | ||
| 19 | mutating func cancel() { latest += 1 } | |
| 20 | } | |
| 21 | ||
| 22 | /// Speaks a card's line, after its sound if it has one. A newer card, or | |
| 23 | /// leaving the screen, cancels a line that has not started yet. | |
| 24 | final class CardVoice { | |
| 25 | static let shared = CardVoice() | |
| 26 | ||
| 27 | private var queue = LineQueue() | |
| 28 | /// The last card sound, which may still be playing under a new card's line. | |
| 29 | private var lastSound: SoundEffect? | |
| 30 | ||
| 31 | /// Wait for whichever sound is still playing, then a short gap. | |
| 32 | nonisolated static func delay(newSound: Double, earlierSound: Double) -> Double { | |
| 33 | let longest = max(newSound, earlierSound) | |
| 34 | return longest > 0 ? longest + 0.2 : 0 | |
| 35 | } | |
| 36 | ||
| 37 | func say(_ line: String, after sound: SoundEffect?) { | |
| 38 | let token = queue.next() | |
| 39 | SpeechSynthesizer.shared.stop() | |
| 40 | if let sound { SoundEngine.shared.play(sound) } | |
| 41 | let delay = Self.delay(newSound: sound.map(SoundEngine.shared.remaining) ?? 0, | |
| 42 | earlierSound: lastSound.map(SoundEngine.shared.remaining) ?? 0) | |
| 43 | if sound != nil { lastSound = sound } | |
| 44 | DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [self] in | |
| 45 | if queue.isCurrent(token) { SpeechSynthesizer.shared.speak(line) } | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | func cancel() { | |
| 50 | queue.cancel() | |
| 51 | SpeechSynthesizer.shared.stop() | |
| 52 | } | |
| 53 | } | |
DayByDay/Quiz/ClothingTile.swift added +29
| @@ -0,0 +1,29 @@ | ||
| 1 | // | |
| 2 | // ClothingTile.swift | |
| 3 | // DayByDay | |
| 4 | // | |
| 5 | ||
| 6 | import SwiftUI | |
| 7 | ||
| 8 | /// A thing to wear, white on the weather topic color. | |
| 9 | struct ClothingTile: View { | |
| 10 | let item: QuizItem | |
| 11 | ||
| 12 | var body: some View { | |
| 13 | let clothing = Clothing.from(item) | |
| 14 | VStack(spacing: 10) { | |
| 15 | Image(systemName: clothing?.symbol ?? "questionmark") | |
| 16 | .font(.system(size: 56)) | |
| 17 | .frame(height: 64) | |
| 18 | Text(item.name) | |
| 19 | .font(.system(size: 24, weight: .bold, design: .rounded)) | |
| 20 | .lineLimit(1) | |
| 21 | .minimumScaleFactor(0.6) | |
| 22 | } | |
| 23 | .foregroundStyle(.white) | |
| 24 | .frame(maxWidth: .infinity, maxHeight: .infinity) | |
| 25 | .padding(12) | |
| 26 | .background(RoundedRectangle(cornerRadius: 32, style: .continuous).fill(Category.weather.color)) | |
| 27 | .contentShape(RoundedRectangle(cornerRadius: 32, style: .continuous)) | |
| 28 | } | |
| 29 | } | |
DayByDay/Quiz/DressUp.swift added +84
| @@ -0,0 +1,84 @@ | ||
| 1 | // | |
| 2 | // DressUp.swift | |
| 3 | // DayByDay | |
| 4 | // | |
| 5 | ||
| 6 | /// Things to wear, for "Dress for the weather". | |
| 7 | nonisolated enum Clothing: Int, CaseIterable { | |
| 8 | case sunglasses, sunHat, tshirt, umbrella, coat, jacket | |
| 9 | ||
| 10 | var name: String { | |
| 11 | switch self { | |
| 12 | case .sunglasses: "Sunglasses" | |
| 13 | case .sunHat: "Sun hat" | |
| 14 | case .tshirt: "T-shirt" | |
| 15 | case .umbrella: "Umbrella" | |
| 16 | case .coat: "Coat" | |
| 17 | case .jacket: "Jacket" | |
| 18 | } | |
| 19 | } | |
| 20 | ||
| 21 | var symbol: String { | |
| 22 | switch self { | |
| 23 | case .sunglasses: "sunglasses.fill" | |
| 24 | case .sunHat: "hat.widebrim.fill" | |
| 25 | case .tshirt: "tshirt.fill" | |
| 26 | case .umbrella: "umbrella.fill" | |
| 27 | case .coat: "coat.fill" | |
| 28 | case .jacket: "jacket.fill" | |
| 29 | } | |
| 30 | } | |
| 31 | ||
| 32 | var suits: Set<LearnWeather> { | |
| 33 | switch self { | |
| 34 | case .sunglasses, .sunHat, .tshirt: [.sunny] | |
| 35 | case .umbrella: [.rainy] | |
| 36 | // Coat and jacket look alike, so they suit the same weather; both | |
| 37 | // are fine in the rain, so neither is a wrong answer there. | |
| 38 | case .coat, .jacket: [.rainy, .snowy, .windy] | |
| 39 | } | |
| 40 | } | |
| 41 | ||
| 42 | var fact: String { | |
| 43 | switch self { | |
| 44 | case .sunglasses: "Sunglasses are for sunny days." | |
| 45 | case .sunHat: "A sun hat is for sunny days." | |
| 46 | case .tshirt: "A T-shirt is for warm days." | |
| 47 | case .umbrella: "An umbrella is for rainy days." | |
| 48 | case .coat: "A coat is for cold days." | |
| 49 | case .jacket: "A jacket is for cold, windy days." | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | /// Recorded under the weather topic with a "clothing." id. | |
| 54 | var item: QuizItem { | |
| 55 | QuizItem(id: "clothing.\(self)", name: name, category: .weather) | |
| 56 | } | |
| 57 | ||
| 58 | static func from(_ item: QuizItem) -> Clothing? { | |
| 59 | allCases.first { $0.item.id == item.id } | |
| 60 | } | |
| 61 | } | |
| 62 | ||
| 63 | /// "It's rainy! What should we wear?" Exactly one choice suits the weather. | |
| 64 | nonisolated struct DressUp: QuestionSource { | |
| 65 | static let weathers: [LearnWeather] = [.sunny, .rainy, .snowy, .windy] | |
| 66 | ||
| 67 | func next(correctCount: Int, excluding previous: QuizItem?, using rng: inout some RandomNumberGenerator) -> Question { | |
| 68 | let previousClothing = previous.flatMap(Clothing.from) | |
| 69 | func fresh(_ w: LearnWeather) -> [Clothing] { | |
| 70 | Clothing.allCases.filter { $0.suits.contains(w) && $0 != previousClothing } | |
| 71 | } | |
| 72 | let weather = Self.weathers.filter { !fresh($0).isEmpty }.randomElement(using: &rng)! | |
| 73 | let answer = fresh(weather).randomElement(using: &rng)! | |
| 74 | let count = Round.choiceCount(correctCount: correctCount) | |
| 75 | let others = Clothing.allCases.filter { !$0.suits.contains(weather) } | |
| 76 | .shuffled(using: &rng).prefix(count - 1) | |
| 77 | var facts: [QuizItem: String] = [:] | |
| 78 | for c in others { facts[c.item] = c.fact } | |
| 79 | return Question(prompt: "It's \(weather.name.lowercased())! What should we wear?", | |
| 80 | choices: ([answer] + others).map(\.item).shuffled(using: &rng), | |
| 81 | answer: answer.item, facts: facts, | |
| 82 | shown: [Category.weather.item(weather)]) | |
| 83 | } | |
| 84 | } | |
DayByDay/Quiz/Game.swift +21 −3
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | nonisolated enum Game: String, CaseIterable { |
| 7 | 7 | case findIt, oddOneOut, listen |
| 8 | 8 | case comesNext, weekTrain, yesterdayTomorrow, yearWheel, whichSeason, counting, most |
| 9 | case mixColors, trace, dressUp, sortFood, moves | |
| 9 | 10 | |
| 10 | 11 | var title: String { |
| 11 | 12 | switch self { |
| @@ -19,6 +20,11 @@ nonisolated enum Game: String, CaseIterable { | ||
| 19 | 20 | case .whichSeason: "Which season?" |
| 20 | 21 | case .counting: "Count with me" |
| 21 | 22 | case .most: "Which has the most?" |
| 23 | case .mixColors: "Mix colors" | |
| 24 | case .trace: "Trace it" | |
| 25 | case .dressUp: "Dress for the weather" | |
| 26 | case .sortFood: "Fruit or vegetable?" | |
| 27 | case .moves: "Move your body" | |
| 22 | 28 | } |
| 23 | 29 | } |
| 24 | 30 | |
| @@ -34,14 +40,19 @@ nonisolated enum Game: String, CaseIterable { | ||
| 34 | 40 | case .whichSeason: "leaf.fill" |
| 35 | 41 | case .counting: "hand.tap.fill" |
| 36 | 42 | case .most: "chart.bar.fill" |
| 43 | case .mixColors: "paintbrush.fill" | |
| 44 | case .trace: "hand.draw.fill" | |
| 45 | case .dressUp: "tshirt.fill" | |
| 46 | case .sortFood: "basket.fill" | |
| 47 | case .moves: "figure.wave" | |
| 37 | 48 | } |
| 38 | 49 | } |
| 39 | 50 | |
| 40 | 51 | /// Played as a `Round` of questions; the rest have their own screens. |
| 41 | 52 | var usesRound: Bool { |
| 42 | 53 | switch self { |
| 43 | case .findIt, .oddOneOut, .listen, .comesNext, .whichSeason, .most: true | |
| 44 | case .weekTrain, .yesterdayTomorrow, .yearWheel, .counting: false | |
| 54 | case .findIt, .oddOneOut, .listen, .comesNext, .whichSeason, .most, .mixColors, .dressUp: true | |
| 55 | case .weekTrain, .yesterdayTomorrow, .yearWheel, .counting, .trace, .sortFood, .moves: false | |
| 45 | 56 | } |
| 46 | 57 | } |
| 47 | 58 | } |
| @@ -83,6 +94,11 @@ nonisolated extension Category { | ||
| 83 | 94 | case .months: games += [.comesNext, .yearWheel] |
| 84 | 95 | case .seasons: games.append(.whichSeason) |
| 85 | 96 | case .numbers: games += [.counting, .most] |
| 97 | case .colors: games.append(.mixColors) | |
| 98 | case .shapes, .alphabet: games.append(.trace) | |
| 99 | case .weather: games.append(.dressUp) | |
| 100 | case .food: games.append(.sortFood) | |
| 101 | case .bodyParts: games.append(.moves) | |
| 86 | 102 | default: break |
| 87 | 103 | } |
| 88 | 104 | return games |
| @@ -96,7 +112,9 @@ nonisolated extension Category { | ||
| 96 | 112 | case .comesNext: ComesNext(items: quizItems, noun: self == .days ? "day" : "month") |
| 97 | 113 | case .whichSeason: WhichSeason() |
| 98 | 114 | case .most: Most() |
| 99 | case .weekTrain, .yesterdayTomorrow, .yearWheel, .counting: | |
| 115 | case .mixColors: MixColors() | |
| 116 | case .dressUp: DressUp() | |
| 117 | case .weekTrain, .yesterdayTomorrow, .yearWheel, .counting, .trace, .sortFood, .moves: | |
| 100 | 118 | preconditionFailure("\(game) is not a question round") |
| 101 | 119 | } |
| 102 | 120 | } |
DayByDay/Quiz/GameDestination.swift +9 −1
| @@ -19,7 +19,9 @@ struct GameDestination: View { | ||
| 19 | 19 | switch game { |
| 20 | 20 | case .most: |
| 21 | 21 | RoundView(source: category.questionSource(for: game), game: game) { PileTile(item: $0) } |
| 22 | case .findIt, .oddOneOut, .listen, .comesNext, .whichSeason: | |
| 22 | case .dressUp: | |
| 23 | RoundView(source: category.questionSource(for: game), game: game) { ClothingTile(item: $0) } | |
| 24 | case .findIt, .oddOneOut, .listen, .comesNext, .whichSeason, .mixColors: | |
| 23 | 25 | RoundView(source: category.questionSource(for: game), game: game) { ChoiceTile(item: $0) } |
| 24 | 26 | case .weekTrain: |
| 25 | 27 | SequencePuzzleView(game: game) { |
| @@ -35,6 +37,12 @@ struct GameDestination: View { | ||
| 35 | 37 | YearWheelView() |
| 36 | 38 | case .counting: |
| 37 | 39 | CountingView() |
| 40 | case .trace: | |
| 41 | TraceView(category: category) | |
| 42 | case .sortFood: | |
| 43 | SortView() | |
| 44 | case .moves: | |
| 45 | MovesView() | |
| 38 | 46 | } |
| 39 | 47 | } |
| 40 | 48 | } |
DayByDay/Quiz/MixColors.swift added +27
| @@ -0,0 +1,27 @@ | ||
| 1 | // | |
| 2 | // MixColors.swift | |
| 3 | // DayByDay | |
| 4 | // | |
| 5 | ||
| 6 | /// "What do red and yellow make?" The two paints are shown above the choices. | |
| 7 | nonisolated struct MixColors: QuestionSource { | |
| 8 | static let mixes: [(LearnColor, LearnColor, LearnColor)] = [ | |
| 9 | (.red, .yellow, .orange), (.blue, .yellow, .green), (.red, .blue, .purple), (.red, .white, .pink), | |
| 10 | ] | |
| 11 | ||
| 12 | func next(correctCount: Int, excluding previous: QuizItem?, using rng: inout some RandomNumberGenerator) -> Question { | |
| 13 | let colors = Category.colors | |
| 14 | let (a, b, result) = Self.mixes.filter { colors.item($0.2) != previous }.randomElement(using: &rng)! | |
| 15 | let answer = colors.item(result) | |
| 16 | let ingredients = [colors.item(a), colors.item(b)].shuffled(using: &rng) | |
| 17 | let count = Round.choiceCount(correctCount: correctCount) | |
| 18 | let others = colors.quizItems.filter { $0 != answer && !ingredients.contains($0) } | |
| 19 | .shuffled(using: &rng).prefix(count - 1) | |
| 20 | let (first, second) = (ingredients[0].name, ingredients[1].name.lowercased()) | |
| 21 | var facts: [QuizItem: String] = [:] | |
| 22 | for item in others { facts[item] = "\(first) and \(second) make \(answer.name.lowercased())." } | |
| 23 | return Question(prompt: "What do \(first.lowercased()) and \(second) make?", | |
| 24 | choices: ([answer] + others).shuffled(using: &rng), | |
| 25 | answer: answer, facts: facts, shown: ingredients) | |
| 26 | } | |
| 27 | } | |
DayByDay/Quiz/MovesRound.swift added +43
| @@ -0,0 +1,43 @@ | ||
| 1 | // | |
| 2 | // MovesRound.swift | |
| 3 | // DayByDay | |
| 4 | // | |
| 5 | ||
| 6 | nonisolated extension LearnBodyPart { | |
| 7 | /// Something to do with this part, said aloud. | |
| 8 | var command: String { | |
| 9 | switch self { | |
| 10 | case .head: "Touch your head!" | |
| 11 | case .eyes: "Blink your eyes!" | |
| 12 | case .ears: "Touch your ears!" | |
| 13 | case .nose: "Touch your nose!" | |
| 14 | case .mouth: "Point to your mouth!" | |
| 15 | case .hands: "Clap your hands!" | |
| 16 | case .fingers: "Wiggle your fingers!" | |
| 17 | case .arms: "Wave your arms!" | |
| 18 | case .legs: "March in place!" | |
| 19 | case .feet: "Stomp your feet!" | |
| 20 | case .heart: "Put your hand on your heart!" | |
| 21 | case .belly: "Pat your belly!" | |
| 22 | } | |
| 23 | } | |
| 24 | } | |
| 25 | ||
| 26 | /// Five different moves, one after another. | |
| 27 | nonisolated struct MovesRound { | |
| 28 | static let length = 5 | |
| 29 | ||
| 30 | let parts: [LearnBodyPart] | |
| 31 | private(set) var index = 0 | |
| 32 | ||
| 33 | init(using rng: inout some RandomNumberGenerator) { | |
| 34 | parts = Array(LearnBodyPart.allCases.shuffled(using: &rng).prefix(Self.length)) | |
| 35 | } | |
| 36 | ||
| 37 | var current: LearnBodyPart? { index < parts.count ? parts[index] : nil } | |
| 38 | var isDone: Bool { current == nil } | |
| 39 | ||
| 40 | mutating func next() { | |
| 41 | if index < parts.count { index += 1 } | |
| 42 | } | |
| 43 | } | |
DayByDay/Quiz/MovesView.swift added +86
| @@ -0,0 +1,86 @@ | ||
| 1 | // | |
| 2 | // MovesView.swift | |
| 3 | // DayByDay | |
| 4 | // | |
| 5 | ||
| 6 | import SwiftUI | |
| 7 | ||
| 8 | /// "Touch your nose!": the app says a move, waits while the child does it, | |
| 9 | /// then cheers. | |
| 10 | struct 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 | /// Bumped by each new move and when the screen goes away. | |
| 17 | @State private var generation = 0 | |
| 18 | ||
| 19 | init() { | |
| 20 | var rng = SystemRandomNumberGenerator() | |
| 21 | _round = State(initialValue: MovesRound(using: &rng)) | |
| 22 | } | |
| 23 | ||
| 24 | var body: some View { | |
| 25 | ZStack { | |
| 26 | if let part = round.current { | |
| 27 | VStack(spacing: 32) { | |
| 28 | Button { start() } label: { | |
| 29 | Label("Hear it again", systemImage: "speaker.wave.2.fill") | |
| 30 | .font(.system(size: 24, weight: .bold, design: .rounded)) | |
| 31 | .padding(.horizontal, 28) | |
| 32 | .padding(.vertical, 16) | |
| 33 | .background(Capsule().fill(Color(.systemGray6))) | |
| 34 | } | |
| 35 | .buttonStyle(.plain) | |
| 36 | Image(systemName: part.symbol) | |
| 37 | .font(.system(size: 140)) | |
| 38 | .foregroundStyle(.white) | |
| 39 | .frame(width: 240, height: 240) | |
| 40 | .background(RoundedRectangle(cornerRadius: 40, style: .continuous).fill(Category.bodyParts.color)) | |
| 41 | .accessibilityLabel(part.name) | |
| 42 | Text(part.command) | |
| 43 | .font(.system(size: 30, weight: .heavy, design: .rounded)) | |
| 44 | .multilineTextAlignment(.center) | |
| 45 | } | |
| 46 | .padding(24) | |
| 47 | .overlay { CelebrationOverlay(isActive: celebrating) } | |
| 48 | } else { | |
| 49 | FinishedView { | |
| 50 | var rng = SystemRandomNumberGenerator() | |
| 51 | round = MovesRound(using: &rng) | |
| 52 | start() | |
| 53 | } | |
| 54 | } | |
| 55 | } | |
| 56 | .onAppear { start() } | |
| 57 | .onDisappear { | |
| 58 | generation += 1 | |
| 59 | SpeechSynthesizer.shared.stop() | |
| 60 | } | |
| 61 | } | |
| 62 | ||
| 63 | /// Says the move, waits, cheers, then goes on to the next move. | |
| 64 | private func start() { | |
| 65 | guard let part = round.current else { return } | |
| 66 | generation += 1 | |
| 67 | let gen = generation | |
| 68 | celebrating = false | |
| 69 | SpeechSynthesizer.shared.speak(part.command) { | |
| 70 | DispatchQueue.main.asyncAfter(deadline: .now() + Self.wait) { | |
| 71 | guard gen == generation else { return } | |
| 72 | SoundEngine.shared.play(.success) | |
| 73 | celebrating = true | |
| 74 | var rng = SystemRandomNumberGenerator() | |
| 75 | DispatchQueue.main.asyncAfter(deadline: .now() + RoundStyle.speechDelay) { | |
| 76 | guard gen == generation else { return } | |
| 77 | SpeechSynthesizer.shared.speak(praise.next(using: &rng)) { | |
| 78 | guard gen == generation else { return } | |
| 79 | round.next() | |
| 80 | start() | |
| 81 | } | |
| 82 | } | |
| 83 | } | |
| 84 | } | |
| 85 | } | |
| 86 | } | |
DayByDay/Quiz/Question.swift +2
| @@ -11,6 +11,8 @@ nonisolated struct Question: Equatable { | ||
| 11 | 11 | var sound: SoundEffect? = nil |
| 12 | 12 | /// What to say about a choice when it is tapped by mistake. |
| 13 | 13 | var facts: [QuizItem: String] = [:] |
| 14 | /// Items drawn above the choices, e.g. the two paints being mixed. | |
| 15 | var shown: [QuizItem] = [] | |
| 14 | 16 | |
| 15 | 17 | func correction(for tapped: QuizItem) -> String { |
| 16 | 18 | if let fact = facts[tapped] { return "\(fact) Try again!" } |
DayByDay/Quiz/RoundView.swift +28
| @@ -65,6 +65,10 @@ struct RoundView<Choice: View>: View { | ||
| 65 | 65 | |
| 66 | 66 | ProgressDots(filled: round.correctCount, total: Round.length) |
| 67 | 67 | |
| 68 | if !round.current.shown.isEmpty { | |
| 69 | ShownItems(items: round.current.shown) | |
| 70 | } | |
| 71 | ||
| 68 | 72 | LazyVGrid(columns: columns, spacing: 24) { |
| 69 | 73 | ForEach(round.current.choices) { item in |
| 70 | 74 | Button { tap(item) } label: { |
| @@ -140,6 +144,30 @@ struct RoundView<Choice: View>: View { | ||
| 140 | 144 | } |
| 141 | 145 | } |
| 142 | 146 | |
| 147 | /// The items a question is about, above the choices: "red + yellow = ?". | |
| 148 | private struct ShownItems: View { | |
| 149 | let items: [QuizItem] | |
| 150 | ||
| 151 | var body: some View { | |
| 152 | HStack(spacing: 12) { | |
| 153 | ForEach(Array(items.enumerated()), id: \.offset) { i, item in | |
| 154 | if i > 0 { sign("+") } | |
| 155 | ChoiceTile(item: item).frame(width: 90, height: 90) | |
| 156 | } | |
| 157 | if items.count > 1 { sign("= ?") } | |
| 158 | } | |
| 159 | .accessibilityElement(children: .ignore) | |
| 160 | .accessibilityLabel(items.map(\.name).joined(separator: " and ")) | |
| 161 | } | |
| 162 | ||
| 163 | private func sign(_ text: String) -> some View { | |
| 164 | Text(text) | |
| 165 | .font(.system(size: 36, weight: .heavy, design: .rounded)) | |
| 166 | .lineLimit(1) | |
| 167 | .fixedSize() | |
| 168 | } | |
| 169 | } | |
| 170 | ||
| 143 | 171 | /// Filled dots showing how many correct answers are done in this round. |
| 144 | 172 | private struct ProgressDots: View { |
| 145 | 173 | let filled: Int |
DayByDay/Quiz/ShakeRain.swift added +72
| @@ -0,0 +1,72 @@ | ||
| 1 | // | |
| 2 | // ShakeRain.swift | |
| 3 | // DayByDay | |
| 4 | // | |
| 5 | ||
| 6 | import SwiftUI | |
| 7 | import UIKit | |
| 8 | ||
| 9 | extension Notification.Name { | |
| 10 | static let deviceDidShake = Notification.Name("deviceDidShake") | |
| 11 | } | |
| 12 | ||
| 13 | extension UIWindow { | |
| 14 | open override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { | |
| 15 | if motion == .motionShake { | |
| 16 | NotificationCenter.default.post(name: .deviceDidShake, object: nil) | |
| 17 | } | |
| 18 | super.motionEnded(motion, with: event) | |
| 19 | } | |
| 20 | } | |
| 21 | ||
| 22 | extension View { | |
| 23 | func onShake(_ action: @escaping () -> Void) -> some View { | |
| 24 | onReceive(NotificationCenter.default.publisher(for: .deviceDidShake)) { _ in action() } | |
| 25 | } | |
| 26 | } | |
| 27 | ||
| 28 | /// Rain falling over the screen for a few seconds. With Reduce Motion the | |
| 29 | /// drops hold still and fade. | |
| 30 | struct RainOverlay: View { | |
| 31 | static let duration = 3.0 | |
| 32 | static let dropColor = Color(red: 0.10, green: 0.40, blue: 0.85) | |
| 33 | ||
| 34 | let isActive: Bool | |
| 35 | ||
| 36 | @Environment(\.accessibilityReduceMotion) private var reduceMotion | |
| 37 | @State private var start = Date() | |
| 38 | ||
| 39 | private static let drops: [(x: Double, phase: Double, speed: Double)] = { | |
| 40 | var rng = SystemRandomNumberGenerator() | |
| 41 | return (0..<70).map { _ in | |
| 42 | (Double.random(in: 0...1, using: &rng), Double.random(in: 0...1, using: &rng), | |
| 43 | Double.random(in: 0.8...1.3, using: &rng)) | |
| 44 | } | |
| 45 | }() | |
| 46 | ||
| 47 | var body: some View { | |
| 48 | Group { | |
| 49 | if isActive { | |
| 50 | TimelineView(.animation) { timeline in | |
| 51 | Canvas { context, size in | |
| 52 | let t = timeline.date.timeIntervalSince(start) | |
| 53 | guard t < Self.duration else { return } | |
| 54 | let fade = min(1, (Self.duration - t) / 0.6) | |
| 55 | context.opacity = fade | |
| 56 | for drop in Self.drops { | |
| 57 | let y = reduceMotion | |
| 58 | ? drop.phase | |
| 59 | : (drop.phase + t * drop.speed * 0.9).truncatingRemainder(dividingBy: 1.1) - 0.05 | |
| 60 | let rect = CGRect(x: drop.x * size.width, y: y * size.height, width: 5, height: 22) | |
| 61 | context.fill(Path(roundedRect: rect, cornerRadius: 2.5), with: .color(Self.dropColor)) | |
| 62 | } | |
| 63 | } | |
| 64 | } | |
| 65 | } | |
| 66 | } | |
| 67 | .allowsHitTesting(false) | |
| 68 | .onChange(of: isActive) { _, active in | |
| 69 | if active { start = Date() } | |
| 70 | } | |
| 71 | } | |
| 72 | } | |
DayByDay/Quiz/SortRound.swift added +63
| @@ -0,0 +1,63 @@ | ||
| 1 | // | |
| 2 | // SortRound.swift | |
| 3 | // DayByDay | |
| 4 | // | |
| 5 | ||
| 6 | /// Six foods, one at a time, into a fruit bin or a vegetable bin. | |
| 7 | nonisolated struct SortRound { | |
| 8 | let foods: [LearnFood] | |
| 9 | private(set) var fruitBin: [LearnFood] = [] | |
| 10 | private(set) var vegetableBin: [LearnFood] = [] | |
| 11 | private(set) var isShowingCorrection = false | |
| 12 | ||
| 13 | init(using rng: inout some RandomNumberGenerator) { | |
| 14 | let fruit = LearnFood.allCases.filter(\.isFruit).shuffled(using: &rng).prefix(3) | |
| 15 | let vegetables = LearnFood.allCases.filter { !$0.isFruit }.shuffled(using: &rng).prefix(3) | |
| 16 | foods = Array(fruit + vegetables).shuffled(using: &rng) | |
| 17 | } | |
| 18 | ||
| 19 | private var sorted: Int { fruitBin.count + vegetableBin.count } | |
| 20 | var current: LearnFood? { sorted < foods.count ? foods[sorted] : nil } | |
| 21 | var isDone: Bool { current == nil } | |
| 22 | ||
| 23 | var prompt: String? { current.map(Self.question) } | |
| 24 | ||
| 25 | mutating func place(isFruit: Bool) -> AnswerOutcome { | |
| 26 | guard let food = current, !isShowingCorrection else { return .ignored } | |
| 27 | guard isFruit == food.isFruit else { | |
| 28 | isShowingCorrection = true | |
| 29 | return .incorrect(correctName: food.name) | |
| 30 | } | |
| 31 | if isFruit { fruitBin.append(food) } else { vegetableBin.append(food) } | |
| 32 | return .correct | |
| 33 | } | |
| 34 | ||
| 35 | mutating func resume() { | |
| 36 | isShowingCorrection = false | |
| 37 | } | |
| 38 | ||
| 39 | func correction() -> String { | |
| 40 | current.map { "\(Self.fact(for: $0)) Try again!" } ?? "" | |
| 41 | } | |
| 42 | ||
| 43 | static func question(for food: LearnFood) -> String { | |
| 44 | let name = food.name.lowercased() | |
| 45 | switch food { | |
| 46 | case .grapes: return "Are grapes a fruit or a vegetable?" | |
| 47 | case .corn, .broccoli: return "Is \(name) a fruit or a vegetable?" | |
| 48 | default: return "Is the \(name) a fruit or a vegetable?" | |
| 49 | } | |
| 50 | } | |
| 51 | ||
| 52 | static func fact(for food: LearnFood) -> String { | |
| 53 | let name = food.name.lowercased() | |
| 54 | let kind = food.isFruit ? "fruit" : "vegetable" | |
| 55 | switch food { | |
| 56 | case .grapes: return "Grapes are a \(kind)." | |
| 57 | case .corn, .broccoli: return "\(food.name) is a \(kind)." | |
| 58 | default: | |
| 59 | let article = "aeiou".contains(name.first!) ? "An" : "A" | |
| 60 | return "\(article) \(name) is a \(kind)." | |
| 61 | } | |
| 62 | } | |
| 63 | } | |
DayByDay/Quiz/SortView.swift added +161
| @@ -0,0 +1,161 @@ | ||
| 1 | // | |
| 2 | // SortView.swift | |
| 3 | // DayByDay | |
| 4 | // | |
| 5 | ||
| 6 | import SwiftUI | |
| 7 | ||
| 8 | enum SortStyle { | |
| 9 | /// Differs from the dark food card and stands out on black. | |
| 10 | static let binFill = Category.food.color | |
| 11 | } | |
| 12 | ||
| 13 | /// "Fruit or vegetable?": tap a bin, or drag the food into it. | |
| 14 | struct SortView: View { | |
| 15 | @Environment(ProfileSession.self) private var session | |
| 16 | @State private var round: SortRound | |
| 17 | @State private var praise = PraisePhrases() | |
| 18 | @State private var celebrating = false | |
| 19 | @State private var shaking = false | |
| 20 | @State private var speechToken = 0 | |
| 21 | /// Bumped when the screen goes away, so pending work does nothing. | |
| 22 | @State private var generation = 0 | |
| 23 | /// Counts feedback lines, so a backstop from an earlier one does nothing. | |
| 24 | @State private var feedback = 0 | |
| 25 | @State private var finished = false | |
| 26 | ||
| 27 | init() { | |
| 28 | var rng = SystemRandomNumberGenerator() | |
| 29 | _round = State(initialValue: SortRound(using: &rng)) | |
| 30 | } | |
| 31 | ||
| 32 | var body: some View { | |
| 33 | ZStack { | |
| 34 | if finished { | |
| 35 | FinishedView { | |
| 36 | var rng = SystemRandomNumberGenerator() | |
| 37 | round = SortRound(using: &rng) | |
| 38 | finished = false | |
| 39 | say(round.prompt, after: 0.3) | |
| 40 | } | |
| 41 | } else { | |
| 42 | board | |
| 43 | } | |
| 44 | } | |
| 45 | .onAppear { say(round.prompt, after: 0) } | |
| 46 | .onDisappear { | |
| 47 | generation += 1 | |
| 48 | speechToken += 1 | |
| 49 | SpeechSynthesizer.shared.stop() | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | private var board: some View { | |
| 54 | ScrollView { | |
| 55 | VStack(spacing: 28) { | |
| 56 | Button { say(round.prompt, after: 0) } label: { | |
| 57 | Label("Hear it again", systemImage: "speaker.wave.2.fill") | |
| 58 | .font(.system(size: 24, weight: .bold, design: .rounded)) | |
| 59 | .padding(.horizontal, 28) | |
| 60 | .padding(.vertical, 16) | |
| 61 | .background(Capsule().fill(Color(.systemGray6))) | |
| 62 | } | |
| 63 | .buttonStyle(.plain) | |
| 64 | ||
| 65 | if let food = round.current { | |
| 66 | let item = Category.food.item(food) | |
| 67 | ChoiceTile(item: item) | |
| 68 | .frame(width: 180, height: 180) | |
| 69 | .offset(x: shaking ? -10 : 0) | |
| 70 | .draggable(item.id) { ChoiceTile(item: item).frame(width: 140, height: 140) } | |
| 71 | .accessibilityLabel(food.name) | |
| 72 | } | |
| 73 | ||
| 74 | HStack(spacing: 20) { | |
| 75 | bin(isFruit: true) | |
| 76 | bin(isFruit: false) | |
| 77 | } | |
| 78 | } | |
| 79 | .padding(24) | |
| 80 | } | |
| 81 | .overlay { CelebrationOverlay(isActive: celebrating) } | |
| 82 | } | |
| 83 | ||
| 84 | private func bin(isFruit: Bool) -> some View { | |
| 85 | let contents = isFruit ? round.fruitBin : round.vegetableBin | |
| 86 | return Button { place(isFruit: isFruit) } label: { | |
| 87 | VStack(spacing: 8) { | |
| 88 | Text(isFruit ? "🍎🍌" : "🥕🥦").font(.system(size: 44)) | |
| 89 | Text(isFruit ? "Fruit" : "Vegetables") | |
| 90 | .font(.system(size: 22, weight: .bold, design: .rounded)) | |
| 91 | .foregroundStyle(.white) | |
| 92 | Text(contents.map(\.emoji).joined()) | |
| 93 | .font(.system(size: 30)) | |
| 94 | .frame(height: 40) | |
| 95 | } | |
| 96 | .frame(maxWidth: .infinity) | |
| 97 | .padding(.vertical, 18) | |
| 98 | .background(RoundedRectangle(cornerRadius: 28, style: .continuous).fill(SortStyle.binFill)) | |
| 99 | } | |
| 100 | .buttonStyle(.plain) | |
| 101 | .dropDestination(for: String.self) { _, _ in | |
| 102 | place(isFruit: isFruit) | |
| 103 | return true | |
| 104 | } | |
| 105 | .accessibilityLabel(isFruit ? "Fruit" : "Vegetables") | |
| 106 | } | |
| 107 | ||
| 108 | private func place(isFruit: Bool) { | |
| 109 | guard let food = round.current else { return } | |
| 110 | let outcome = round.place(isFruit: isFruit) | |
| 111 | guard outcome != .ignored else { return } | |
| 112 | if let profile = session.current { | |
| 113 | session.store.record(Category.food.item(food), game: .sortFood, correct: outcome == .correct, for: profile) | |
| 114 | } | |
| 115 | feedback += 1 | |
| 116 | let gen = generation, line = feedback | |
| 117 | switch outcome { | |
| 118 | case .correct: | |
| 119 | SoundEngine.shared.play(.success) | |
| 120 | celebrating = true | |
| 121 | var rng = SystemRandomNumberGenerator() | |
| 122 | let text = "\(praise.next(using: &rng)) \(SortRound.fact(for: food)) \(round.prompt ?? "")" | |
| 123 | DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) { | |
| 124 | if gen == generation { celebrating = false } | |
| 125 | } | |
| 126 | guard round.isDone else { | |
| 127 | say(text, after: RoundStyle.speechDelay) | |
| 128 | break | |
| 129 | } | |
| 130 | // Finish once the last food's line has been said. | |
| 131 | let finish = { | |
| 132 | guard gen == generation, line == feedback, round.isDone else { return } | |
| 133 | finished = true | |
| 134 | } | |
| 135 | say(text, after: RoundStyle.speechDelay, then: finish) | |
| 136 | DispatchQueue.main.asyncAfter(deadline: .now() + 8, execute: finish) | |
| 137 | case .incorrect: | |
| 138 | SoundEngine.shared.play(.gentleMiss) | |
| 139 | withAnimation(.default.repeatCount(3)) { shaking = true } | |
| 140 | // Taps stay ignored until the correction has been said. | |
| 141 | let resume = { | |
| 142 | guard gen == generation, line == feedback, round.isShowingCorrection else { return } | |
| 143 | shaking = false | |
| 144 | round.resume() | |
| 145 | } | |
| 146 | say(round.correction(), after: RoundStyle.speechDelay, then: resume) | |
| 147 | DispatchQueue.main.asyncAfter(deadline: .now() + 6, execute: resume) | |
| 148 | case .ignored: | |
| 149 | break | |
| 150 | } | |
| 151 | } | |
| 152 | ||
| 153 | private func say(_ text: String?, after delay: TimeInterval, then completion: (() -> Void)? = nil) { | |
| 154 | guard let text, !text.isEmpty else { return } | |
| 155 | speechToken += 1 | |
| 156 | let token = speechToken, gen = generation | |
| 157 | DispatchQueue.main.asyncAfter(deadline: .now() + delay) { | |
| 158 | if token == speechToken, gen == generation { SpeechSynthesizer.shared.speak(text, completion: completion) } | |
| 159 | } | |
| 160 | } | |
| 161 | } | |
DayByDay/Quiz/Trace.swift added +122
| @@ -0,0 +1,122 @@ | ||
| 1 | // | |
| 2 | // Trace.swift | |
| 3 | // DayByDay | |
| 4 | // | |
| 5 | ||
| 6 | import CoreGraphics | |
| 7 | import CoreText | |
| 8 | import UIKit | |
| 9 | ||
| 10 | /// The area a child traces over, in a `size`×`size` canvas: a letter's filled | |
| 11 | /// shape, or a shape's thick outline. | |
| 12 | nonisolated enum TraceGuide { | |
| 13 | static func region(for item: QuizItem, size: CGFloat) -> CGPath { | |
| 14 | if let letter = item.value(LearnLetter.self) { | |
| 15 | return fitted(glyph(letter.character), size: size) | |
| 16 | } | |
| 17 | let outline = fitted(shapeOutline(item.value(LearnShape.self)!), size: size) | |
| 18 | // normalized() removes the stroke's self-overlaps at corners. | |
| 19 | return outline.copy(strokingWithWidth: size * 0.12, lineCap: .round, lineJoin: .round, miterLimit: 10) | |
| 20 | .normalized() | |
| 21 | } | |
| 22 | ||
| 23 | /// Scales and centers `path` into the middle 80% of the canvas. | |
| 24 | private static func fitted(_ path: CGPath, size: CGFloat) -> CGPath { | |
| 25 | let box = path.boundingBoxOfPath | |
| 26 | let scale = size * 0.8 / max(box.width, box.height) | |
| 27 | var t = CGAffineTransform(translationX: size / 2, y: size / 2) | |
| 28 | .scaledBy(x: scale, y: scale) | |
| 29 | .translatedBy(x: -box.midX, y: -box.midY) | |
| 30 | return path.copy(using: &t) ?? path | |
| 31 | } | |
| 32 | ||
| 33 | /// The uppercase letter in the system rounded heavy font, y pointing down. | |
| 34 | private static func glyph(_ character: String) -> CGPath { | |
| 35 | let base = UIFont.systemFont(ofSize: 200, weight: .heavy) | |
| 36 | let descriptor = base.fontDescriptor.withDesign(.rounded) ?? base.fontDescriptor | |
| 37 | let font = CTFontCreateWithFontDescriptor(descriptor as CTFontDescriptor, 200, nil) | |
| 38 | var chars = Array(character.utf16) | |
| 39 | var glyphs = [CGGlyph](repeating: 0, count: chars.count) | |
| 40 | CTFontGetGlyphsForCharacters(font, &chars, &glyphs, chars.count) | |
| 41 | var flip = CGAffineTransform(scaleX: 1, y: -1) | |
| 42 | return CTFontCreatePathForGlyph(font, glyphs[0], &flip) ?? CGPath(rect: .zero, transform: nil) | |
| 43 | } | |
| 44 | ||
| 45 | /// Outlines in a unit square, y pointing down. | |
| 46 | private static func shapeOutline(_ shape: LearnShape) -> CGPath { | |
| 47 | let p = CGMutablePath() | |
| 48 | switch shape { | |
| 49 | case .circle: | |
| 50 | p.addEllipse(in: CGRect(x: 0, y: 0, width: 1, height: 1)) | |
| 51 | case .square: | |
| 52 | p.addRect(CGRect(x: 0, y: 0, width: 1, height: 1)) | |
| 53 | case .triangle: | |
| 54 | p.addLines(between: [CGPoint(x: 0.5, y: 0), CGPoint(x: 1, y: 0.9), CGPoint(x: 0, y: 0.9)]) | |
| 55 | p.closeSubpath() | |
| 56 | case .star: | |
| 57 | let points = (0..<10).map { i -> CGPoint in | |
| 58 | let r = i.isMultiple(of: 2) ? 0.5 : 0.22 | |
| 59 | let a = -Double.pi / 2 + Double(i) * .pi / 5 | |
| 60 | return CGPoint(x: 0.5 + r * cos(a), y: 0.5 + r * sin(a)) | |
| 61 | } | |
| 62 | p.addLines(between: points) | |
| 63 | p.closeSubpath() | |
| 64 | case .heart: | |
| 65 | p.move(to: CGPoint(x: 0.5, y: 0.95)) | |
| 66 | p.addCurve(to: CGPoint(x: 0, y: 0.3), control1: CGPoint(x: 0.2, y: 0.75), control2: CGPoint(x: 0, y: 0.55)) | |
| 67 | p.addCurve(to: CGPoint(x: 0.5, y: 0.2), control1: CGPoint(x: 0, y: 0), control2: CGPoint(x: 0.4, y: -0.05)) | |
| 68 | p.addCurve(to: CGPoint(x: 1, y: 0.3), control1: CGPoint(x: 0.6, y: -0.05), control2: CGPoint(x: 1, y: 0)) | |
| 69 | p.addCurve(to: CGPoint(x: 0.5, y: 0.95), control1: CGPoint(x: 1, y: 0.55), control2: CGPoint(x: 0.8, y: 0.75)) | |
| 70 | p.closeSubpath() | |
| 71 | case .oval: | |
| 72 | p.addEllipse(in: CGRect(x: 0, y: 0, width: 1, height: 0.65)) | |
| 73 | case .diamond: | |
| 74 | p.addLines(between: [CGPoint(x: 0.5, y: 0), CGPoint(x: 1, y: 0.5), CGPoint(x: 0.5, y: 1), CGPoint(x: 0, y: 0.5)]) | |
| 75 | p.closeSubpath() | |
| 76 | case .rectangle: | |
| 77 | p.addRect(CGRect(x: 0, y: 0, width: 1, height: 0.6)) | |
| 78 | } | |
| 79 | return p | |
| 80 | } | |
| 81 | } | |
| 82 | ||
| 83 | /// Scores a finger trace against a guide region. | |
| 84 | nonisolated struct TraceChecker { | |
| 85 | /// Points inside the region, on a grid. | |
| 86 | let samples: [CGPoint] | |
| 87 | private let region: CGPath | |
| 88 | private let tolerance: CGFloat | |
| 89 | ||
| 90 | init(region: CGPath, size: CGFloat) { | |
| 91 | self.region = region | |
| 92 | tolerance = size * 0.07 | |
| 93 | let step = size / 30 | |
| 94 | var samples: [CGPoint] = [] | |
| 95 | for row in 0...30 { | |
| 96 | for col in 0...30 { | |
| 97 | let point = CGPoint(x: CGFloat(col) * step, y: CGFloat(row) * step) | |
| 98 | if region.contains(point) { samples.append(point) } | |
| 99 | } | |
| 100 | } | |
| 101 | self.samples = samples | |
| 102 | } | |
| 103 | ||
| 104 | /// Coverage: share of the region the finger passed near. Accuracy: share | |
| 105 | /// of the finger's points on or near the region. | |
| 106 | func score(_ drawn: [CGPoint]) -> (coverage: Double, accuracy: Double) { | |
| 107 | guard !drawn.isEmpty, !samples.isEmpty else { return (0, 0) } | |
| 108 | let t2 = tolerance * tolerance | |
| 109 | func near(_ a: CGPoint, _ b: CGPoint) -> Bool { | |
| 110 | let dx = a.x - b.x, dy = a.y - b.y | |
| 111 | return dx * dx + dy * dy <= t2 | |
| 112 | } | |
| 113 | let covered = samples.filter { s in drawn.contains { near($0, s) } }.count | |
| 114 | let onTarget = drawn.filter { d in region.contains(d) || samples.contains { near($0, d) } }.count | |
| 115 | return (Double(covered) / Double(samples.count), Double(onTarget) / Double(drawn.count)) | |
| 116 | } | |
| 117 | ||
| 118 | func passes(_ drawn: [CGPoint]) -> Bool { | |
| 119 | let s = score(drawn) | |
| 120 | return s.coverage >= 0.75 && s.accuracy >= 0.75 | |
| 121 | } | |
| 122 | } | |
DayByDay/Quiz/TraceView.swift added +163
| @@ -0,0 +1,163 @@ | ||
| 1 | // | |
| 2 | // TraceView.swift | |
| 3 | // DayByDay | |
| 4 | // | |
| 5 | ||
| 6 | import SwiftUI | |
| 7 | ||
| 8 | /// Trace five shapes or letters with a finger. | |
| 9 | struct TraceView: View { | |
| 10 | static let length = 5 | |
| 11 | ||
| 12 | let category: Category | |
| 13 | ||
| 14 | @Environment(ProfileSession.self) private var session | |
| 15 | @State private var items: [QuizItem] | |
| 16 | @State private var index = 0 | |
| 17 | @State private var strokes: [[CGPoint]] = [] | |
| 18 | @State private var celebrating = false | |
| 19 | @State private var busy = false | |
| 20 | @State private var speechToken = 0 | |
| 21 | /// Bumped when the screen goes away, so pending work does nothing. | |
| 22 | @State private var generation = 0 | |
| 23 | /// Counts passes, so a backstop from an earlier pass does nothing. | |
| 24 | @State private var passes = 0 | |
| 25 | ||
| 26 | init(category: Category) { | |
| 27 | self.category = category | |
| 28 | _items = State(initialValue: Self.pick(category)) | |
| 29 | } | |
| 30 | ||
| 31 | private static func pick(_ category: Category) -> [QuizItem] { | |
| 32 | Array(category.quizItems.shuffled().prefix(length)) | |
| 33 | } | |
| 34 | ||
| 35 | private var item: QuizItem? { index < items.count ? items[index] : nil } | |
| 36 | ||
| 37 | private func label(_ item: QuizItem) -> String { | |
| 38 | category == .alphabet ? "the letter \(item.name)" : "the \(item.name.lowercased())" | |
| 39 | } | |
| 40 | ||
| 41 | var body: some View { | |
| 42 | ZStack { | |
| 43 | if let item { | |
| 44 | board(item) | |
| 45 | } else { | |
| 46 | FinishedView { | |
| 47 | items = Self.pick(category) | |
| 48 | index = 0 | |
| 49 | strokes = [] | |
| 50 | prompt() | |
| 51 | } | |
| 52 | } | |
| 53 | } | |
| 54 | .onAppear { prompt() } | |
| 55 | .onDisappear { | |
| 56 | generation += 1 | |
| 57 | speechToken += 1 | |
| 58 | SpeechSynthesizer.shared.stop() | |
| 59 | } | |
| 60 | } | |
| 61 | ||
| 62 | private func board(_ item: QuizItem) -> some View { | |
| 63 | VStack(spacing: 20) { | |
| 64 | HStack(spacing: 16) { | |
| 65 | pill("Hear it again", "speaker.wave.2.fill", action: prompt) | |
| 66 | pill("Start over", "eraser.fill") { strokes = [] } | |
| 67 | } | |
| 68 | GeometryReader { geo in | |
| 69 | let size = min(geo.size.width, geo.size.height) | |
| 70 | let region = Path(TraceGuide.region(for: item, size: size)) | |
| 71 | ZStack { | |
| 72 | region.fill(category.color.opacity(0.4)) | |
| 73 | region.stroke(category.color, style: StrokeStyle(lineWidth: 3, dash: [10, 8])) | |
| 74 | ForEach(Array(strokes.enumerated()), id: \.offset) { _, stroke in | |
| 75 | Path { p in p.addLines(stroke) } | |
| 76 | // Primary, not the topic color, so the ink shows on the guide in both modes. | |
| 77 | .stroke(Color.primary, style: StrokeStyle(lineWidth: size * 0.08, lineCap: .round, lineJoin: .round)) | |
| 78 | } | |
| 79 | } | |
| 80 | .frame(width: size, height: size) | |
| 81 | .contentShape(Rectangle()) | |
| 82 | .gesture( | |
| 83 | DragGesture(minimumDistance: 0) | |
| 84 | .onChanged { value in | |
| 85 | guard !busy else { return } | |
| 86 | if value.translation == .zero || strokes.isEmpty { strokes.append([]) } | |
| 87 | strokes[strokes.count - 1].append(value.location) | |
| 88 | } | |
| 89 | .onEnded { _ in check(item, size: size) } | |
| 90 | ) | |
| 91 | .frame(maxWidth: .infinity, maxHeight: .infinity) | |
| 92 | .accessibilityLabel("Trace \(label(item))") | |
| 93 | } | |
| 94 | .frame(maxWidth: 520) | |
| 95 | } | |
| 96 | .padding(24) | |
| 97 | .overlay { CelebrationOverlay(isActive: celebrating) } | |
| 98 | } | |
| 99 | ||
| 100 | private func pill(_ title: String, _ symbol: String, action: @escaping () -> Void) -> some View { | |
| 101 | Button(action: action) { | |
| 102 | Label(title, systemImage: symbol) | |
| 103 | .font(.system(size: 19, weight: .bold, design: .rounded)) | |
| 104 | .lineLimit(1) | |
| 105 | .minimumScaleFactor(0.7) | |
| 106 | .padding(.horizontal, 20) | |
| 107 | .padding(.vertical, 14) | |
| 108 | .background(Capsule().fill(Color(.systemGray6))) | |
| 109 | } | |
| 110 | .buttonStyle(.plain) | |
| 111 | } | |
| 112 | ||
| 113 | private func check(_ item: QuizItem, size: CGFloat) { | |
| 114 | guard !busy else { return } | |
| 115 | let checker = TraceChecker(region: TraceGuide.region(for: item, size: size), size: size) | |
| 116 | let drawn = strokes.flatMap { $0 } | |
| 117 | let score = checker.score(drawn) | |
| 118 | if checker.passes(drawn) { | |
| 119 | record(item, correct: true) | |
| 120 | busy = true | |
| 121 | celebrating = true | |
| 122 | SoundEngine.shared.play(.success) | |
| 123 | passes += 1 | |
| 124 | let gen = generation, pass = passes | |
| 125 | let advance = { | |
| 126 | guard gen == generation, pass == passes, busy else { return } | |
| 127 | busy = false | |
| 128 | celebrating = false | |
| 129 | strokes = [] | |
| 130 | index += 1 | |
| 131 | if self.item != nil { prompt() } | |
| 132 | } | |
| 133 | say("You traced \(label(item))!", after: RoundStyle.speechDelay, then: advance) | |
| 134 | DispatchQueue.main.asyncAfter(deadline: .now() + 8, execute: advance) | |
| 135 | } else if score.accuracy < 0.5 && score.coverage > 0.4 { | |
| 136 | record(item, correct: false) | |
| 137 | SoundEngine.shared.play(.gentleMiss) | |
| 138 | strokes = [] | |
| 139 | say("Stay on \(label(item)). Let's try again.", after: RoundStyle.speechDelay) | |
| 140 | } | |
| 141 | } | |
| 142 | ||
| 143 | private func record(_ item: QuizItem, correct: Bool) { | |
| 144 | if let profile = session.current { | |
| 145 | session.store.record(item, game: .trace, correct: correct, for: profile) | |
| 146 | } | |
| 147 | } | |
| 148 | ||
| 149 | private func prompt() { | |
| 150 | guard let item else { return } | |
| 151 | say(category == .alphabet ? "Trace the letter \(item.name) with your finger." | |
| 152 | : "Trace the \(item.name.lowercased()) with your finger.", after: 0) | |
| 153 | } | |
| 154 | ||
| 155 | private func say(_ text: String, after delay: TimeInterval, then completion: (() -> Void)? = nil) { | |
| 156 | speechToken += 1 | |
| 157 | let token = speechToken, gen = generation | |
| 158 | DispatchQueue.main.asyncAfter(deadline: .now() + delay) { | |
| 159 | guard token == speechToken, gen == generation else { return } | |
| 160 | SpeechSynthesizer.shared.speak(text, completion: completion) | |
| 161 | } | |
| 162 | } | |
| 163 | } | |
DayByDayTests/CardLineTests.swift added +32
| @@ -0,0 +1,32 @@ | ||
| 1 | import Testing | |
| 2 | @testable import DayByDay | |
| 3 | ||
| 4 | struct CardLineTests { | |
| 5 | @Test func animalLines() { | |
| 6 | #expect(LearnAnimal.cow.cardLine == "Cow! Cows say moo.") | |
| 7 | #expect(LearnAnimal.fish.cardLine == "Fish! Fish swim in water.") | |
| 8 | #expect(LearnAnimal.allCases.allSatisfy { $0.cardLine.hasPrefix("\($0.name)! ") && $0.fact.hasSuffix(".") }) | |
| 9 | } | |
| 10 | ||
| 11 | @Test func letterLines() { | |
| 12 | #expect(LearnLetter.b.cardLine == "b. b is for ball.") | |
| 13 | #expect(LearnLetter.allCases.allSatisfy { $0.cardLine.contains($0.exampleWord) }) | |
| 14 | } | |
| 15 | ||
| 16 | @Test func lineQueueKeepsOnlyLatest() { | |
| 17 | var q = LineQueue() | |
| 18 | let first = q.next() | |
| 19 | let second = q.next() | |
| 20 | #expect(!q.isCurrent(first)) | |
| 21 | #expect(q.isCurrent(second)) | |
| 22 | q.cancel() | |
| 23 | #expect(!q.isCurrent(second)) | |
| 24 | } | |
| 25 | ||
| 26 | /// A card's line waits for whichever sound is still playing, its own or an earlier card's. | |
| 27 | @Test func lineWaitsForEarlierSound() { | |
| 28 | #expect(abs(CardVoice.delay(newSound: 0, earlierSound: 1.1) - 1.3) < 1e-9) | |
| 29 | #expect(abs(CardVoice.delay(newSound: 0.9, earlierSound: 0.4) - 1.1) < 1e-9) | |
| 30 | #expect(CardVoice.delay(newSound: 0, earlierSound: 0) == 0) | |
| 31 | } | |
| 32 | } | |
DayByDayTests/ContrastTests.swift +14
| @@ -54,6 +54,20 @@ struct ContrastTests { | ||
| 54 | 54 | #expect(contrast(TrainStyle.tire, .black, .dark) >= 3) |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | @Test(arguments: LearnAnimal.allCases) func animalColorAgainstWhite(_ animal: LearnAnimal) { | |
| 58 | #expect(contrast(animal.color, .white, .light) >= 3, "\(animal)") | |
| 59 | } | |
| 60 | ||
| 61 | @Test(arguments: LearnLetter.allCases) func letterColorAgainstWhite(_ letter: LearnLetter) { | |
| 62 | #expect(contrast(letter.color, .white, .light) >= 3, "\(letter)") | |
| 63 | } | |
| 64 | ||
| 65 | /// Sort bins carry white labels and must stand out on a black page. | |
| 66 | @Test func sortBinFill() { | |
| 67 | #expect(contrast(SortStyle.binFill, .white, .light) >= 3) | |
| 68 | #expect(contrast(SortStyle.binFill, .black, .dark) >= 3) | |
| 69 | } | |
| 70 | ||
| 57 | 71 | @Test func playAgainAgainstWhite() { |
| 58 | 72 | #expect(contrast(RoundStyle.playAgainFill, .white, .light) >= 3) |
| 59 | 73 | } |
DayByDayTests/DressUpTests.swift added +48
| @@ -0,0 +1,48 @@ | ||
| 1 | import Testing | |
| 2 | import UIKit | |
| 3 | @testable import DayByDay | |
| 4 | ||
| 5 | struct DressUpTests { | |
| 6 | @Test func onlyOneSuitableChoice() { | |
| 7 | var rng = SeededGenerator(seed: 81) | |
| 8 | var previous: QuizItem? | |
| 9 | var weathers = Set<LearnWeather>() | |
| 10 | for i in 0..<300 { | |
| 11 | let q = DressUp().next(correctCount: i % 5, excluding: previous, using: &rng) | |
| 12 | let weather = q.shown[0].value(LearnWeather.self)! | |
| 13 | weathers.insert(weather) | |
| 14 | let suitable = q.choices.filter { Clothing.from($0)!.suits.contains(weather) } | |
| 15 | #expect(suitable == [q.answer]) | |
| 16 | #expect((3...4).contains(q.choices.count)) | |
| 17 | #expect(q.answer != previous) | |
| 18 | previous = q.answer | |
| 19 | } | |
| 20 | #expect(weathers == [.sunny, .rainy, .snowy, .windy]) | |
| 21 | } | |
| 22 | ||
| 23 | @Test func promptShownAndFacts() { | |
| 24 | var rng = SeededGenerator(seed: 82) | |
| 25 | let q = DressUp().next(correctCount: 0, excluding: nil, using: &rng) | |
| 26 | let weather = q.shown[0].value(LearnWeather.self)! | |
| 27 | #expect(q.prompt == "It's \(weather.name.lowercased())! What should we wear?") | |
| 28 | let wrong = q.choices.first { $0 != q.answer }! | |
| 29 | #expect(q.correction(for: wrong) == "\(Clothing.from(wrong)!.fact) Try again!") | |
| 30 | #expect(Clothing.umbrella.fact == "An umbrella is for rainy days.") | |
| 31 | // Coat and jacket look alike, so neither is ever the wrong one beside the other. | |
| 32 | #expect(Clothing.coat.suits == Clothing.jacket.suits) | |
| 33 | // A coat is fine in the rain too, so it is never a wrong answer there. | |
| 34 | #expect(Clothing.coat.suits.contains(.rainy)) | |
| 35 | } | |
| 36 | ||
| 37 | @Test(arguments: Clothing.allCases) func symbolExists(_ c: Clothing) { | |
| 38 | #expect(UIImage(systemName: c.symbol) != nil, "\(c.symbol)") | |
| 39 | } | |
| 40 | ||
| 41 | @Test func idsPinned() { | |
| 42 | #expect(Clothing.allCases.map(\.item.id) == [ | |
| 43 | "clothing.sunglasses", "clothing.sunHat", "clothing.tshirt", "clothing.umbrella", | |
| 44 | "clothing.coat", "clothing.jacket", | |
| 45 | ]) | |
| 46 | #expect(Clothing.allCases.allSatisfy { Clothing.from($0.item) == $0 }) | |
| 47 | } | |
| 48 | } | |
DayByDayTests/GameTests.swift +9 −2
| @@ -14,6 +14,12 @@ struct GameTests { | ||
| 14 | 14 | #expect(Topic.days.games == [.findIt, .comesNext, .weekTrain, .yesterdayTomorrow]) |
| 15 | 15 | #expect(Topic.months.games == [.findIt, .comesNext, .yearWheel]) |
| 16 | 16 | #expect(Topic.seasons.games == [.findIt, .whichSeason]) |
| 17 | #expect(Topic.colors.games == [.findIt, .mixColors]) | |
| 18 | #expect(Topic.shapes.games == [.findIt, .oddOneOut, .trace]) | |
| 19 | #expect(Topic.alphabet.games == [.findIt, .listen, .trace]) | |
| 20 | #expect(Topic.weather.games == [.findIt, .listen, .dressUp]) | |
| 21 | #expect(Topic.food.games == [.findIt, .oddOneOut, .sortFood]) | |
| 22 | #expect(Topic.bodyParts.games == [.findIt, .moves]) | |
| 17 | 23 | #expect(Topic.numbers.games == [.findIt, .listen, .counting, .most]) |
| 18 | 24 | } |
| 19 | 25 | |
| @@ -28,7 +34,7 @@ struct GameTests { | ||
| 28 | 34 | #expect(Set(q.choices).count == q.choices.count, "\(c) \(game)") |
| 29 | 35 | #expect((3...4).contains(q.choices.count), "\(c) \(game)") |
| 30 | 36 | #expect(q.answer != previous, "\(c) \(game)") |
| 31 | #expect(q.choices.allSatisfy { $0.category == c }) | |
| 37 | #expect(q.choices.allSatisfy { $0.category == c }, "\(c) \(game)") | |
| 32 | 38 | #expect(!q.prompt.isEmpty) |
| 33 | 39 | previous = q.answer |
| 34 | 40 | } |
| @@ -119,7 +125,8 @@ struct GameTests { | ||
| 119 | 125 | @Test func titles() { |
| 120 | 126 | #expect(Game.allCases.map(\.title) == ["Find it!", "Odd one out", "Listen", |
| 121 | 127 | "What comes next?", "Week train", "Yesterday and tomorrow", |
| 122 | "Year wheel", "Which season?", "Count with me", "Which has the most?"]) | |
| 128 | "Year wheel", "Which season?", "Count with me", "Which has the most?", | |
| 129 | "Mix colors", "Trace it", "Dress for the weather", "Fruit or vegetable?", "Move your body"]) | |
| 123 | 130 | } |
| 124 | 131 | |
| 125 | 132 | /// The prompt names the rule, so only one choice fits it. |
DayByDayTests/MixColorsTests.swift added +44
| @@ -0,0 +1,44 @@ | ||
| 1 | import Testing | |
| 2 | @testable import DayByDay | |
| 3 | ||
| 4 | struct MixColorsTests { | |
| 5 | typealias Topic = DayByDay.Category | |
| 6 | ||
| 7 | func color(_ item: QuizItem) -> LearnColor { item.value(LearnColor.self)! } | |
| 8 | ||
| 9 | @Test func everyMixIsRight() { | |
| 10 | let table: [Set<LearnColor>: LearnColor] = [ | |
| 11 | [.red, .yellow]: .orange, [.blue, .yellow]: .green, [.red, .blue]: .purple, [.red, .white]: .pink, | |
| 12 | ] | |
| 13 | var rng = SeededGenerator(seed: 61) | |
| 14 | var seen = Set<LearnColor>() | |
| 15 | for i in 0..<300 { | |
| 16 | let q = MixColors().next(correctCount: i % 5, excluding: nil, using: &rng) | |
| 17 | #expect(q.shown.count == 2) | |
| 18 | #expect(table[Set(q.shown.map(color))] == color(q.answer)) | |
| 19 | seen.insert(color(q.answer)) | |
| 20 | } | |
| 21 | #expect(seen == [.orange, .green, .purple, .pink]) | |
| 22 | } | |
| 23 | ||
| 24 | @Test func mixNeverOffersIngredients() { | |
| 25 | var rng = SeededGenerator(seed: 62) | |
| 26 | var previous: QuizItem? | |
| 27 | for i in 0..<300 { | |
| 28 | let q = MixColors().next(correctCount: i % 5, excluding: previous, using: &rng) | |
| 29 | #expect(Set(q.choices).isDisjoint(with: q.shown)) | |
| 30 | #expect(q.choices.contains(q.answer)) | |
| 31 | #expect(q.answer != previous) | |
| 32 | previous = q.answer | |
| 33 | } | |
| 34 | } | |
| 35 | ||
| 36 | @Test func mixPromptAndCorrection() { | |
| 37 | var rng = SeededGenerator(seed: 63) | |
| 38 | let q = MixColors().next(correctCount: 0, excluding: nil, using: &rng) | |
| 39 | let (a, b) = (q.shown[0].name, q.shown[1].name) | |
| 40 | #expect(q.prompt == "What do \(a.lowercased()) and \(b.lowercased()) make?") | |
| 41 | let wrong = q.choices.first { $0 != q.answer }! | |
| 42 | #expect(q.correction(for: wrong) == "\(a) and \(b.lowercased()) make \(q.answer.name.lowercased()). Try again!") | |
| 43 | } | |
| 44 | } | |
DayByDayTests/MovesRoundTests.swift added +33
| @@ -0,0 +1,33 @@ | ||
| 1 | import Testing | |
| 2 | @testable import DayByDay | |
| 3 | ||
| 4 | struct MovesRoundTests { | |
| 5 | @Test func fiveDistinctParts() { | |
| 6 | for seed in 0..<50 { | |
| 7 | var rng = SeededGenerator(seed: UInt64(seed)) | |
| 8 | let r = MovesRound(using: &rng) | |
| 9 | #expect(r.parts.count == MovesRound.length) | |
| 10 | #expect(Set(r.parts).count == MovesRound.length) | |
| 11 | } | |
| 12 | } | |
| 13 | ||
| 14 | @Test func commandsForAll() { | |
| 15 | #expect(LearnBodyPart.nose.command == "Touch your nose!") | |
| 16 | #expect(LearnBodyPart.hands.command == "Clap your hands!") | |
| 17 | #expect(LearnBodyPart.allCases.allSatisfy { $0.command.hasSuffix("!") }) | |
| 18 | #expect(Set(LearnBodyPart.allCases.map(\.command)).count == LearnBodyPart.allCases.count) | |
| 19 | } | |
| 20 | ||
| 21 | @Test func advancesToEnd() { | |
| 22 | var rng = SeededGenerator(seed: 101) | |
| 23 | var r = MovesRound(using: &rng) | |
| 24 | for i in 0..<MovesRound.length { | |
| 25 | #expect(r.current == r.parts[i]) | |
| 26 | r.next() | |
| 27 | } | |
| 28 | #expect(r.current == nil) | |
| 29 | #expect(r.isDone) | |
| 30 | r.next() | |
| 31 | #expect(r.isDone) | |
| 32 | } | |
| 33 | } | |
DayByDayTests/ReviewExport.swift +10
| @@ -67,6 +67,16 @@ struct ReviewExport { | ||
| 67 | 67 | ("train", AnyView(SequencePuzzleView(game: .weekTrain) { train })), |
| 68 | 68 | ("slots", AnyView(SequencePuzzleView(game: .yesterdayTomorrow) { slots })), |
| 69 | 69 | ("wheel", AnyView(YearWheelView())), |
| 70 | ("mix", AnyView(RoundView(source: MixColors(), game: .mixColors) { ChoiceTile(item: $0) })), | |
| 71 | ("trace-shape", AnyView(TraceView(category: .shapes))), | |
| 72 | ("trace-letter", AnyView(TraceView(category: .alphabet))), | |
| 73 | ("dress", AnyView(RoundView(source: DressUp(), game: .dressUp) { ClothingTile(item: $0) })), | |
| 74 | ("sort", AnyView(SortView())), | |
| 75 | ("moves", AnyView(MovesView())), | |
| 76 | ("weather", AnyView(WeatherView())), | |
| 77 | ("rain", AnyView(RainOverlay(isActive: true).background(Color(.systemBackground)))), | |
| 78 | ("animals", AnyView(AnimalsView())), | |
| 79 | ("alphabet", AnyView(AlphabetView())), | |
| 70 | 80 | ("counting", AnyView(CountingView())), |
| 71 | 81 | ("piles", AnyView(HStack(spacing: 20) { |
| 72 | 82 | ForEach([3, 7, 10], id: \.self) { n in |
DayByDayTests/SortRoundTests.swift added +62
| @@ -0,0 +1,62 @@ | ||
| 1 | import Testing | |
| 2 | @testable import DayByDay | |
| 3 | ||
| 4 | struct SortRoundTests { | |
| 5 | func round(_ seed: UInt64 = 91) -> SortRound { | |
| 6 | var rng = SeededGenerator(seed: seed) | |
| 7 | return SortRound(using: &rng) | |
| 8 | } | |
| 9 | ||
| 10 | @Test func threeOfEach() { | |
| 11 | for seed in 0..<50 { | |
| 12 | let r = round(UInt64(seed)) | |
| 13 | #expect(r.foods.filter(\.isFruit).count == 3) | |
| 14 | #expect(r.foods.filter { !$0.isFruit }.count == 3) | |
| 15 | #expect(Set(r.foods).count == 6) | |
| 16 | } | |
| 17 | } | |
| 18 | ||
| 19 | @Test func correctBinAdvances() { | |
| 20 | var r = round() | |
| 21 | let first = r.current! | |
| 22 | #expect(r.place(isFruit: first.isFruit) == .correct) | |
| 23 | #expect((first.isFruit ? r.fruitBin : r.vegetableBin) == [first]) | |
| 24 | #expect(r.current == r.foods[1]) | |
| 25 | } | |
| 26 | ||
| 27 | @Test func wrongBinWaits() { | |
| 28 | var r = round() | |
| 29 | let first = r.current! | |
| 30 | #expect(r.place(isFruit: !first.isFruit) == .incorrect(correctName: first.name)) | |
| 31 | #expect(r.place(isFruit: first.isFruit) == .ignored) | |
| 32 | r.resume() | |
| 33 | #expect(r.place(isFruit: first.isFruit) == .correct) | |
| 34 | } | |
| 35 | ||
| 36 | @Test func doneIgnoresInput() { | |
| 37 | var r = round() | |
| 38 | while let food = r.current { _ = r.place(isFruit: food.isFruit) } | |
| 39 | #expect(r.isDone) | |
| 40 | #expect(r.place(isFruit: true) == .ignored) | |
| 41 | #expect(r.fruitBin.count + r.vegetableBin.count == 6) | |
| 42 | } | |
| 43 | ||
| 44 | @Test func promptAndCorrection() { | |
| 45 | var r = round() | |
| 46 | let food = r.current! | |
| 47 | #expect(r.prompt == SortRound.question(for: food)) | |
| 48 | _ = r.place(isFruit: !food.isFruit) | |
| 49 | #expect(r.correction().hasSuffix("is a \(food.isFruit ? "fruit" : "vegetable"). Try again!")) | |
| 50 | #expect(SortRound.fact(for: .apple) == "An apple is a fruit.") | |
| 51 | #expect(SortRound.fact(for: .carrot) == "A carrot is a vegetable.") | |
| 52 | } | |
| 53 | ||
| 54 | @Test func grammarForEveryFood() { | |
| 55 | #expect(SortRound.fact(for: .grapes) == "Grapes are a fruit.") | |
| 56 | #expect(SortRound.fact(for: .corn) == "Corn is a vegetable.") | |
| 57 | #expect(SortRound.fact(for: .broccoli) == "Broccoli is a vegetable.") | |
| 58 | #expect(SortRound.question(for: .grapes) == "Are grapes a fruit or a vegetable?") | |
| 59 | #expect(SortRound.question(for: .corn) == "Is corn a fruit or a vegetable?") | |
| 60 | #expect(SortRound.question(for: .apple) == "Is the apple a fruit or a vegetable?") | |
| 61 | } | |
| 62 | } | |
DayByDayTests/TraceTests.swift added +56
| @@ -0,0 +1,56 @@ | ||
| 1 | import CoreGraphics | |
| 2 | import Testing | |
| 3 | @testable import DayByDay | |
| 4 | ||
| 5 | struct TraceTests { | |
| 6 | typealias Topic = DayByDay.Category | |
| 7 | ||
| 8 | static let cases: [(QuizItem, CGFloat)] = [ | |
| 9 | Topic.shapes.item(LearnShape.circle), Topic.shapes.item(LearnShape.star), | |
| 10 | Topic.alphabet.item(LearnLetter.a), Topic.alphabet.item(LearnLetter.s), | |
| 11 | ].flatMap { item in [(item, CGFloat(300)), (item, CGFloat(600))] } | |
| 12 | ||
| 13 | func checker(_ item: QuizItem, _ size: CGFloat) -> TraceChecker { | |
| 14 | TraceChecker(region: TraceGuide.region(for: item, size: size), size: size) | |
| 15 | } | |
| 16 | ||
| 17 | @Test(arguments: cases) func tracingTheGuidePasses(_ item: QuizItem, _ size: CGFloat) { | |
| 18 | let c = checker(item, size) | |
| 19 | var rng = SeededGenerator(seed: 71) | |
| 20 | let wobble = size * 0.03 | |
| 21 | let drawn = c.samples.map { | |
| 22 | CGPoint(x: $0.x + .random(in: -wobble...wobble, using: &rng), | |
| 23 | y: $0.y + .random(in: -wobble...wobble, using: &rng)) | |
| 24 | } | |
| 25 | #expect(c.passes(drawn), "\(item.name) \(size)") | |
| 26 | } | |
| 27 | ||
| 28 | @Test(arguments: cases) func scribbleElsewhereFails(_ item: QuizItem, _ size: CGFloat) { | |
| 29 | let c = checker(item, size) | |
| 30 | let region = TraceGuide.region(for: item, size: size) | |
| 31 | let away = (0..<400).map { i in | |
| 32 | CGPoint(x: CGFloat(i % 20) * size / 20, y: CGFloat(i / 20) * size / 20) | |
| 33 | }.filter { !region.contains($0) } | |
| 34 | #expect(!c.passes(away), "\(item.name) \(size)") | |
| 35 | #expect(c.score(away).accuracy < 0.5) | |
| 36 | } | |
| 37 | ||
| 38 | @Test(arguments: cases) func halfTraceFails(_ item: QuizItem, _ size: CGFloat) { | |
| 39 | let c = checker(item, size) | |
| 40 | let half = c.samples.sorted { $0.x < $1.x }.prefix(c.samples.count / 2) | |
| 41 | #expect(!c.passes(Array(half)), "\(item.name) \(size)") | |
| 42 | } | |
| 43 | ||
| 44 | @Test(arguments: cases) func regionsFitTheCanvas(_ item: QuizItem, _ size: CGFloat) { | |
| 45 | let box = TraceGuide.region(for: item, size: size).boundingBox | |
| 46 | #expect(CGRect(x: 0, y: 0, width: size, height: size).contains(box)) | |
| 47 | #expect(box.width >= size * 0.4 || box.height >= size * 0.4) | |
| 48 | #expect(checker(item, size).samples.count > 40) | |
| 49 | } | |
| 50 | ||
| 51 | @Test func everyItemHasARegion() { | |
| 52 | for item in Topic.shapes.quizItems + Topic.alphabet.quizItems { | |
| 53 | #expect(!TraceGuide.region(for: item, size: 300).isEmpty, "\(item.name)") | |
| 54 | } | |
| 55 | } | |
| 56 | } | |
LetterCard.swift +13 −10
| @@ -14,7 +14,7 @@ struct LetterCard: View { | ||
| 14 | 14 | var body: some View { |
| 15 | 15 | ZStack { |
| 16 | 16 | RoundedRectangle(cornerRadius: 32, style: .continuous) |
| 17 | .fill(letter.color.gradient) | |
| 17 | .fill(letter.color) | |
| 18 | 18 | .shadow(color: letter.color.opacity(0.4), radius: 8, y: 4) |
| 19 | 19 | |
| 20 | 20 | Text(letter.character) |
| @@ -27,7 +27,7 @@ struct LetterCard: View { | ||
| 27 | 27 | .animation(.spring(response: 0.35, dampingFraction: 0.5), value: isTapped) |
| 28 | 28 | .onTapGesture { |
| 29 | 29 | isTapped = true |
| 30 | SpeechSynthesizer.shared.speak(letter.spokenName) | |
| 30 | CardVoice.shared.say(letter.cardLine, after: nil) | |
| 31 | 31 | DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) { |
| 32 | 32 | isTapped = false |
| 33 | 33 | } |
| @@ -63,17 +63,20 @@ nonisolated enum LearnLetter: Int, CaseIterable, Identifiable { | ||
| 63 | 63 | "sun", "tiger", "umbrella", "van", "web", "fox", "yo-yo", "zebra"][rawValue] |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | /// Cycle through 8 bright colors to give variety without unique-per-letter mapping. | |
| 66 | /// Said when the card is tapped. | |
| 67 | var cardLine: String { "\(spokenName). \(spokenName) is for \(exampleWord)." } | |
| 68 | ||
| 69 | /// Cycle through 8 colors, each at least 3:1 against the white letter. | |
| 67 | 70 | var color: Color { |
| 68 | 71 | let palette: [Color] = [ |
| 69 | Color(red: 0.9, green: 0.3, blue: 0.3), // red | |
| 70 | Color(red: 1.0, green: 0.55, blue: 0.2), // orange | |
| 71 | Color(red: 1.0, green: 0.8, blue: 0.2), // yellow | |
| 72 | Color(red: 0.35, green: 0.75, blue: 0.4), // green | |
| 73 | Color(red: 0.2, green: 0.65, blue: 0.9), // blue | |
| 72 | Color(red: 0.86, green: 0.20, blue: 0.22), // red | |
| 73 | Color(red: 0.80, green: 0.42, blue: 0.08), // orange | |
| 74 | Color(red: 0.66, green: 0.50, blue: 0.0), // mustard | |
| 75 | Color(red: 0.18, green: 0.56, blue: 0.28), // green | |
| 76 | Color(red: 0.0, green: 0.42, blue: 0.85), // blue | |
| 74 | 77 | Color(red: 0.55, green: 0.4, blue: 0.85), // purple |
| 75 | Color(red: 0.85, green: 0.4, blue: 0.6), // pink | |
| 76 | Color(red: 0.2, green: 0.75, blue: 0.75), // teal | |
| 78 | Color(red: 0.82, green: 0.25, blue: 0.52), // pink | |
| 79 | Color(red: 0.0, green: 0.53, blue: 0.56), // teal | |
| 77 | 80 | ] |
| 78 | 81 | return palette[rawValue % palette.count] |
| 79 | 82 | } |
WeatherView.swift +33
| @@ -6,13 +6,28 @@ | ||
| 6 | 6 | import SwiftUI |
| 7 | 7 | |
| 8 | 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. | |
| 9 | 10 | struct WeatherView: View { |
| 10 | 11 | private let columns = [ |
| 11 | 12 | GridItem(.adaptive(minimum: 280), spacing: 24) |
| 12 | 13 | ] |
| 13 | 14 | |
| 15 | @State private var raining = false | |
| 16 | /// Bumped when the screen goes away, so a pending stop does nothing. | |
| 17 | @State private var generation = 0 | |
| 18 | ||
| 14 | 19 | var body: some View { |
| 15 | 20 | ScrollView { |
| 21 | Button(action: rain) { | |
| 22 | Label("Shake for rain!", systemImage: "cloud.rain.fill") | |
| 23 | .font(.system(size: 22, weight: .bold, design: .rounded)) | |
| 24 | .padding(.horizontal, 24) | |
| 25 | .padding(.vertical, 14) | |
| 26 | .background(Capsule().fill(Color(.systemGray6))) | |
| 27 | } | |
| 28 | .buttonStyle(.plain) | |
| 29 | .padding(.top, 16) | |
| 30 | ||
| 16 | 31 | LazyVGrid(columns: columns, spacing: 24) { |
| 17 | 32 | ForEach(LearnWeather.allCases) { weather in |
| 18 | 33 | WeatherCard(weather: weather) |
| @@ -21,6 +36,24 @@ struct WeatherView: View { | ||
| 21 | 36 | } |
| 22 | 37 | .padding(32) |
| 23 | 38 | } |
| 39 | .overlay { RainOverlay(isActive: raining) } | |
| 40 | .onShake(rain) | |
| 41 | .onDisappear { | |
| 42 | generation += 1 | |
| 43 | raining = false | |
| 44 | CardVoice.shared.cancel() | |
| 45 | } | |
| 46 | } | |
| 47 | ||
| 48 | private func rain() { | |
| 49 | guard !raining else { return } | |
| 50 | raining = true | |
| 51 | SoundEngine.shared.play(.rain) | |
| 52 | CardVoice.shared.say("It's raining!", after: nil) | |
| 53 | let gen = generation | |
| 54 | DispatchQueue.main.asyncAfter(deadline: .now() + RainOverlay.duration) { | |
| 55 | if gen == generation { raining = false } | |
| 56 | } | |
| 24 | 57 | } |
| 25 | 58 | } |
| 26 | 59 | |
docs/superpowers/plans/2026-10-16-part4-richer-cards.md added +134
| @@ -0,0 +1,134 @@ | ||
| 1 | # Part 4: Richer Cards Implementation Plan | |
| 2 | ||
| 3 | > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. | |
| 4 | ||
| 5 | **Goal:** Cards that do more than say their name (animal sound and fact, letter and example word), shake-for-rain on the weather cards, and five topic activities: mix colors, trace shapes and letters, dress for the weather, sort fruit and vegetables, and "Touch your nose!" moves. | |
| 6 | ||
| 7 | **Architecture:** Card lines live on the item enums (`cardLine`) and are spoken through a small `CardVoice` that plays a sound first and cancels stale lines. Mix colors and dress for the weather are `QuestionSource`s run by `RoundView`, which gains a header that shows a question's `shown` items. Tracing, sorting and moves are pure models (`TraceChecker`/`TraceGuide`, `SortRound`, `MovesRound`) with their own views. All five are `Game` cases opened by `GameDestination`. | |
| 8 | ||
| 9 | **Tech Stack:** SwiftUI, CoreGraphics/CoreText (trace regions), AVFoundation, Swift Testing, Xcode 27. | |
| 10 | ||
| 11 | **Spec:** `docs/superpowers/specs/2026-10-16-learning-games-design.md` (part 4). Builds on the part 1–3 plans in this folder. | |
| 12 | ||
| 13 | ## Global Constraints | |
| 14 | ||
| 15 | - Effective deployment target is iOS 17.5; SF Symbols used must exist on iOS 17 (SF Symbols 5). Each new symbol gets a `UIImage(systemName:)` test. | |
| 16 | - `SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor`; pure logic is `nonisolated`. | |
| 17 | - New files under `DayByDay/` and `DayByDayTests/`. No bundled assets; sound synthesized, art in SwiftUI/SF Symbols/the food emoji already in use. | |
| 18 | - Nothing requires reading: every prompt is spoken; bins and choices are pictures. | |
| 19 | - Wrong answers: `.gentleMiss` plus a spoken correction. No buzzer, no red X. | |
| 20 | - Every touched card uses flat fills; every new fill with white content is ≥ 3:1 in `ContrastTests`. | |
| 21 | - Lifecycle: every `asyncAfter` checks a `generation` counter that only `onDisappear` bumps; screen states sit in a `ZStack`, not a `Group`; card grids stay stable. | |
| 22 | - Advance after speech with `SpeechSynthesizer.speak(_:completion:)` instead of guessing its length, with a backstop timer. | |
| 23 | - Reduce Motion: rain from shaking shows still drops that fade instead of falling. | |
| 24 | - `Game` raw values are persisted; add new cases, never rename. | |
| 25 | - No camera or microphone. Shake uses the UIKit shake event. | |
| 26 | - Branch `feat/richer-cards`. Every commit `Ref #N` (last `Closes #N`). No AI attribution. | |
| 27 | - Test command: `xcodebuild test -project DayByDay.xcodeproj -scheme DayByDay -destination 'platform=iOS Simulator,name=iPhone 18 Pro'` (about 10 min for the full suite; run in the background). | |
| 28 | ||
| 29 | ## Review Focus | |
| 30 | ||
| 31 | 1. Tracing accepts a child's wobbly but on-target trace and rejects a scribble elsewhere or a half-finished trace, at any canvas size. Test in Task 3. | |
| 32 | 2. Dress for the weather never offers a second suitable item as a distractor (coat and jacket suit rain, snow and wind). Test in Task 5. | |
| 33 | 3. Rapid taps on look-and-listen cards: only the last card's line is spoken; a sound from an earlier card never runs into a later card's line. Test in Task 1 (`CardVoice` token logic). | |
| 34 | 4. Sorting: a tap on a bin while a correction plays, or after the last food, is ignored and records nothing. Test in Task 6. | |
| 35 | 5. Leaving any new screen mid-feedback says nothing afterwards (generation guard). Checked in review. | |
| 36 | ||
| 37 | --- | |
| 38 | ||
| 39 | ### Task 0: Branch and issue | |
| 40 | ||
| 41 | - [ ] `git checkout -b feat/richer-cards main`; `gitbay issue create --title "Part 4: richer cards and topic activities" --json --file -`; commit this plan with `Ref #N`. | |
| 42 | ||
| 43 | ### Task 1: Card lines, CardVoice, flat animal and letter cards | |
| 44 | ||
| 45 | **Files:** Modify `AnimalCard.swift`, `LetterCard.swift`, `AnimalsView.swift`, `AlphabetView.swift`. Create `DayByDay/Quiz/CardVoice.swift`. Test `DayByDayTests/CardLineTests.swift`, `ContrastTests.swift`. | |
| 46 | ||
| 47 | **Interfaces:** | |
| 48 | - `nonisolated extension LearnAnimal { var fact: String; var cardLine: String }`. `cardLine == "\(name)! \(fact)"`. Facts: cat "Cats say meow.", dog "Dogs say woof.", bird "Birds can fly.", fish "Fish swim in water.", rabbit "Rabbits hop.", turtle "Turtles carry their shell.", ladybug "Ladybugs have spots.", cow "Cows say moo.", duck "Ducks say quack.", sheep "Sheep say baa.", pig "Pigs say oink." | |
| 49 | - `nonisolated extension LearnLetter { var cardLine: String }` = `"\(spokenName). \(spokenName) is for \(exampleWord)."` | |
| 50 | - `@MainActor final class CardVoice { static let shared; func say(_ line: String, after sound: SoundEffect?); func cancel() }`. Bumps a token, stops speech, plays the sound, speaks after `SoundEngine.remaining(sound) + 0.2` only if the token is unchanged. Testable seam: `nonisolated struct LineQueue { mutating func next() -> Int; func isCurrent(_ token: Int) -> Bool }` used by `CardVoice`. | |
| 51 | - `LearnAnimal.color` and `LearnLetter.color` changed to values ≥ 3:1 against white; cards use flat fills. Card taps call `CardVoice.shared.say(animal.cardLine, after: animal.sound)` / `say(letter.cardLine, after: nil)`. Views call `CardVoice.shared.cancel()` on disappear. | |
| 52 | ||
| 53 | - [ ] Tests: `animalLines` (cow → "Cow! Cows say moo."), `letterLines` (b → "b. b is for ball."), `lineQueueKeepsOnlyLatest` (tokens 1, 2: isCurrent(1) false, isCurrent(2) true), `ContrastTests.animalColorAgainstWhite` and `letterColorAgainstWhite` over all cases. RED, implement, GREEN, commit `feat: animal and letter cards say more`. | |
| 54 | ||
| 55 | ### Task 2: Question header and Mix colors | |
| 56 | ||
| 57 | **Files:** Modify `Question.swift` (`shown`), `RoundView.swift` (header), `Game.swift`, `GameDestination.swift`. Create `DayByDay/Quiz/MixColors.swift`. Test `DayByDayTests/MixColorsTests.swift`, `GameTests.swift`. | |
| 58 | ||
| 59 | **Interfaces:** | |
| 60 | - `Question.shown: [QuizItem] = []`: items drawn above the choices. | |
| 61 | - `RoundView` draws a header when `current.shown` is not empty: the shown items as 110 pt tiles joined by "+" and followed by "= ?" when there are two; a single item is drawn alone. Existing call sites unchanged. | |
| 62 | - `nonisolated struct MixColors: QuestionSource`. Mixes: red + yellow = orange, blue + yellow = green, red + blue = purple, red + white = pink. Prompt `"What do \(a) and \(b) make?"` (lower-case color names). Choices: the answer plus other colors, never `a` or `b`. Correction fact on every wrong choice: `"\(A) and \(b) make \(answer)."`. | |
| 63 | - `Game.mixColors` ("Mix colors", symbol `paintbrush.fill`, `usesRound`). Colors games `[findIt, mixColors]`. | |
| 64 | ||
| 65 | - [ ] Tests: `everyMixIsRight` (answer matches the table for the shown pair), `mixNeverOffersIngredients`, `mixPrompt`, `mixCorrection`; catalog. RED, implement, GREEN, render check, commit `feat: mix colors`. | |
| 66 | ||
| 67 | ### Task 3: Trace model | |
| 68 | ||
| 69 | **Files:** Create `DayByDay/Quiz/Trace.swift`. Test `DayByDayTests/TraceTests.swift`. | |
| 70 | ||
| 71 | **Interfaces:** | |
| 72 | - `nonisolated enum TraceGuide { static func region(for item: QuizItem, size: CGFloat) -> CGPath }`: letters are the filled glyph of the uppercase letter in the system rounded heavy font, scaled to fit 80% of a `size`×`size` square and centered; shapes are their outline stroked at `0.12 * size` (circle, square, triangle, star (5 points), heart, oval, diamond, rectangle). | |
| 73 | - `nonisolated struct TraceChecker { init(region: CGPath, size: CGFloat); let samples: [CGPoint]; func score(_ drawn: [CGPoint]) -> (coverage: Double, accuracy: Double); func passes(_ drawn: [CGPoint]) -> Bool }`. Samples are grid points inside the region at spacing `size / 30`. Tolerance `size * 0.07`. Coverage: share of samples within tolerance of a drawn point. Accuracy: share of drawn points inside the region or within tolerance of a sample. Passes at coverage ≥ 0.75 and accuracy ≥ 0.75. | |
| 74 | ||
| 75 | - [ ] Tests, for the circle, star and letters A and S at sizes 300 and 600: `tracingTheGuidePasses` (drawn = samples jittered by up to 0.03·size), `scribbleElsewhereFails` (points in a box outside the region), `halfTraceFails` (first half of samples by x), `regionsFitTheCanvas` (bounding box inside the square, at least 40% of it). RED, implement, GREEN, commit `feat: trace checker`. | |
| 76 | ||
| 77 | ### Task 4: Trace game | |
| 78 | ||
| 79 | **Files:** Create `DayByDay/Quiz/TraceView.swift`. Modify `Game.swift`, `GameDestination.swift`. | |
| 80 | ||
| 81 | **Interfaces:** | |
| 82 | - `Game.trace` ("Trace it", symbol `hand.draw.fill`, not `usesRound`). Shapes games `[findIt, oddOneOut, trace]`; alphabet games `[findIt, listen, trace]`. | |
| 83 | - `TraceView(category:)`: five random items per round. The guide region is filled in the topic color at 30% opacity with a dashed topic-color edge; the finger's ink is the topic color at 0.08·size wide. It speaks `"Trace the \(name) with your finger."` (letters: `"Trace the letter \(spokenName)."`). On each lift, if `passes` → `.success`, "You traced the \(name)!", next item after the line ends; if accuracy < 0.5 and coverage > 0.4 → `.gentleMiss`, "Stay on the \(name). Let's try again.", ink cleared. "Start over" clears the ink. After five → `FinishedView`. Records `record(item, game: .trace, correct:)` for each pass or miss. | |
| 84 | ||
| 85 | - [ ] Catalog test update; build; render check; commit `feat: trace shapes and letters`. | |
| 86 | ||
| 87 | ### Task 5: Dress for the weather | |
| 88 | ||
| 89 | **Files:** Create `DayByDay/Quiz/DressUp.swift`, `DayByDay/Quiz/ClothingTile.swift`. Modify `Game.swift`, `GameDestination.swift`. Test `DayByDayTests/DressUpTests.swift`. | |
| 90 | ||
| 91 | **Interfaces:** | |
| 92 | - `nonisolated enum Clothing: Int, CaseIterable { sunglasses, sunHat, tshirt, umbrella, coat, jacket }` with `name`, `symbol` (`sunglasses.fill`, `hat.widebrim.fill`, `tshirt.fill`, `umbrella.fill`, `coat.fill`, `jacket.fill`), `suits: Set<LearnWeather>` (sunglasses, sunHat, tshirt: sunny; umbrella: rainy; coat and jacket: rainy, snowy, windy), `item: QuizItem` (`id "clothing.<case>"`, category `.weather`), `static func from(_ item: QuizItem) -> Clothing?`. | |
| 93 | - `nonisolated struct DressUp: QuestionSource`: weather from `[sunny, rainy, snowy, windy]` (never the previous question's weather); answer from the clothing that suits it; distractors only from clothing that does not. Prompt `"It's \(weather.name.lowercased())! What should we wear?"`; `shown = [weather item]`. Facts on wrong choices: `"\(Name) is not for \(weather) days."` (umbrella: "An umbrella is for rainy days." style — use `fact(for:)` per clothing: "Sunglasses are for sunny days.", "A sun hat is for sunny days.", "A T-shirt is for warm days.", "An umbrella is for rainy days.", "A coat is for cold days.", "A jacket is for cold, windy days."). | |
| 94 | - `ClothingTile(item:)`: weather-topic flat fill, white symbol. | |
| 95 | - `Game.dressUp` ("Dress for the weather", symbol `tshirt.fill`, `usesRound`). Weather games `[findIt, listen, dressUp]`. | |
| 96 | ||
| 97 | - [ ] Tests: `onlyOneSuitableChoice` (300 draws: exactly one choice suits the shown weather), `promptAndShown`, `clothingSymbolsExist`, `idsPinned` (all six ids). RED, implement, GREEN, render, commit `feat: dress for the weather`. | |
| 98 | ||
| 99 | ### Task 6: Sort fruit and vegetables | |
| 100 | ||
| 101 | **Files:** Create `DayByDay/Quiz/SortRound.swift`, `DayByDay/Quiz/SortView.swift`. Modify `Game.swift`, `GameDestination.swift`. Test `DayByDayTests/SortRoundTests.swift`. | |
| 102 | ||
| 103 | **Interfaces:** | |
| 104 | - `nonisolated struct SortRound { init(using:); let foods: [LearnFood] (3 fruit + 3 vegetables, shuffled); var current: LearnFood?; private(set) var fruitBin, vegetableBin: [LearnFood]; private(set) var isShowingCorrection; mutating func place(isFruit: Bool) -> AnswerOutcome; mutating func resume(); func correction() -> String; var prompt: String? }`. Prompt `"Is the \(name.lowercased()) a fruit or a vegetable?"`. Correction `"\(A/An) \(name.lowercased()) is a \(fruit|vegetable). Try again!"`. | |
| 105 | - `SortView`: current food emoji large (draggable); two bins below (🍓 "Fruit", 🥕 "Vegetables" as pictures with labels), tap or drop. Correct food drops into its bin (bin shows the foods it holds). Records `record(food item, game: .sortFood, correct:)`. After six → `FinishedView`. | |
| 106 | - `Game.sortFood` ("Fruit or vegetable?", symbol `basket.fill`, not `usesRound`). Food games `[findIt, oddOneOut, sortFood]`. | |
| 107 | ||
| 108 | - [ ] Tests: `threeOfEach`, `correctBinAdvances`, `wrongBinWaits` (.incorrect, then .ignored until resume), `doneIgnoresInput`, `correctionCopy` (apple → "An apple is a fruit. Try again!"). RED, implement, GREEN, render, commit `feat: sort fruit and vegetables`. | |
| 109 | ||
| 110 | ### Task 7: Touch your nose | |
| 111 | ||
| 112 | **Files:** Create `DayByDay/Quiz/MovesRound.swift`, `DayByDay/Quiz/MovesView.swift`. Modify `Game.swift`, `GameDestination.swift`. Test `DayByDayTests/MovesRoundTests.swift`. | |
| 113 | ||
| 114 | **Interfaces:** | |
| 115 | - `nonisolated extension LearnBodyPart { var command: String }`: head "Touch your head!", eyes "Blink your eyes!", ears "Touch your ears!", nose "Touch your nose!", mouth "Point to your mouth!", hands "Clap your hands!", fingers "Wiggle your fingers!", arms "Wave your arms!", legs "March in place!", feet "Stomp your feet!", heart "Put your hand on your heart!", belly "Pat your belly!". | |
| 116 | - `nonisolated struct MovesRound { static let length = 5; init(using:); let parts: [LearnBodyPart] (5 distinct); private(set) var index; var current: LearnBodyPart?; mutating func next() }`. | |
| 117 | - `MovesView`: the body-part tile large; speaks the command; 3 s after the line ends plays `.success` and a praise phrase; moves on after that line ends. "Hear it again". After five → `FinishedView`. Nothing recorded. | |
| 118 | - `Game.moves` ("Move your body", symbol `figure.wave`, not `usesRound`). Body games `[findIt, moves]`. | |
| 119 | ||
| 120 | - [ ] Tests: `fiveDistinctParts`, `commandsForAll`, `advancesToEnd`. RED, implement, GREEN, render, commit `feat: move your body`. | |
| 121 | ||
| 122 | ### Task 8: Shake for rain | |
| 123 | ||
| 124 | **Files:** Create `DayByDay/Quiz/ShakeRain.swift`. Modify `WeatherView.swift`. | |
| 125 | ||
| 126 | **Interfaces:** | |
| 127 | - `extension UIWindow { open override func motionEnded(_:with:) }` posts `Notification.Name.deviceDidShake` for `.motionShake`. `View.onShake(_:)` modifier. | |
| 128 | - `RainOverlay(isActive:)`: falling drops for 3 s in a light-and-dark-visible blue; Reduce Motion shows still drops that fade. `WeatherView` plays `.rain`, shows the overlay and says "It's raining!" on shake; a shake during rain is ignored. | |
| 129 | ||
| 130 | - [ ] Build; render check of the overlay; commit `feat: shake for rain`. | |
| 131 | ||
| 132 | ### Task 9: Reviews, MR, merge | |
| 133 | ||
| 134 | - [ ] Extend `ReviewExport` with the new screens and run it. Fresh art/sound reviewer (two rounds max, block on clear defects). Fresh whole-branch code reviewer with this Review Focus. One fix pass, each fix RED→GREEN. Full suite green. Push, MR with `Closes #N` and open notes, squash-merge, delete branch. | |