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.

History's search works out its matches off the main thread

Țugui Dragoș01ea9c0cb936
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/SearchText.swift b/Packages/PeelCore/Sources/PeelCore/Support/SearchText.swift
index 78d62453..e50b3ed9 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/SearchText.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/SearchText.swift
@@ -9,6 +9,23 @@ public enum SearchText {
return text.range(of: query, options: options, locale: locale) != nil
|| text.range(of: query, options: options) != nil
}
+
+ /// The items whose key holds `query`, in their order, worked out off the main actor for keys that are long, such
+ /// as History's, where a removal's key names every item in it. Nil when the task that asked was canceled first, as
+ /// it is when another key is typed.
+ @concurrent
+ public static func matching<Item: Identifiable & Sendable>(
+ _ query: String, among items: [Item], keys: [Item.ID: String]
+ ) async -> [Item]? where Item.ID: Sendable {
+ var found: [Item] = []
+ for item in items {
+ if Task.isCancelled { return nil }
+ if let key = keys[item.id], matches(key, query) {
+ found.append(item)
+ }
+ }
+ return found
+ }
}
/// A list and what a search looks in for each of its items, read once: typing in a search field would otherwise
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SearchTextTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SearchTextTests.swift
index 3b0518c0..3d5840c9 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SearchTextTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SearchTextTests.swift
@@ -25,6 +25,27 @@ import Testing
#expect(SearchText.matches("Safari", "safari", locale: Locale(identifier: "ja_JP")))
}
+ private struct Entry: Identifiable, Equatable {
+ let id: Int
+ }
+
+ @Test func findsTheItemsWhoseKeyHoldsTheQueryInTheirOrder() async {
+ let items = (0..<5).map(Entry.init)
+ let keys = [0: "Duplicates\nReport 1.pdf", 1: "Developer\nnotes.txt", 2: "Duplicates\nRÉPORT 2.pdf", 4: "report"]
+
+ #expect(await SearchText.matching("report", among: items, keys: keys) == [items[0], items[2], items[4]])
+ #expect(await SearchText.matching("nothing", among: items, keys: keys) == [])
+ }
+
+ @Test func givesUpWhenTheSearchIsCanceled() async {
+ let items = (0..<5).map(Entry.init)
+ let task = Task { () -> [Entry]? in
+ withUnsafeCurrentTask { $0?.cancel() }
+ return await SearchText.matching("report", among: items, keys: [0: "report"])
+ }
+ #expect(await task.value == nil)
+ }
+
@Test func findsNothingThatIsNotThere() {
#expect(!SearchText.matches("Safari", "chrome", locale: Self.turkish))
#expect(!SearchText.matches("Safari", "", locale: Self.turkish))
diff --git a/Peel/Model/RemovalHistoryStore.swift b/Peel/Model/RemovalHistoryStore.swift
index f143c5e5..ff52426f 100644
--- a/Peel/Model/RemovalHistoryStore.swift
+++ b/Peel/Model/RemovalHistoryStore.swift
@@ -114,6 +114,8 @@ final class RemovalHistoryStore {
/// What Peel was asked to move and did not, one removal to an entry, newest first.
private(set) var refusalBatches: [RefusalBatch] = []
private(set) var refusalSearchKeys: [RefusalBatch.ID: String] = [:]
+ /// Moves only when the removals or refusals listed change, so a search over the earlier lists is made again.
+ private(set) var revision = 0
/// What the last read or change of the refusal log met, which the Not Moved list says.
private(set) var refusalProblem: RefusalLogProblem?
private(set) var problem: RemovalLogProblem?
@@ -170,6 +172,7 @@ final class RemovalHistoryStore {
guard change == refusalChange else { return }
refusalBatches = listed.batches
refusalSearchKeys = listed.searchKeys
+ revision += 1
}
/// Records a removal from one place in History, as one entry. When Peel wrote the source itself, pass it in
@@ -255,6 +258,7 @@ final class RemovalHistoryStore {
self.records = records
batches = listed.batches
searchKeys = listed.searchKeys
+ revision += 1
}
/// History's batches, newest first, and what a search looks through, or nil when `records` are those listed
diff --git a/Peel/Views/History/HistoryList.swift b/Peel/Views/History/HistoryList.swift
index 15ed47d9..1b695474 100644
--- a/Peel/Views/History/HistoryList.swift
+++ b/Peel/Views/History/HistoryList.swift
@@ -6,17 +6,27 @@ struct HistoryList: View {
@Environment(RemovalHistoryStore.self) private var history
@State private var isReloading = false
@State private var searchText = ""
+ /// What the search found, worked out off the main actor, since a removal's key names every item in it. Kept while
+ /// the next answer is worked out.
+ @State private var found: Found?
+
+ private struct Found {
+ let batches: [RemovalBatch]
+ let refusals: [RefusalBatch]
+ }
+
+ /// What starts the search again: other words, or other lists.
+ private struct Search: Hashable {
+ let text: String
+ let revision: Int
+ }
- /// The batches that match the search, or all of them when the search is empty. History keeps up to
- /// `RemovalLog.maximumRecords` records, so this list can be long.
private var batches: [RemovalBatch] {
- guard !searchText.isEmpty else { return history.batches }
- return history.batches.filter { batch in history.searchKeys[batch.id].map { SearchText.matches($0, searchText) } == true }
+ searchText.isEmpty ? history.batches : found?.batches ?? history.batches
}
private var refusals: [RefusalBatch] {
- guard !searchText.isEmpty else { return history.refusalBatches }
- return history.refusalBatches.filter { batch in history.refusalSearchKeys[batch.id].map { SearchText.matches($0, searchText) } == true }
+ searchText.isEmpty ? history.refusalBatches : found?.refusals ?? history.refusalBatches
}
var body: some View {
@@ -84,6 +94,16 @@ struct HistoryList: View {
.task {
await history.load()
}
+ .task(id: Search(text: searchText, revision: history.revision)) {
+ guard !searchText.isEmpty else {
+ found = nil
+ return
+ }
+ guard let batches = await SearchText.matching(searchText, among: history.batches, keys: history.searchKeys),
+ let refusals = await SearchText.matching(searchText, among: history.refusalBatches, keys: history.refusalSearchKeys)
+ else { return }
+ found = Found(batches: batches, refusals: refusals)
+ }
}
}