DayByDay/TapCard.swift
90 lines · 2872 bytes
2 symbols in this file
1//
2// TapCard.swift
3// DayByDay
4//
5
6import SwiftUI
7
8/// The rounded, colored card shared by every learning category.
9/// Tapping it plays a bounce animation and runs `onTap`, which speaks the card.
10/// A highlighted card (today's day, month, or season) is enlarged, glows, and has a white border.
11struct TapCard<Content: View, Badge: View>: View {
12 let color: Color
13 let label: String
14 var isHighlighted = false
15 let onTap: () -> Void
16 @ViewBuilder let content: () -> Content
17 @ViewBuilder var badge: () -> Badge
18 @State private var isTapped = false
19
20 private var cardScale: CGFloat {
21 if isTapped { return 1.12 }
22 if isHighlighted { return 1.04 }
23 return 1.0
24 }
25
26 var body: some View {
27 ZStack {
28 RoundedRectangle(cornerRadius: 32, style: .continuous)
29 .fill(color)
30 .shadow(color: color.opacity(0.4), radius: 8, y: 4)
31 // Glow on the card only: on the label it would lower the contrast.
32 .shadow(color: isHighlighted ? .white.opacity(0.8) : .clear, radius: 12)
33
34 content()
35 }
36 .overlay(alignment: .topTrailing) { badge() }
37 .overlay {
38 if isHighlighted {
39 RoundedRectangle(cornerRadius: 32, style: .continuous)
40 .strokeBorder(.white, lineWidth: 5)
41 }
42 }
43 .scaleEffect(cardScale)
44 .contentShape(RoundedRectangle(cornerRadius: 32, style: .continuous))
45 .rotation3DEffect(.degrees(isTapped ? 6 : 0), axis: (x: 1, y: 0, z: 0))
46 .animation(.spring(response: 0.35, dampingFraction: 0.5), value: isTapped)
47 .onTapGesture {
48 isTapped = true
49 onTap()
50 DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
51 isTapped = false
52 }
53 }
54 .accessibilityElement(children: .ignore)
55 .accessibilityLabel(label)
56 .accessibilityAddTraits(.isButton)
57 }
58}
59
60extension TapCard where Badge == EmptyView {
61 init(
62 color: Color,
63 label: String,
64 isHighlighted: Bool = false,
65 onTap: @escaping () -> Void,
66 @ViewBuilder content: @escaping () -> Content
67 ) {
68 self.init(color: color, label: label, isHighlighted: isHighlighted, onTap: onTap, content: content, badge: { EmptyView() })
69 }
70}
71
72/// An SF Symbol above a name, the content of most category cards.
73struct SymbolLabel: View {
74 let symbol: String
75 let name: String
76
77 var body: some View {
78 VStack(spacing: 16) {
79 Image(systemName: symbol)
80 .font(.system(size: 64))
81 .foregroundStyle(.white)
82 .symbolRenderingMode(.hierarchical)
83
84 Text(name)
85 .font(.heading(32))
86 .foregroundStyle(.white)
87 }
88 .padding(16)
89 }
90}