This repository has been archived on 2026-09-14. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
ScarriffleandClaude Opus 5 f9bc1965a5 Add the watch app and its eight complications
The watch receives the phone's payload over WatchConnectivity and writes it
into its own App Group container through the same SnapshotStore, so there is
one wire format and one reader. The agenda, the detail screen and the filter
all read that; the complications read what the app last wrote, since an
extension cannot run a WCSession of its own.

Complication timelines land on event boundaries rather than a schedule, so the
next appointment flips in the exact minute it changes without spending refresh
budget.

CalendarrWatchShared holds what both watch targets need, attached to each the
way Shared/ is attached to the app and the widgets.

recommendations() is implemented because watchOS requires it — iOS has a
default, the watch does not, since the complication gallery has to be
populated. Found by building, not by reading.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-11 17:08:27 +02:00

53 lines
1.7 KiB
Swift

import Foundation
import WidgetKit
import CalendarrCore
/// What the watch views read. Holds the last received snapshot, the calendar
/// list, and this watch's filter.
@MainActor
@Observable
final class WatchSnapshotModel {
private(set) var result: SnapshotReadResult = .neverWritten
private(set) var calendars: [SnapshotCalendar] = []
private(set) var hiddenKeys: Set<String> = WatchFilterStore.load()
private let store = SnapshotStore()
private var changeToken: SnapshotChangeNotifier.ObservationToken?
init() {
reload()
// The receiver writes through SnapshotStore, which broadcasts. Listening
// here means the UI does not have to be notified separately.
changeToken = SnapshotChangeNotifier.observe { [weak self] in
Task { @MainActor in self?.reload() }
}
}
func reload() {
result = store.read()
calendars = store.readCalendars()
}
/// The snapshot with this watch's filter applied, or nil when there is
/// nothing usable to show.
var visible: CalendarrSnapshot? {
result.snapshot?.filtered(excludingCalendarKeys: hiddenKeys)
}
var language: String { result.snapshot?.language ?? "system" }
func isHidden(_ key: String) -> Bool { hiddenKeys.contains(key) }
/// The display name for an event's calendar. Events carry only a
/// `calendarKey`; the names travel separately, in the calendar list.
func calendarName(for key: String) -> String? {
calendars.first { $0.id == key }?.name
}
func setHidden(_ key: String, _ hidden: Bool) {
if hidden { hiddenKeys.insert(key) } else { hiddenKeys.remove(key) }
WatchFilterStore.save(hiddenKeys)
WidgetCenter.shared.reloadAllTimelines()
}
}