Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

4.1 KB
Raw Blame
import Foundation
import Observation
import PeelCore

@Observable
final class ExtensionLibrary {
    private(set) var extensions: [AppExtension]?
    /// The kinds macOS gave no answer about, which is not the same as there being none of them.
    private(set) var unanswered: Set<AppExtension.Kind> = []
    /// The scan this page runs, which a newer one or the Stop button ends.
    let scanRun = ScanRun()
    var isScanning: Bool { scanRun.isRunning }
    var selection: AppExtension.ID?

    var selectedExtension: AppExtension? {
        extensions?.first { $0.id == selection }
    }

    private let scan: @MainActor (Exclusions) async -> ExtensionScan

    init(
        scan: @escaping @MainActor (Exclusions) async -> ExtensionScan = { await AppExtensions.scan(exclusions: $0) }
    ) {
        self.scan = scan
    }

    func refresh() async {
        let scan = scan
        guard let result = await scanRun.run({ await scan(ExclusionsStore.shared.exclusions) }) else { return }
        show(result)
    }

    /// Reads the page again without showing a scan, once it has been read.
    func readAgain() async {
        guard extensions != nil else { return }
        let scan = scan
        guard let result = await scanRun.runQuietly({ await scan(ExclusionsStore.shared.exclusions) }) else { return }
        show(result)
    }

    private func show(_ scan: ExtensionScan) {
        let result = scan.extensions
        extensions = result
        unanswered = scan.unanswered
        if let selection, !result.contains(where: { $0.id == selection }) {
            self.selection = nil
        }
    }
}

extension AppExtension.Kind {
    var title: LocalizedStringResource {
        switch self {
        case .appExtension: "Part of an app"
        case .systemExtension: "Installed into macOS"
        }
    }

    var systemImage: String {
        switch self {
        case .appExtension: "puzzlepiece.extension"
        case .systemExtension: "gearshape.2"
        }
    }

    /// What the page says when macOS gave no answer about this kind.
    var unansweredNote: LocalizedStringResource {
        switch self {
        case .appExtension: "macOS didn’t say which app extensions are on this Mac, so none are listed here."
        case .systemExtension: "macOS didn’t say which system extensions are on this Mac, so none are listed here."
        }
    }
}

extension AppExtension.Election {
    var title: LocalizedStringResource {
        switch self {
        case .on: "Turned on"
        case .off: "Turned off"
        case .asItCame: "Default"
        case .superseded: "Replaced"
        case .waitingForApproval: "Waiting for approval"
        case .beingRemoved: "Being removed"
        case .changing: "Changing"
        case .unknown: LocalizedStringResource("Unknown (extension state)", defaultValue: "Unknown")
        case .unrecognized: "Unknown to Peel"
        }
    }

    var explanation: LocalizedStringResource {
        switch self {
        case .on: "Someone turned this on and macOS kept the choice."
        case .off: "Someone turned this off and macOS kept the choice."
        case .asItCame: "Nobody has decided about it, so macOS chooses."
        case .superseded: "Another copy of this extension is the one macOS runs."
        case .waitingForApproval: "macOS won’t run this until you allow it in System Settings."
        case .beingRemoved: "macOS is removing this. Some removals finish only when the Mac restarts."
        case .changing: "macOS is partway through changing this."
        case .unknown: "macOS marks this extension’s state as unknown, so it doesn’t say whether the extension is on."
        case .unrecognized: "macOS reports a state Peel doesn’t know, shown below as macOS calls it."
        }
    }

    /// The symbol the list shows in the accent color beside what someone turned on, and beside waiting for approval,
    /// the only state the user can do something about.
    var mark: String? {
        switch self {
        case .on: "checkmark.circle"
        case .waitingForApproval: "exclamationmark.circle"
        default: nil
        }
    }
}