From ab0320e4d1b3b4a6a9177208c46d384f575404af Mon Sep 17 00:00:00 2001 From: Nihaal Sharma <69979447+neon443@users.noreply.github.com> Date: Fri, 1 May 2026 17:00:43 +0100 Subject: [PATCH] no longer redoing search tableview on each search --- StickerSlack/SwiftUI/SearchView.swift | 2 +- .../UIKit/TableView/EmojiTableView.swift | 54 ++++++++++++------- .../UIKit/TableView/EmojiTableViewCell.swift | 18 ------- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/StickerSlack/SwiftUI/SearchView.swift b/StickerSlack/SwiftUI/SearchView.swift index 521426c..2153113 100644 --- a/StickerSlack/SwiftUI/SearchView.swift +++ b/StickerSlack/SwiftUI/SearchView.swift @@ -60,8 +60,8 @@ struct SearchView: View { .pickerStyle(.segmented) switch stickerType { case .slackEmoji: + Text("\(searchResult.count)") EmojiTableView(hoarder: hoarder, items: searchResult) - .id(searchResult) .padding(.bottom, 10) case .giphyGifs: Text("uhh") diff --git a/StickerSlack/UIKit/TableView/EmojiTableView.swift b/StickerSlack/UIKit/TableView/EmojiTableView.swift index 24f4382..7118ea0 100644 --- a/StickerSlack/UIKit/TableView/EmojiTableView.swift +++ b/StickerSlack/UIKit/TableView/EmojiTableView.swift @@ -17,7 +17,6 @@ struct EmojiTableView: UIViewRepresentable { func makeUIView(context: Context) -> UITableView { let tableView = context.coordinator as UITableViewController tableView.tableView.register(EmojiTableViewCell.self, forCellReuseIdentifier: "cell") - tableView.tableView.dataSource = context.coordinator return tableView.tableView } @@ -26,19 +25,13 @@ struct EmojiTableView: UIViewRepresentable { let itemsBefore = context.coordinator.items let itemsAfter = items - context.coordinator.items = itemsAfter - - if itemsAfter.count > itemsBefore.count { - var indexPaths: [IndexPath] = [] - for i in (itemsBefore.count-1)..<(itemsAfter.count-1) { - indexPaths.append(IndexPath(row: i, section: 0)) + context.coordinator.items = items + Task.detached { + if itemsAfter.count == 0 { + await context.coordinator.instantApplySnapshot() + } else { + await context.coordinator.applySnapshot(animated: true) } -// uiView.performBatchUpdates { -// context.coordinator.items = itemsAfter - uiView.insertRows(at: indexPaths, with: .fade) -// } - } else { - uiView.reloadData() } } @@ -47,6 +40,7 @@ struct EmojiTableView: UIViewRepresentable { } final class Coordinator: UITableViewController { + var dataSource: UITableViewDiffableDataSource! var hoarder: EmojiHoarder var items: [String] @@ -54,29 +48,53 @@ struct EmojiTableView: UIViewRepresentable { self.hoarder = hoarder self.items = items super.init(style: .plain) + self.dataSource = UITableViewDiffableDataSource(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) { fatalError("init(coder:) has not been implemented") } - override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return items.count + func makeSnapshot() -> NSDiffableDataSourceSnapshot { + var snapshot = NSDiffableDataSourceSnapshot() + 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 cell.selectionStyle = .none guard !hoarder.trie.dict.isEmpty else { return cell } - let emojiName = items[indexPath.row] - guard let emoji = hoarder.trie.dict[emojiName] else { return cell } + guard let emoji = hoarder.trie.dict[itemIdentifier] else { return cell } cell.configure(with: hoarder, emoji: emoji) return cell } + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return items.count + } + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // <#code#> } diff --git a/StickerSlack/UIKit/TableView/EmojiTableViewCell.swift b/StickerSlack/UIKit/TableView/EmojiTableViewCell.swift index a861d97..855e2a9 100644 --- a/StickerSlack/UIKit/TableView/EmojiTableViewCell.swift +++ b/StickerSlack/UIKit/TableView/EmojiTableViewCell.swift @@ -10,7 +10,6 @@ import UIKit import SwiftUI class EmojiTableViewCell: UITableViewCell { - private var spinner: UIActivityIndicatorView = UIActivityIndicatorView(style: .large) private var hostingController: UIHostingController>? override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { @@ -25,23 +24,10 @@ class EmojiTableViewCell: UITableViewCell { } 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) { let swiftUIView = StickerRow(hoarder: hoarder, sticker: emoji) - spinner.removeFromSuperview() if let hostingController { hostingController.rootView = swiftUIView @@ -66,9 +52,5 @@ class EmojiTableViewCell: UITableViewCell { hostingController?.view.removeFromSuperview() hostingController = nil - if spinner.superview == nil { - configureSkeleton() - } - spinner.startAnimating() } }