mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 20:54:51 +00:00
no longer redoing search tableview on each search
This commit is contained in:
3 files changed
+37
-37
No files matched your search
@@ -60,8 +60,8 @@ struct SearchView: View {
|
|||||||
.pickerStyle(.segmented)
|
.pickerStyle(.segmented)
|
||||||
switch stickerType {
|
switch stickerType {
|
||||||
case .slackEmoji:
|
case .slackEmoji:
|
||||||
|
Text("\(searchResult.count)")
|
||||||
EmojiTableView(hoarder: hoarder, items: searchResult)
|
EmojiTableView(hoarder: hoarder, items: searchResult)
|
||||||
.id(searchResult)
|
|
||||||
.padding(.bottom, 10)
|
.padding(.bottom, 10)
|
||||||
case .giphyGifs:
|
case .giphyGifs:
|
||||||
Text("uhh")
|
Text("uhh")
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ struct EmojiTableView: UIViewRepresentable {
|
|||||||
func makeUIView(context: Context) -> UITableView {
|
func makeUIView(context: Context) -> UITableView {
|
||||||
let tableView = context.coordinator as UITableViewController
|
let tableView = context.coordinator as UITableViewController
|
||||||
tableView.tableView.register(EmojiTableViewCell.self, forCellReuseIdentifier: "cell")
|
tableView.tableView.register(EmojiTableViewCell.self, forCellReuseIdentifier: "cell")
|
||||||
tableView.tableView.dataSource = context.coordinator
|
|
||||||
return tableView.tableView
|
return tableView.tableView
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,19 +25,13 @@ struct EmojiTableView: UIViewRepresentable {
|
|||||||
|
|
||||||
let itemsBefore = context.coordinator.items
|
let itemsBefore = context.coordinator.items
|
||||||
let itemsAfter = items
|
let itemsAfter = items
|
||||||
context.coordinator.items = itemsAfter
|
context.coordinator.items = items
|
||||||
|
Task.detached {
|
||||||
if itemsAfter.count > itemsBefore.count {
|
if itemsAfter.count == 0 {
|
||||||
var indexPaths: [IndexPath] = []
|
await context.coordinator.instantApplySnapshot()
|
||||||
for i in (itemsBefore.count-1)..<(itemsAfter.count-1) {
|
|
||||||
indexPaths.append(IndexPath(row: i, section: 0))
|
|
||||||
}
|
|
||||||
// uiView.performBatchUpdates {
|
|
||||||
// context.coordinator.items = itemsAfter
|
|
||||||
uiView.insertRows(at: indexPaths, with: .fade)
|
|
||||||
// }
|
|
||||||
} else {
|
} else {
|
||||||
uiView.reloadData()
|
await context.coordinator.applySnapshot(animated: true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,6 +40,7 @@ struct EmojiTableView: UIViewRepresentable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final class Coordinator: UITableViewController {
|
final class Coordinator: UITableViewController {
|
||||||
|
var dataSource: UITableViewDiffableDataSource<Int, String>!
|
||||||
var hoarder: EmojiHoarder
|
var hoarder: EmojiHoarder
|
||||||
var items: [String]
|
var items: [String]
|
||||||
|
|
||||||
@@ -54,29 +48,53 @@ struct EmojiTableView: UIViewRepresentable {
|
|||||||
self.hoarder = hoarder
|
self.hoarder = hoarder
|
||||||
self.items = items
|
self.items = items
|
||||||
super.init(style: .plain)
|
super.init(style: .plain)
|
||||||
|
self.dataSource = UITableViewDiffableDataSource<Int, String>(tableView: tableView) { (tableView: UITableView, indexPath: IndexPath, itemIdentifier: String) -> UITableViewCell? in
|
||||||
|
return self.cell(tableView, indexPath: indexPath, forItemIdentifier: itemIdentifier)
|
||||||
|
}
|
||||||
|
tableView.dataSource = dataSource
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
func makeSnapshot() -> NSDiffableDataSourceSnapshot<Int, String> {
|
||||||
return items.count
|
var snapshot = NSDiffableDataSourceSnapshot<Int, String>()
|
||||||
|
snapshot.appendSections([0])
|
||||||
|
snapshot.appendItems(items, toSection: 0)
|
||||||
|
return snapshot
|
||||||
}
|
}
|
||||||
|
|
||||||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
func applySnapshot(animated: Bool) async {
|
||||||
|
let snapshot = makeSnapshot()
|
||||||
|
await (self.tableView.dataSource as! UITableViewDiffableDataSource).apply(snapshot, animatingDifferences: animated)
|
||||||
|
}
|
||||||
|
|
||||||
|
func instantApplySnapshot() async {
|
||||||
|
let snapshot = makeSnapshot()
|
||||||
|
await (self.tableView.dataSource as! UITableViewDiffableDataSource).applySnapshotUsingReloadData(snapshot)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cell(
|
||||||
|
_ tableView: UITableView,
|
||||||
|
indexPath: IndexPath,
|
||||||
|
forItemIdentifier itemIdentifier: String
|
||||||
|
) -> UITableViewCell {
|
||||||
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EmojiTableViewCell
|
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! EmojiTableViewCell
|
||||||
cell.selectionStyle = .none
|
cell.selectionStyle = .none
|
||||||
|
|
||||||
guard !hoarder.trie.dict.isEmpty else { return cell }
|
guard !hoarder.trie.dict.isEmpty else { return cell }
|
||||||
|
|
||||||
let emojiName = items[indexPath.row]
|
guard let emoji = hoarder.trie.dict[itemIdentifier] else { return cell }
|
||||||
guard let emoji = hoarder.trie.dict[emojiName] else { return cell }
|
|
||||||
|
|
||||||
cell.configure(with: hoarder, emoji: emoji)
|
cell.configure(with: hoarder, emoji: emoji)
|
||||||
return cell
|
return cell
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||||
|
return items.count
|
||||||
|
}
|
||||||
|
|
||||||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||||
// <#code#>
|
// <#code#>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import UIKit
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
class EmojiTableViewCell: UITableViewCell {
|
class EmojiTableViewCell: UITableViewCell {
|
||||||
private var spinner: UIActivityIndicatorView = UIActivityIndicatorView(style: .large)
|
|
||||||
private var hostingController: UIHostingController<StickerRow<EmojiHoarder>>?
|
private var hostingController: UIHostingController<StickerRow<EmojiHoarder>>?
|
||||||
|
|
||||||
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
||||||
@@ -25,23 +24,10 @@ class EmojiTableViewCell: UITableViewCell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func configureSkeleton() {
|
func configureSkeleton() {
|
||||||
spinner.frame = frame
|
|
||||||
spinner.startAnimating()
|
|
||||||
contentView.addSubview(spinner)
|
|
||||||
|
|
||||||
spinner.translatesAutoresizingMaskIntoConstraints = false
|
|
||||||
NSLayoutConstraint.activate([
|
|
||||||
spinner.topAnchor.constraint(equalTo: contentView.topAnchor),
|
|
||||||
spinner.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
|
|
||||||
spinner.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
|
|
||||||
spinner.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
|
|
||||||
])
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func configure(with hoarder: EmojiHoarder, emoji: Emoji) {
|
func configure(with hoarder: EmojiHoarder, emoji: Emoji) {
|
||||||
let swiftUIView = StickerRow(hoarder: hoarder, sticker: emoji)
|
let swiftUIView = StickerRow(hoarder: hoarder, sticker: emoji)
|
||||||
spinner.removeFromSuperview()
|
|
||||||
|
|
||||||
if let hostingController {
|
if let hostingController {
|
||||||
hostingController.rootView = swiftUIView
|
hostingController.rootView = swiftUIView
|
||||||
@@ -66,9 +52,5 @@ class EmojiTableViewCell: UITableViewCell {
|
|||||||
|
|
||||||
hostingController?.view.removeFromSuperview()
|
hostingController?.view.removeFromSuperview()
|
||||||
hostingController = nil
|
hostingController = nil
|
||||||
if spinner.superview == nil {
|
|
||||||
configureSkeleton()
|
|
||||||
}
|
|
||||||
spinner.startAnimating()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||