import RastrilloApple import SwiftUI @MainActor public struct CalendarWorkspace: View { public let client: NativeHTTP public let signOut: () -> Void private let accounts: (() -> Void)? @StateObject private var model: CalendarModel @State private var quickAdding = false @State private var settings = false @State private var editing: CalendarEvent? @State private var pendingEdit: CalendarEvent? @State private var inspecting: CalendarEvent? @State private var filter: Int64? = 0 @State private var column: NavigationSplitViewColumn = .detail @Environment(\.dynamicTypeSize) private var typeSize private let accent = Color(red: 0.78, green: 0.22, blue: 0.29) 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()) } public var body: some View { NavigationSplitView(preferredCompactColumn: $column) { List(selection: $filter) { Section("Calendars") { Label("All calendars", systemImage: "calendar").tag(Int64(0)) ForEach(model.calendars) { calendar in Label(calendar.name, systemImage: "circle.fill") .listItemTint(Color(hue: Double(calendar.hue % 360) / 360, saturation: 0.6, brightness: 0.75)) .tag(calendar.id) } } Section { LabeledContent("Time zone", value: model.zone).font(.footnote) Button("Settings", systemImage: "gear") { settings = true } Button("Sign out", action: signOut) } }.toolbar { if let accounts { ToolbarItem(placement: .navigation) { Button("Accounts", systemImage: "chevron.backward", action: accounts) } } }.navigationTitle("Calendar").navigationSplitViewColumnWidth(min: 220, ideal: 260, max: 340) } detail: { List { Section { CalendarMonthView(model: model).listRowSeparator(.hidden) } if !model.notice.isEmpty { Label(model.notice, systemImage: "info.circle").font(.callout).padding() } if model.loading { ProgressView().padding(8) } if !model.error.isEmpty { if model.hasLoaded { // Stale, not gone: say so above what was last loaded. HStack(alignment: .firstTextBaseline, spacing: 10) { Label { VStack(alignment: .leading, spacing: 2) { Text("Couldn’t refresh. Showing what was last loaded.").font(.callout) Text(model.error).font(.caption).foregroundStyle(.secondary) } } icon: { Image(systemName: "wifi.exclamationmark") } Spacer() Button("Try again") { Task { await model.load(client) } } }.padding(.vertical, 6).accessibilityElement(children: .contain).accessibilityIdentifier("staleBanner") } else { ContentUnavailableView("Couldn’t load your calendar", systemImage: "wifi.exclamationmark", description: Text(model.error)) Button("Try again") { Task { await model.load(client) } }.padding() } } if model.error.isEmpty || model.hasLoaded { Section(model.display(model.date, template: "EEEE d MMMM")) { let events = model.dayEvents(model.date) if events.isEmpty { ContentUnavailableView("No events", systemImage: "calendar", description: Text("Enjoy the space, or add something to your day.")) } ForEach(events, id: \.identity) { event in Button { inspecting = event } label: { HStack(alignment: .top, spacing: 14) { RoundedRectangle(cornerRadius: 2).fill(calendarColor(event.calendar_id)).frame(width: 4) VStack(alignment: .leading, spacing: 5) { Text(event.title).font(.headline).foregroundStyle(.primary).strikethrough(event.status == "cancelled") if event.status == "cancelled" { Label("Cancelled", systemImage: "xmark.circle").font(.caption).foregroundStyle(.secondary) } if event.status == "tentative" { Label("Tentative", systemImage: "questionmark.circle").font(.caption).foregroundStyle(.secondary) } Text(event.calendar_name).font(.caption).foregroundStyle(.secondary) if !event.location.isEmpty { Label(event.location, systemImage: "mappin").font(.callout).foregroundStyle(.secondary) } } Spacer() Text(event.all_day ? "All day" : model.display(Date(timeIntervalSince1970: Double(event.start)), template: "jm")) .font(.callout.monospacedDigit()).foregroundStyle(.secondary) }.padding(.vertical, 6) }.buttonStyle(.plain) } } } }.listStyle(.plain).refreshable { await model.load(client) } .navigationTitle(model.calendars.first(where: { $0.id == model.selectedCalendar })?.name ?? "Calendar") #if os(iOS) .navigationBarTitleDisplayMode(.inline) #endif .toolbar { ToolbarItem { Button("Quick add", systemImage: "text.badge.plus") { quickAdding = true }.keyboardShortcut("k", modifiers: .command) } ToolbarItem(placement: .primaryAction) { Button("New event", systemImage: "plus") { editing = model.newEvent() } .disabled(!model.calendars.contains(where: \.writable)).keyboardShortcut("n", modifiers: .command) } ToolbarItem { Button("Refresh", systemImage: "arrow.clockwise") { Task { await model.load(client) } } } } } .environment(\.timeZone, TimeZone(identifier: model.zone) ?? .gmt) .task { await model.load(client) } .onChange(of: model.monthStart) { _, _ in Task { await model.load(client) } } .onChange(of: filter) { _, value in model.selectedCalendar = value == 0 ? nil : value; column = .detail } .onReceive(NotificationCenter.default.publisher(for: UserDefaults.didChangeNotification)) { _ in if model.zone != CalendarModel.preferredZone { Task { await model.load(client) } } } .onReceive(NotificationCenter.default.publisher(for: .NSSystemTimeZoneDidChange)) { _ in Task { await model.load(client) } } .sheet(isPresented: $quickAdding) { NavigationStack { QuickAddView(client: client, model: model) .padding().navigationTitle("Quick add") .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Done") { quickAdding = false }.disabled(model.quickSaving) } } } .interactiveDismissDisabled(model.quickSaving) #if os(macOS) .frame(width: 440, height: 540) #endif } .sheet(isPresented: $settings) { NavigationStack { CalendarSettingsView().toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { settings = false } } } } } .sheet(item: $editing) { EventEditor(model: model, client: client, event: $0) } .sheet(item: $inspecting, onDismiss: { if let event = pendingEdit { pendingEdit = nil; editing = event } }) { event in NavigationStack { Form { Section { Text(event.title).font(.title2.bold()); Text(event.calendar_name).foregroundStyle(.secondary) } if event.status == "cancelled" { Section { Label("Cancelled", systemImage: "xmark.circle") } } if event.status == "tentative" { Section { Label("Tentative", systemImage: "questionmark.circle") } } Section("When") { 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)) 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)) if event.all_day { Text("The end date is the first day after the event.").font(.callout).foregroundStyle(.secondary) } else { LabeledContent("Time zone", value: event.tz) } if event.recurring { Label("Repeating event", systemImage: "repeat") } } if !event.location.isEmpty { Section("Location") { Text(event.location).textSelection(.enabled) } } if !event.notes.isEmpty { Section("Notes") { Text(event.notes).textSelection(.enabled) } } if event.writable { Section { Button("Edit event") { pendingEdit = event; inspecting = nil } } } else { Section { Link("Open in web calendar", destination: client.origin.appendingPathComponent("events/\(event.id)")) } } }.formStyle(.grouped).navigationTitle("Event") .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { inspecting = nil } } } } #if os(macOS) .frame(width: 480, height: 500) #endif } } private func calendarColor(_ id: Int64) -> Color { guard let calendar = model.calendars.first(where: { $0.id == id }) else { return accent } return Color(hue: Double(calendar.hue % 360) / 360, saturation: 0.6, brightness: 0.75) } }