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.

Keep a list's selection rules in one type

Țugui Dragoș3c6f5906cc02
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/SelectAll.swift b/Packages/PeelCore/Sources/PeelCore/Removal/SelectAll.swift
deleted file mode 100644
index 5d639f7f..00000000
--- a/Packages/PeelCore/Sources/PeelCore/Removal/SelectAll.swift
+++ /dev/null
@@ -1,12 +0,0 @@
-/// What a section's Select All and Deselect All do to the selection.
-public enum SelectAll {
- public static func isAllSelected<ID: Hashable>(_ selectable: [ID], in selection: Set<ID>) -> Bool {
- selectable.allSatisfy(selection.contains)
- }
-
- /// Select All adds the rows that can be selected. Deselect All takes out every row of the section, a row
- /// selected by hand that Select All leaves alone included.
- public static func toggled<ID: Hashable>(_ selection: Set<ID>, selectable: [ID], rows: [ID]) -> Set<ID> {
- isAllSelected(selectable, in: selection) ? selection.subtracting(rows) : selection.union(selectable)
- }
-}
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/SelectableRows.swift b/Packages/PeelCore/Sources/PeelCore/Removal/SelectableRows.swift
new file mode 100644
index 00000000..631376ac
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/SelectableRows.swift
@@ -0,0 +1,45 @@
+/// The rows of one list, for its Select All, Select Recommended and Deselect All: every row, those a click can
+/// select, and those Peel recommends.
+public struct SelectableRows<ID: Hashable> {
+ public let rows: [ID]
+ public let selectable: [ID]
+ /// Never a row a click cannot select.
+ public let recommended: [ID]
+
+ public init(rows: [ID], selectable: [ID], recommended: [ID]) {
+ self.rows = rows
+ self.selectable = selectable
+ self.recommended = recommended.filter(Set(selectable).contains)
+ }
+
+ public func isAllSelected(in selection: Set<ID>) -> Bool {
+ selectable.allSatisfy(selection.contains)
+ }
+
+ public func isRecommendedSelected(in selection: Set<ID>) -> Bool {
+ selection.intersection(rows) == Set(recommended)
+ }
+
+ public func isNoneSelected(in selection: Set<ID>) -> Bool {
+ !rows.contains(where: selection.contains)
+ }
+
+ public func selectingAll(in selection: Set<ID>) -> Set<ID> {
+ selection.union(selectable)
+ }
+
+ public func selectingRecommended(in selection: Set<ID>) -> Set<ID> {
+ selection.subtracting(rows).union(recommended)
+ }
+
+ /// Takes out every row of the list, one selected by hand that Select All would leave alone included.
+ public func deselectingAll(in selection: Set<ID>) -> Set<ID> {
+ selection.subtracting(rows)
+ }
+
+ /// What Select All would add that Peel does not recommend, which it asks about first.
+ public func notRecommendedAdded(by selection: Set<ID>) -> [ID] {
+ let recommended = Set(recommended)
+ return selectable.filter { !recommended.contains($0) && !selection.contains($0) }
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SelectAllTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SelectAllTests.swift
deleted file mode 100644
index 9547b476..00000000
--- a/Packages/PeelCore/Tests/PeelCoreTests/SelectAllTests.swift
+++ /dev/null
@@ -1,28 +0,0 @@
-import Foundation
-@testable import PeelCore
-import Testing
-
-struct SelectAllTests {
- private let repository = URL(filePath: "/tmp/org.example.Work")
- private let cache = URL(filePath: "/tmp/org.example.Cache")
- private let log = URL(filePath: "/tmp/org.example.Log")
- private let elsewhere = URL(filePath: "/tmp/org.example.Elsewhere")
-
- /// A row held back from Select All can still be selected by hand, and Deselect All takes it out with the rest:
- /// otherwise it would stay selected unseen and move with the next small file.
- @Test func deselectAllTakesOutARowSelectedByHand() {
- let rows = [repository, cache, log]
- let selectable = [cache, log]
- let selection: Set = [repository, cache, log, elsewhere]
- #expect(SelectAll.isAllSelected(selectable, in: selection))
- #expect(SelectAll.toggled(selection, selectable: selectable, rows: rows) == [elsewhere])
- }
-
- @Test func selectAllAddsOnlyWhatCanBeSelected() {
- let rows = [repository, cache, log]
- let selectable = [cache, log]
- let selection: Set = [cache]
- #expect(!SelectAll.isAllSelected(selectable, in: selection))
- #expect(SelectAll.toggled(selection, selectable: selectable, rows: rows) == [cache, log])
- }
-}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SelectableRowsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SelectableRowsTests.swift
new file mode 100644
index 00000000..835ac7cf
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SelectableRowsTests.swift
@@ -0,0 +1,68 @@
+import Foundation
+@testable import PeelCore
+import Testing
+
+struct SelectableRowsTests {
+ private let repository = URL(filePath: "/tmp/org.example.Work")
+ private let cache = URL(filePath: "/tmp/org.example.Cache")
+ private let log = URL(filePath: "/tmp/org.example.Log")
+ private let elsewhere = URL(filePath: "/tmp/org.example.Elsewhere")
+
+ /// A row selected by hand that Select All leaves alone goes with Deselect All too: otherwise it would stay
+ /// selected unseen and move with the next small file.
+ @Test func deselectAllTakesOutARowSelectedByHand() {
+ let list = SelectableRows(rows: [repository, cache, log], selectable: [cache, log], recommended: [cache, log])
+ let selection: Set = [repository, cache, log, elsewhere]
+
+ #expect(list.isAllSelected(in: selection))
+ #expect(list.deselectingAll(in: selection) == [elsewhere])
+ }
+
+ @Test func selectAllAddsWhatAClickCanSelect() {
+ let list = SelectableRows(rows: [repository, cache, log], selectable: [cache, log], recommended: [cache])
+
+ #expect(list.selectingAll(in: [elsewhere]) == [cache, log, elsewhere])
+ #expect(list.isAllSelected(in: [cache, log]))
+ }
+
+ @Test func selectRecommendedTakesOutWhatPeelDoesNotRecommend() {
+ let list = SelectableRows(
+ rows: [repository, cache, log], selectable: [repository, cache, log], recommended: [cache]
+ )
+ let selection: Set = [repository, log, elsewhere]
+
+ #expect(!list.isRecommendedSelected(in: selection))
+ #expect(list.selectingRecommended(in: selection) == [cache, elsewhere])
+ #expect(list.isRecommendedSelected(in: [cache, elsewhere]))
+ #expect(!list.isRecommendedSelected(in: [cache, log]))
+ }
+
+ @Test func selectAllAsksOnlyAboutWhatPeelDoesNotRecommend() {
+ let list = SelectableRows(
+ rows: [repository, cache, log], selectable: [repository, cache, log], recommended: [cache]
+ )
+
+ #expect(list.notRecommendedAdded(by: []) == [repository, log])
+ #expect(list.notRecommendedAdded(by: [repository]) == [log])
+ #expect(list.notRecommendedAdded(by: [repository, log]).isEmpty)
+ let everything = SelectableRows(rows: [cache, log], selectable: [cache, log], recommended: [cache, log])
+ #expect(everything.notRecommendedAdded(by: []).isEmpty)
+ }
+
+ @Test func peelNeverRecommendsWhatAClickCannotSelect() {
+ let list = SelectableRows(rows: [repository, cache], selectable: [cache], recommended: [repository, cache])
+
+ #expect(list.recommended == [cache])
+ #expect(list.selectingRecommended(in: []) == [cache])
+ }
+
+ @Test func deselectAllLeavesOtherListsAlone() {
+ let list = SelectableRows(rows: [cache, log], selectable: [cache, log], recommended: [cache])
+
+ #expect(list.isNoneSelected(in: [elsewhere]))
+ #expect(!list.isNoneSelected(in: [log, elsewhere]))
+ #expect(list.deselectingAll(in: [log, elsewhere]) == [elsewhere])
+ let locked = SelectableRows(rows: [repository, cache], selectable: [cache], recommended: [cache])
+ #expect(!locked.isNoneSelected(in: [repository]))
+ }
+}
diff --git a/Peel/Views/SelectAllButton.swift b/Peel/Views/SelectAllButton.swift
index ef0eae91..f2b9a031 100644
--- a/Peel/Views/SelectAllButton.swift
+++ b/Peel/Views/SelectAllButton.swift
@@ -11,9 +11,10 @@ struct SelectAllButton<ID: Hashable>: View {
var body: some View {
if !selectable.isEmpty {
- let isAllSelected = SelectAll.isAllSelected(selectable, in: selection)
+ let list = SelectableRows(rows: rows, selectable: selectable, recommended: selectable)
+ let isAllSelected = list.isAllSelected(in: selection)
Button {
- selection = SelectAll.toggled(selection, selectable: selectable, rows: rows)
+ selection = isAllSelected ? list.deselectingAll(in: selection) : list.selectingAll(in: selection)
} label: {
Text(isAllSelected ? "Deselect All" : "Select All")
.minimumTarget()