fix performance for search and colleciton view add and remove

fixed remove button working when its not supposed to
This commit is contained in:
Nihaal Sharma committed 2026-05-08 13:18:32 +01:00
1 parent ea5f342d10
commit 744271fbb3
6 files changed
+64 -64

No files matched your search

@@ -6,6 +6,7 @@
//
import SwiftUI
import Haptics
struct EmojiPackDetailView: View {
@ObservedObject var hoarder: EmojiHoarder
@@ -110,12 +111,16 @@ struct EmojiPackDetailView: View {
SearchView(
hoarder: hoarder,
fromPackEditor: true,
callback: { pack.add($0); showAdder.toggle() }
callback: {
pack.add($0)
Haptic.success.trigger()
showAdder.toggle()
}
)
.navigationTitle("Search Emojis")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
Button("", systemImage: "check") {
Button("", systemImage: "checkmark") {
showAdder.toggle()
}
}
+7 -1
View File
@@ -51,7 +51,13 @@ struct SearchView: View {
// .padding()
// }
// }
EmojiCollectionView(hoarder: hoarder, items: searchResult, width: 75, style: .plain, onTapCallback: { callback($0) })
EmojiCollectionView(
hoarder: hoarder,
items: searchResult,
width: 75,
style: .plainWithLabel,
onTapCallback: { callback($0)
})
} else {
Picker("", selection: $stickerType) {
ForEach(StickerType.allCases) { type in
@@ -53,14 +53,12 @@ struct EmojiCollectionView: UIViewRepresentable {
let itemsAfter = items
context.coordinator.items = items
Task.detached {
// if !(-10_000...10_000).contains(itemsBefore.count-itemsAfter.count) ||
// itemsAfter == itemsBefore {
// //diff of more than 10k
// fatalError()
// await context.coordinator.instantApplySnapshot()
// } else {
await context.coordinator.applySnapshot(animated: true)
// }
//if its being cleared or adding 10k+
guard !itemsAfter.isEmpty && itemsAfter.count-itemsBefore.count < 10_000 else {
await context.coordinator.instantApplySnapshot()
return
}
await context.coordinator.applySnapshot(animated: true)
}
}
@@ -75,6 +73,7 @@ struct EmojiCollectionView: UIViewRepresentable {
enum Style {
case plain
case plainWithLabel
case plainWithMenu
case full
case jumboMoji
@@ -124,6 +123,7 @@ struct EmojiCollectionView: UIViewRepresentable {
self.layout.minimumInteritemSpacing = 8
collectionView.contentInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
}
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
}
required init?(coder: NSCoder) {
@@ -136,11 +136,14 @@ struct EmojiCollectionView: UIViewRepresentable {
snapshot.appendItems(items, toSection: 0)
return snapshot
}
func applySnapshot(animated: Bool) async {
let snapshot = makeSnapshot()
await (self.collectionView.dataSource as! UICollectionViewDiffableDataSource).apply(snapshot, animatingDifferences: animated)
}
func instantApplySnapshot() async {
let snapshot = makeSnapshot()
await (self.collectionView.dataSource as! UICollectionViewDiffableDataSource).applySnapshotUsingReloadData(snapshot)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
@@ -157,7 +160,7 @@ struct EmojiCollectionView: UIViewRepresentable {
switch style {
case .plain, .plainWithMenu, .jumboMoji:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "plain", for: indexPath) as! PlainEmojiCollectionViewCell
case .full:
case .full, .plainWithLabel:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "full", for: indexPath) as! EmojiCollectionViewCell
(cell as! EmojiCollectionViewCell).setEdit(to: edit)
(cell as! EmojiCollectionViewCell).onRemove = {
@@ -169,10 +172,10 @@ struct EmojiCollectionView: UIViewRepresentable {
self.onRemove?($0)
}
}
cell.onTap = { self.onTap?(emojiName) }
guard let emoji = hoarder.trie.dict[emojiName] else { return cell }
cell.width = width
cell.style = style
cell.onTap = { self.onTap?(emojiName) }
cell.configure(with: hoarder, emoji: emoji)
return cell
}
@@ -200,53 +203,6 @@ struct EmojiCollectionView: UIViewRepresentable {
}
}
func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
let totalWidth = collectionView.bounds.width
let availWidth = totalWidth-(collectionView.contentInset.left*2)
let cols: CGFloat
if style == .jumboMoji {
cols = width
} else {
cols = max(1, floor(availWidth/width))
}
let totalSpacing = layout.minimumInteritemSpacing * (cols - 1)
let itemWidth = (availWidth-totalSpacing)/cols
let defaultSize = CGSize(width: itemWidth, height: itemWidth)
guard self.style == .full else { return defaultSize }
guard !hoarder.trie.dict.isEmpty else { return defaultSize }
let emojiName = items[indexPath.item]
let labelHeight: CGFloat
if let widthDict = EmojiCollectionView.Coordinator.labelSizeDict[itemWidth],
let storedHeight = widthDict[emojiName] {
labelHeight = storedHeight
} else {
let label = UILabel()
label.font = .preferredFont(forTextStyle: .caption1)
label.numberOfLines = 0
label.text = emojiName
labelHeight = label.sizeThatFits(CGSize(width: itemWidth, height: .infinity)).height
if EmojiCollectionView.Coordinator.labelSizeDict[itemWidth] != nil {
EmojiCollectionView.Coordinator.labelSizeDict[itemWidth]![emojiName] = labelHeight
} else {
EmojiCollectionView.Coordinator.labelSizeDict[itemWidth] = [emojiName: labelHeight]
}
}
return CGSize(width: itemWidth, height: itemWidth+labelHeight+4)
// let font = UIFont.preferredFont(forTextStyle: .caption1)
// let rect = (emojiName as NSString).boundingRect(
// with: CGSize(width: itemWidth, height: .greatestFiniteMagnitude),
// options: [.usesLineFragmentOrigin, .usesFontLeading],
// attributes: [.font: font],
// context: nil
// )
}
override func collectionView(
_ collectionView: UICollectionView,
contextMenuConfigurationForItemAt indexPath: IndexPath,
@@ -26,6 +26,7 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
button.alpha = 0
button.isHidden = true
button.isEnabled = false
button.setImage(UIImage(systemName: "minus.circle.fill"), for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.contentHorizontalAlignment = .fill
@@ -67,6 +68,7 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
if edit ?? false {
contentView.bringSubviewToFront(button)
self.button.isHidden = false
button.isEnabled = true
UIView.animate(withDuration: 0.2) {
self.button.alpha = 1
}
@@ -75,6 +77,7 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
self.button.alpha = 0
} completion: { _ in
self.button.isHidden = true
self.button.isEnabled = false
}
}
}
@@ -100,6 +103,14 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
return super.hitTest(point, with: event)
}
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
super.preferredLayoutAttributesFitting(layoutAttributes)
guard label.text != nil else { return layoutAttributes }
let labelHeight = label.sizeThatFits(CGSize(width: self.contentView.frame.width, height: .infinity)).height
layoutAttributes.size = CGSize(width: self.contentView.frame.width, height: labelHeight+4+view.frame.height)
return layoutAttributes
}
override func prepareForReuse() {
super.prepareForReuse()
label.text = nil
@@ -13,6 +13,8 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell {
var view: UIKitGifView = UIKitGifView(image: nil)
var hostingController: UIHostingController<StickerPreview>?
var imageLoadTask: Task<Void, Never>?
var width: CGFloat = 0
var style: EmojiCollectionView.Style = .plain
var onTap: (() -> Void)?
override init(frame: CGRect) {
@@ -64,6 +66,25 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell {
print("tap")
}
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
super.preferredLayoutAttributesFitting(layoutAttributes)
guard let superview,
let collectionView = superview as? UICollectionView else { return layoutAttributes }
let totalWidth = collectionView.bounds.width
let availWidth = totalWidth-collectionView.contentInset.left-collectionView.contentInset.right
let cols: CGFloat
if style == .jumboMoji {
cols = width
} else {
cols = max(1, floor(availWidth/width))
}
let totalSpacing = (collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.minimumInteritemSpacing ?? 0 * (cols-1)
let itemWidth = (availWidth-totalSpacing)/cols
layoutAttributes.size = CGSize(width: itemWidth, height: layoutAttributes.size.height)
return layoutAttributes
}
override func prepareForReuse() {
super.prepareForReuse()
@@ -27,7 +27,8 @@ struct EmojiTableView: UIViewRepresentable {
let itemsAfter = items
context.coordinator.items = items
Task.detached {
guard !itemsAfter.isEmpty || itemsBefore.count-itemsAfter.count > 10_000 else {
//if its being cleared or removign 10k+
guard !itemsAfter.isEmpty && itemsBefore.count-itemsAfter.count < 10_000 else {
await context.coordinator.instantApplySnapshot()
return
}
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.