| 1 | import RastrilloApple |
| 2 | import SwiftUI |
| 3 | |
| 4 | @MainActor public struct CalendarWorkspace: View { |
| 5 | public let client: NativeHTTP |
| 6 | public let signOut: () -> Void |
| 7 | private let accounts: (() -> Void)? |
| 8 | @StateObject private var model: CalendarModel |
| 9 | @State private var quickAdding = false |
| 10 | @State private var settings = false |
| 11 | @State private var editing: CalendarEvent? |
| 12 | @State private var pendingEdit: CalendarEvent? |
| 13 | @State private var inspecting: CalendarEvent? |
| 14 | @State private var filter: Int64? = 0 |
| 15 | @State private var column: NavigationSplitViewColumn = .detail |
| 16 | @Environment(\.dynamicTypeSize) private var typeSize |
| 17 | private let accent = Color(red: 0.78, green: 0.22, blue: 0.29) |
| 18 | public init(client: NativeHTTP, model: CalendarModel? = nil, accounts: (() -> Void)? = nil, signOut: @escaping () -> Void) { self.client = client; self.signOut = signOut; self.accounts = accounts; _model = StateObject(wrappedValue: model ?? CalendarModel()) } |
| 19 | |
| 20 | public var body: some View { |
| 21 | NavigationSplitView(preferredCompactColumn: $column) { |
| 22 | List(selection: $filter) { |
| 23 | Section("Calendars") { |
| 24 | Label("All calendars", systemImage: "calendar").tag(Int64(0)) |
| 25 | ForEach(model.calendars) { calendar in |
| 26 | Label(calendar.name, systemImage: "circle.fill") |
| 27 | .listItemTint(Color(hue: Double(calendar.hue % 360) / 360, saturation: 0.6, brightness: 0.75)) |
| 28 | .tag(calendar.id) |
| 29 | } |
| 30 | } |
| 31 | Section { |
| 32 | LabeledContent("Time zone", value: model.zone).font(.footnote) |
| 33 | Button("Settings", systemImage: "gear") { settings = true } |
| 34 | Button("Sign out", action: signOut) |
| 35 | } |
| 36 | }.toolbar { if let accounts { ToolbarItem(placement: .navigation) { Button("Accounts", systemImage: "chevron.backward", action: accounts) } } }.navigationTitle("Calendar").navigationSplitViewColumnWidth(min: 220, ideal: 260, max: 340) |
| 37 | } detail: { |
| 38 | List { |
| 39 | Section { |
| 40 | CalendarMonthView(model: model).listRowSeparator(.hidden) |
| 41 | } |
| 42 | if !model.notice.isEmpty { Label(model.notice, systemImage: "info.circle").font(.callout).padding() } |
| 43 | if model.loading { ProgressView().padding(8) } |
| 44 | if !model.error.isEmpty { |
| 45 | if model.hasLoaded { |
| 46 | // Stale, not gone: say so above what was last loaded. |
| 47 | HStack(alignment: .firstTextBaseline, spacing: 10) { |
| 48 | Label { |
| 49 | VStack(alignment: .leading, spacing: 2) { |
| 50 | Text("Couldn’t refresh. Showing what was last loaded.").font(.callout) |
| 51 | Text(model.error).font(.caption).foregroundStyle(.secondary) |
| 52 | } |
| 53 | } icon: { Image(systemName: "wifi.exclamationmark") } |
| 54 | Spacer() |
| 55 | Button("Try again") { Task { await model.load(client) } } |
| 56 | }.padding(.vertical, 6).accessibilityElement(children: .contain).accessibilityIdentifier("staleBanner") |
| 57 | } else { |
| 58 | ContentUnavailableView("Couldn’t load your calendar", systemImage: "wifi.exclamationmark", |
| 59 | description: Text(model.error)) |
| 60 | Button("Try again") { Task { await model.load(client) } }.padding() |
| 61 | } |
| 62 | } |
| 63 | if model.error.isEmpty || model.hasLoaded { |
| 64 | Section(model.display(model.date, template: "EEEE d MMMM")) { |
| 65 | let events = model.dayEvents(model.date) |
| 66 | if events.isEmpty { |
| 67 | ContentUnavailableView("No events", systemImage: "calendar", |
| 68 | description: Text("Enjoy the space, or add something to your day.")) |
| 69 | } |
| 70 | ForEach(events, id: \.identity) { event in |
| 71 | Button { |
| 72 | inspecting = event |
| 73 | } label: { |
| 74 | HStack(alignment: .top, spacing: 14) { |
| 75 | RoundedRectangle(cornerRadius: 2).fill(calendarColor(event.calendar_id)).frame(width: 4) |
| 76 | VStack(alignment: .leading, spacing: 5) { |
| 77 | Text(event.title).font(.headline).foregroundStyle(.primary).strikethrough(event.status == "cancelled") |
| 78 | if event.status == "cancelled" { Label("Cancelled", systemImage: "xmark.circle").font(.caption).foregroundStyle(.secondary) } |
| 79 | if event.status == "tentative" { Label("Tentative", systemImage: "questionmark.circle").font(.caption).foregroundStyle(.secondary) } |
| 80 | Text(event.calendar_name).font(.caption).foregroundStyle(.secondary) |
| 81 | if !event.location.isEmpty { Label(event.location, systemImage: "mappin").font(.callout).foregroundStyle(.secondary) } |
| 82 | } |
| 83 | Spacer() |
| 84 | Text(event.all_day ? "All day" : model.display(Date(timeIntervalSince1970: Double(event.start)), template: "jm")) |
| 85 | .font(.callout.monospacedDigit()).foregroundStyle(.secondary) |
| 86 | }.padding(.vertical, 6) |
| 87 | }.buttonStyle(.plain) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | }.listStyle(.plain).refreshable { await model.load(client) } |
| 92 | .navigationTitle(model.calendars.first(where: { $0.id == model.selectedCalendar })?.name ?? "Calendar") |
| 93 | #if os(iOS) |
| 94 | .navigationBarTitleDisplayMode(.inline) |
| 95 | #endif |
| 96 | .toolbar { |
| 97 | ToolbarItem { Button("Quick add", systemImage: "text.badge.plus") { quickAdding = true }.keyboardShortcut("k", modifiers: .command) } |
| 98 | ToolbarItem(placement: .primaryAction) { |
| 99 | Button("New event", systemImage: "plus") { editing = model.newEvent() } |
| 100 | .disabled(!model.calendars.contains(where: \.writable)).keyboardShortcut("n", modifiers: .command) |
| 101 | } |
| 102 | ToolbarItem { Button("Refresh", systemImage: "arrow.clockwise") { Task { await model.load(client) } } } |
| 103 | } |
| 104 | } |
| 105 | .environment(\.timeZone, TimeZone(identifier: model.zone) ?? .gmt) |
| 106 | .task { await model.load(client) } |
| 107 | .onChange(of: model.monthStart) { _, _ in Task { await model.load(client) } } |
| 108 | .onChange(of: filter) { _, value in model.selectedCalendar = value == 0 ? nil : value; column = .detail } |
| 109 | .onReceive(NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification)) { _ in |
| 110 | if model.zone != CalendarModel.preferredZone { Task { await model.load(client) } } |
| 111 | } |
| 112 | .onReceive(NotificationCenter.default.publisher(for: .NSSystemTimeZoneDidChange)) { _ in Task { await model.load(client) } } |
| 113 | .sheet(isPresented: $quickAdding) { |
| 114 | NavigationStack { |
| 115 | QuickAddView(client: client, model: model) |
| 116 | .padding().navigationTitle("Quick add") |
| 117 | .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Done") { quickAdding = false }.disabled(model.quickSaving) } } |
| 118 | } |
| 119 | .interactiveDismissDisabled(model.quickSaving) |
| 120 | #if os(macOS) |
| 121 | .frame(width: 440, height: 540) |
| 122 | #endif |
| 123 | } |
| 124 | .sheet(isPresented: $settings) { NavigationStack { CalendarSettingsView().toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { settings = false } } } } } |
| 125 | .sheet(item: $editing) { EventEditor(model: model, client: client, event: $0) } |
| 126 | .sheet(item: $inspecting, onDismiss: { if let event = pendingEdit { pendingEdit = nil; editing = event } }) { event in |
| 127 | NavigationStack { |
| 128 | Form { |
| 129 | Section { Text(event.title).font(.title2.bold()); Text(event.calendar_name).foregroundStyle(.secondary) } |
| 130 | if event.status == "cancelled" { Section { Label("Cancelled", systemImage: "xmark.circle") } } |
| 131 | if event.status == "tentative" { Section { Label("Tentative", systemImage: "questionmark.circle") } } |
| 132 | Section("When") { |
| 133 | LabeledContent("Starts", value: event.all_day ? event.day_start : model.display(Date(timeIntervalSince1970: Double(event.start)), template: event.all_day ? "d MMM yyyy" : "d MMM yyyy jm", zone: event.tz)) |
| 134 | LabeledContent(event.all_day ? "Ends before" : "Ends", value: event.all_day ? event.day_end : model.display(Date(timeIntervalSince1970: Double(event.end)), template: event.all_day ? "d MMM yyyy" : "d MMM yyyy jm", zone: event.tz)) |
| 135 | if event.all_day { Text("The end date is the first day after the event.").font(.callout).foregroundStyle(.secondary) } |
| 136 | else { LabeledContent("Time zone", value: event.tz) } |
| 137 | if event.recurring { Label("Repeating event", systemImage: "repeat") } |
| 138 | } |
| 139 | if !event.location.isEmpty { Section("Location") { Text(event.location).textSelection(.enabled) } } |
| 140 | if !event.notes.isEmpty { Section("Notes") { Text(event.notes).textSelection(.enabled) } } |
| 141 | if event.writable { |
| 142 | Section { Button("Edit event") { pendingEdit = event; inspecting = nil } } |
| 143 | } else { |
| 144 | Section { Link("Open in web calendar", destination: client.origin.appendingPathComponent("events/\(event.id)")) } |
| 145 | } |
| 146 | }.formStyle(.grouped).navigationTitle("Event") |
| 147 | .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { inspecting = nil } } } |
| 148 | } |
| 149 | #if os(macOS) |
| 150 | .frame(width: 480, height: 500) |
| 151 | #endif |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private func calendarColor(_ id: Int64) -> Color { |
| 156 | guard let calendar = model.calendars.first(where: { $0.id == id }) else { return accent } |
| 157 | return Color(hue: Double(calendar.hue % 360) / 360, saturation: 0.6, brightness: 0.75) |
| 158 | } |
| 159 | } |
| 160 | |