Sources/OrgApp/HiddenFolders.swift
40 lines · 1767 bytes
3 symbols in this file
1import Foundation
2import Observation
3
4/// The folders the agenda leaves out on this device, by root path: the ones hidden from the
5/// agenda's folder menu and the ones the active Focus filter hides. A folder either hides is
6/// hidden. Reminders stay on for both; the Focus filter mutes its folders' through the
7/// system, by each notification's `filterCriteria`.
8@MainActor
9@Observable
10public final class HiddenFolders {
11 public static let shared = HiddenFolders()
12
13 /// Hidden from the agenda's folder menu.
14 public var manual: Set<String> { didSet { defaults.set(manual.sorted(), forKey: Self.manualKey) } }
15 /// Hidden by the active Focus filter.
16 public var focus: Set<String> { didSet { defaults.set(focus.sorted(), forKey: Self.focusKey) } }
17
18 public var all: Set<String> { manual.union(focus) }
19
20 static let manualKey = "agendaHiddenFolders"
21 static let focusKey = "agendaFocusHiddenFolders"
22 @ObservationIgnored private let defaults: UserDefaults
23
24 public init(defaults: UserDefaults = .standard) {
25 self.defaults = defaults
26 manual = Set(defaults.stringArray(forKey: Self.manualKey) ?? [])
27 focus = Set(defaults.stringArray(forKey: Self.focusKey) ?? [])
28 }
29
30 /// `path` is one of `folders` or inside one.
31 public nonisolated static func hides(_ folders: Set<String>, path: String) -> Bool {
32 folders.contains { path == $0 || path.hasPrefix($0.hasSuffix("/") ? $0 : $0 + "/") }
33 }
34
35 /// For a Focus filter's app context: notifications whose `filterCriteria` (their folder) is
36 /// not one of `folders`.
37 public nonisolated static func notificationPredicate(hiding folders: Set<String>) -> NSPredicate {
38 NSPredicate(format: "NOT (SELF IN %@)", folders.sorted())
39 }
40}