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.

7.3 KB
Raw Blame
import Foundation
import Observation
import PeelCore

/// A storage tool whose selection travels: Move to Trash on any storage tool's page moves what is selected on this
/// tool's pages too, once each has been seen (`CarriedSelection`).
protocol CarriesSelection: AnyObject {
    /// True while a Move to Trash with a part of this tool runs, from its first move until every tool is up to date. It
    /// is the removal's and not each part's, so what a page shows holds still between its parts.
    var isRemoving: Bool { get set }
    /// What is selected on each of the tool's pages, a part for each page with something selected.
    var carriedParts: [CarriedSelection.Part] { get }
    /// The app to quit before `part` can move, or nil when none of its apps is open.
    func appToQuit(for part: CarriedSelection.Part) -> String?
    /// Moves what is still selected of `part`, by the tool's own routine and checks. Nil, moving nothing, when the
    /// tool keeps the whole part because one of its apps is open.
    func move(_ part: CarriedSelection.Part, apps: AppLibrary) async -> TrashResult?
    /// Brings the tool up to date once `parts` have moved, as after its own Move to Trash.
    func refresh(after parts: [CarriedSelection.Part], apps: AppLibrary) async
    func deselect(_ part: CarriedSelection.Part)
    /// Chooses the page in its tool, so the tool shows it when it is shown.
    func choose(_ page: CarriedSelection.Page)
}

extension CarriesSelection {
    func appToQuit(for part: CarriedSelection.Part) -> String? { nil }
    func choose(_ page: CarriedSelection.Page) {}
}

extension CarriesSelection where Self: RowSelection {
    func deselect(_ part: CarriedSelection.Part) {
        selectedURLs.subtract(part.sizes.keys)
    }
}

extension Tool {
    /// One of the tool's pages: `scope` names the page within the tool, and is empty for a tool that is one page.
    func page(_ scope: String = "") -> CarriedSelection.Page {
        CarriedSelection.Page(tool: rawValue, scope: scope)
    }
}

/// Carries what is selected on the storage tools' pages to Move to Trash on any of them, and moves it as one
/// removal: each part by its own tool, with that tool's checks, into one History entry.
@Observable
final class SelectionCarrier {
    private var carried = CarriedSelection()
    /// A Move to Trash moving the parts, one after another, until every tool that moved is up to date.
    let progress = MoveProgress()
    @ObservationIgnored private let tools: [Tool: any CarriesSelection]
    @ObservationIgnored private let apps: AppLibrary
    @ObservationIgnored private let history: RemovalHistoryStore
    @ObservationIgnored private let outcome: RemovalOutcome

    init(tools: [Tool: any CarriesSelection], apps: AppLibrary, history: RemovalHistoryStore, outcome: RemovalOutcome) {
        self.tools = tools
        self.apps = apps
        self.history = history
        self.outcome = outcome
    }

    func saw(_ page: CarriedSelection.Page) {
        carried.saw(page)
    }

    func hasSeen(_ page: CarriedSelection.Page) -> Bool {
        carried.hasSeen(page)
    }

    /// What Move to Trash moves from `page`: what is selected there and on every page seen before, in the
    /// sidebar's order.
    func parts(from page: CarriedSelection.Page) -> [CarriedSelection.Part] {
        var carried = carried
        carried.saw(page)
        let order = Tool.Group.allCases.flatMap(\.tools)
        return carried.parts(
            from: order.compactMap { tools[$0] }.flatMap(\.carriedParts), order: order.map(\.rawValue), asking: page
        )
    }

    /// The parts an open app keeps, each with that app, in the order of `parts`.
    func kept(among parts: [CarriedSelection.Part]) -> [(part: CarriedSelection.Part, app: String)] {
        parts.compactMap { part in self.tool(of: part)?.appToQuit(for: part).map { (part, $0) } }
    }

    /// The parts no open app keeps, which can move while the others wait for their apps to quit.
    func free(among parts: [CarriedSelection.Part]) -> [CarriedSelection.Part] {
        let kept = Set(kept(among: parts).map(\.part))
        return parts.filter { !kept.contains($0) }
    }

    func deselect(_ part: CarriedSelection.Part) {
        tool(of: part)?.deselect(part)
    }

    func show(_ page: CarriedSelection.Page) {
        guard let tool = Tool(rawValue: page.tool) else { return }
        tools[tool]?.choose(page)
        Navigator.shared.requestedTool = tool
    }

    /// Moves `parts` as one removal. Each part is in History as soon as it moved, so nothing that moved is lost if
    /// Peel stops halfway, and a part whose tool kept it stays selected, with the app to quit named after.
    func move(_ parts: [CarriedSelection.Part]) async {
        let moving = parts.compactMap(tool(of:))
        moving.forEach { $0.isRemoving = true }
        defer { moving.forEach { $0.isRemoving = false } }
        await progress.run(toMove: parts.reduce(0) { $0 + $1.count }) {
            var removal = RemovalInProgress()
            var appsToQuit: [String] = []
            let pass = await QuitGuard.shared.run {
                // Every part shares one administrator's approval, for an account that isn't one.
                await PrivilegedHelper.withOneConsent {
                    await CarriedSelection.pass(parts) { part in
                        // What a part moves is written down with the removal and the part, so a stop before the part is
                        // recorded leaves one removal in History, not two.
                        let recorded = RecordedRemoval(
                            batch: removal.batch, part: part.removalPart, sizes: part.measuredSizes
                        )
                        guard let tool = tool(of: part),
                              let result = await RecordedRemoval.$current.withValue(recorded, operation: {
                                  await tool.move(part, apps: apps)
                              })
                        else {
                            appsToQuit.append(tool(of: part)?.appToQuit(for: part) ?? part.title)
                            return nil
                        }
                        await history.record(result, part: part.removalPart, sizes: part.measuredSizes, in: &removal)
                        return result
                    }
                }
            }
            history.finish(removal)
            outcome.report(pass.result, appsToQuit: appsToQuit)
            for part in pass.refused {
                tool(of: part)?.deselect(part)
            }
            // A tool whose selection holds what another page moved is brought up to date as well, and one that refused
            // a part, which may be because of what another page moved.
            let held = Tool.Group.allCases.flatMap(\.tools).compactMap { tools[$0] }.flatMap(\.carriedParts)
                .holding(pass.result.trashed.map(\.originalURL))
            let changed = pass.moved.map(\.part) + held + pass.refused
            for name in changed.tools {
                guard let tool = Tool(rawValue: name) else { continue }
                await tools[tool]?.refresh(after: changed.filter { $0.page.tool == name }, apps: apps)
            }
        }
    }

    private func tool(of part: CarriedSelection.Part) -> (any CarriesSelection)? {
        Tool(rawValue: part.page.tool).flatMap { tools[$0] }
    }
}