Glimmer / app · glimmertoday.ai / native / Glimmer / GlimmerCalendarGrid.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | /// A month grid uses Glimmer's selection and focus colors on every supported platform. |
| 4 | struct GlimmerCalendarGrid: View { |
| 5 | @Binding var selection: Date |
| 6 | var latestDay: Date? = nil |
| 7 | @Environment(\.calendar) private var calendar |
| 8 | @Environment(\.locale) private var locale |
| 9 | @Environment(\.readingTheme) private var theme |
| 10 | @State private var displayedMonth: Date? |
| 11 | @FocusState private var focusedDay: Date? |
| 12 | |
| 13 | private var month: Date { displayedMonth ?? monthStart(selection) } |
| 14 | private var columns: [GridItem] { Array(repeating: GridItem(.flexible(), spacing: 2), count: 7) } |
| 15 | |
| 16 | private var weekdayNames: [String] { |
| 17 | var localized = calendar |
| 18 | localized.locale = locale |
| 19 | let symbols = localized.veryShortStandaloneWeekdaySymbols |
| 20 | let offset = calendar.firstWeekday - 1 |
| 21 | return Array(symbols[offset...] + symbols[..<offset]) |
| 22 | } |
| 23 | |
| 24 | private var days: [Date?] { |
| 25 | guard let range = calendar.range(of: .day, in: .month, for: month) else { return [] } |
| 26 | let weekday = calendar.component(.weekday, from: month) |
| 27 | let leading = (weekday - calendar.firstWeekday + 7) % 7 |
| 28 | let cells = Array<Date?>(repeating: nil, count: leading) |
| 29 | + range.map { calendar.date(byAdding: .day, value: $0 - range.lowerBound, to: month) } |
| 30 | return cells + Array<Date?>(repeating: nil, count: max(0, 42 - cells.count)) |
| 31 | } |
| 32 | |
| 33 | var body: some View { |
| 34 | VStack(spacing: 8) { |
| 35 | HStack(spacing: 6) { |
| 36 | Text(month.formatted(.dateTime.month(.wide).year().locale(locale))) |
| 37 | .font(.headline) |
| 38 | .accessibilityAddTraits(.isHeader) |
| 39 | .accessibilityIdentifier("calendar.month") |
| 40 | Spacer(minLength: 4) |
| 41 | if month != monthStart(.now) { |
| 42 | Button("Today") { displayedMonth = monthStart(.now) } |
| 43 | .font(.callout.weight(.medium)) |
| 44 | .foregroundStyle(theme.selectionInk) |
| 45 | .buttonStyle(.plain) |
| 46 | .padding(.vertical, 8) |
| 47 | } |
| 48 | arrow(-1, title: "Previous month", glyph: "chevron.left", enabled: true) |
| 49 | arrow(1, title: "Next month", glyph: "chevron.right", enabled: latestDay.map { month < monthStart($0) } ?? true) |
| 50 | } |
| 51 | LazyVGrid(columns: columns, spacing: 2) { |
| 52 | ForEach(Array(weekdayNames.enumerated()), id: \.offset) { _, text in |
| 53 | Text(text).font(.caption.weight(.medium)).foregroundStyle(.secondary) |
| 54 | .accessibilityHidden(true) |
| 55 | } |
| 56 | ForEach(Array(days.enumerated()), id: \.offset) { _, date in |
| 57 | if let date { day(date) } else { Color.clear.frame(minHeight: 34).accessibilityHidden(true) } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private func arrow(_ step: Int, title: String, glyph: String, enabled: Bool) -> some View { |
| 64 | Button { |
| 65 | if let next = calendar.date(byAdding: .month, value: step, to: month) { displayedMonth = next } |
| 66 | } label: { |
| 67 | Label(title, systemImage: glyph).labelStyle(.iconOnly).font(.subheadline.weight(.semibold)) |
| 68 | .foregroundStyle(enabled ? theme.selectionInk : Color.secondary.opacity(0.45)) |
| 69 | .frame(width: 42, height: 42) |
| 70 | .contentShape(.rect) |
| 71 | } |
| 72 | .buttonStyle(.plain) |
| 73 | .disabled(!enabled) |
| 74 | } |
| 75 | |
| 76 | private func day(_ date: Date) -> some View { |
| 77 | let isSelected = calendar.isDate(date, inSameDayAs: selection) |
| 78 | let isToday = calendar.isDateInToday(date) |
| 79 | let isEnabled = latestDay.map { date <= $0 } ?? true |
| 80 | return Button { |
| 81 | selection = date |
| 82 | } label: { |
| 83 | VStack(spacing: 1) { |
| 84 | Text(date.formatted(.dateTime.day().locale(locale))) |
| 85 | .font(.callout.monospacedDigit().weight(isSelected ? .semibold : .medium)) |
| 86 | Circle() |
| 87 | .frame(width: 3, height: 3) |
| 88 | .opacity(isToday ? 1 : 0) |
| 89 | .accessibilityHidden(true) |
| 90 | } |
| 91 | .foregroundStyle(isSelected ? theme.selectionOnInk : isEnabled ? Color.primary : Color.secondary.opacity(0.58)) |
| 92 | .frame(width: 32, height: 32) |
| 93 | .background(isSelected ? theme.selectionInk : Color.clear, in: .circle) |
| 94 | .overlay { |
| 95 | Circle().strokeBorder(focusedDay == date ? theme.selectionInk : .clear, lineWidth: 2) |
| 96 | .padding(-3) |
| 97 | } |
| 98 | .frame(maxWidth: .infinity, minHeight: 40) |
| 99 | .contentShape(.rect) |
| 100 | } |
| 101 | .buttonStyle(.plain) |
| 102 | .disabled(!isEnabled) |
| 103 | .focused($focusedDay, equals: date) |
| 104 | .focusEffectDisabled() |
| 105 | .accessibilityLabel(date.formatted(date: .complete, time: .omitted)) |
| 106 | .accessibilityAddTraits(isSelected ? .isSelected : []) |
| 107 | } |
| 108 | |
| 109 | private func monthStart(_ date: Date) -> Date { |
| 110 | calendar.dateInterval(of: .month, for: date)?.start ?? date |
| 111 | } |
| 112 | } |