From ca30343e792263a9035bedd132408adea935c5d1 Mon Sep 17 00:00:00 2001 From: Nihaal Sharma <69979447+neon443@users.noreply.github.com> Date: Wed, 18 Mar 2026 09:13:17 +0000 Subject: [PATCH] improve search --- StickerSlack.xcodeproj/project.pbxproj | 6 ++++++ StickerSlack/Extensions/Int.swift | 14 ++++++++++++++ StickerSlack/SwiftUI/SearchView.swift | 12 +++++++++--- StickerSlack/Trie/Trie.swift | 4 ++-- 4 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 StickerSlack/Extensions/Int.swift diff --git a/StickerSlack.xcodeproj/project.pbxproj b/StickerSlack.xcodeproj/project.pbxproj index b984393..dc40fd5 100644 --- a/StickerSlack.xcodeproj/project.pbxproj +++ b/StickerSlack.xcodeproj/project.pbxproj @@ -79,6 +79,8 @@ A9A7A45D2F69569100BB84C6 /* GiphyUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9A7A44A2F6837A800BB84C6 /* GiphyUser.swift */; }; A9A7A45E2F6956B700BB84C6 /* StickerType.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9BE30EB2F5AFE1E00A57668 /* StickerType.swift */; }; A9A7A45F2F69570200BB84C6 /* Gif.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9BE30F12F5B004E00A57668 /* Gif.swift */; }; + A9A7A4682F6AA17C00BB84C6 /* Int.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9A7A4672F6AA17C00BB84C6 /* Int.swift */; }; + A9A7A4692F6AA17C00BB84C6 /* Int.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9A7A4672F6AA17C00BB84C6 /* Int.swift */; }; A9B9A82E2EB2CCBE004C9245 /* StickerSlackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9B9A82C2EB2CCBE004C9245 /* StickerSlackTests.swift */; }; A9B9A82F2EB2CCED004C9245 /* EmojiHoarder.swift in Sources */ = {isa = PBXBuildFile; fileRef = A949B1F72EA04F2300215164 /* EmojiHoarder.swift */; }; A9B9A8302EB2CD0B004C9245 /* Emoji.swift in Sources */ = {isa = PBXBuildFile; fileRef = A924C3712EA9127200F20781 /* Emoji.swift */; }; @@ -189,6 +191,7 @@ A9A7A4472F68361200BB84C6 /* Meta.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Meta.swift; sourceTree = ""; }; A9A7A44A2F6837A800BB84C6 /* GiphyUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiphyUser.swift; sourceTree = ""; }; A9A7A44D2F68383900BB84C6 /* GiphyImages.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiphyImages.swift; sourceTree = ""; }; + A9A7A4672F6AA17C00BB84C6 /* Int.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Int.swift; sourceTree = ""; }; A9B9A8232EB2CCB5004C9245 /* StickerSlackTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = StickerSlackTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; A9B9A82C2EB2CCBE004C9245 /* StickerSlackTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StickerSlackTests.swift; sourceTree = ""; }; A9BBC5172EB8FA4500FFE82F /* ViewModifiers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewModifiers.swift; sourceTree = ""; }; @@ -253,6 +256,7 @@ children = ( A9104C7D2EB4022500D160EA /* MSSticker.swift */, A957C1762ECD008E00EA3EE9 /* Bundle.swift */, + A9A7A4672F6AA17C00BB84C6 /* Int.swift */, ); path = Extensions; sourceTree = ""; @@ -623,6 +627,7 @@ A9A7A44E2F68383900BB84C6 /* GiphyImages.swift in Sources */, A9C172DD2EB8C9AC008A7885 /* Trie.swift in Sources */, A955B3F22EC22E9700E1732D /* BrowseView.swift in Sources */, + A9A7A4692F6AA17C00BB84C6 /* Int.swift in Sources */, A9EB724F2EB94A6B00658CEB /* TrieTestingView.swift in Sources */, A924C3732EA9127200F20781 /* Emoji.swift in Sources */, A9D15B8B2EB1142C00404792 /* EmojiPack.swift in Sources */, @@ -693,6 +698,7 @@ A9EB724D2EB94A6B00658CEB /* TrieTestingView.swift in Sources */, A9BBC51A2EB8FA4500FFE82F /* ViewModifiers.swift in Sources */, A955B3ED2EC22C4A00E1732D /* DownloadedView.swift in Sources */, + A9A7A4682F6AA17C00BB84C6 /* Int.swift in Sources */, A9C172DC2EB8C9AC008A7885 /* Trie.swift in Sources */, A957C1752ECCE2CE00EA3EE9 /* SettingsView.swift in Sources */, A9CD6C362EDDE22800B7F421 /* EmojiPackView.swift in Sources */, diff --git a/StickerSlack/Extensions/Int.swift b/StickerSlack/Extensions/Int.swift new file mode 100644 index 0000000..fdcb576 --- /dev/null +++ b/StickerSlack/Extensions/Int.swift @@ -0,0 +1,14 @@ +// +// Int.swift +// StickerSlack +// +// Created by neon443 on 18/03/2026. +// + +import Foundation + +extension Int { + var plural: String { + return self == 1 ? "" : "s" + } +} diff --git a/StickerSlack/SwiftUI/SearchView.swift b/StickerSlack/SwiftUI/SearchView.swift index 4859a30..aa1179d 100644 --- a/StickerSlack/SwiftUI/SearchView.swift +++ b/StickerSlack/SwiftUI/SearchView.swift @@ -16,11 +16,18 @@ struct SearchView: View { var body: some View { NavigationStack { - List(searchResult, id: \.self) { name in - StickerRow(hoarder: hoarder, emoji: hoarder.trie.dict[name]!) + List() { + Text("\(searchResult.count) Result\(searchResult.count.plural)") + ForEach(searchResult, id: \.self) { name in + StickerRow(hoarder: hoarder, emoji: hoarder.trie.dict[name]!) + } } } + .searchable(text: $searchTerm) .onChange(of: searchTerm) { _ in + if searchTerm.isEmpty { searchResult = [] } + } + .onSubmit(of: .search) { guard !searchTerm.isEmpty else { currentSearch?.cancel() searchResult = [] @@ -36,7 +43,6 @@ struct SearchView: View { } } } - .searchable(text: $searchTerm) } } diff --git a/StickerSlack/Trie/Trie.swift b/StickerSlack/Trie/Trie.swift index faf688d..b1c9649 100644 --- a/StickerSlack/Trie/Trie.swift +++ b/StickerSlack/Trie/Trie.swift @@ -42,11 +42,11 @@ class Trie: ObservableObject { } func search(for query: String) -> [String] { - var result: [String] = wordlist + var result: Set = Set(wordlist) for word in query.split(separator: " ") { result = result.filter { $0.localizedCaseInsensitiveContains(word) } } - return result + return Array(result) } func search(exactly query: String) -> Bool {