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