diff --git a/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift b/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift index b6fcb84..7598124 100644 --- a/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift +++ b/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift @@ -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()) } } } diff --git a/StickerSlack/SwiftUI/EmojiPacks/EmojiPackGridView.swift b/StickerSlack/SwiftUI/EmojiPacks/EmojiPackGridView.swift index 0a537cc..45e2542 100644 --- a/StickerSlack/SwiftUI/EmojiPacks/EmojiPackGridView.swift +++ b/StickerSlack/SwiftUI/EmojiPacks/EmojiPackGridView.swift @@ -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") diff --git a/StickerSlack/SwiftUI/SearchView.swift b/StickerSlack/SwiftUI/SearchView.swift index 0890960..42e8422 100644 --- a/StickerSlack/SwiftUI/SearchView.swift +++ b/StickerSlack/SwiftUI/SearchView.swift @@ -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 diff --git a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift index d7a8e6a..4ce8655 100644 --- a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift +++ b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift @@ -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 { - context.coordinator.items = items - uiView.reloadData() +// if items != context.coordinator.items { +// context.coordinator.items = items +// uiView.reloadData() +// } + let itemsBefore = context.coordinator.items + 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) +// } } } @@ -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! @@ -97,7 +112,7 @@ struct EmojiCollectionView: UIViewRepresentable { super.init(collectionViewLayout: layout) self.dataSource = UICollectionViewDiffableDataSource(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) diff --git a/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift b/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift index 26e9002..45ccce8 100644 --- a/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift +++ b/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift @@ -13,6 +13,7 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell { var view: UIKitGifView = UIKitGifView(image: nil) var hostingController: UIHostingController? var imageLoadTask: Task? + 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") } diff --git a/StickerSlack/UIKit/TableView/EmojiTableView.swift b/StickerSlack/UIKit/TableView/EmojiTableView.swift index bc0a309..3949c78 100644 --- a/StickerSlack/UIKit/TableView/EmojiTableView.swift +++ b/StickerSlack/UIKit/TableView/EmojiTableView.swift @@ -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) - } +// } } } diff --git a/StickerSlack/UIKit/UIKitGifView.swift b/StickerSlack/UIKit/UIKitGifView.swift index 07742b0..295f599 100644 --- a/StickerSlack/UIKit/UIKitGifView.swift +++ b/StickerSlack/UIKit/UIKitGifView.swift @@ -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) {