From 0c216bb8008070081a71e198ea5ddf5e84614984 Mon Sep 17 00:00:00 2001 From: Nihaal Sharma <69979447+neon443@users.noreply.github.com> Date: Fri, 20 Mar 2026 18:08:26 +0000 Subject: [PATCH 1/2] 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 --- StickerSlack/SwiftUI/DownloadedView.swift | 12 ++++++++++++ StickerSlack/SwiftUI/SearchView.swift | 23 +++++++++++------------ StickerSlack/Trie/Trie.swift | 9 +++++++-- StickerSlack/Trie/TrieTestingView.swift | 2 +- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/StickerSlack/SwiftUI/DownloadedView.swift b/StickerSlack/SwiftUI/DownloadedView.swift index e2a8c16..c2bb674 100644 --- a/StickerSlack/SwiftUI/DownloadedView.swift +++ b/StickerSlack/SwiftUI/DownloadedView.swift @@ -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) + } + } } } diff --git a/StickerSlack/SwiftUI/SearchView.swift b/StickerSlack/SwiftUI/SearchView.swift index 74afd8b..2eea310 100644 --- a/StickerSlack/SwiftUI/SearchView.swift +++ b/StickerSlack/SwiftUI/SearchView.swift @@ -12,6 +12,7 @@ struct SearchView: View { @State private var currentSearch: Task? @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 } -// } -// } + } + } } } } diff --git a/StickerSlack/Trie/Trie.swift b/StickerSlack/Trie/Trie.swift index f41fb10..b4d5398 100644 --- a/StickerSlack/Trie/Trie.swift +++ b/StickerSlack/Trie/Trie.swift @@ -42,12 +42,17 @@ class Trie: ObservableObject { } } - func search(for query: String) -> [String] { + func search(for query: String, previousQuery: String?, previousResult: Set?) -> [String] { var result: Set = 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 { diff --git a/StickerSlack/Trie/TrieTestingView.swift b/StickerSlack/Trie/TrieTestingView.swift index b83450c..e773216 100644 --- a/StickerSlack/Trie/TrieTestingView.swift +++ b/StickerSlack/Trie/TrieTestingView.swift @@ -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") From 6e5cc2d845f023c2529cfe3f4a26defbe93461e4 Mon Sep 17 00:00:00 2001 From: Nihaal Sharma <69979447+neon443@users.noreply.github.com> Date: Sat, 21 Mar 2026 11:05:45 +0000 Subject: [PATCH 2/2] fix searching again --- StickerSlack/SwiftUI/BrowseView.swift | 4 +-- StickerSlack/SwiftUI/DownloadedView.swift | 24 +++++++-------- StickerSlack/SwiftUI/SearchView.swift | 37 ++++++++++++++++++----- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/StickerSlack/SwiftUI/BrowseView.swift b/StickerSlack/SwiftUI/BrowseView.swift index 4ed1ca9..0030350 100644 --- a/StickerSlack/SwiftUI/BrowseView.swift +++ b/StickerSlack/SwiftUI/BrowseView.swift @@ -15,8 +15,8 @@ struct BrowseView: View { var body: some View { List { Picker("", selection: $browseWhat) { - ForEach(StickerType.allCases, id: \.self) { type in - Text(type.description) + ForEach(StickerType.allCases) { type in + Text(type.description).tag(type) } } .pickerStyle(.segmented) diff --git a/StickerSlack/SwiftUI/DownloadedView.swift b/StickerSlack/SwiftUI/DownloadedView.swift index c2bb674..c17e41c 100644 --- a/StickerSlack/SwiftUI/DownloadedView.swift +++ b/StickerSlack/SwiftUI/DownloadedView.swift @@ -25,6 +25,18 @@ struct DownloadedView: View { } var body: some View { + 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) + } + } GeometryReader { geo in ScrollView { let columns: Int = max(1, Int((geo.size.width - 2*spacing) / (minColWidth + spacing))) @@ -62,18 +74,6 @@ 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) - } - } } } diff --git a/StickerSlack/SwiftUI/SearchView.swift b/StickerSlack/SwiftUI/SearchView.swift index 2eea310..01f6199 100644 --- a/StickerSlack/SwiftUI/SearchView.swift +++ b/StickerSlack/SwiftUI/SearchView.swift @@ -15,30 +15,51 @@ struct SearchView: View { @State var previousSearches: [String] = [] @State var searchResult: [String] = [] + @State var stickerType: StickerType = .slackEmoji + var body: some View { NavigationStack { - List { - Text("\(searchResult.count) Result\(searchResult.count.plural)") - .contentTransition(.numericText()) - ForEach(searchResult, id: \.self) { name in - StickerRow(hoarder: hoarder, emoji: hoarder.trie.dict[name]!) + Picker("", selection: $stickerType) { + ForEach(StickerType.allCases) { type in + Text(type.description).tag(type) } } + .pickerStyle(.segmented) + switch stickerType { + case .slackEmoji: + List { + Text("\(searchResult.count) Result\(searchResult.count.plural)") + .contentTransition(.numericText()) + ForEach(searchResult, id: \.self) { name in + StickerRow(hoarder: hoarder, emoji: hoarder.trie.dict[name]!) + } + } + .fixedSize() + EmojiCollectionView(hoarder: hoarder, items: searchResult) + .id(searchResult) + case .giphyGifs: + Text("uhh") + } } .searchable(text: $searchTerm) .onChange(of: searchTerm) { new in if currentSearch != nil { currentSearch?.cancel() } - defer { previousSearches.append("") } guard !searchTerm.isEmpty else { currentSearch?.cancel() searchResult = [] + previousSearches.append(searchTerm) return } - currentSearch = Task { - let result = hoarder.trie.search(for: searchTerm, previousQuery: previousSearches.last, previousResult: Set(searchResult)) + currentSearch = Task.detached { + let result = await hoarder.trie.search( + for: searchTerm, + previousQuery: previousSearches.last, + previousResult: Set(searchResult) + ) await MainActor.run { withAnimation { searchResult = result + previousSearches.append(searchTerm) } } }