Commit b2cde9506e
Unsigned
Layout: unified · split
DayByDay/Parent/ChildProgressView.swift added +44
| @@ -0,0 +1,44 @@ | |||
| 1 | // | ||
| 2 | // ChildProgressView.swift | ||
| 3 | // DayByDay | ||
| 4 | // | ||
| 5 | |||
| 6 | import SwiftUI | ||
| 7 | |||
| 8 | /// For grown-ups: what one child knows and is still mixing up, per topic. | ||
| 9 | struct ChildProgressView: View { | ||
| 10 | let profile: Profile | ||
| 11 | |||
| 12 | @Environment(ProfileSession.self) private var session | ||
| 13 | |||
| 14 | var body: some View { | ||
| 15 | List(DayByDay.Category.gridCases) { category in | ||
| 16 | let summary = ProgressSummary(category: category, progress: session.store.progress(for: profile, category: category)) | ||
| 17 | HStack(alignment: .top, spacing: 14) { | ||
| 18 | // A fixed box, so wide symbols such as "abc" stay inside the circle. | ||
| 19 | Image(systemName: category.symbol) | ||
| 20 | .resizable() | ||
| 21 | .scaledToFit() | ||
| 22 | .fontWeight(.semibold) | ||
| 23 | .frame(width: 24, height: 24) | ||
| 24 | .foregroundStyle(.white) | ||
| 25 | .frame(width: 44, height: 44) | ||
| 26 | .background(Circle().fill(category.color)) | ||
| 27 | VStack(alignment: .leading, spacing: 4) { | ||
| 28 | Text(category.label) | ||
| 29 | .font(.system(size: 20, weight: .semibold, design: .rounded)) | ||
| 30 | if let known = summary.known { | ||
| 31 | Text(known) | ||
| 32 | } | ||
| 33 | if let missing = summary.missing { | ||
| 34 | Text(missing) | ||
| 35 | .fontWeight(.semibold) | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | .padding(.vertical, 4) | ||
| 40 | .accessibilityElement(children: .combine) | ||
| 41 | } | ||
| 42 | .navigationTitle("\(profile.name)'s progress") | ||
| 43 | } | ||
| 44 | } | ||
DayByDay/Parent/ParentAreaView.swift +13 −1
| @@ -5,7 +5,8 @@ | |||
| 5 | 5 | ||
| 6 | import SwiftUI | 6 | import SwiftUI |
| 7 | 7 | ||
| 8 | /// Grown-up screen for adding, editing, and deleting profiles. | 8 | /// Grown-up screen for adding, editing, and deleting profiles, and for |
| 9 | /// each child's progress. | ||
| 9 | struct ParentAreaView: View { | 10 | struct ParentAreaView: View { |
| 10 | let onDone: () -> Void | 11 | let onDone: () -> Void |
| 11 | 12 | ||
| @@ -32,6 +33,17 @@ struct ParentAreaView: View { | |||
| 32 | Label("Add a child", systemImage: "plus.circle.fill") | 33 | Label("Add a child", systemImage: "plus.circle.fill") |
| 33 | } | 34 | } |
| 34 | } | 35 | } |
| 36 | if !session.profiles.isEmpty { | ||
| 37 | Section("Progress") { | ||
| 38 | ForEach(session.profiles) { profile in | ||
| 39 | NavigationLink { | ||
| 40 | ChildProgressView(profile: profile) | ||
| 41 | } label: { | ||
| 42 | ProfileRow(profile: profile) | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 35 | } | 47 | } |
| 36 | .navigationTitle("Grown-Ups") | 48 | .navigationTitle("Grown-Ups") |
| 37 | .toolbar { | 49 | .toolbar { |
DayByDay/Parent/ProgressSummary.swift added +61
| @@ -0,0 +1,61 @@ | |||
| 1 | // | ||
| 2 | // ProgressSummary.swift | ||
| 3 | // DayByDay | ||
| 4 | // | ||
| 5 | |||
| 6 | nonisolated extension Category { | ||
| 7 | /// "Knows 5 of 7 days." | ||
| 8 | var progressNoun: String { | ||
| 9 | switch self { | ||
| 10 | case .today, .days: "days" | ||
| 11 | case .months: "months" | ||
| 12 | case .seasons: "seasons" | ||
| 13 | case .numbers: "numbers" | ||
| 14 | case .colors: "colors" | ||
| 15 | case .shapes: "shapes" | ||
| 16 | case .animals: "animals" | ||
| 17 | case .alphabet: "letters" | ||
| 18 | case .weather: "kinds of weather" | ||
| 19 | case .bodyParts: "body parts" | ||
| 20 | case .food: "foods" | ||
| 21 | } | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | /// One child's progress in one topic, in plain words for a parent. | ||
| 26 | nonisolated struct ProgressSummary { | ||
| 27 | static let listLimit = 4 | ||
| 28 | |||
| 29 | let known: String? | ||
| 30 | let missing: String? | ||
| 31 | |||
| 32 | init(category: Category, progress: ItemProgress) { | ||
| 33 | let items = category.quizItems | ||
| 34 | guard items.contains(where: { progress.statuses[$0.id] != nil }) else { | ||
| 35 | known = "Not played yet." | ||
| 36 | missing = nil | ||
| 37 | return | ||
| 38 | } | ||
| 39 | let knownCount = items.filter { progress.status($0.id) == .known }.count | ||
| 40 | let noun = category.progressNoun | ||
| 41 | let knownLine: String? = switch knownCount { | ||
| 42 | case items.count: "Knows all \(items.count) \(noun)." | ||
| 43 | case 0: nil | ||
| 44 | default: "Knows \(knownCount) of \(items.count) \(noun)." | ||
| 45 | } | ||
| 46 | // Letter items are named lowercase for speech; parents read capitals. | ||
| 47 | let learning = items.filter { progress.status($0.id) == .learning } | ||
| 48 | .map { category == .alphabet ? $0.name.uppercased() : $0.name } | ||
| 49 | missing = learning.isEmpty ? nil : "Still mixing up \(Self.list(learning))." | ||
| 50 | known = knownLine ?? (missing == nil ? "Just started." : nil) | ||
| 51 | } | ||
| 52 | |||
| 53 | /// "A", "A and B", "A, B and C", "A, B, C, D and 2 more". | ||
| 54 | static func list(_ names: [String]) -> String { | ||
| 55 | if names.count > listLimit { | ||
| 56 | return names.prefix(listLimit).joined(separator: ", ") + " and \(names.count - listLimit) more" | ||
| 57 | } | ||
| 58 | guard names.count > 1 else { return names.first ?? "" } | ||
| 59 | return names.dropLast().joined(separator: ", ") + " and " + names.last! | ||
| 60 | } | ||
| 61 | } | ||
DayByDay/Profiles/ItemProgress.swift added +64
| @@ -0,0 +1,64 @@ | |||
| 1 | // | ||
| 2 | // ItemProgress.swift | ||
| 3 | // DayByDay | ||
| 4 | // | ||
| 5 | |||
| 6 | import Foundation | ||
| 7 | |||
| 8 | /// One answer, as `ItemProgress` needs it. | ||
| 9 | nonisolated struct AnswerFact: Equatable { | ||
| 10 | /// The question's answer. | ||
| 11 | let itemID: String | ||
| 12 | let isCorrect: Bool | ||
| 13 | let timestamp: Date | ||
| 14 | } | ||
| 15 | |||
| 16 | nonisolated enum ItemStatus: Equatable { | ||
| 17 | case new, learning, known | ||
| 18 | } | ||
| 19 | |||
| 20 | /// How one child is doing with each item in a topic. A missed item stays | ||
| 21 | /// learning until it is answered right `clearAfter` times; the right answer | ||
| 22 | /// that just retries the missed question does not count. | ||
| 23 | nonisolated struct ItemProgress { | ||
| 24 | static let retryWindow: TimeInterval = 60 | ||
| 25 | static let clearAfter = 3 | ||
| 26 | static let knownAfter = 2 | ||
| 27 | static let learningWeight = 3.0 | ||
| 28 | |||
| 29 | private(set) var statuses: [String: ItemStatus] = [:] | ||
| 30 | private(set) var lastMiss: [String: Date] = [:] | ||
| 31 | |||
| 32 | init(_ facts: [AnswerFact]) { | ||
| 33 | var counted: [String: Int] = [:] | ||
| 34 | var awaitingRetry: Set<String> = [] | ||
| 35 | for fact in facts.sorted(by: { $0.timestamp < $1.timestamp }) { | ||
| 36 | let id = fact.itemID | ||
| 37 | if !fact.isCorrect { | ||
| 38 | lastMiss[id] = fact.timestamp | ||
| 39 | counted[id] = 0 | ||
| 40 | awaitingRetry.insert(id) | ||
| 41 | } else if awaitingRetry.remove(id) != nil, | ||
| 42 | let miss = lastMiss[id], fact.timestamp.timeIntervalSince(miss) < Self.retryWindow { | ||
| 43 | continue | ||
| 44 | } else { | ||
| 45 | counted[id, default: 0] += 1 | ||
| 46 | } | ||
| 47 | } | ||
| 48 | for (id, count) in counted { | ||
| 49 | if lastMiss[id] != nil && count < Self.clearAfter { | ||
| 50 | statuses[id] = .learning | ||
| 51 | } else { | ||
| 52 | statuses[id] = count >= Self.knownAfter ? .known : .new | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | func status(_ itemID: String) -> ItemStatus { | ||
| 58 | statuses[itemID] ?? .new | ||
| 59 | } | ||
| 60 | |||
| 61 | func weight(_ itemID: String) -> Double { | ||
| 62 | status(itemID) == .learning ? Self.learningWeight : 1 | ||
| 63 | } | ||
| 64 | } | ||
DayByDay/Profiles/ProfileStore.swift +21
| @@ -77,6 +77,27 @@ final class ProfileStore { | |||
| 77 | .sorted { $0.timestamp < $1.timestamp } | 77 | .sorted { $0.timestamp < $1.timestamp } |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | /// Games where an answer shows whether the child recognizes the item. | ||
| 81 | /// Tracing, sorting, ordering and comparing are left out. Answers with | ||
| 82 | /// no game were recorded before part 2 and are kept. | ||
| 83 | static let recognitionGames: Set<String> = Set([Game.findIt, .listen, .oddOneOut, .comesNext, .whichSeason].map(\.rawValue)) | ||
| 84 | |||
| 85 | func progress(for profile: Profile, category: Category) -> ItemProgress { | ||
| 86 | ItemProgress(answers(for: profile, category: category) | ||
| 87 | .filter { $0.gameRaw.map(Self.recognitionGames.contains) ?? true } | ||
| 88 | .map { AnswerFact(itemID: $0.itemID, isCorrect: $0.isCorrect, timestamp: $0.timestamp) }) | ||
| 89 | } | ||
| 90 | |||
| 91 | /// Pick weights for the topic's items the child is missing; the rest | ||
| 92 | /// weigh 1. Empty when nothing is being missed. | ||
| 93 | func reviewWeights(for profile: Profile, category: Category) -> [QuizItem: Double] { | ||
| 94 | let progress = progress(for: profile, category: category) | ||
| 95 | return Dictionary(uniqueKeysWithValues: category.quizItems.compactMap { item in | ||
| 96 | let weight = progress.weight(item.id) | ||
| 97 | return weight > 1 ? (item, weight) : nil | ||
| 98 | }) | ||
| 99 | } | ||
| 100 | |||
| 80 | /// Days per month in a leap year, so February 29 birthdays are allowed. | 101 | /// Days per month in a leap year, so February 29 birthdays are allowed. |
| 81 | static let daysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | 102 | static let daysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
| 82 | 103 | ||
DayByDay/Quiz/Game.swift +18 −4
| @@ -104,11 +104,13 @@ nonisolated extension Category { | |||
| 104 | return games | 104 | return games |
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | func questionSource(for game: Game) -> any QuestionSource { | 107 | /// `weights` favor answers the child has been missing; only the pick-one |
| 108 | /// games use them. | ||
| 109 | func questionSource(for game: Game, weights: [QuizItem: Double] = [:]) -> any QuestionSource { | ||
| 108 | switch game { | 110 | switch game { |
| 109 | case .findIt: findIt | 111 | case .findIt: with(findIt, weights) |
| 110 | case .oddOneOut: oddOneOut | 112 | case .oddOneOut: with(oddOneOut, weights) |
| 111 | case .listen: listen | 113 | case .listen: with(listen, weights) |
| 112 | case .comesNext: ComesNext(items: quizItems, noun: self == .days ? "day" : "month") | 114 | case .comesNext: ComesNext(items: quizItems, noun: self == .days ? "day" : "month") |
| 113 | case .whichSeason: WhichSeason() | 115 | case .whichSeason: WhichSeason() |
| 114 | case .most: Most() | 116 | case .most: Most() |
| @@ -172,6 +174,18 @@ nonisolated extension Category { | |||
| 172 | } | 174 | } |
| 173 | } | 175 | } |
| 174 | 176 | ||
| 177 | private func with(_ picker: QuestionPicker, _ weights: [QuizItem: Double]) -> QuestionPicker { | ||
| 178 | var picker = picker | ||
| 179 | picker.weights = weights | ||
| 180 | return picker | ||
| 181 | } | ||
| 182 | |||
| 183 | private func with(_ source: OddOneOut, _ weights: [QuizItem: Double]) -> OddOneOut { | ||
| 184 | var source = source | ||
| 185 | source.weights = weights | ||
| 186 | return source | ||
| 187 | } | ||
| 188 | |||
| 175 | private func items<T: CaseIterable>(_ values: [T]) -> [QuizItem] { | 189 | private func items<T: CaseIterable>(_ values: [T]) -> [QuizItem] { |
| 176 | values.map { item($0) } | 190 | values.map { item($0) } |
| 177 | } | 191 | } |
DayByDay/Quiz/GameDestination.swift +12 −3
| @@ -10,19 +10,28 @@ struct GameDestination: View { | |||
| 10 | let category: Category | 10 | let category: Category |
| 11 | let game: Game | 11 | let game: Game |
| 12 | 12 | ||
| 13 | @Environment(ProfileSession.self) private var session | ||
| 14 | |||
| 13 | var body: some View { | 15 | var body: some View { |
| 14 | content.navigationTitle(game.title) | 16 | content.navigationTitle(game.title) |
| 15 | } | 17 | } |
| 16 | 18 | ||
| 19 | /// Weights as of this render: the open round keeps its first question, | ||
| 20 | /// and the next round uses the latest answers. | ||
| 21 | private var source: any QuestionSource { | ||
| 22 | let weights = session.current.map { session.store.reviewWeights(for: $0, category: category) } ?? [:] | ||
| 23 | return category.questionSource(for: game, weights: weights) | ||
| 24 | } | ||
| 25 | |||
| 17 | @ViewBuilder | 26 | @ViewBuilder |
| 18 | private var content: some View { | 27 | private var content: some View { |
| 19 | switch game { | 28 | switch game { |
| 20 | case .most: | 29 | case .most: |
| 21 | RoundView(source: category.questionSource(for: game), game: game) { PileTile(item: $0) } | 30 | RoundView(source: source, game: game) { PileTile(item: $0) } |
| 22 | case .dressUp: | 31 | case .dressUp: |
| 23 | RoundView(source: category.questionSource(for: game), game: game) { ClothingTile(item: $0) } | 32 | RoundView(source: source, game: game) { ClothingTile(item: $0) } |
| 24 | case .findIt, .oddOneOut, .listen, .comesNext, .whichSeason, .mixColors: | 33 | case .findIt, .oddOneOut, .listen, .comesNext, .whichSeason, .mixColors: |
| 25 | RoundView(source: category.questionSource(for: game), game: game) { ChoiceTile(item: $0) } | 34 | RoundView(source: source, game: game) { ChoiceTile(item: $0) } |
| 26 | case .weekTrain: | 35 | case .weekTrain: |
| 27 | SequencePuzzleView(game: game) { | 36 | SequencePuzzleView(game: game) { |
| 28 | var rng = SystemRandomNumberGenerator() | 37 | var rng = SystemRandomNumberGenerator() |
DayByDay/Quiz/Question.swift +20 −2
| @@ -31,6 +31,8 @@ nonisolated struct QuestionPicker: QuestionSource { | |||
| 31 | let distractors: [QuizItem] | 31 | let distractors: [QuizItem] |
| 32 | let prompt: (QuizItem) -> String | 32 | let prompt: (QuizItem) -> String |
| 33 | let sound: (QuizItem) -> SoundEffect? | 33 | let sound: (QuizItem) -> SoundEffect? |
| 34 | /// Answer pick weights; unlisted items weigh 1. Empty picks evenly. | ||
| 35 | var weights: [QuizItem: Double] = [:] | ||
| 34 | 36 | ||
| 35 | init(answers: [QuizItem], distractors: [QuizItem]? = nil, | 37 | init(answers: [QuizItem], distractors: [QuizItem]? = nil, |
| 36 | prompt: @escaping (QuizItem) -> String, | 38 | prompt: @escaping (QuizItem) -> String, |
| @@ -46,7 +48,7 @@ nonisolated struct QuestionPicker: QuestionSource { | |||
| 46 | /// distinct distractors, shuffled. | 48 | /// distinct distractors, shuffled. |
| 47 | func next(correctCount: Int, excluding previous: QuizItem?, using rng: inout some RandomNumberGenerator) -> Question { | 49 | func next(correctCount: Int, excluding previous: QuizItem?, using rng: inout some RandomNumberGenerator) -> Question { |
| 48 | let candidates = answers.count > 1 ? answers.filter { $0 != previous } : answers | 50 | let candidates = answers.count > 1 ? answers.filter { $0 != previous } : answers |
| 49 | let answer = candidates.randomElement(using: &rng)! | 51 | let answer = weights.isEmpty ? candidates.randomElement(using: &rng)! : candidates.weightedRandom(weights, using: &rng) |
| 50 | let others = distractors.filter { $0 != answer }.shuffled(using: &rng) | 52 | let others = distractors.filter { $0 != answer }.shuffled(using: &rng) |
| 51 | let count = min(Round.choiceCount(correctCount: correctCount), others.count + 1) | 53 | let count = min(Round.choiceCount(correctCount: correctCount), others.count + 1) |
| 52 | let choices = ([answer] + others.prefix(count - 1)).shuffled(using: &rng) | 54 | let choices = ([answer] + others.prefix(count - 1)).shuffled(using: &rng) |
| @@ -65,6 +67,8 @@ nonisolated struct OddOneOut: QuestionSource { | |||
| 65 | } | 67 | } |
| 66 | 68 | ||
| 67 | let groups: (Group, Group) | 69 | let groups: (Group, Group) |
| 70 | /// Answer pick weights; unlisted items weigh 1. Empty picks evenly. | ||
| 71 | var weights: [QuizItem: Double] = [:] | ||
| 68 | 72 | ||
| 69 | init(_ a: Group, _ b: Group) { | 73 | init(_ a: Group, _ b: Group) { |
| 70 | groups = (a, b) | 74 | groups = (a, b) |
| @@ -80,7 +84,7 @@ nonisolated struct OddOneOut: QuestionSource { | |||
| 80 | } | 84 | } |
| 81 | let (majority, odd) = fitting.randomElement(using: &rng)! | 85 | let (majority, odd) = fitting.randomElement(using: &rng)! |
| 82 | let candidates = odd.members.count > 1 ? odd.members.filter { $0 != previous } : odd.members | 86 | let candidates = odd.members.count > 1 ? odd.members.filter { $0 != previous } : odd.members |
| 83 | let answer = candidates.randomElement(using: &rng)! | 87 | let answer = weights.isEmpty ? candidates.randomElement(using: &rng)! : candidates.weightedRandom(weights, using: &rng) |
| 84 | let others = Array(majority.members.shuffled(using: &rng).prefix(count - 1)) | 88 | let others = Array(majority.members.shuffled(using: &rng).prefix(count - 1)) |
| 85 | var facts = [answer: odd.fact(answer)] | 89 | var facts = [answer: odd.fact(answer)] |
| 86 | for item in others { facts[item] = majority.fact(item) } | 90 | for item in others { facts[item] = majority.fact(item) } |
| @@ -89,3 +93,17 @@ nonisolated struct OddOneOut: QuestionSource { | |||
| 89 | answer: answer, facts: facts) | 93 | answer: answer, facts: facts) |
| 90 | } | 94 | } |
| 91 | } | 95 | } |
| 96 | |||
| 97 | nonisolated extension Array where Element == QuizItem { | ||
| 98 | /// A non-empty array's element, chosen in proportion to its weight | ||
| 99 | /// (1 when unlisted). | ||
| 100 | func weightedRandom(_ weights: [QuizItem: Double], using rng: inout some RandomNumberGenerator) -> QuizItem { | ||
| 101 | let total = reduce(0) { $0 + weights[$1, default: 1] } | ||
| 102 | var target = Double.random(in: 0..<total, using: &rng) | ||
| 103 | for item in self { | ||
| 104 | target -= weights[item, default: 1] | ||
| 105 | if target < 0 { return item } | ||
| 106 | } | ||
| 107 | return last! | ||
| 108 | } | ||
| 109 | } | ||
DayByDayTests/ItemProgressTests.swift added +64
| @@ -0,0 +1,64 @@ | |||
| 1 | import Foundation | ||
| 2 | import Testing | ||
| 3 | @testable import DayByDay | ||
| 4 | |||
| 5 | struct ItemProgressTests { | ||
| 6 | let start = Date(timeIntervalSince1970: 1_800_000_000) | ||
| 7 | |||
| 8 | /// `(id, correct, seconds after start)`. | ||
| 9 | func progress(_ answers: [(String, Bool, TimeInterval)]) -> ItemProgress { | ||
| 10 | ItemProgress(answers.map { AnswerFact(itemID: $0.0, isCorrect: $0.1, timestamp: start + $0.2) }) | ||
| 11 | } | ||
| 12 | |||
| 13 | @Test func untouchedIsNew() { | ||
| 14 | #expect(progress([]).status("monday") == .new) | ||
| 15 | } | ||
| 16 | |||
| 17 | @Test func oneRightIsNew() { | ||
| 18 | #expect(progress([("monday", true, 0)]).status("monday") == .new) | ||
| 19 | } | ||
| 20 | |||
| 21 | @Test func twoRightIsKnown() { | ||
| 22 | #expect(progress([("monday", true, 0), ("monday", true, 300)]).status("monday") == .known) | ||
| 23 | } | ||
| 24 | |||
| 25 | @Test func missIsLearning() { | ||
| 26 | #expect(progress([("june", false, 0)]).status("june") == .learning) | ||
| 27 | } | ||
| 28 | |||
| 29 | @Test func threeRightClearsMiss() { | ||
| 30 | let p = progress([("june", false, 0), ("june", true, 300), ("june", true, 600), ("june", true, 900)]) | ||
| 31 | #expect(p.status("june") == .known) | ||
| 32 | let two = progress([("june", false, 0), ("june", true, 300), ("june", true, 600)]) | ||
| 33 | #expect(two.status("june") == .learning) | ||
| 34 | } | ||
| 35 | |||
| 36 | @Test func retryDoesNotCount() { | ||
| 37 | var answers: [(String, Bool, TimeInterval)] = [("june", false, 0), ("june", true, 5), ("june", true, 300), ("june", true, 600)] | ||
| 38 | #expect(progress(answers).status("june") == .learning) | ||
| 39 | answers.append(("june", true, 900)) | ||
| 40 | #expect(progress(answers).status("june") == .known) | ||
| 41 | } | ||
| 42 | |||
| 43 | @Test func newMissReopens() { | ||
| 44 | let answers = (0..<5).map { ("july", true, TimeInterval($0) * 300) } + [("july", false, 2000)] | ||
| 45 | #expect(progress(answers).status("july") == .learning) | ||
| 46 | } | ||
| 47 | |||
| 48 | @Test func weights() { | ||
| 49 | let p = progress([("june", false, 0), ("july", true, 0), ("july", true, 300)]) | ||
| 50 | #expect(p.weight("june") == 3) | ||
| 51 | #expect(p.weight("july") == 1) | ||
| 52 | #expect(p.weight("august") == 1) | ||
| 53 | } | ||
| 54 | |||
| 55 | @Test func lastMissIsMostRecent() { | ||
| 56 | let p = progress([("june", false, 0), ("june", true, 300), ("june", false, 600)]) | ||
| 57 | #expect(p.lastMiss["june"] == start + 600) | ||
| 58 | } | ||
| 59 | |||
| 60 | @Test func unorderedInput() { | ||
| 61 | let p = progress([("june", true, 900), ("june", true, 300), ("june", false, 0), ("june", true, 600)]) | ||
| 62 | #expect(p.status("june") == .known) | ||
| 63 | } | ||
| 64 | } | ||
DayByDayTests/ProfileStoreTests.swift +28
| @@ -81,4 +81,32 @@ struct ProfileStoreTests { | |||
| 81 | #expect(records.map(\.chosenID) == ["days.monday", nil]) | 81 | #expect(records.map(\.chosenID) == ["days.monday", nil]) |
| 82 | #expect(records.map(\.gameRaw) == ["findIt", nil]) | 82 | #expect(records.map(\.gameRaw) == ["findIt", nil]) |
| 83 | } | 83 | } |
| 84 | |||
| 85 | @Test func storeReviewWeights() throws { | ||
| 86 | let s = store | ||
| 87 | let a = try s.create(name: "A", avatar: .cat, color: .red, birthday: nil) | ||
| 88 | let b = try s.create(name: "B", avatar: .dog, color: .blue, birthday: nil) | ||
| 89 | let monday = Category.days.item(DayOfWeek.monday) | ||
| 90 | s.record(monday, correct: false, for: a) | ||
| 91 | #expect(s.reviewWeights(for: a, category: .days) == [monday: 3]) | ||
| 92 | #expect(s.reviewWeights(for: b, category: .days).isEmpty) | ||
| 93 | #expect(s.progress(for: a, category: .days).status(monday.id) == .learning) | ||
| 94 | } | ||
| 95 | |||
| 96 | @Test func progressCountsOnlyRecognitionGames() throws { | ||
| 97 | let s = store | ||
| 98 | let a = try s.create(name: "A", avatar: .cat, color: .red, birthday: nil) | ||
| 99 | let b = Category.alphabet.item(LearnLetter.b) | ||
| 100 | s.record(b, game: .trace, correct: false, for: a) | ||
| 101 | let monday = Category.days.item(DayOfWeek.monday) | ||
| 102 | s.record(monday, game: .weekTrain, correct: true, for: a) | ||
| 103 | s.record(monday, game: .weekTrain, correct: true, for: a) | ||
| 104 | #expect(s.progress(for: a, category: .alphabet).status(b.id) == .new) | ||
| 105 | #expect(s.progress(for: a, category: .days).status(monday.id) == .new) | ||
| 106 | s.record(b, game: .findIt, correct: false, for: a) | ||
| 107 | #expect(s.progress(for: a, category: .alphabet).status(b.id) == .learning) | ||
| 108 | let june = Category.months.item(MonthOfYear.june) | ||
| 109 | s.record(june, correct: false, for: a) | ||
| 110 | #expect(s.progress(for: a, category: .months).status(june.id) == .learning) | ||
| 111 | } | ||
| 84 | } | 112 | } |
DayByDayTests/ProgressSummaryTests.swift added +68
| @@ -0,0 +1,68 @@ | |||
| 1 | import Foundation | ||
| 2 | import Testing | ||
| 3 | @testable import DayByDay | ||
| 4 | |||
| 5 | struct ProgressSummaryTests { | ||
| 6 | let start = Date(timeIntervalSince1970: 1_800_000_000) | ||
| 7 | |||
| 8 | /// Each known item gets two right answers; each missed item one miss. | ||
| 9 | func summary(_ category: DayByDay.Category, known: [Int] = [], missed: [Int] = [], extra: [AnswerFact] = []) -> ProgressSummary { | ||
| 10 | let items = category.quizItems | ||
| 11 | var facts = extra | ||
| 12 | for i in known { | ||
| 13 | facts += [AnswerFact(itemID: items[i].id, isCorrect: true, timestamp: start), | ||
| 14 | AnswerFact(itemID: items[i].id, isCorrect: true, timestamp: start + 300)] | ||
| 15 | } | ||
| 16 | for i in missed { | ||
| 17 | facts.append(AnswerFact(itemID: items[i].id, isCorrect: false, timestamp: start + Double(i))) | ||
| 18 | } | ||
| 19 | return ProgressSummary(category: category, progress: ItemProgress(facts)) | ||
| 20 | } | ||
| 21 | |||
| 22 | @Test func notPlayed() { | ||
| 23 | let s = summary(.food) | ||
| 24 | #expect(s.known == "Not played yet.") | ||
| 25 | #expect(s.missing == nil) | ||
| 26 | } | ||
| 27 | |||
| 28 | @Test func knowsAll() { | ||
| 29 | let s = summary(.seasons, known: [0, 1, 2, 3]) | ||
| 30 | #expect(s.known == "Knows all 4 seasons.") | ||
| 31 | #expect(s.missing == nil) | ||
| 32 | } | ||
| 33 | |||
| 34 | @Test func knowsSome() { | ||
| 35 | #expect(summary(.days, known: [0, 1, 2, 3, 4]).known == "Knows 5 of 7 days.") | ||
| 36 | #expect(summary(.weather, known: [0]).known?.hasSuffix("kinds of weather.") == true) | ||
| 37 | } | ||
| 38 | |||
| 39 | @Test func mixingUpList() { | ||
| 40 | let s = summary(.months, known: [0], missed: [7, 5, 6]) | ||
| 41 | #expect(s.known == "Knows 1 of 12 months.") | ||
| 42 | #expect(s.missing == "Still mixing up June, July and August.") | ||
| 43 | #expect(summary(.months, missed: [5]).known == nil) | ||
| 44 | } | ||
| 45 | |||
| 46 | @Test func twoItemsUseAnd() { | ||
| 47 | #expect(summary(.days, missed: [3, 4]).missing == "Still mixing up Wednesday and Thursday.") | ||
| 48 | #expect(summary(.days, missed: [4]).missing == "Still mixing up Thursday.") | ||
| 49 | } | ||
| 50 | |||
| 51 | @Test func longListTruncates() { | ||
| 52 | let s = summary(.alphabet, missed: [1, 3, 15, 16, 20, 21]) | ||
| 53 | #expect(s.missing == "Still mixing up B, D, P, Q and 2 more.") | ||
| 54 | } | ||
| 55 | |||
| 56 | @Test func unknownIDsIgnored() { | ||
| 57 | let old = [AnswerFact(itemID: "lizard", isCorrect: false, timestamp: start)] | ||
| 58 | #expect(summary(.animals, extra: old).known == "Not played yet.") | ||
| 59 | #expect(summary(.animals, known: [0], extra: old).missing == nil) | ||
| 60 | } | ||
| 61 | |||
| 62 | @Test func justStarted() { | ||
| 63 | let once = [AnswerFact(itemID: DayByDay.Category.days.quizItems[1].id, isCorrect: true, timestamp: start)] | ||
| 64 | let s = summary(.days, extra: once) | ||
| 65 | #expect(s.known == "Just started.") | ||
| 66 | #expect(s.missing == nil) | ||
| 67 | } | ||
| 68 | } | ||
DayByDayTests/ReviewExport.swift +13
| @@ -88,6 +88,18 @@ struct ReviewExport { | |||
| 88 | for (d, w) in [(1, LearnWeather.sunny), (2, .cloudy), (5, .rainy), (9, .windy), (12, .snowy), (15, .sunny)] { | 88 | for (d, w) in [(1, LearnWeather.sunny), (2, .cloudy), (5, .rainy), (9, .windy), (12, .snowy), (15, .sunny)] { |
| 89 | session.store.addSticker(for: maya, on: DateComponents(year: 2026, month: 10, day: d), weather: w) | 89 | session.store.addSticker(for: maya, on: DateComponents(year: 2026, month: 10, day: d), weather: w) |
| 90 | } | 90 | } |
| 91 | let days = DayByDay.Category.days.quizItems, months = DayByDay.Category.months.quizItems | ||
| 92 | let t = Date(timeIntervalSince1970: 1_800_000_000) | ||
| 93 | for (i, day) in days.enumerated() { | ||
| 94 | session.store.record(day, correct: true, for: maya, at: t + Double(i) * 300) | ||
| 95 | session.store.record(day, correct: true, for: maya, at: t + 5000 + Double(i) * 300) | ||
| 96 | } | ||
| 97 | for (i, month) in months.enumerated() where i < 5 { | ||
| 98 | session.store.record(month, correct: true, for: maya, at: t + Double(i) * 300) | ||
| 99 | session.store.record(month, correct: true, for: maya, at: t + 5000 + Double(i) * 300) | ||
| 100 | } | ||
| 101 | for month in months[5...7] { session.store.record(month, correct: false, for: maya, at: t + 9000) } | ||
| 102 | session.store.record(DayByDay.Category.alphabet.quizItems[1], correct: false, for: maya, at: t) | ||
| 91 | session.profilesChanged() | 103 | session.profilesChanged() |
| 92 | } | 104 | } |
| 93 | var rng = SeededGenerator(seed: 1) | 105 | var rng = SeededGenerator(seed: 1) |
| @@ -113,6 +125,7 @@ struct ReviewExport { | |||
| 113 | ("calendar", AnyView(StickerCalendarView(profile: session.current, now: DateComponents(calendar: .current, year: 2026, month: 10, day: 16).date!))), | 125 | ("calendar", AnyView(StickerCalendarView(profile: session.current, now: DateComponents(calendar: .current, year: 2026, month: 10, day: 16).date!))), |
| 114 | ("banner", AnyView(TodayBanner().padding())), | 126 | ("banner", AnyView(TodayBanner().padding())), |
| 115 | ("picker", AnyView(ProfilePickerView())), | 127 | ("picker", AnyView(ProfilePickerView())), |
| 128 | ("parent-progress", AnyView(NavigationStack { ChildProgressView(profile: session.profiles[0]) })), | ||
| 116 | ("welcome", AnyView(ProfileSetupView())), | 129 | ("welcome", AnyView(ProfileSetupView())), |
| 117 | ("finished", AnyView(FinishedView {})), | 130 | ("finished", AnyView(FinishedView {})), |
| 118 | ("find-days", AnyView(RoundView(source: DayByDay.Category.days.questionSource(for: .findIt), game: .findIt) { ChoiceTile(item: $0) })), | 131 | ("find-days", AnyView(RoundView(source: DayByDay.Category.days.questionSource(for: .findIt), game: .findIt) { ChoiceTile(item: $0) })), |
DayByDayTests/RoundTests.swift +49
| @@ -187,4 +187,53 @@ struct RoundTests { | |||
| 187 | #expect(round.current.answer != days[3]) | 187 | #expect(round.current.answer != days[3]) |
| 188 | } | 188 | } |
| 189 | } | 189 | } |
| 190 | |||
| 191 | @Test func weightedPickFavorsLearning() { | ||
| 192 | var weighted = picker | ||
| 193 | weighted.weights = [days[3]: 3] | ||
| 194 | var rng = SeededGenerator(seed: 70) | ||
| 195 | let hits = (0..<2000).filter { _ in weighted.next(correctCount: 0, excluding: nil, using: &rng).answer == days[3] }.count | ||
| 196 | // 3 of 9 shares, against 1 of 7 unweighted. | ||
| 197 | #expect((500...900).contains(hits)) | ||
| 198 | } | ||
| 199 | |||
| 200 | @Test func allLearningStillVaries() { | ||
| 201 | var weighted = picker | ||
| 202 | weighted.weights = Dictionary(uniqueKeysWithValues: days.map { ($0, 3.0) }) | ||
| 203 | var rng = SeededGenerator(seed: 71) | ||
| 204 | let answers = Set((0..<200).map { _ in weighted.next(correctCount: 0, excluding: nil, using: &rng).answer }) | ||
| 205 | #expect(answers.count == days.count) | ||
| 206 | } | ||
| 207 | |||
| 208 | @Test func weightsKeepPreviousRule() { | ||
| 209 | var weighted = picker | ||
| 210 | weighted.weights = [days[0]: 3] | ||
| 211 | var rng = SeededGenerator(seed: 72) | ||
| 212 | for _ in 0..<200 { | ||
| 213 | #expect(weighted.next(correctCount: 0, excluding: days[0], using: &rng).answer != days[0]) | ||
| 214 | } | ||
| 215 | var single = QuestionPicker(answers: [days[0]], distractors: days) { $0.name } | ||
| 216 | single.weights = [days[0]: 3] | ||
| 217 | #expect(single.next(correctCount: 0, excluding: days[0], using: &rng).answer == days[0]) | ||
| 218 | } | ||
| 219 | |||
| 220 | @Test func emptyWeightsMatchOldPicks() { | ||
| 221 | var a = SeededGenerator(seed: 73), b = SeededGenerator(seed: 73) | ||
| 222 | var unweighted = picker | ||
| 223 | unweighted.weights = [:] | ||
| 224 | for _ in 0..<50 { | ||
| 225 | #expect(picker.next(correctCount: 2, excluding: nil, using: &a) == unweighted.next(correctCount: 2, excluding: nil, using: &b)) | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | @Test func oddOneOutUsesWeights() { | ||
| 230 | guard var source = Category.animals.questionSource(for: .oddOneOut) as? OddOneOut else { | ||
| 231 | Issue.record("not odd one out"); return | ||
| 232 | } | ||
| 233 | let bird = Category.animals.item(LearnAnimal.bird) | ||
| 234 | source.weights = [bird: 50] | ||
| 235 | var rng = SeededGenerator(seed: 74) | ||
| 236 | let hits = (0..<400).filter { _ in source.next(correctCount: 0, excluding: nil, using: &rng).answer == bird }.count | ||
| 237 | #expect(hits > 120) | ||
| 238 | } | ||
| 190 | } | 239 | } |
docs/superpowers/plans/2026-10-16-part7-adaptive-review.md added +91
| @@ -0,0 +1,91 @@ | |||
| 1 | # Part 7: Adaptive Review and Parent Screen 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:** Missed items come up more often in "pick one" games until the child gets them right a few times, and a gated parent screen says per child and per topic what is known and what is still being missed, in plain words. | ||
| 6 | |||
| 7 | **Architecture:** A pure `ItemProgress` reads one child's answers in one topic and gives each item a status (new, learning, known) plus a pick weight. `QuestionPicker` and `OddOneOut` take optional weights and pick answers with them. `GameDestination` reads the current child's weights from the store and passes them in. A pure `ProgressSummary` turns statuses into sentences; `ChildProgressView` lists them per topic inside the existing parent area. | ||
| 8 | |||
| 9 | **Tech Stack:** SwiftUI, SwiftData, Swift Testing, Xcode 27. | ||
| 10 | |||
| 11 | **Spec:** `docs/superpowers/specs/2026-10-16-learning-games-design.md` (part 7). Builds on the part 1–6 plans in this folder. | ||
| 12 | |||
| 13 | ## Global Constraints | ||
| 14 | |||
| 15 | - iOS 17.5 target; `SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor`; pure logic `nonisolated`. | ||
| 16 | - No entity changes: everything is read from `AnswerRecord` (`itemID` is the question's answer, `isCorrect`, `timestamp`). No `VersionedSchema` needed in this part. | ||
| 17 | - An item's misses count against the question's answer. A correct answer within 60 s after a miss of the same item is the retry of that question and does not count toward getting it right. | ||
| 18 | - Learning: the item has a miss and fewer than 3 counted correct answers since its last miss. Known: no open miss and at least 2 counted correct answers. New: anything else. | ||
| 19 | - Pick weight: learning 3, otherwise 1. With no weights, `QuestionPicker` and `OddOneOut` pick exactly as before (same RNG use), so existing seeded tests are unchanged. | ||
| 20 | - "Never the previous answer when there is an alternative" still holds with weights. | ||
| 21 | - Parent copy is plain and neutral, never scolding: "Knows all 7 days.", "Knows 5 of 7 days.", "Still mixing up June, July and August.", "Not played yet." | ||
| 22 | - The parent screen is reached only from the gated parent area. | ||
| 23 | - Branch `feat/adaptive-review`. Every commit `Ref #N` (last `Closes #N`). No AI attribution. | ||
| 24 | - Test command: `xcodebuild test -project DayByDay.xcodeproj -scheme DayByDay -destination 'platform=iOS Simulator,name=iPhone 18 Pro'` (full suite ~10 min; run in background). | ||
| 25 | |||
| 26 | ## Review Focus | ||
| 27 | |||
| 28 | 1. The retry after a wrong tap (same question, seconds later) must not count as a correct answer toward clearing the miss. Test `retryDoesNotCount` in Task 1. | ||
| 29 | 2. A miss after earlier corrects reopens the item; corrects before the last miss do not count. Test `newMissReopens` in Task 1. | ||
| 30 | 3. Every item in learning (weights all 3) and a one-item pool still pick, and never return the previous answer when another exists. Tests `allLearningStillVaries`, `weightsKeepPreviousRule` in Task 2. | ||
| 31 | 4. Answers from items no longer in the topic (removed in an earlier part) are ignored, not listed by ID. Test `unknownIDsIgnored` in Task 3. | ||
| 32 | 5. Long missed lists stay readable: more than 4 items list the first 4 in topic order and "and N more". Test `longListTruncates` in Task 3. | ||
| 33 | |||
| 34 | --- | ||
| 35 | |||
| 36 | ### Task 0: Branch and issue | ||
| 37 | |||
| 38 | - [ ] `git checkout -b feat/adaptive-review main`; `gitbay issue create --title "Part 7: Adaptive review and parent screen" --json --file -` (#7); commit this plan with `Ref #N`. | ||
| 39 | |||
| 40 | ### Task 1: Item progress | ||
| 41 | |||
| 42 | **Files:** Create `DayByDay/Profiles/ItemProgress.swift`. Test `DayByDayTests/ItemProgressTests.swift`. | ||
| 43 | |||
| 44 | **Interfaces:** | ||
| 45 | - `nonisolated struct AnswerFact: Equatable { let itemID: String; let isCorrect: Bool; let timestamp: Date }`. | ||
| 46 | - `nonisolated enum ItemStatus: Equatable { case new, learning, known }`. | ||
| 47 | - `nonisolated struct ItemProgress { static let retryWindow: TimeInterval = 60; static let clearAfter = 3; static let knownAfter = 2; static let learningWeight = 3.0; private(set) var statuses: [String: ItemStatus]; private(set) var lastMiss: [String: Date]; init(_ facts: [AnswerFact]); func status(_ itemID: String) -> ItemStatus; func weight(_ itemID: String) -> Double }`. Facts may arrive in any order; sort by timestamp. | ||
| 48 | |||
| 49 | - [ ] Tests: `untouchedIsNew`, `twoRightIsKnown`, `oneRightIsNew`, `missIsLearning`, `threeRightClearsMiss` (miss, then 3 counted corrects → known), `retryDoesNotCount` (miss, correct 5 s later, 2 more corrects minutes apart → still learning; a third → known), `newMissReopens` (5 corrects, miss → learning), `weights` (learning 3, others 1), `unorderedInput`. RED, implement, GREEN, commit `feat: item progress from answers`. | ||
| 50 | |||
| 51 | ### Task 2: Weighted picks | ||
| 52 | |||
| 53 | **Files:** Modify `DayByDay/Quiz/Question.swift`, `DayByDay/Quiz/Game.swift`, `DayByDay/Quiz/GameDestination.swift`, `DayByDay/Profiles/ProfileStore.swift`. Test `DayByDayTests/RoundTests.swift`, `DayByDayTests/ProfileStoreTests.swift`. | ||
| 54 | |||
| 55 | **Interfaces:** | ||
| 56 | - `QuestionPicker` and `OddOneOut` gain `var weights: [QuizItem: Double] = [:]`. When non-empty, the answer is drawn from the same candidates by weight (default 1 for unlisted items); when empty, the existing `randomElement` call is kept. | ||
| 57 | - `nonisolated extension Array where Element == QuizItem { func weightedRandom(_ weights: [QuizItem: Double], using: inout some RandomNumberGenerator) -> QuizItem }` in `Question.swift`. | ||
| 58 | - `Category.questionSource(for: Game, weights: [QuizItem: Double] = [:]) -> any QuestionSource`: findIt, oddOneOut and listen get the weights; the other sources ignore them. | ||
| 59 | - `ProfileStore.progress(for: Profile, category: Category) -> ItemProgress` and `reviewWeights(for: Profile, category: Category) -> [QuizItem: Double]` (only items in `category.quizItems`). | ||
| 60 | - `GameDestination` reads `ProfileSession` and passes `reviewWeights` for the current child. | ||
| 61 | |||
| 62 | - [ ] Tests: `weightedPickFavorsLearning` (one of 7 days weighted 3: picked between 25% and 45% of 2000 seeded draws, against ~14% unweighted), `allLearningStillVaries`, `weightsKeepPreviousRule`, `emptyWeightsMatchOldPicks` (same seed, same answers with and without an empty map), `storeReviewWeights` (record a miss of Monday for one child → weight 3 for that child, 1 for a sibling). RED, implement, GREEN, commit `feat: missed items come up more often`. | ||
| 63 | |||
| 64 | ### Task 3: Progress summary | ||
| 65 | |||
| 66 | **Files:** Create `DayByDay/Parent/ProgressSummary.swift`. Test `DayByDayTests/ProgressSummaryTests.swift`. | ||
| 67 | |||
| 68 | **Interfaces:** | ||
| 69 | - `nonisolated extension Category { var progressNoun: String }`: days, months, seasons, numbers, colors, shapes, animals, letters, kinds of weather, body parts, foods. | ||
| 70 | - `nonisolated struct ProgressSummary { init(category: Category, progress: ItemProgress); let known: String?; let missing: String? }`; played means any of the topic's items has a status. Sentences: | ||
| 71 | - not played → `known = "Not played yet."`, `missing = nil`; | ||
| 72 | - all known → `"Knows all \(count) \(noun)."` (seasons: "Knows all 4 seasons."); | ||
| 73 | - some known → `"Knows \(k) of \(count) \(noun)."`; none known → nil; | ||
| 74 | - learning items in topic order → `"Still mixing up A, B and C."`; more than 4 → first 4 then `"and \(n) more."` ("Still mixing up A, B, C, D and 2 more."). | ||
| 75 | - Item names come from `category.quizItems`; unknown IDs are ignored. | ||
| 76 | |||
| 77 | - [ ] Tests: `notPlayed`, `knowsAll`, `knowsSome`, `mixingUpList` (June, July, August), `twoItemsUseAnd`, `longListTruncates`, `unknownIDsIgnored`. RED, implement, GREEN, commit `feat: progress summary`. | ||
| 78 | |||
| 79 | ### Task 4: Parent progress screen | ||
| 80 | |||
| 81 | **Files:** Create `DayByDay/Parent/ChildProgressView.swift`. Modify `DayByDay/Parent/ParentAreaView.swift`. | ||
| 82 | |||
| 83 | **Interfaces:** | ||
| 84 | - `ParentAreaView`: a "Progress" section with one `NavigationLink` per child (`ProfileRow`) to `ChildProgressView(profile:)`. | ||
| 85 | - `ChildProgressView(profile:)`: `List` of `Category.gridCases`, each row the topic symbol in a circle of its color (white symbol), the topic label, and the summary's lines (missing line in `.secondary` weight semibold). Title "\(name)'s progress". | ||
| 86 | |||
| 87 | - [ ] Add `parent-progress` to `ReviewExport` with a seeded child who has known days, missed months and no food answers. Build, render light and dark, commit `feat: parent progress screen`. | ||
| 88 | |||
| 89 | ### Task 5: Reviews, MR, merge | ||
| 90 | |||
| 91 | - [ ] Fresh whole-branch code reviewer with this Review Focus. Fresh reviewer for the parent screen render and copy (plain, neutral, readable in both modes). One fix pass, RED→GREEN. Full suite green. Push, MR `Closes #N`, squash-merge, delete the branch locally and on the remote. | ||