mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 20:54:51 +00:00
cleanup; fixing collectionview issues
This commit is contained in:
2 files changed
+39
-30
No files matched your search
@@ -9,6 +9,7 @@ import Foundation
|
|||||||
import UIKit
|
import UIKit
|
||||||
import Haptics
|
import Haptics
|
||||||
|
|
||||||
|
@MainActor
|
||||||
final class EmojiCollectionView: UICollectionViewController, UICollectionViewDelegateFlowLayout {
|
final class EmojiCollectionView: UICollectionViewController, UICollectionViewDelegateFlowLayout {
|
||||||
var hoarder: EmojiHoarder
|
var hoarder: EmojiHoarder
|
||||||
var items: [String]
|
var items: [String]
|
||||||
@@ -44,8 +45,12 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel
|
|||||||
collectionView.register(EmojiCollectionViewCell.self, forCellWithReuseIdentifier: "full")
|
collectionView.register(EmojiCollectionViewCell.self, forCellWithReuseIdentifier: "full")
|
||||||
collectionView.delegate = self
|
collectionView.delegate = self
|
||||||
|
|
||||||
self.dataSource = UICollectionViewDiffableDataSource<Int, String>(collectionView: collectionView) { (collectionView: UICollectionView, indexPath: IndexPath, itemIdentifier: String) -> UICollectionViewCell? in
|
self.dataSource = UICollectionViewDiffableDataSource<Int, String>(collectionView: collectionView) { (
|
||||||
self.collectionView(collectionView, cellForItemAt: indexPath)
|
collectionView: UICollectionView,
|
||||||
|
indexPath: IndexPath,
|
||||||
|
itemIdentifier: String
|
||||||
|
) -> UICollectionViewCell? in
|
||||||
|
self.cellForItemInAt(collectionView, indexPath: indexPath, itemIdentifier: itemIdentifier)
|
||||||
}
|
}
|
||||||
collectionView.dataSource = dataSource
|
collectionView.dataSource = dataSource
|
||||||
|
|
||||||
@@ -115,16 +120,13 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel
|
|||||||
await (self.collectionView.dataSource as! UICollectionViewDiffableDataSource).applySnapshotUsingReloadData(snapshot)
|
await (self.collectionView.dataSource as! UICollectionViewDiffableDataSource).applySnapshotUsingReloadData(snapshot)
|
||||||
}
|
}
|
||||||
|
|
||||||
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
func cellForItemInAt(
|
||||||
return items.count
|
|
||||||
}
|
|
||||||
|
|
||||||
override func collectionView(
|
|
||||||
_ collectionView: UICollectionView,
|
_ collectionView: UICollectionView,
|
||||||
cellForItemAt indexPath: IndexPath
|
indexPath: IndexPath,
|
||||||
|
itemIdentifier: String
|
||||||
) -> UICollectionViewCell {
|
) -> UICollectionViewCell {
|
||||||
let cell: PlainEmojiCollectionViewCell
|
let cell: PlainEmojiCollectionViewCell
|
||||||
let emojiName = items[indexPath.item]
|
let emojiName = itemIdentifier
|
||||||
|
|
||||||
switch style {
|
switch style {
|
||||||
case .plain, .plainWithMenu, .jumboMoji:
|
case .plain, .plainWithMenu, .jumboMoji:
|
||||||
@@ -135,7 +137,7 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel
|
|||||||
(cell as! EmojiCollectionViewCell).onRemove = {
|
(cell as! EmojiCollectionViewCell).onRemove = {
|
||||||
guard let index = self.items.firstIndex(of: $0) else { return }
|
guard let index = self.items.firstIndex(of: $0) else { return }
|
||||||
self.items.remove(at: index)
|
self.items.remove(at: index)
|
||||||
Task.detached {
|
Task.detached { @MainActor in
|
||||||
await self.applySnapshot(animated: true)
|
await self.applySnapshot(animated: true)
|
||||||
}
|
}
|
||||||
self.onRemove?($0)
|
self.onRemove?($0)
|
||||||
@@ -175,6 +177,8 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel
|
|||||||
override func setEditing(_ editing: Bool, animated: Bool) {
|
override func setEditing(_ editing: Bool, animated: Bool) {
|
||||||
super.setEditing(editing, animated: animated)
|
super.setEditing(editing, animated: animated)
|
||||||
|
|
||||||
|
let offset = collectionView.contentOffset
|
||||||
|
|
||||||
onEditChange?()
|
onEditChange?()
|
||||||
|
|
||||||
for cell in collectionView.visibleCells {
|
for cell in collectionView.visibleCells {
|
||||||
@@ -185,10 +189,12 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel
|
|||||||
|
|
||||||
if editing {
|
if editing {
|
||||||
self.startAnimating()
|
self.startAnimating()
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
self.stopAnimating()
|
self.stopAnimating()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
collectionView.layoutIfNeeded()
|
||||||
|
collectionView.contentOffset = offset
|
||||||
}
|
}
|
||||||
|
|
||||||
override func collectionView(
|
override func collectionView(
|
||||||
@@ -245,7 +251,7 @@ final class EmojiCollectionView: UICollectionViewController, UICollectionViewDel
|
|||||||
await self.hoarder.delete(emoji: self.hoarder.trie.dict[emojiName])
|
await self.hoarder.delete(emoji: self.hoarder.trie.dict[emojiName])
|
||||||
}
|
}
|
||||||
self.items.remove(at: indexPath.row)
|
self.items.remove(at: indexPath.row)
|
||||||
Task.detached {
|
Task.detached { @MainActor in
|
||||||
await self.applySnapshot(animated: true)
|
await self.applySnapshot(animated: true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import SwiftUI
|
|||||||
class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
|
class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
|
||||||
let label = UILabel()
|
let label = UILabel()
|
||||||
let button = UIButton(type: .custom)
|
let button = UIButton(type: .custom)
|
||||||
var edit: Bool?
|
var edit: Bool = false
|
||||||
var onRemove: ((String) -> Void)?
|
var onRemove: ((String) -> Void)?
|
||||||
|
|
||||||
override init(frame: CGRect) {
|
override init(frame: CGRect) {
|
||||||
@@ -25,7 +25,6 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
|
|||||||
contentView.addSubview(label)
|
contentView.addSubview(label)
|
||||||
|
|
||||||
button.alpha = 0
|
button.alpha = 0
|
||||||
button.isHidden = true
|
|
||||||
button.isEnabled = false
|
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
|
||||||
@@ -63,22 +62,19 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
|
|||||||
contentView.bringSubviewToFront(button)
|
contentView.bringSubviewToFront(button)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setEdit(to newValue: Bool?) {
|
func setEdit(to editing: Bool, animated: Bool = true) {
|
||||||
self.edit = newValue
|
self.edit = editing
|
||||||
if edit ?? false {
|
button.isEnabled = editing
|
||||||
contentView.bringSubviewToFront(button)
|
button.isUserInteractionEnabled = editing
|
||||||
self.button.isHidden = false
|
|
||||||
button.isEnabled = true
|
let changes = {
|
||||||
UIView.animate(withDuration: 0.2) {
|
self.button.alpha = editing ? 1 : 0
|
||||||
self.button.alpha = 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if animated {
|
||||||
|
UIView.animate(withDuration: 0.2, animations: changes)
|
||||||
} else {
|
} else {
|
||||||
UIView.animate(withDuration: 0.2) {
|
changes()
|
||||||
self.button.alpha = 0
|
|
||||||
} completion: { _ in
|
|
||||||
self.button.isHidden = true
|
|
||||||
self.button.isEnabled = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,13 +103,20 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
|
|||||||
let layoutAttributes = super.preferredLayoutAttributesFitting(layoutAttributes)
|
let layoutAttributes = super.preferredLayoutAttributesFitting(layoutAttributes)
|
||||||
guard label.text != nil else { return layoutAttributes }
|
guard label.text != nil else { return layoutAttributes }
|
||||||
let labelHeight = label.sizeThatFits(CGSize(width: layoutAttributes.size.width, height: .infinity)).height
|
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
|
return layoutAttributes
|
||||||
}
|
}
|
||||||
|
|
||||||
override func prepareForReuse() {
|
override func prepareForReuse() {
|
||||||
super.prepareForReuse()
|
super.prepareForReuse()
|
||||||
label.text = nil
|
label.text = nil
|
||||||
setEdit(to: edit)
|
onRemove = nil
|
||||||
|
button.alpha = 0
|
||||||
|
button.isEnabled = false
|
||||||
|
button.isUserInteractionEnabled = false
|
||||||
|
edit = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||