SPB Git

Repository Public

NoteEntity.swift 3.9 KB 90 lines swift

1// SP Notes iOS2// File: WidgetShared/NoteEntity.swift3// Description: A note as the system sees it (Shortcuts, Siri, widget configuration): identifier, title, preview, modification date,4//              with a query that searches the local store. Compiled into the app and into the widget extension so that the5//              configurable widget and the app's intents share the same entity.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import AppIntents10import Foundation11import WidgetKit1213struct NoteEntity: AppEntity, Identifiable, Sendable {14    static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Note")15    static let defaultQuery = NoteEntityQuery()1617    var id: String18    @Property(title: "Titre") var title: String19    @Property(title: "Aperçu") var preview: String20    @Property(title: "Modifiée le") var modifiedAt: Date2122    init(id: String, title: String, preview: String, modifiedAt: Date) {23        self.id = id24        self.title = title25        self.preview = preview26        self.modifiedAt = modifiedAt27    }2829    init(_ note: WidgetNote) { self.init(id: note.id, title: note.title, preview: note.preview, modifiedAt: note.updatedAt) }3031    var displayRepresentation: DisplayRepresentation {32        preview.isEmpty ? DisplayRepresentation(title: "\(title)") : DisplayRepresentation(title: "\(title)", subtitle: "\(preview)")33    }34}3536/// What the note picker may list. In the widget extension (the picker of the configurable widget, shown over the Home37/// Screen) no title is listed while the app lock is on; the app's own intents (Shortcuts, Siri) run in the app and keep working.38struct NotePickerPolicy: Sendable, Equatable {39    var lock = WidgetLock.shared40    var appliesLock = WidgetLock.isExtensionProcess4142    var hidesTitles: Bool { appliesLock && lock.isEnabled }4344    /// Shown by the picker instead of the notes.45    static let lockedPrompt: LocalizedStringResource = "SP Notes est verrouillé : désactive le verrouillage dans l’app pour choisir une note."46    static let lockedTitle = String(localized: "Note verrouillée")4748    /// The chosen notes resolved without their content: the widget keeps its configuration, the picker shows no title.49    static func redacted(ids: [String]) -> [NoteEntity] {50        ids.map { NoteEntity(id: $0, title: lockedTitle, preview: "", modifiedAt: Date(timeIntervalSince1970: 0)) }51    }52}5354struct NoteEntityQuery: EntityStringQuery {55    var data = WidgetDataProvider()56    var policy = NotePickerPolicy()5758    func entities(for identifiers: [String]) async throws -> [NoteEntity] {59        if policy.hidesTitles { return NotePickerPolicy.redacted(ids: identifiers) }60        return await data.notes(ids: identifiers).map(NoteEntity.init)61    }6263    func entities(matching string: String) async throws -> IntentItemCollection<NoteEntity> {64        if policy.hidesTitles { return Self.lockedCollection }65        return IntentItemCollection(items: await data.search(string, limit: 25).map(NoteEntity.init))66    }6768    /// The most recent notes, offered before anything is typed.69    func suggestedEntities() async throws -> IntentItemCollection<NoteEntity> {70        if policy.hidesTitles { return Self.lockedCollection }71        return IntentItemCollection(items: await data.recent(limit: 12).notes.map(NoteEntity.init))72    }7374    /// No note, and the reason.75    static var lockedCollection: IntentItemCollection<NoteEntity> {76        IntentItemCollection(promptLabel: NotePickerPolicy.lockedPrompt, usesIndexedCollation: false, sections: [])77    }78}7980/// Configuration of the "Note" widget: the note it shows.81struct SelectNoteIntent: WidgetConfigurationIntent {82    static let title: LocalizedStringResource = "Note"83    static let description = IntentDescription("Choisis la note à afficher dans le widget.")8485    @Parameter(title: "Note") var note: NoteEntity?8687    init() {}88    init(note: NoteEntity?) { self.note = note }89}90
Files main
Menu

Theme