mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 12:44:51 +00:00
animated delete of cells from pack
This commit is contained in:
3 files changed
+32
-20
No files matched your search
@@ -79,7 +79,7 @@ struct EmojiPackDetailView: View {
|
|||||||
width: 75,
|
width: 75,
|
||||||
style: .full,
|
style: .full,
|
||||||
edit: edit,
|
edit: edit,
|
||||||
onRemove: { pack.remove($0) }
|
onRemoveSUI: { pack.remove($0) }
|
||||||
)
|
)
|
||||||
|
|
||||||
GeometryReader { geo in
|
GeometryReader { geo in
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ struct EmojiCollectionView: UIViewRepresentable {
|
|||||||
let width: CGFloat
|
let width: CGFloat
|
||||||
let style: EmojiCollectionView.Style
|
let style: EmojiCollectionView.Style
|
||||||
var edit: Bool?
|
var edit: Bool?
|
||||||
var onRemove: ((String) -> Void)?
|
var onRemoveSUI: ((String) -> Void)?
|
||||||
|
|
||||||
func makeUIView(context: Context) -> UICollectionView {
|
func makeUIView(context: Context) -> UICollectionView {
|
||||||
let collectionView = context.coordinator as UICollectionViewController
|
let collectionView = context.coordinator as UICollectionViewController
|
||||||
@@ -30,7 +30,9 @@ struct EmojiCollectionView: UIViewRepresentable {
|
|||||||
|
|
||||||
func updateUIView(_ uiView: UICollectionView, context: Context) {
|
func updateUIView(_ uiView: UICollectionView, context: Context) {
|
||||||
context.coordinator.hoarder = hoarder
|
context.coordinator.hoarder = hoarder
|
||||||
|
context.coordinator.onRemove = onRemoveSUI
|
||||||
|
|
||||||
|
context.coordinator.edit = edit
|
||||||
if edit ?? false {
|
if edit ?? false {
|
||||||
context.coordinator.startAnimating()
|
context.coordinator.startAnimating()
|
||||||
} else {
|
} else {
|
||||||
@@ -40,15 +42,20 @@ struct EmojiCollectionView: UIViewRepresentable {
|
|||||||
for cell in uiView.visibleCells {
|
for cell in uiView.visibleCells {
|
||||||
if let cell = cell as? EmojiCollectionViewCell {
|
if let cell = cell as? EmojiCollectionViewCell {
|
||||||
cell.setEdit(to: edit)
|
cell.setEdit(to: edit)
|
||||||
cell.onDelete = onRemove
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if items != context.coordinator.items || context.coordinator.pack != pack {
|
// if items != context.coordinator.items || context.coordinator.pack != pack {
|
||||||
context.coordinator.items = items
|
// context.coordinator.items = items
|
||||||
context.coordinator.pack = pack
|
// context.coordinator.pack = pack
|
||||||
uiView.reloadData()
|
// uiView.reloadData()
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// let oldItems = context.coordinator.items
|
||||||
|
// let newItems = items
|
||||||
|
// newItems.difference(from: oldItems)
|
||||||
|
//// context.coordinator.items
|
||||||
|
// uiView.deleteItems(at: <#T##[IndexPath]#>)
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeCoordinator() -> Coordinator {
|
func makeCoordinator() -> Coordinator {
|
||||||
@@ -73,6 +80,8 @@ struct EmojiCollectionView: UIViewRepresentable {
|
|||||||
var pack: EmojiPack?
|
var pack: EmojiPack?
|
||||||
var width: CGFloat
|
var width: CGFloat
|
||||||
var style: EmojiCollectionView.Style
|
var style: EmojiCollectionView.Style
|
||||||
|
var edit: Bool? = false
|
||||||
|
var onRemove: ((String) -> Void)?
|
||||||
|
|
||||||
let initDate = Date.now
|
let initDate = Date.now
|
||||||
var layout = UICollectionViewFlowLayout()
|
var layout = UICollectionViewFlowLayout()
|
||||||
@@ -104,10 +113,6 @@ struct EmojiCollectionView: UIViewRepresentable {
|
|||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewDidLayoutSubviews() {
|
|
||||||
layout.itemSize = CGSize(width: width, height: width)
|
|
||||||
}
|
|
||||||
|
|
||||||
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
||||||
return items.count
|
return items.count
|
||||||
}
|
}
|
||||||
@@ -122,6 +127,15 @@ struct EmojiCollectionView: UIViewRepresentable {
|
|||||||
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "plain", for: indexPath) as! PlainEmojiCollectionViewCell
|
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "plain", for: indexPath) as! PlainEmojiCollectionViewCell
|
||||||
case .full:
|
case .full:
|
||||||
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).onRemove = {
|
||||||
|
guard let index = self.items.firstIndex(of: $0) else { return }
|
||||||
|
self.items.remove(at: index)
|
||||||
|
collectionView.performBatchUpdates {
|
||||||
|
collectionView.deleteItems(at: [IndexPath(row: index, section: 0)])
|
||||||
|
}
|
||||||
|
self.onRemove?($0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
guard !hoarder.trie.dict.isEmpty else { return cell }
|
guard !hoarder.trie.dict.isEmpty else { return cell }
|
||||||
@@ -133,7 +147,7 @@ struct EmojiCollectionView: UIViewRepresentable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func startAnimating() {
|
func startAnimating() {
|
||||||
displayLink = CADisplayLink(target: self, selector: #selector(update))
|
displayLink = CADisplayLink(target: self, selector: #selector(updateAnimation))
|
||||||
displayLink.add(to: .main, forMode: .common)
|
displayLink.add(to: .main, forMode: .common)
|
||||||
}
|
}
|
||||||
func stopAnimating() {
|
func stopAnimating() {
|
||||||
@@ -145,7 +159,7 @@ struct EmojiCollectionView: UIViewRepresentable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
@objc
|
@objc
|
||||||
func update() {
|
func updateAnimation() {
|
||||||
let t = Date().timeIntervalSince(initDate)
|
let t = Date().timeIntervalSince(initDate)
|
||||||
let angle = ((sin(t*20)*4)/360)*2*CGFloat.pi
|
let angle = ((sin(t*20)*4)/360)*2*CGFloat.pi
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ 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?
|
||||||
var onDelete: ((String) -> Void)?
|
var onRemove: ((String) -> Void)?
|
||||||
|
|
||||||
override init(frame: CGRect) {
|
override init(frame: CGRect) {
|
||||||
super.init(frame: frame)
|
super.init(frame: frame)
|
||||||
@@ -25,6 +25,7 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
|
|||||||
contentView.addSubview(label)
|
contentView.addSubview(label)
|
||||||
|
|
||||||
button.alpha = 0
|
button.alpha = 0
|
||||||
|
button.isHidden = true
|
||||||
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
|
||||||
@@ -78,14 +79,10 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setOnDelete(to newValue: ((String) -> Void)?) {
|
|
||||||
self.onDelete = newValue
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc
|
@objc
|
||||||
func deleteTapped() {
|
func deleteTapped() {
|
||||||
if let emojiName = label.text {
|
if let emojiName = label.text {
|
||||||
onDelete?(emojiName)
|
onRemove?(emojiName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,5 +103,6 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
|
|||||||
override func prepareForReuse() {
|
override func prepareForReuse() {
|
||||||
super.prepareForReuse()
|
super.prepareForReuse()
|
||||||
label.text = nil
|
label.text = nil
|
||||||
|
setEdit(to: edit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||