From de4b987d48cba9b4740c4353268cae1d2e7e88c6 Mon Sep 17 00:00:00 2001 From: Nihaal Sharma <69979447+neon443@users.noreply.github.com> Date: Tue, 25 Aug 2026 16:07:37 +0530 Subject: [PATCH] cleanup; fixing collectionview issues --- .../CollectionView/EmojiCollectionView.swift | 30 ++++++++------ .../EmojiCollectionViewCell.swift | 41 ++++++++++--------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift index 78f20d2..c13e01e 100644 --- a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift +++ b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift @@ -9,6 +9,7 @@ import Foundation import UIKit import Haptics +@MainActor final class EmojiCollectionView: UICollectionViewController, UICollectionViewDelegateFlowLayout { var hoarder: EmojiHoarder var items: [String] @@ -44,8 +45,12 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel collectionView.register(EmojiCollectionViewCell.self, forCellWithReuseIdentifier: "full") collectionView.delegate = self - self.dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { (collectionView: UICollectionView, indexPath: IndexPath, itemIdentifier: String) -> UICollectionViewCell? in - self.collectionView(collectionView, cellForItemAt: indexPath) + self.dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { ( + collectionView: UICollectionView, + indexPath: IndexPath, + itemIdentifier: String + ) -> UICollectionViewCell? in + self.cellForItemInAt(collectionView, indexPath: indexPath, itemIdentifier: itemIdentifier) } collectionView.dataSource = dataSource @@ -115,16 +120,13 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel await (self.collectionView.dataSource as! UICollectionViewDiffableDataSource).applySnapshotUsingReloadData(snapshot) } - override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return items.count - } - - override func collectionView( + func cellForItemInAt( _ collectionView: UICollectionView, - cellForItemAt indexPath: IndexPath + indexPath: IndexPath, + itemIdentifier: String ) -> UICollectionViewCell { let cell: PlainEmojiCollectionViewCell - let emojiName = items[indexPath.item] + let emojiName = itemIdentifier switch style { case .plain, .plainWithMenu, .jumboMoji: @@ -135,7 +137,7 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel (cell as! EmojiCollectionViewCell).onRemove = { guard let index = self.items.firstIndex(of: $0) else { return } self.items.remove(at: index) - Task.detached { + Task.detached { @MainActor in await self.applySnapshot(animated: true) } self.onRemove?($0) @@ -175,6 +177,8 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel override func setEditing(_ editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) + let offset = collectionView.contentOffset + onEditChange?() for cell in collectionView.visibleCells { @@ -185,10 +189,12 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel if editing { self.startAnimating() - } else { self.stopAnimating() } + + collectionView.layoutIfNeeded() + collectionView.contentOffset = offset } override func collectionView( @@ -245,7 +251,7 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel await self.hoarder.delete(emoji: self.hoarder.trie.dict[emojiName]) } self.items.remove(at: indexPath.row) - Task.detached { + Task.detached { @MainActor in await self.applySnapshot(animated: true) } } diff --git a/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift b/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift index 0737cce..43f6a9f 100644 --- a/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift +++ b/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift @@ -12,7 +12,7 @@ import SwiftUI class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { let label = UILabel() let button = UIButton(type: .custom) - var edit: Bool? + var edit: Bool = false var onRemove: ((String) -> Void)? override init(frame: CGRect) { @@ -25,7 +25,6 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { contentView.addSubview(label) button.alpha = 0 - button.isHidden = true button.isEnabled = false button.setImage(UIImage(systemName: "minus.circle.fill"), for: .normal) button.imageView?.contentMode = .scaleAspectFit @@ -63,22 +62,19 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { contentView.bringSubviewToFront(button) } - func setEdit(to newValue: Bool?) { - self.edit = newValue - if edit ?? false { - contentView.bringSubviewToFront(button) - self.button.isHidden = false - button.isEnabled = true - UIView.animate(withDuration: 0.2) { - self.button.alpha = 1 - } + func setEdit(to editing: Bool, animated: Bool = true) { + self.edit = editing + button.isEnabled = editing + button.isUserInteractionEnabled = editing + + let changes = { + self.button.alpha = editing ? 1 : 0 + } + + if animated { + UIView.animate(withDuration: 0.2, animations: changes) } else { - UIView.animate(withDuration: 0.2) { - self.button.alpha = 0 - } completion: { _ in - self.button.isHidden = true - self.button.isEnabled = false - } + changes() } } @@ -107,13 +103,20 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { let layoutAttributes = super.preferredLayoutAttributesFitting(layoutAttributes) guard label.text != nil else { return layoutAttributes } let labelHeight = label.sizeThatFits(CGSize(width: layoutAttributes.size.width, height: .infinity)).height - layoutAttributes.size = CGSize(width: layoutAttributes.size.width, height: labelHeight+4+view.frame.height) + layoutAttributes.size = CGSize( + width: layoutAttributes.size.width, + height: labelHeight + 4 + layoutAttributes.size.height + ) return layoutAttributes } override func prepareForReuse() { super.prepareForReuse() label.text = nil - setEdit(to: edit) + onRemove = nil + button.alpha = 0 + button.isEnabled = false + button.isUserInteractionEnabled = false + edit = false } }