krz/daybyday

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

DayByDay/Quiz/ChoiceTile.swift

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

148 lines · 4873 bytes

6 symbols in this file
  1//
  2//  ChoiceTile.swift
  3//  DayByDay
  4//
  5
  6import SwiftUI
  7
  8enum ChoiceStyle {
  9    /// Dark enough that every food emoji stands out.
 10    static let foodFill = Color(red: 0.24, green: 0.16, blue: 0.10)
 11    /// Edge for color swatches, visible on light and dark backgrounds.
 12    static let swatchOutline = Color(white: 0.45)
 13}
 14
 15/// One answer choice in a game. Flat fills only: white content sits on the
 16/// topic color, which is at least 3:1 against white.
 17struct ChoiceTile: View {
 18    let item: QuizItem
 19
 20    @Environment(\.colorScheme) private var colorScheme
 21
 22    private static let shape = RoundedRectangle(cornerRadius: 32, style: .continuous)
 23
 24    var body: some View {
 25        content
 26            .frame(maxWidth: .infinity, maxHeight: .infinity)
 27            .padding(12)
 28            .background(Self.shape.fill(fill))
 29            .overlay {
 30                if item.category == .colors {
 31                    Self.shape.strokeBorder(ChoiceStyle.swatchOutline, lineWidth: 4)
 32                } else if item.category == .food, colorScheme == .dark {
 33                    Self.shape.strokeBorder(.white.opacity(0.25), lineWidth: 2)
 34                }
 35            }
 36            .contentShape(Self.shape)
 37    }
 38
 39    private var fill: Color {
 40        switch item.category {
 41        case .colors: item.value(LearnColor.self)!.color
 42        case .food:   ChoiceStyle.foodFill
 43        default:      item.category.color
 44        }
 45    }
 46
 47    @ViewBuilder
 48    private var content: some View {
 49        switch item.category {
 50        case .colors:
 51            Color.clear
 52        case .food:
 53            Text(item.value(LearnFood.self)!.emoji)
 54                .font(.system(size: 80))
 55        case .animals:
 56            AnimalIcon(animal: item.value(LearnAnimal.self)!)
 57                .font(.system(size: 72))
 58                .frame(width: 100, height: 100)
 59                .foregroundStyle(.white)
 60        case .weather:
 61            symbol(item.value(LearnWeather.self)!.symbol)
 62        case .bodyParts:
 63            symbol(item.value(LearnBodyPart.self)!.symbol)
 64        case .shapes:
 65            let shape = item.value(LearnShape.self)!
 66            shape.shapeView
 67                .frame(width: 90, height: 90)
 68                .scaleEffect([.star, .heart].contains(shape) ? 1.15 : 1)
 69        case .alphabet:
 70            Text(item.value(LearnLetter.self)!.character)
 71                .font(.heading(80, weight: .heavy))
 72                .foregroundStyle(.white)
 73        case .numbers:
 74            let number = item.value(LearnNumber.self)!
 75            VStack(spacing: 8) {
 76                Text("\(number.numeral)")
 77                    .font(.heading(56, weight: .heavy))
 78                Dots(count: number.numeral)
 79            }
 80            .foregroundStyle(.white)
 81        case .days:
 82            labeled(item.value(DayOfWeek.self)!.symbol)
 83        case .months:
 84            labeled(item.value(MonthOfYear.self)!.symbol)
 85        case .seasons:
 86            labeled(item.value(Season.self)!.symbol)
 87        case .today:
 88            EmptyView()
 89        }
 90    }
 91
 92    private func symbol(_ name: String) -> some View {
 93        Image(systemName: name)
 94            .font(.system(size: 72))
 95            .foregroundStyle(.white)
 96    }
 97
 98    private func labeled(_ name: String) -> some View {
 99        VStack(spacing: 10) {
100            Image(systemName: name)
101                .font(.system(size: 52))
102                .frame(height: 60)
103            Text(item.name)
104                .font(.heading(22))
105        }
106        .foregroundStyle(.white)
107    }
108}
109
110/// A pile for "Which has the most?": dots only, so the child compares amounts.
111struct PileTile: View {
112    let item: QuizItem
113
114    var body: some View {
115        Dots(count: item.value(LearnNumber.self)!.numeral, size: 22)
116            .foregroundStyle(.white)
117            .frame(maxWidth: .infinity, maxHeight: .infinity)
118            .background(RoundedRectangle(cornerRadius: 32, style: .continuous).fill(Category.numbers.color))
119            .contentShape(RoundedRectangle(cornerRadius: 32, style: .continuous))
120    }
121}
122
123/// Solid dots for a quantity, in rows of five.
124struct Dots: View {
125    let count: Int
126    var size: CGFloat = 16
127
128    var body: some View {
129        VStack(spacing: 6) {
130            ForEach(Array(stride(from: 0, to: count, by: 5)), id: \.self) { start in
131                HStack(spacing: 6) {
132                    ForEach(start..<min(start + 5, count), id: \.self) { _ in
133                        Circle().frame(width: size, height: size)
134                    }
135                }
136            }
137        }
138    }
139}
140
141#Preview {
142    LazyVGrid(columns: [GridItem(.adaptive(minimum: 150), spacing: 20)], spacing: 20) {
143        ForEach([Category.days, .colors, .food, .animals, .numbers, .shapes, .alphabet, .weather], id: \.self) { c in
144            ChoiceTile(item: c.quizItems[1]).frame(height: 160)
145        }
146    }
147    .padding()
148}