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.

The iCloud Drive page filters its list once, not at every checkbox

Țugui Dragoș99fc156c850c
diff --git a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
index 7eb1b7ae..c040ee4b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Space/CloudStorage.swift
@@ -261,11 +261,9 @@ public enum CloudStorage {
}
extension CloudFile {
- /// The row shows an abbreviated path, so the whole path is searched and not only the file name.
- public func matches(_ query: String) -> Bool {
- guard !query.isEmpty else { return true }
- return SearchText.matches(name, query)
- || SearchText.matches(container, query)
- || SearchText.matches(url.path(percentEncoded: false), query)
+ /// What a search looks in: the folder it belongs to, and the whole path, which ends in its name, since the row
+ /// shows an abbreviated path. A line end keeps a query from matching across the two.
+ public var searchKey: String {
+ container + "\n" + url.path(percentEncoded: false)
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/SearchText.swift b/Packages/PeelCore/Sources/PeelCore/Support/SearchText.swift
index e1e40db0..78d62453 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/SearchText.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/SearchText.swift
@@ -10,3 +10,21 @@ public enum SearchText {
|| text.range(of: query, options: options) != nil
}
}
+
+/// A list and what a search looks in for each of its items, read once: typing in a search field would otherwise
+/// read every item again at each key.
+public struct SearchableList<Item: Sendable>: Sendable {
+ public let items: [Item]
+ private let keys: [String]
+
+ public init(_ items: [Item], key: (Item) -> String) {
+ self.items = items
+ keys = items.map(key)
+ }
+
+ /// The items whose key holds `query`, as `SearchText` compares; all of them for an empty query.
+ public func matching(_ query: String) -> [Item] {
+ guard !query.isEmpty else { return items }
+ return zip(items, keys).filter { SearchText.matches($0.1, query) }.map(\.0)
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift
index 71f47663..e8e9c62b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift
@@ -52,10 +52,12 @@ struct SearchingTests {
modified: nil
)
- #expect(file.matches("muzica"))
- #expect(file.matches("CloudDocs"))
- #expect(file.matches("iCloud"))
- #expect(!file.matches("Wallpaper"))
+ let list = SearchableList([file], key: \.searchKey)
+ #expect(list.matching("muzica") == [file])
+ #expect(list.matching("CloudDocs") == [file])
+ #expect(list.matching("iCloud") == [file])
+ #expect(list.matching("Wallpaper").isEmpty)
+ #expect(list.matching("Drive/Users").isEmpty)
}
@Test func findsIntelSoftwareByNameOrTheAppItSitsIn() {
@@ -81,13 +83,15 @@ struct SearchingTests {
modified: nil
)
- #expect(file.matches("sedinta"))
- #expect(file.matches("ȘEDINȚĂ"))
+ let list = SearchableList([file], key: \.searchKey)
+ #expect(list.matching("sedinta") == [file])
+ #expect(list.matching("ȘEDINȚĂ") == [file])
}
@Test func anEmptyQueryKeepsEverything() {
#expect(duplicates("/a/b.txt").matches(""))
- #expect(CloudFile(url: URL(filePath: "/a"), name: "a", container: "c", size: 1, modified: nil).matches(""))
+ let file = CloudFile(url: URL(filePath: "/a"), name: "a", container: "c", size: 1, modified: nil)
+ #expect(SearchableList([file], key: \.searchKey).matching("") == [file])
#expect(IntelFinding(url: URL(filePath: "/a"), kind: .app, name: "a", owner: nil, size: 1).matches(""))
}
diff --git a/Peel/Model/CloudLibrary.swift b/Peel/Model/CloudLibrary.swift
index 5b4bc139..2c32b306 100644
--- a/Peel/Model/CloudLibrary.swift
+++ b/Peel/Model/CloudLibrary.swift
@@ -4,7 +4,18 @@ import PeelCore
@Observable
final class CloudLibrary {
- private(set) var files: [CloudFile]?
+ private(set) var files: [CloudFile]? {
+ didSet {
+ searchable = SearchableList(files ?? [], key: \.searchKey)
+ lastMatch = nil
+ sizes = Dictionary((files ?? []).map { ($0.url, $0.size) }, uniquingKeysWith: { first, _ in first })
+ updateSelectedSize()
+ }
+ }
+ @ObservationIgnored private var searchable = SearchableList<CloudFile>([], key: \.searchKey)
+ /// The last search and what it found. A view asks again whenever it is drawn, a checkbox included.
+ @ObservationIgnored private var lastMatch: (query: String, files: [CloudFile])?
+ @ObservationIgnored private var sizes: [URL: Int64] = [:]
/// The scan this page runs, which a newer one or the Stop button ends.
let scanRun = ScanRun()
var isScanning: Bool { scanRun.isRunning }
@@ -13,14 +24,26 @@ final class CloudLibrary {
/// `wasCutShort`: there is more than the list shows. `couldNotRead`: iCloud Drive couldn't be read at all.
private(set) var wasCutShort = false
private(set) var couldNotRead = false
- var selectedURLs: Set<URL> = []
+ var selectedURLs: Set<URL> = [] {
+ didSet { updateSelectedSize() }
+ }
+ private(set) var selectedSize: Int64 = 0
var totalSize: Int64 {
files?.reduce(0) { $0 + $1.size } ?? 0
}
- var selectedSize: Int64 {
- files?.filter { selectedURLs.contains($0.url) }.reduce(0) { $0 + $1.size } ?? 0
+ /// The files whose folder or path holds `query`, every file for an empty one.
+ func files(matching query: String) -> [CloudFile] {
+ guard files != nil else { return [] }
+ if let lastMatch, lastMatch.query == query { return lastMatch.files }
+ let found = searchable.matching(query)
+ lastMatch = (query, found)
+ return found
+ }
+
+ private func updateSelectedSize() {
+ selectedSize = selectedURLs.reduce(0) { $0 + (sizes[$1] ?? 0) }
}
/// Scans again. What the last Free Up Space refused is cleared, since it described the old list.
diff --git a/Peel/Views/Cloud/CloudView.swift b/Peel/Views/Cloud/CloudView.swift
index 7b9ddf2a..782a9d1d 100644
--- a/Peel/Views/Cloud/CloudView.swift
+++ b/Peel/Views/Cloud/CloudView.swift
@@ -15,11 +15,8 @@ struct CloudView: View {
@State private var isRescanning = false
@State private var searchText = ""
- private var listed: [CloudFile] {
- (cloud.files ?? []).filter { $0.matches(searchText) }
- }
-
var body: some View {
+ let listed = cloud.files(matching: searchText)
List {
header
.listRowSeparator(.hidden)
@@ -77,7 +74,7 @@ struct CloudView: View {
}
.dimmedWhileBusy(cloud.isScanning)
.columnSearch(text: $searchText, prompt: "Search iCloud Drive", when: cloud.files?.isEmpty == false)
- .scanState(phase, scan: cloud.scanRun) {
+ .scanState(phase(listed: listed), scan: cloud.scanRun) {
if !searchText.isEmpty, cloud.files?.isEmpty == false, listed.isEmpty {
ContentUnavailableView.search(text: searchText)
} else if cloud.couldNotRead {
@@ -170,7 +167,7 @@ struct CloudView: View {
}
}
- private var phase: ScanPhase {
+ private func phase(listed: [CloudFile]) -> ScanPhase {
if cloud.files == nil { return cloud.scanRun.wasStopped ? .stopped : .scanning(.walk) }
if !searchText.isEmpty, cloud.files?.isEmpty == false, listed.isEmpty { return .message }
if cloud.couldNotRead { return .message }