DayByDay/Parent/ProgressSummary.swift
61 lines · 2171 bytes
1 symbol in this file
1//
2// ProgressSummary.swift
3// DayByDay
4//
5
6nonisolated 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.
26nonisolated 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}