updated search algorithm to use previous search result to speed up when typing things out, eg

before
        "a": 47k -> 25k emojis
        "ab": 47k -> ~850
    after
        "a": 47k -> 25k
        "ab" 25k -> ~850
also sort search results
using a Task again
added a ui thingy for no downloaded emojis
This commit is contained in:
Nihaal Sharma committed 2026-03-20 18:08:26 +00:00
1 parent d64ca5cd42
commit 0c216bb800
4 files changed
+31 -15

No files matched your search

+12
View File
@@ -62,6 +62,18 @@ struct DownloadedView: View {
.padding(.horizontal, spacing)
}
}
if emojiHoarder.downloadedStickers.isEmpty && gifHoarder.downloadedStickers.isEmpty {
VStack(alignment: .center) {
Image(systemName: "questionmark.app.dashed")
.resizable().scaledToFit()
.frame(width: 75, height: 75)
Text("No Downloaded Stickers")
.font(.title)
.bold()
Text("Download some Slack Emojis or GIFs to use in iMessage!")
.foregroundStyle(.gray)
}
}
}
}
+11 -12
View File
@@ -12,6 +12,7 @@ struct SearchView: View {
@State private var currentSearch: Task<Void, Never>?
@State var searchTerm: String = ""
@State var previousSearches: [String] = []
@State var searchResult: [String] = []
var body: some View {
@@ -25,24 +26,22 @@ struct SearchView: View {
}
}
.searchable(text: $searchTerm)
.onChange(of: searchTerm) { _ in
.onChange(of: searchTerm) { new in
if currentSearch != nil { currentSearch?.cancel() }
defer { previousSearches.append("") }
guard !searchTerm.isEmpty else {
currentSearch?.cancel()
withAnimation { searchResult = [] }
searchResult = []
return
}
if currentSearch != nil {
withAnimation { currentSearch?.cancel() }
}
// currentSearch = Task {
let result = hoarder.trie.search(for: searchTerm)
guard !Task.isCancelled else { return }
// await MainActor.run {
withAnimation(.snappy) {
currentSearch = Task {
let result = hoarder.trie.search(for: searchTerm, previousQuery: previousSearches.last, previousResult: Set(searchResult))
await MainActor.run {
withAnimation {
searchResult = result
}
// }
// }
}
}
}
}
}
+7 -2
View File
@@ -42,12 +42,17 @@ class Trie: ObservableObject {
}
}
func search(for query: String) -> [String] {
func search(for query: String, previousQuery: String?, previousResult: Set<String>?) -> [String] {
var result: Set<String> = wordlist
if let previousQuery,
let previousResult,
query.hasPrefix(previousQuery) {
result = previousResult
}
for word in query.split(separator: " ") {
result = result.filter { $0.localizedCaseInsensitiveContains(word) }
}
return Array(result)
return Array(result).sorted()
}
func search(exactly query: String) -> Bool {
+1 -1
View File
@@ -37,7 +37,7 @@ struct TrieTestingView: View {
case .contains:
if searchTask != nil { searchTask?.cancel() }
searchTask = Task.detached {
let result = await hoarder.trie.search(for: searchTerm)
let result = await hoarder.trie.search(for: searchTerm, previousQuery: nil, previousResult: nil)
await MainActor.run {
withAnimation(.snappy) { searchResults = result }
print("testing: contains search complete")
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.