Ariver
2026-06-19 50e354e29ead4f3f2ef794cd6c92522ea563fc43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import Foundation
 
struct AppLibrarySnapshot {
    let apps: [AppInfo]
    let quickSearchDocuments: [QuickSearchDocument]
    let tagColors: [String: Int]
    let tagOrder: [String]
    let tagDefinitions: [String: TagDatabase.TagDef]
    let containerAppOrder: [String: [String]]
}
 
struct AppLibraryRefreshResult {
    let snapshot: AppLibrarySnapshot
    let smartStartResult: SmartStartRunResult
}
 
struct AppLibrarySmartStartApplyResult {
    let snapshot: AppLibrarySnapshot
    let summary: SmartStartSummary?
}
 
struct AppLibrarySystemSchemeApplyResult {
    let snapshot: AppLibrarySnapshot
    let summary: SmartStartSummary?
}
 
struct AppLibraryUncategorizedResetResult {
    let snapshot: AppLibrarySnapshot
}
 
enum AppLibraryController {
    static func refresh(useCache: Bool = true) -> AppLibraryRefreshResult {
        let scannedApps = AppIndexer.scan(useCache: useCache)
        let reconciledStore = TagEditor.reconcileScannedApps(scannedApps)
        let smartStartResult = SmartStartService.runIfNeeded(
            apps: scannedApps,
            store: reconciledStore
        )
        return AppLibraryRefreshResult(
            snapshot: makeSnapshot(scannedApps: scannedApps, store: smartStartResult.store),
            smartStartResult: smartStartResult
        )
    }
 
    static func applySmartStartSuggestion(
        _ draft: SmartCategorizationDraft,
        scannedApps: [AppInfo]
    ) -> AppLibrarySmartStartApplyResult {
        let result = SmartStartService.applySuggestion(draft)
        return AppLibrarySmartStartApplyResult(
            snapshot: makeSnapshot(scannedApps: scannedApps, store: result.store),
            summary: result.summary
        )
    }
 
    static func applySystemInitialScheme() -> AppLibrarySystemSchemeApplyResult {
        let scannedApps = AppIndexer.scan(useCache: false)
        let result = SmartStartService.applySystemInitialScheme(apps: scannedApps)
        return AppLibrarySystemSchemeApplyResult(
            snapshot: makeSnapshot(scannedApps: scannedApps, store: result.store),
            summary: result.summary
        )
    }
 
    static func resetToUncategorized() -> AppLibraryUncategorizedResetResult {
        let scannedApps = AppIndexer.scan(useCache: false)
        let store = TagDatabase.resetAppTagAssignmentsToUncategorized()
        return AppLibraryUncategorizedResetResult(
            snapshot: makeSnapshot(scannedApps: scannedApps, store: store)
        )
    }
 
    private static func makeSnapshot(
        scannedApps: [AppInfo],
        store: TagDatabase.Store
    ) -> AppLibrarySnapshot {
        let apps = TagEditor.annotate(apps: scannedApps, store: store)
        return AppLibrarySnapshot(
            apps: apps,
            quickSearchDocuments: QuickSearchEngine.makeDocuments(apps: apps, store: store),
            tagColors: store.tags.mapValues { $0.color },
            tagOrder: TagEditor.orderedTagNames(in: store),
            tagDefinitions: store.tags,
            containerAppOrder: TagDatabase.normalizedContainerAppOrder(
                store.containerAppOrder,
                tags: store.tags,
                appTags: store.appTags
            )
        )
    }
}