using diffable datasource for collectionview

fixed stretched images in uikit gifview
testing always animating tableview changes
added adding to emojipack support from the sheet lol
This commit is contained in:
Nihaal Sharma committed 2026-05-08 09:23:00 +01:00
1 parent d0358e48f0
commit cdd6dc5b4e
7 files changed
+44 -24

No files matched your search

@@ -110,19 +110,17 @@ struct EmojiPackDetailView: View {
SearchView(
hoarder: hoarder,
fromPackEditor: true,
callback: { emoji in
pack.add(emoji.name)
}
callback: { pack.add($0); showAdder.toggle() }
)
.navigationTitle("Search Emojis")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
Button("", systemImage: "xmark") {
Button("", systemImage: "check") {
showAdder.toggle()
}
}
.modifier(presentationHalfAndFullIfAv())
}
.modifier(presentationHalfAndFullIfAv())
}
}
}
@@ -111,7 +111,7 @@ struct EmojiPackGridView: View {
NavigationView2 {
SearchView(hoarder: hoarder, fromPackEditor: true) { selection in
withAnimation(.spring) {
pack.add(selection.name)
pack.add(selection)
}
}
.navigationTitle("Search Emojis")
+2 -2
View File
@@ -18,7 +18,7 @@ struct SearchView: View {
@State var stickerType: StickerType = .slackEmoji
@State var fromPackEditor: Bool = false
@State var callback: ((Emoji) -> Void) = { _ in }
@State var callback: ((String) -> Void) = { _ in }
var minColWidth: CGFloat { 75 }
var spacing: CGFloat { 10 }
var col: GridItem {
@@ -51,7 +51,7 @@ struct SearchView: View {
// .padding()
// }
// }
EmojiCollectionView(hoarder: hoarder, items: searchResult, width: 75, style: .plain)
EmojiCollectionView(hoarder: hoarder, items: searchResult, width: 75, style: .plain, onTapCallback: { callback($0) })
} else {
Picker("", selection: $stickerType) {
ForEach(StickerType.allCases) { type in
@@ -17,12 +17,12 @@ struct EmojiCollectionView: UIViewRepresentable {
let style: EmojiCollectionView.Style
var edit: Bool?
var onRemoveSUI: ((String) -> Void)?
var onTapCallback: ((String) -> Void)?
func makeUIView(context: Context) -> UICollectionView {
let collectionView = context.coordinator as UICollectionViewController
collectionView.collectionView.register(PlainEmojiCollectionViewCell.self, forCellWithReuseIdentifier: "plain")
collectionView.collectionView.register(EmojiCollectionViewCell.self, forCellWithReuseIdentifier: "full")
collectionView.collectionView.dataSource = context.coordinator
collectionView.collectionView.delegate = context.coordinator
return collectionView.collectionView
}
@@ -30,6 +30,7 @@ struct EmojiCollectionView: UIViewRepresentable {
func updateUIView(_ uiView: UICollectionView, context: Context) {
context.coordinator.hoarder = hoarder
context.coordinator.onRemove = onRemoveSUI
context.coordinator.onTap = onTapCallback
context.coordinator.edit = edit
if edit ?? false {
@@ -44,9 +45,22 @@ struct EmojiCollectionView: UIViewRepresentable {
}
}
if items != context.coordinator.items {
// if items != context.coordinator.items {
// context.coordinator.items = items
// uiView.reloadData()
// }
let itemsBefore = context.coordinator.items
let itemsAfter = items
context.coordinator.items = items
uiView.reloadData()
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)
// }
}
}
@@ -73,6 +87,7 @@ struct EmojiCollectionView: UIViewRepresentable {
var style: EmojiCollectionView.Style
var edit: Bool? = false
var onRemove: ((String) -> Void)?
var onTap: ((String) -> Void)?
var dataSource: UICollectionViewDiffableDataSource<Int, String>!
@@ -97,7 +112,7 @@ struct EmojiCollectionView: UIViewRepresentable {
super.init(collectionViewLayout: layout)
self.dataSource = UICollectionViewDiffableDataSource<Int, String>(collectionView: collectionView) { (collectionView: UICollectionView, indexPath: IndexPath, itemIdentifier: String) -> UICollectionViewCell? in
collectionView.cellForItem(at: indexPath)
self.collectionView(collectionView, cellForItemAt: indexPath)
}
collectionView.dataSource = dataSource
@@ -136,6 +151,9 @@ struct EmojiCollectionView: UIViewRepresentable {
cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell {
let cell: PlainEmojiCollectionViewCell
guard !hoarder.trie.dict.isEmpty else { return UICollectionViewCell() }
let emojiName = items[indexPath.item]
switch style {
case .plain, .plainWithMenu, .jumboMoji:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "plain", for: indexPath) as! PlainEmojiCollectionViewCell
@@ -145,13 +163,14 @@ struct EmojiCollectionView: UIViewRepresentable {
(cell as! EmojiCollectionViewCell).onRemove = {
guard let index = self.items.firstIndex(of: $0) else { return }
self.items.remove(at: index)
collectionView.deleteItems(at: [IndexPath(row: index, section: 0)])
Task.detached {
await self.applySnapshot(animated: true)
}
self.onRemove?($0)
}
}
guard !hoarder.trie.dict.isEmpty else { return cell }
let emojiName = items[indexPath.item]
cell.onTap = { self.onTap?(emojiName) }
guard let emoji = hoarder.trie.dict[emojiName] else { return cell }
cell.configure(with: hoarder, emoji: emoji)
@@ -13,6 +13,7 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell {
var view: UIKitGifView = UIKitGifView(image: nil)
var hostingController: UIHostingController<StickerPreview>?
var imageLoadTask: Task<Void, Never>?
var onTap: (() -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
@@ -24,7 +25,8 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell {
view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
view.heightAnchor.constraint(equalTo: view.widthAnchor),
])
addGestureRecognizer(UITapGestureRecognizer())
let gestureRecogniser = UITapGestureRecognizer(target: self, action: #selector(handleTap))
addGestureRecognizer(gestureRecogniser)
}
required init?(coder: NSCoder) {
@@ -56,8 +58,9 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell {
// ])
}
func handleTap(sender: UITapGestureRecognizer) {
@objc func handleTap(sender: UITapGestureRecognizer) {
guard sender.state == .ended else { return }
onTap?()
print("tap")
}
@@ -28,13 +28,13 @@ struct EmojiTableView: UIViewRepresentable {
context.coordinator.items = items
// guard itemsBefore != itemsAfter else { return }
Task.detached {
if !(-10_000...10_000).contains(itemsBefore.count-itemsAfter.count) ||
itemsAfter == itemsBefore {
//diff of more than 10k
await context.coordinator.instantApplySnapshot()
} else {
// if !(-10_000...10_000).contains(itemsBefore.count-itemsAfter.count) ||
// itemsAfter == itemsBefore {
// //diff of more than 10k
// await context.coordinator.instantApplySnapshot()
// } else {
await context.coordinator.applySnapshot(animated: true)
}
// }
}
}
+1 -1
View File
@@ -10,8 +10,8 @@ import UIKit
class UIKitGifView: UIImageView {
override init(image: UIImage?) {
// <#code#>
super.init(image: image)
self.contentMode = .scaleAspectFit
}
required init?(coder: NSCoder) {
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.