DayByDay/Today/StickerCalendarView.swift
132 lines · 5103 bytes
5 symbols in this file
1//
2// StickerCalendarView.swift
3// DayByDay
4//
5
6import SwiftUI
7
8enum StickerStyle {
9 /// White weather symbols sit on this, at least 3:1.
10 static let fill = Category.weather.color
11}
12
13/// A sticker in the weather of that day.
14struct StickerBadge: View {
15 let weather: LearnWeather
16
17 var body: some View {
18 Circle()
19 .fill(StickerStyle.fill)
20 .overlay {
21 Image(systemName: weather.symbol)
22 .resizable()
23 .scaledToFit()
24 .foregroundStyle(.white)
25 .padding(5)
26 }
27 .accessibilityLabel(weather.name)
28 }
29}
30
31/// One month of a child's stickers. Arrows go back through earlier months,
32/// never past the current one.
33struct StickerCalendarView: View {
34 let profile: Profile?
35
36 @Environment(ProfileSession.self) private var session
37 @State private var grid: MonthGrid
38 private let today: DateComponents
39 private let calendar: Calendar
40
41 init(profile: Profile?, calendar: Calendar = AppCalendar.current, now: Date = .now) {
42 self.profile = profile
43 self.calendar = calendar
44 today = calendar.dateComponents([.year, .month, .day], from: now)
45 _grid = State(initialValue: MonthGrid(year: today.year!, month: today.month!, calendar: calendar))
46 }
47
48 private var isCurrentMonth: Bool { grid.year == today.year && grid.month == today.month }
49 private var canGoLater: Bool { grid.canGoLater(than: (year: today.year!, month: today.month!)) }
50 private var monthName: String { MonthOfYear(rawValue: grid.month - 1)!.name }
51
52 private var stickers: [Int: Sticker] {
53 guard let profile else { return [:] }
54 return Dictionary(session.store.stickers(for: profile, year: grid.year, month: grid.month).map { ($0.day, $0) },
55 uniquingKeysWith: { a, _ in a })
56 }
57
58 var body: some View {
59 let stickers = self.stickers
60 VStack(spacing: 16) {
61 HStack {
62 arrow("chevron.left", label: "Earlier month") { grid = grid.shifted(by: -1) }
63 Spacer()
64 Text(verbatim: "\(monthName) \(grid.year)")
65 .font(.heading(28, weight: .heavy))
66 Spacer()
67 arrow("chevron.right", label: "Later month") { grid = grid.shifted(by: 1) }
68 .disabled(!canGoLater)
69 .opacity(canGoLater ? 1 : 0.3)
70 }
71 LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 6), count: 7), spacing: 6) {
72 ForEach(Array(["S", "M", "T", "W", "T", "F", "S"].enumerated()), id: \.offset) { _, d in
73 Text(d).font(.heading(16)).foregroundStyle(.primary)
74 .accessibilityHidden(true)
75 }
76 ForEach(Array(grid.cells.enumerated()), id: \.offset) { _, day in
77 if let day {
78 cell(day, sticker: stickers[day])
79 } else {
80 Color.clear.frame(height: 48)
81 }
82 }
83 }
84 // Seven columns leave no room for larger dates; VoiceOver reads each one in full.
85 .dynamicTypeSize(...DynamicTypeSize.accessibility1)
86 }
87 .padding(16)
88 }
89
90 private func cell(_ day: Int, sticker: Sticker?) -> some View {
91 let isToday = isCurrentMonth && day == today.day
92 return ZStack {
93 if let sticker {
94 StickerBadge(weather: sticker.weather)
95 .padding(isToday ? 5 : 0)
96 .overlay(alignment: .bottomTrailing) {
97 // Keep the date readable on a stickered day.
98 Text("\(day)")
99 .font(.heading(11))
100 .padding(3)
101 .background(Circle().fill(Color(.systemBackground)))
102 }
103 } else {
104 Text("\(day)").font(.system(.body, design: .rounded, weight: .semibold))
105 }
106 }
107 .frame(height: 48)
108 .frame(maxWidth: .infinity)
109 .overlay {
110 if isToday {
111 Circle().strokeBorder(Color.primary, lineWidth: 4).frame(width: 52, height: 52)
112 }
113 }
114 .contentShape(Rectangle())
115 .onTapGesture {
116 let line = Lines.calendarDay(MonthOfYear(rawValue: grid.month - 1)!, day, weather: sticker?.weather)
117 CardVoice.shared.say(line, after: nil)
118 }
119 .accessibilityLabel(sticker.map { "\(monthName) \(day), \($0.weather.name)" } ?? "\(monthName) \(day)")
120 }
121
122 private func arrow(_ symbol: String, label: String, action: @escaping () -> Void) -> some View {
123 Button(action: action) {
124 Image(systemName: symbol)
125 .font(.system(size: 26, weight: .bold))
126 .frame(width: 56, height: 56)
127 .background(Circle().fill(Color(.systemGray6)))
128 }
129 .buttonStyle(.plain)
130 .accessibilityLabel(label)
131 }
132}