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
+63 -63

No files matched your search

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