update trietestingview

made uikit list view have updated emojirow
made reindexing 5x faster by making it not recompute container location
This commit is contained in:
neon443 committed 2026-03-14 22:43:31 +00:00
1 parent 7d6bad2f69
commit de9fbf0b12
5 files changed
+88 -62

No files matched your search

+1
View File
@@ -12,6 +12,7 @@ import UniformTypeIdentifiers
import Haptics import Haptics
class EmojiHoarder: Hoarder, ObservableObject { class EmojiHoarder: Hoarder, ObservableObject {
static let container: URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.neon443.StickerSlack")!.appendingPathComponent("Library", conformingTo: .directory)
nonisolated static let localEmojiDB: URL = EmojiHoarder.container.appendingPathComponent("_localEmojiDB.json", conformingTo: .fileURL) nonisolated static let localEmojiDB: URL = EmojiHoarder.container.appendingPathComponent("_localEmojiDB.json", conformingTo: .fileURL)
nonisolated static let localTrie: URL = EmojiHoarder.container.appendingPathComponent("_localTrie.json", conformingTo: .fileURL) nonisolated static let localTrie: URL = EmojiHoarder.container.appendingPathComponent("_localTrie.json", conformingTo: .fileURL)
nonisolated static let localTrieDict: URL = EmojiHoarder.container.appendingPathComponent("_localTrieDict.json", conformingTo: .fileURL) nonisolated static let localTrieDict: URL = EmojiHoarder.container.appendingPathComponent("_localTrieDict.json", conformingTo: .fileURL)
+3
View File
@@ -7,9 +7,12 @@
import Foundation import Foundation
import SwiftUI import SwiftUI
import UniformTypeIdentifiers
import Combine import Combine
class GifHoarder: Hoarder, ObservableObject { class GifHoarder: Hoarder, ObservableObject {
static let container: URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.neon443.StickerSlack")!.appendingPathComponent("Library", conformingTo: .directory)
var endpoint: URL = URL(string: "api.giphy.com/v1/gifs/trending")! var endpoint: URL = URL(string: "api.giphy.com/v1/gifs/trending")!
var endpointSearch: URL = URL(string: "api.giphy.com/v1/gifs/search")! var endpointSearch: URL = URL(string: "api.giphy.com/v1/gifs/search")!
-3
View File
@@ -22,9 +22,6 @@ protocol Hoarder {
} }
extension Hoarder { extension Hoarder {
static var container: URL { FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.neon443.StickerSlack")!.appendingPathComponent("Library", conformingTo: .directory)
}
var encoder: JSONEncoder { JSONEncoder() } var encoder: JSONEncoder { JSONEncoder() }
var decoder: JSONDecoder { JSONDecoder() } var decoder: JSONDecoder { JSONDecoder() }
} }
+82 -38
View File
@@ -10,67 +10,110 @@ import SwiftUI
struct TrieTestingView: View { struct TrieTestingView: View {
@ObservedObject var hoarder: EmojiHoarder @ObservedObject var hoarder: EmojiHoarder
@State var searchTerm: String = ""
@State var searchStatus: Bool? = nil @State var searchStatus: Bool? = nil
@State var filterTerm: String = "" @State var filterTerm: String = ""
@State var filterResult: [String] = [] @State var filterResult: [String] = []
@State var filterTerm2: String = ""
@State var uikit: Bool = false @State var uikit: Bool = false
@State private var searchType: SearchType = .contains
private enum SearchType: String, CaseIterable {
case trie = "trie"
case contains = "contains"
case exact = "exact"
}
private func runSearch() {
filterResult = []
searchStatus = nil
guard !filterTerm.isEmpty else { return }
switch searchType {
case .trie:
withAnimation(.snappy) {
filterResult = hoarder.trie.search(prefix: filterTerm)
}
print("testing: trie search")
case .contains:
withAnimation(.snappy) {
filterResult = hoarder.emojis.filter({ $0.name.localizedCaseInsensitiveContains(filterTerm) }).map({ $0.name })
}
print("testing: contains search")
case .exact:
withAnimation(.snappy) {
searchStatus = hoarder.trie.search(for: filterTerm)
}
print("testing: exact search")
}
}
var body: some View { var body: some View {
VStack { VStack {
Toggle("uikit!!", isOn: $uikit) Toggle("uikit!!", isOn: $uikit)
.foregroundStyle(.blue) .foregroundStyle(.blue)
Button("reset", role: .destructive) {
hoarder.resetAllIndexes()
}
Button("add emojis!") {
hoarder.buildTrie()
}
.buttonStyle(.borderedProminent)
HStack { HStack {
TextField("", text: $searchTerm) Button("reset", role: .destructive) {
.textFieldStyle(.roundedBorder) hoarder.resetAllIndexes()
.border(.orange) }
.onChange(of: searchTerm) { _ in Spacer()
searchStatus = hoarder.trie.search(for: searchTerm) Button("add emojis!") {
hoarder.buildTrie()
}
.buttonStyle(.borderedProminent)
}
Picker("", selection: $searchType) {
ForEach(SearchType.allCases, id: \.self) { typee in
Text(typee.rawValue).tag(typee)
}
}
.pickerStyle(.segmented)
.onChange(of: searchType) { _ in
runSearch()
}
.onChange(of: filterTerm) { _ in
runSearch()
}
if searchType == .exact {
HStack {
TextField("", text: $filterTerm)
.textFieldStyle(.roundedBorder)
.border(.orange)
if let searchStatus {
Circle()
.frame(width: 20, height: 20)
.foregroundStyle(searchStatus ? .green : .red)
} else {
Text("?")
.frame(width: 20, height: 20)
} }
if let searchStatus {
Circle()
.frame(width: 20, height: 20)
.foregroundStyle(searchStatus ? .green : .red)
} else {
Text("?")
.frame(width: 20, height: 20)
} }
} }
HStack { if searchType == .trie {
TextField("", text: $filterTerm) HStack {
.textFieldStyle(.roundedBorder) TextField("", text: $filterTerm)
.border(.orange) .textFieldStyle(.roundedBorder)
.onChange(of: filterTerm) { _ in .border(.orange)
withAnimation(.snappy) { filterResult = hoarder.trie.search(prefix: filterTerm) } Text("\(filterResult.count)")
} .modifier(numericTextCompat())
Text("\(filterResult.count)") }
.modifier(numericTextCompat())
} }
TextField("", text: $filterTerm2) if searchType == .contains {
.textFieldStyle(.roundedBorder) HStack {
.border(.orange) TextField("", text: $filterTerm)
.onChange(of: filterTerm2) { _ in .textFieldStyle(.roundedBorder)
withAnimation(.snappy) { filterResult = hoarder.emojis.filter({ $0.name.localizedCaseInsensitiveContains(filterTerm2) }).map({ $0.name }) } .border(.orange)
Text("\(filterResult.count)")
.modifier(numericTextCompat())
} }
}
if uikit { if uikit {
EmojiCollectionView(hoarder: hoarder, items: filterResult) EmojiCollectionView(hoarder: hoarder, items: filterResult)
.id(filterResult)
} else { } else {
List(filterResult, id: \.self) { item in List(filterResult, id: \.self) { item in
EmojiRow(hoarder: hoarder, emoji: hoarder.trie.dict[item]!) EmojiRow(hoarder: hoarder, emoji: hoarder.trie.dict[item]!)
@@ -79,6 +122,7 @@ struct TrieTestingView: View {
Text("\(hoarder.trie.root.children.count)") Text("\(hoarder.trie.root.children.count)")
} }
.padding(.horizontal)
} }
} }
+2 -21
View File
@@ -50,27 +50,8 @@ struct EmojiCollectionView: UIViewRepresentable {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.contentConfiguration = UIHostingConfiguration { cell.contentConfiguration = UIHostingConfiguration {
HStack { EmojiRow(hoarder: hoarder, emoji: emoji)
EmojiPreview( .id(emoji)
hoarder: hoarder,
emoji: emoji
)
.frame(maxWidth: 100, maxHeight: 100)
Spacer()
if hoarder.downloadedEmojis.contains(emojiName) {
Button("", systemImage: "trash") {
fatalError()
}
.buttonStyle(.plain)
} else {
Button("", systemImage: "arrow.down.circle") {
Task {
await self.hoarder.download(emoji: emoji)
}
}
.buttonStyle(.plain)
}
}
} }
return cell return cell
} }
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.