SPB Git

Repository Public

WidgetProviders.swift 5.9 KB 124 lines swift

1// SP Notes iOS2// File: Widgets/WidgetProviders.swift3// Description: Timeline providers of the widgets. Each timeline holds a single entry read from the shared database (read-only,4//              off the main thread, no network). The app reloads the timelines after every local change, so the refresh policy5//              is long: it only keeps the relative dates right.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import SwiftUI10import WidgetKit1112struct NotesEntry: TimelineEntry, Sendable {13    let date: Date14    let content: WidgetContent15}1617enum WidgetRefresh {18    /// The app reloads the widgets itself; this only refreshes "today" versus a date.19    static var policy: TimelineReloadPolicy { .after(Date().addingTimeInterval(6 * 3_600)) }20}2122struct NotesListProvider: TimelineProvider {23    let kind: WidgetListKind2425    /// `.locked` while the app lock is on: nothing is read (see `WidgetDataProvider.content`).26    private func load(limit: Int) async -> WidgetContent { await WidgetDataProvider().content(kind, limit: limit) }2728    func placeholder(in context: Context) -> NotesEntry {29        if WidgetLock.shared.isEnabled { return NotesEntry(date: .now, content: .locked) }30        return NotesEntry(date: .now, content: .notes(Array(WidgetNote.samples.prefix(context.family.noteCapacity))))31    }3233    func getSnapshot(in context: Context, completion: @escaping @Sendable (NotesEntry) -> Void) {34        let capacity = context.family.noteCapacity, isPreview = context.isPreview35        Task {36            var content = await load(limit: capacity)37            // The widget gallery shows an example rather than an empty state.38            if isPreview, content != .locked, content.notes.isEmpty { content = .notes(Array(WidgetNote.samples.prefix(capacity))) }39            completion(NotesEntry(date: .now, content: content))40        }41    }4243    func getTimeline(in context: Context, completion: @escaping @Sendable (Timeline<NotesEntry>) -> Void) {44        let capacity = context.family.noteCapacity45        Task {46            let entry = NotesEntry(date: .now, content: await load(limit: capacity))47            completion(Timeline(entries: [entry], policy: WidgetRefresh.policy))48        }49    }50}5152struct StaticEntry: TimelineEntry { let date: Date }5354/// For the widget that shows no data (new note).55struct StaticProvider: TimelineProvider {56    func placeholder(in context: Context) -> StaticEntry { StaticEntry(date: .now) }57    func getSnapshot(in context: Context, completion: @escaping @Sendable (StaticEntry) -> Void) { completion(StaticEntry(date: .now)) }58    func getTimeline(in context: Context, completion: @escaping @Sendable (Timeline<StaticEntry>) -> Void) {59        completion(Timeline(entries: [StaticEntry(date: .now)], policy: .never))60    }61}6263struct SingleNoteEntry: TimelineEntry, Sendable {64    let date: Date65    let state: SingleNoteState66}6768struct SingleNoteProvider: AppIntentTimelineProvider {69    func placeholder(in context: Context) -> SingleNoteEntry {70        SingleNoteEntry(date: .now, state: WidgetLock.shared.isEnabled ? .locked : .note(WidgetNote.samples[0]))71    }7273    private func state(for configuration: SelectNoteIntent, family: WidgetFamily) async -> SingleNoteState {74        let length = family == .systemLarge ? 900 : (family == .systemMedium ? 320 : 160)75        return await WidgetDataProvider().single(noteId: configuration.note?.id, textLength: length)76    }7778    func snapshot(for configuration: SelectNoteIntent, in context: Context) async -> SingleNoteEntry {79        let state = await state(for: configuration, family: context.family)80        if context.isPreview, state == .unconfigured { return placeholder(in: context) }81        return SingleNoteEntry(date: .now, state: state)82    }8384    func timeline(for configuration: SelectNoteIntent, in context: Context) async -> Timeline<SingleNoteEntry> {85        Timeline(entries: [SingleNoteEntry(date: .now, state: await state(for: configuration, family: context.family))], policy: WidgetRefresh.policy)86    }87}8889struct TasksEntry: TimelineEntry, Sendable {90    let date: Date91    let content: WidgetTasksContent92}9394/// « Capture et tâches ». The timeline is reloaded by the app after every task change and by the check button itself; the95/// extra entry at the next midnight moves tomorrow's tasks into "today" without waiting for the app.96struct TasksProvider: TimelineProvider {97    private func sample(_ family: WidgetFamily) -> WidgetTasksContent {98        .tasks(Array(WidgetTask.samples.prefix(max(family.taskCapacity, 1))), more: 0, open: WidgetTask.samples.count)99    }100101    func placeholder(in context: Context) -> TasksEntry {102        TasksEntry(date: .now, content: WidgetLock.shared.isEnabled ? .locked : sample(context.family))103    }104105    func getSnapshot(in context: Context, completion: @escaping @Sendable (TasksEntry) -> Void) {106        let family = context.family, isPreview = context.isPreview, example = sample(context.family)107        Task {108            var content = await WidgetDataProvider().tasksContent(limit: family.taskCapacity)109            // The widget gallery shows an example rather than an empty state — never while the lock is on.110            if isPreview, content != .locked, content.tasks.isEmpty { content = example }111            completion(TasksEntry(date: .now, content: content))112        }113    }114115    func getTimeline(in context: Context, completion: @escaping @Sendable (Timeline<TasksEntry>) -> Void) {116        let family = context.family117        Task {118            let entry = TasksEntry(date: .now, content: await WidgetDataProvider().tasksContent(limit: family.taskCapacity))119            let midnight = Calendar.current.nextDate(after: .now, matching: DateComponents(hour: 0, minute: 0, second: 5), matchingPolicy: .nextTime) ?? Date().addingTimeInterval(6 * 3_600)120            completion(Timeline(entries: [entry], policy: .after(min(midnight, Date().addingTimeInterval(6 * 3_600)))))121        }122    }123}124
Files main
Menu

Theme