ShapeCard.swift
124 lines · 3718 bytes
3 symbols in this file
1//
2// ShapeCard.swift
3// DayByDay
4//
5
6import SwiftUI
7
8/// A single large, colorful card representing one shape.
9/// Tapping the card plays a bounce animation and speaks the shape name aloud.
10struct ShapeCard: View {
11 let shape: LearnShape
12
13 var body: some View {
14 TapCard(color: shape.color, label: shape.name) {
15 SpeechSynthesizer.shared.speak(shape.name)
16 } content: {
17 VStack(spacing: 16) {
18 shape.shapeView
19 .frame(width: 80, height: 80)
20 .foregroundStyle(.white)
21
22 Text(shape.name)
23 .font(.heading(32))
24 .foregroundStyle(.white)
25 }
26 .padding(16)
27 }
28 }
29}
30
31// MARK: - Shape model
32
33nonisolated enum LearnShape: Int, CaseIterable, Identifiable {
34 case circle, square, triangle, star, heart, oval, diamond, rectangle
35
36 var id: Int { rawValue }
37
38 var name: String {
39 switch self {
40 case .circle: "Circle"
41 case .square: "Square"
42 case .triangle: "Triangle"
43 case .star: "Star"
44 case .heart: "Heart"
45 case .oval: "Oval"
46 case .diamond: "Diamond"
47 case .rectangle: "Rectangle"
48 }
49 }
50
51 /// Nil where a child could reasonably answer either way (a heart's point).
52 var hasCorners: Bool? {
53 switch self {
54 case .circle, .oval: false
55 case .heart: nil
56 default: true
57 }
58 }
59
60 @MainActor @ViewBuilder
61 var shapeView: some View {
62 switch self {
63 case .circle:
64 Circle().fill(.white)
65 case .square:
66 SwiftUI.Rectangle().fill(.white)
67 case .triangle:
68 TriangleShape().fill(.white)
69 case .star:
70 Image(systemName: "star.fill")
71 .font(.system(size: 72))
72 .foregroundStyle(.white)
73 case .heart:
74 Image(systemName: "heart.fill")
75 .font(.system(size: 72))
76 .foregroundStyle(.white)
77 case .oval:
78 Ellipse().fill(.white)
79 .frame(width: 90, height: 60)
80 case .diamond:
81 SwiftUI.Rectangle().fill(.white)
82 .frame(width: 60, height: 60)
83 .rotationEffect(.degrees(45))
84 case .rectangle:
85 SwiftUI.Rectangle().fill(.white)
86 .frame(width: 90, height: 55)
87 }
88 }
89
90 var color: Color {
91 switch self {
92 case .circle: Color(red: 0.9, green: 0.3, blue: 0.3) // red
93 case .square: Color(red: 0.3, green: 0.5, blue: 0.9) // blue
94 case .triangle: Color(red: 0.29, green: 0.62, blue: 0.38) // green
95 case .star: Color(red: 0.7, green: 0.52, blue: 0.11) // gold
96 case .heart: Color(red: 0.9, green: 0.35, blue: 0.5) // pink
97 case .oval: Color(red: 0.55, green: 0.4, blue: 0.85) // purple
98 case .diamond: Color(red: 0.16, green: 0.6, blue: 0.64) // teal
99 case .rectangle: Color(red: 0.83, green: 0.45, blue: 0.16) // orange
100 }
101 }
102}
103
104/// A simple triangle drawn with a Path.
105struct TriangleShape: Shape {
106 func path(in rect: CGRect) -> Path {
107 Path { path in
108 path.move(to: CGPoint(x: rect.midX, y: rect.minY))
109 path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
110 path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
111 path.closeSubpath()
112 }
113 }
114}
115
116#Preview {
117 HStack(spacing: 24) {
118 ShapeCard(shape: .triangle)
119 .frame(width: 200, height: 260)
120 ShapeCard(shape: .star)
121 .frame(width: 200, height: 260)
122 }
123 .padding()
124}