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/

AnimalCard.swift

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

119 lines · 3748 bytes

1 symbol in this file
  1//
  2//  AnimalCard.swift
  3//  DayByDay
  4//
  5
  6import SwiftUI
  7
  8/// A single large, colorful card representing one animal.
  9/// Tapping the card plays a bounce animation and speaks the animal name aloud.
 10struct AnimalCard: View {
 11    let animal: LearnAnimal
 12
 13    var body: some View {
 14        TapCard(color: animal.color, label: animal.name) {
 15            CardVoice.shared.say(animal.cardLine, after: animal.sound)
 16        } content: {
 17            VStack(spacing: 16) {
 18                AnimalIcon(animal: animal)
 19                    .font(.system(size: 64))
 20                    .frame(height: 72)
 21                    .foregroundStyle(.white)
 22
 23                Text(animal.name)
 24                    .font(.heading(32))
 25                    .foregroundStyle(.white)
 26            }
 27            .padding(16)
 28        }
 29    }
 30}
 31
 32// MARK: - Animal model
 33
 34nonisolated enum LearnAnimal: Int, CaseIterable, Identifiable {
 35    case cat, dog, bird, fish, rabbit, turtle, ladybug, cow, duck, sheep, pig
 36
 37    var id: Int { rawValue }
 38
 39    var name: String {
 40        switch self {
 41        case .cat:       "Cat"
 42        case .dog:       "Dog"
 43        case .bird:      "Bird"
 44        case .fish:      "Fish"
 45        case .rabbit:    "Rabbit"
 46        case .turtle:    "Turtle"
 47        case .ladybug:   "Ladybug"
 48        case .cow:       "Cow"
 49        case .duck:      "Duck"
 50        case .sheep:     "Sheep"
 51        case .pig:       "Pig"
 52        }
 53    }
 54
 55    /// Nil for animals without an SF Symbol; `AnimalIcon` draws those.
 56    var symbol: String? {
 57        switch self {
 58        case .cat:       "cat.fill"
 59        case .dog:       "dog.fill"
 60        case .bird:      "bird.fill"
 61        case .fish:      "fish.fill"
 62        case .rabbit:    "hare.fill"
 63        case .turtle:    "tortoise.fill"
 64        case .ladybug:   "ladybug.fill"
 65        case .cow, .duck, .sheep, .pig: nil
 66        }
 67    }
 68
 69    /// At least 3:1 against the white icon and name.
 70    var color: Color {
 71        switch self {
 72        case .cat:       Color(red: 0.80, green: 0.42, blue: 0.08)  // orange
 73        case .dog:       Color(red: 0.55, green: 0.38, blue: 0.22)  // brown
 74        case .bird:      Color(red: 0.10, green: 0.48, blue: 0.78)  // sky blue
 75        case .fish:      Color(red: 0.0, green: 0.53, blue: 0.56)   // teal
 76        case .rabbit:    Color(red: 0.55, green: 0.40, blue: 0.70)  // lavender
 77        case .turtle:    Color(red: 0.18, green: 0.56, blue: 0.28)  // green
 78        case .ladybug:   Color(red: 0.86, green: 0.20, blue: 0.22)  // red
 79        case .cow:       Color(red: 0.35, green: 0.38, blue: 0.45)  // slate
 80        case .duck:      Color(red: 0.66, green: 0.50, blue: 0.0)   // mustard
 81        case .sheep:     Color(red: 0.40, green: 0.45, blue: 0.55)  // blue gray
 82        case .pig:       Color(red: 0.82, green: 0.25, blue: 0.52)  // pink
 83        }
 84    }
 85
 86    var fact: String {
 87        switch self {
 88        case .cat:     "Cats say meow."
 89        case .dog:     "Dogs say woof."
 90        case .bird:    "Birds can fly."
 91        case .fish:    "Fish swim in water."
 92        case .rabbit:  "Rabbits hop."
 93        case .turtle:  "Turtles carry their shell."
 94        case .ladybug: "Ladybugs have spots."
 95        case .cow:     "Cows say moo."
 96        case .duck:    "Ducks say quack."
 97        case .sheep:   "Sheep say baa."
 98        case .pig:     "Pigs say oink."
 99        }
100    }
101
102    /// Said when the card is tapped, after the animal's sound.
103    var cardLine: String { "\(name)! \(fact)" }
104
105    var canFly: Bool {
106        switch self {
107        case .bird, .ladybug, .duck: true
108        default: false
109        }
110    }
111}
112
113#Preview {
114    HStack(spacing: 24) {
115        AnimalCard(animal: .cat)
116            .frame(width: 200, height: 260)
117    }
118    .padding()
119}