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.

3.3 KB
Raw Blame
import PeelCore
import SwiftUI

/// What keeps Peel from moving anything at all: the exclusions or History can't be read, or History can't be written.
/// Exclusions not read yet are none of these, since they are read as Peel starts.
enum MovesHeld: Equatable {
    case exclusionsUnreadable
    case historyUnreadable(FileProblem)
    case historyUnwritable(HistoryWriteProblem)

    static func reason(
        exclusionsUnreadable: Bool, historyUnreadable: FileProblem?, historyUnwritable: HistoryWriteProblem?,
        recordsInHistory: Bool = true
    ) -> MovesHeld? {
        if exclusionsUnreadable { return .exclusionsUnreadable }
        guard recordsInHistory else { return nil }
        if let historyUnreadable { return .historyUnreadable(historyUnreadable) }
        return historyUnwritable.map(MovesHeld.historyUnwritable)
    }

    @MainActor
    static func reason(
        exclusions: ExclusionsStore, history: RemovalHistoryStore, recordsInHistory: Bool = true
    ) -> MovesHeld? {
        reason(
            exclusionsUnreadable: exclusions.exclusions.isUnreadable, historyUnreadable: history.readProblem,
            historyUnwritable: history.writeProblem, recordsInHistory: recordsInHistory
        )
    }
}

/// Over a page that can move something to the Trash or put it back, while `MovesHeld` names a reason: nothing moves
/// until it is gone, so the page's own button is off, and this says why. It shows only then.
struct RemovalsHeldBanner: View {
    @Environment(ExclusionsStore.self) private var exclusions
    @Environment(RemovalHistoryStore.self) private var history
    @Environment(\.openSettings) private var openSettings
    /// On History's own page, whose list says it with Start Over beside it, History is left unsaid here.
    var isOnHistoryPage = false
    /// False on a page whose action History doesn't record, which only the exclusions hold back.
    var recordsInHistory = true

    var body: some View {
        let held = MovesHeld.reason(exclusions: exclusions, history: history, recordsInHistory: recordsInHistory)
        if held == .exclusionsUnreadable {
            Notice(
                title: Text("Peel couldn’t read your exclusions"),
                detail: Text(verbatim: exclusions.nothingMovesUntilStartedOver)
            ) {
                Button("Open Peel Settings") { SettingsPane.exclusions.open(with: openSettings) }
            }
            .padding(.vertical, 6)
            .listRowSeparator(.hidden)
        } else if case .historyUnreadable(let why) = held, !isOnHistoryPage {
            Notice(
                title: Text("Peel couldn’t read History"),
                detail: Text(verbatim: why.said(after: String(localized: "Peel removes nothing until you start History over, so that everything it moves can be put back."), about: history.historyFile))
            ) {
                Button("Open History") {
                    Navigator.shared.requestedTool = .history
                }
            }
            .padding(.vertical, 6)
            .listRowSeparator(.hidden)
        } else if case .historyUnwritable(let problem) = held {
            Notice(title: Text("Peel can’t write History"), detail: Text(problem.explanation())) {}
            .padding(.vertical, 6)
            .listRowSeparator(.hidden)
        }
    }
}