Sources/OrgstarMobile/FocusFilter.swift

48 lines · 1979 bytes

 1#if os(iOS)
 2import AppIntents
 3import OrgApp
 4
 5/// A folder the user added, by its path.
 6struct FolderEntity: AppEntity {
 7    static let typeDisplayRepresentation: TypeDisplayRepresentation = "Folder"
 8    static let defaultQuery = Query()
 9
10    var id: String
11
12    var displayRepresentation: DisplayRepresentation {
13        DisplayRepresentation(title: "\((id as NSString).lastPathComponent)", subtitle: "\(id)")
14    }
15
16    struct Query: EntityQuery {
17        func entities(for identifiers: [String]) async throws -> [FolderEntity] { identifiers.map(FolderEntity.init) }
18        func suggestedEntities() async throws -> [FolderEntity] { WorkspaceModel.savedRootPaths().map(FolderEntity.init) }
19    }
20}
21
22/// While a Focus with this filter is on, the agenda and widgets leave out its folders and
23/// their reminders are muted. The system performs it again with no folders when the Focus ends.
24struct AgendaFocusFilter: SetFocusFilterIntent {
25    static let title: LocalizedStringResource = "Hide Folders"
26    static let description = IntentDescription("Leaves folders out of the agenda and widgets, and mutes their reminders, while this Focus is on.")
27
28    @Parameter(title: "Folders", default: [])
29    var folders: [FolderEntity]
30
31    var displayRepresentation: DisplayRepresentation {
32        let names = folders.map { ($0.id as NSString).lastPathComponent }.joined(separator: ", ")
33        return DisplayRepresentation(title: names.isEmpty ? "No folders hidden" : "Hide \(names)")
34    }
35
36    var appContext: FocusFilterAppContext {
37        FocusFilterAppContext(notificationFilterPredicate: HiddenFolders.notificationPredicate(hiding: Set(folders.map(\.id))))
38    }
39
40    @MainActor
41    func perform() async throws -> some IntentResult {
42        HiddenFolders.shared.focus = Set(folders.map(\.id))
43        // The app may be in the background, where the widgets would wait for it to come back.
44        await MobileModel.shared.updateWidgets()
45        return .result()
46    }
47}
48#endif