mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 20:54:51 +00:00
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:
5 files changed
+64
-38
No files matched your search
@@ -12,6 +12,7 @@ import UniformTypeIdentifiers
|
||||
import Haptics
|
||||
|
||||
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 localTrie: URL = EmojiHoarder.container.appendingPathComponent("_localTrie.json", conformingTo: .fileURL)
|
||||
nonisolated static let localTrieDict: URL = EmojiHoarder.container.appendingPathComponent("_localTrieDict.json", conformingTo: .fileURL)
|
||||
|
||||
@@ -7,9 +7,12 @@
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import UniformTypeIdentifiers
|
||||
import Combine
|
||||
|
||||
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 endpointSearch: URL = URL(string: "api.giphy.com/v1/gifs/search")!
|
||||
|
||||
|
||||
@@ -22,9 +22,6 @@ protocol Hoarder {
|
||||
}
|
||||
|
||||
extension Hoarder {
|
||||
static var container: URL { FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.neon443.StickerSlack")!.appendingPathComponent("Library", conformingTo: .directory)
|
||||
}
|
||||
|
||||
var encoder: JSONEncoder { JSONEncoder() }
|
||||
var decoder: JSONDecoder { JSONDecoder() }
|
||||
}
|
||||
@@ -10,36 +10,77 @@ import SwiftUI
|
||||
struct TrieTestingView: View {
|
||||
@ObservedObject var hoarder: EmojiHoarder
|
||||
|
||||
@State var searchTerm: String = ""
|
||||
@State var searchStatus: Bool? = nil
|
||||
|
||||
@State var filterTerm: String = ""
|
||||
@State var filterResult: [String] = []
|
||||
|
||||
@State var filterTerm2: String = ""
|
||||
|
||||
@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 {
|
||||
VStack {
|
||||
Toggle("uikit!!", isOn: $uikit)
|
||||
.foregroundStyle(.blue)
|
||||
|
||||
HStack {
|
||||
Button("reset", role: .destructive) {
|
||||
hoarder.resetAllIndexes()
|
||||
}
|
||||
Spacer()
|
||||
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: $searchTerm)
|
||||
TextField("", text: $filterTerm)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.border(.orange)
|
||||
.onChange(of: searchTerm) { _ in
|
||||
searchStatus = hoarder.trie.search(for: searchTerm)
|
||||
}
|
||||
if let searchStatus {
|
||||
Circle()
|
||||
.frame(width: 20, height: 20)
|
||||
@@ -49,28 +90,30 @@ struct TrieTestingView: View {
|
||||
.frame(width: 20, height: 20)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if searchType == .trie {
|
||||
HStack {
|
||||
TextField("", text: $filterTerm)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.border(.orange)
|
||||
.onChange(of: filterTerm) { _ in
|
||||
withAnimation(.snappy) { filterResult = hoarder.trie.search(prefix: filterTerm) }
|
||||
}
|
||||
Text("\(filterResult.count)")
|
||||
.modifier(numericTextCompat())
|
||||
}
|
||||
}
|
||||
|
||||
TextField("", text: $filterTerm2)
|
||||
if searchType == .contains {
|
||||
HStack {
|
||||
TextField("", text: $filterTerm)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.border(.orange)
|
||||
.onChange(of: filterTerm2) { _ in
|
||||
withAnimation(.snappy) { filterResult = hoarder.emojis.filter({ $0.name.localizedCaseInsensitiveContains(filterTerm2) }).map({ $0.name }) }
|
||||
Text("\(filterResult.count)")
|
||||
.modifier(numericTextCompat())
|
||||
}
|
||||
}
|
||||
|
||||
if uikit {
|
||||
EmojiCollectionView(hoarder: hoarder, items: filterResult)
|
||||
.id(filterResult)
|
||||
} else {
|
||||
List(filterResult, id: \.self) { item in
|
||||
EmojiRow(hoarder: hoarder, emoji: hoarder.trie.dict[item]!)
|
||||
@@ -79,6 +122,7 @@ struct TrieTestingView: View {
|
||||
|
||||
Text("\(hoarder.trie.root.children.count)")
|
||||
}
|
||||
.padding(.horizontal)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,27 +50,8 @@ struct EmojiCollectionView: UIViewRepresentable {
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
|
||||
|
||||
cell.contentConfiguration = UIHostingConfiguration {
|
||||
HStack {
|
||||
EmojiPreview(
|
||||
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)
|
||||
}
|
||||
}
|
||||
EmojiRow(hoarder: hoarder, emoji: emoji)
|
||||
.id(emoji)
|
||||
}
|
||||
return cell
|
||||
}
|
||||
|
||||