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.

6.4 KB
Raw Blame
import AppKit
import PeelCore
import SwiftUI

struct InstallerDetailView: View {
    @Environment(InstallerLibrary.self) private var installers
    @Environment(HelperModel.self) private var helper
    let kind: InstallerItem.Kind

    var body: some View {
        let rows = items
        let hasNoteColumn = rows.contains { !$0.isReadOnly && (detail(for: $0) != nil || $0.heldBack != nil) }

        List {
            header(rows)
                .listRowSeparator(.hidden)
            if kind != .deviceBackup {
                RemovalsHeldBanner()
            }

            let locked = rows.count { $0.isLocked(canUseHelper: helper.canAct) }
            if locked > 0 {
                HelperRequiredBanner(need: .moving(locked))
            }

            Section {
                ForEach(rows) { item in
                    if item.isReadOnly {
                        BackupRow(item: item, isFirst: item.id == rows.first?.id)
                    } else {
                        RemovalRow(
                            url: item.url,
                            icon: .file(item.url),
                            detail: detail(for: item),
                            warning: item.heldBack.map {
                                String(localized: $0.explanation(keptInStepBy: item.syncedBy, openBy: item.openBy))
                            },
                            size: item.size ?? 0,
                            isMeasured: item.size != nil,
                            isLocked: item.isLocked(canUseHelper: helper.canAct),
                            isLeftAlone: item.heldBack?.cannotBeMoved == true,
                            isFirst: item.id == rows.first?.id,
                            hasNoteColumn: hasNoteColumn,
                            selection: installers, isSelected: installers.isSelected(item.url)
                        )
                    }
                }
                .listRowSeparator(.hidden)
            } header: {
                SectionHeaderLine {
                    heading(kind.title, kind.explanation)
                } actions: {
                    SelectMenu(
                        list: rows.selectableRows(canUseHelper: helper.canAct),
                        place: Text(kind.title),
                        selection: installers
                    )
                }
            }

            if kind == .deviceBackup {
                Section {
                } header: {
                    HeadingWithActions {
                        heading(
                            "Device Backups",
                            "A backup is deleted in System Settings > General > Storage > iOS Files, or in Finder from a connected device’s General > Manage Backups. Peel doesn’t touch them: what is inside came off a device and may be the only copy."
                        )
                    } actions: {
                        Button("Open System Settings") {
                            NSWorkspace.shared.open(Installers.storageSettingsURL)
                        }
                    }
                }
            }
        }
        .scrollBarBelowSectionHeaders()
        .dimmedWhileBusy(installers.isScanning)
        .edgeBar(.bottom) {
            if kind != .deviceBackup {
                RemovalBar(
                    page: kind.page,
                    isScanning: installers.isScanning,
                    scan: installers.scanRun
                )
            }
        }
        .fadesInColumn(whenRowsChange: items.map(\.id))
        .navigationTitle(Text(kind.title))
        .toolbar(removing: .title)
    }

    private var items: [InstallerItem] {
        installers.shownItems(in: kind)
    }

    private func header(_ rows: [InstallerItem]) -> some View {
        PageHeader(systemImage: kind.systemImage) {
            Text(kind.title)
                .pageHeading()
        } details: {
            FlowLayout {
                Badge(title: Text("^[\(rows.count) item](inflect: true)"), systemImage: "square.and.arrow.down")
                if kind == .deviceBackup {
                    Badge(title: Text("Read only"), systemImage: "hand.raised")
                }
            }
        } trailing: {
            TotalLabel(
                total: rows.shownTotal(of: kind, canUseHelper: helper.canAct),
                caption: Text(kind == .deviceBackup ? "on this Mac" : "to remove")
            )
        }
    }

    private func detail(for item: InstallerItem) -> LocalizedStringResource? {
        if let app = item.installedApp {
            return "\(app) is already installed"
        }
        return item.notes.first.map(\.words)
    }
}

extension InstallerItem.Note {
    var words: LocalizedStringResource {
        switch self {
        case .version(let version): "Version \(version)"
        case .name(let name): "\(name)"
        case .encrypted: "Encrypted"
        case .downloadedBy(let app): "An update \(app) downloaded"
        case .unusedByAnyVirtualMachine: "Not used by any virtual machine"
        }
    }
}

/// A row for a device backup. It has no checkbox: Peel never removes a backup, because it may be the only copy
/// of a device's data.
private struct BackupRow: View {
    let item: InstallerItem
    let isFirst: Bool

    var body: some View {
        HStack(alignment: .firstTextBaseline, spacing: 10) {
            Image(systemName: "iphone.gen3")
                .foregroundStyle(.secondary)
                .frame(width: 20)
                .accessibilityHidden(true)
            VStack(alignment: .leading, spacing: 2) {
                Text(verbatim: item.name)
                    .lineLimit(1)
                HStack(spacing: 6) {
                    ForEach(item.notes, id: \.self) { note in
                        Text(note.words)
                    }
                    if let date = item.date {
                        Text(date, format: .dateTime.day().month().year())
                    }
                }
                .font(.caption)
                .foregroundStyle(.secondary)
            }
            Spacer(minLength: 12)
            Text(item.size.byteCount)
                .monospacedDigit()
                .foregroundStyle(.secondary)
        }
        .accessibilityElement(children: .combine)
        .tableRow(isFirst: isFirst)
        .contextMenu {
            Button("Show in Finder", systemImage: "folder") {
                NSWorkspace.shared.activateFileViewerSelecting([item.url])
            }
        }
    }
}