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/

LetterCard.swift

062549ad26607ce9e973ed2a9a7ef6f853885f83
daybyday/LetterCard.swift history · blame · raw

79 lines · 2617 bytes

1 symbol in this file
 1//
 2//  LetterCard.swift
 3//  DayByDay
 4//
 5
 6import SwiftUI
 7
 8/// A single large, colorful card representing one letter of the alphabet.
 9/// Tapping the card plays a bounce animation and speaks the letter aloud.
10struct LetterCard: View {
11    let letter: LearnLetter
12
13    var body: some View {
14        TapCard(color: letter.color, label: letter.spokenName) {
15            CardVoice.shared.say(letter.cardLine, after: nil)
16        } content: {
17            Text(letter.character)
18                .font(.heading(64, weight: .heavy))
19                .foregroundStyle(.white)
20        }
21    }
22}
23
24// MARK: - Letter model
25
26nonisolated enum LearnLetter: Int, CaseIterable, Identifiable {
27    case a, b, c, d, e, f, g, h, i, j, k, l, m,
28         n, o, p, q, r, s, t, u, v, w, x, y, z
29
30    var id: Int { rawValue }
31
32    var character: String {
33        String(Character(UnicodeScalar(65 + rawValue)!))
34    }
35
36    /// Speak the letter name clearly for a child.
37    /// Lowercase prevents TTS from saying "Capital A".
38    var spokenName: String {
39        character.lowercased()
40    }
41
42    /// A word whose first sound is this letter's sound. X has none that a
43    /// child would know, so its word ends with the sound instead.
44    var exampleWord: String {
45        ["apple", "ball", "cat", "dog", "egg", "fish", "goat", "hat", "igloo",
46         "jam", "kite", "lion", "moon", "nest", "octopus", "pig", "queen", "rain",
47         "sun", "tiger", "umbrella", "van", "web", "fox", "yo-yo", "zebra"][rawValue]
48    }
49
50    /// Said when the card is tapped.
51    var cardLine: String { "\(spokenName). \(spokenName) is for \(exampleWord)." }
52
53    /// Cycle through 8 colors, each at least 3:1 against the white letter.
54    var color: Color {
55        let palette: [Color] = [
56            Color(red: 0.86, green: 0.20, blue: 0.22),  // red
57            Color(red: 0.80, green: 0.42, blue: 0.08),  // orange
58            Color(red: 0.66, green: 0.50, blue: 0.0),   // mustard
59            Color(red: 0.18, green: 0.56, blue: 0.28),  // green
60            Color(red: 0.0, green: 0.42, blue: 0.85),   // blue
61            Color(red: 0.55, green: 0.4, blue: 0.85),   // purple
62            Color(red: 0.82, green: 0.25, blue: 0.52),  // pink
63            Color(red: 0.0, green: 0.53, blue: 0.56),   // teal
64        ]
65        return palette[rawValue % palette.count]
66    }
67}
68
69#Preview {
70    HStack(spacing: 24) {
71        LetterCard(letter: .a)
72            .frame(width: 140, height: 160)
73        LetterCard(letter: .b)
74            .frame(width: 140, height: 160)
75        LetterCard(letter: .c)
76            .frame(width: 140, height: 160)
77    }
78    .padding()
79}