mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 20:54:51 +00:00
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:
4 files changed
+31
-15
No files matched your search
@@ -62,6 +62,18 @@ struct DownloadedView: View {
|
|||||||
.padding(.horizontal, spacing)
|
.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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ struct SearchView: View {
|
|||||||
|
|
||||||
@State private var currentSearch: Task<Void, Never>?
|
@State private var currentSearch: Task<Void, Never>?
|
||||||
@State var searchTerm: String = ""
|
@State var searchTerm: String = ""
|
||||||
|
@State var previousSearches: [String] = []
|
||||||
@State var searchResult: [String] = []
|
@State var searchResult: [String] = []
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
@@ -25,24 +26,22 @@ struct SearchView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.searchable(text: $searchTerm)
|
.searchable(text: $searchTerm)
|
||||||
.onChange(of: searchTerm) { _ in
|
.onChange(of: searchTerm) { new in
|
||||||
|
if currentSearch != nil { currentSearch?.cancel() }
|
||||||
|
defer { previousSearches.append("") }
|
||||||
guard !searchTerm.isEmpty else {
|
guard !searchTerm.isEmpty else {
|
||||||
currentSearch?.cancel()
|
currentSearch?.cancel()
|
||||||
withAnimation { searchResult = [] }
|
searchResult = []
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if currentSearch != nil {
|
currentSearch = Task {
|
||||||
withAnimation { currentSearch?.cancel() }
|
let result = hoarder.trie.search(for: searchTerm, previousQuery: previousSearches.last, previousResult: Set(searchResult))
|
||||||
}
|
await MainActor.run {
|
||||||
// currentSearch = Task {
|
withAnimation {
|
||||||
let result = hoarder.trie.search(for: searchTerm)
|
|
||||||
guard !Task.isCancelled else { return }
|
|
||||||
// await MainActor.run {
|
|
||||||
withAnimation(.snappy) {
|
|
||||||
searchResult = result
|
searchResult = result
|
||||||
}
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
var result: Set<String> = wordlist
|
||||||
|
if let previousQuery,
|
||||||
|
let previousResult,
|
||||||
|
query.hasPrefix(previousQuery) {
|
||||||
|
result = previousResult
|
||||||
|
}
|
||||||
for word in query.split(separator: " ") {
|
for word in query.split(separator: " ") {
|
||||||
result = result.filter { $0.localizedCaseInsensitiveContains(word) }
|
result = result.filter { $0.localizedCaseInsensitiveContains(word) }
|
||||||
}
|
}
|
||||||
return Array(result)
|
return Array(result).sorted()
|
||||||
}
|
}
|
||||||
|
|
||||||
func search(exactly query: String) -> Bool {
|
func search(exactly query: String) -> Bool {
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ struct TrieTestingView: View {
|
|||||||
case .contains:
|
case .contains:
|
||||||
if searchTask != nil { searchTask?.cancel() }
|
if searchTask != nil { searchTask?.cancel() }
|
||||||
searchTask = Task.detached {
|
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 {
|
await MainActor.run {
|
||||||
withAnimation(.snappy) { searchResults = result }
|
withAnimation(.snappy) { searchResults = result }
|
||||||
print("testing: contains search complete")
|
print("testing: contains search complete")
|
||||||
|
|||||||