Ability to Delete GIFs
This commit is contained in:
@@ -16,6 +16,7 @@ class GIFCollectionViewController: UIViewController {
|
|||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
setupCollectionView()
|
setupCollectionView()
|
||||||
setupUI()
|
setupUI()
|
||||||
|
setupGestureRecognizers()
|
||||||
loadGIFs()
|
loadGIFs()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,6 +42,11 @@ class GIFCollectionViewController: UIViewController {
|
|||||||
collectionView.dataSource = self
|
collectionView.dataSource = self
|
||||||
collectionView.register(GIFCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
|
collectionView.register(GIFCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
|
||||||
collectionView.alwaysBounceVertical = true
|
collectionView.alwaysBounceVertical = true
|
||||||
|
|
||||||
|
if #available(iOS 14.0, *) {
|
||||||
|
// Use collection view's built-in contextual menu support
|
||||||
|
// This is set up in collectionView(_:contextMenuConfigurationForItemAt:point:)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setupUI() {
|
private func setupUI() {
|
||||||
@@ -114,6 +120,53 @@ class GIFCollectionViewController: UIViewController {
|
|||||||
navController.modalPresentationStyle = .formSheet
|
navController.modalPresentationStyle = .formSheet
|
||||||
present(navController, animated: true)
|
present(navController, animated: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func setupGestureRecognizers() {
|
||||||
|
// For iOS versions earlier than 14, we'll use a long press gesture recognizer
|
||||||
|
if #unavailable(iOS 14.0) {
|
||||||
|
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
|
||||||
|
collectionView.addGestureRecognizer(longPressGesture)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc private func handleLongPress(_ gesture: UILongPressGestureRecognizer) {
|
||||||
|
if gesture.state == .began {
|
||||||
|
let point = gesture.location(in: collectionView)
|
||||||
|
|
||||||
|
guard let indexPath = collectionView.indexPathForItem(at: point) else { return }
|
||||||
|
|
||||||
|
// Show action sheet for pre-iOS 14 devices
|
||||||
|
showDeleteActionSheet(for: indexPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func showDeleteActionSheet(for indexPath: IndexPath) {
|
||||||
|
let gif = gifs[indexPath.item]
|
||||||
|
|
||||||
|
let alertController = UIAlertController(
|
||||||
|
title: "GIF Options",
|
||||||
|
message: "What would you like to do with this GIF?",
|
||||||
|
preferredStyle: .actionSheet
|
||||||
|
)
|
||||||
|
|
||||||
|
alertController.addAction(UIAlertAction(title: "Delete", style: .destructive) { [weak self] _ in
|
||||||
|
self?.deleteGIF(at: indexPath)
|
||||||
|
})
|
||||||
|
|
||||||
|
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel))
|
||||||
|
|
||||||
|
present(alertController, animated: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func deleteGIF(at indexPath: IndexPath) {
|
||||||
|
let gif = gifs[indexPath.item]
|
||||||
|
GIFStorageService.shared.deleteGIF(with: gif.id)
|
||||||
|
|
||||||
|
// Remove from local array and update collection view
|
||||||
|
gifs.remove(at: indexPath.item)
|
||||||
|
collectionView.deleteItems(at: [indexPath])
|
||||||
|
updateEmptyState()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension GIFCollectionViewController: UICollectionViewDelegate, UICollectionViewDataSource {
|
extension GIFCollectionViewController: UICollectionViewDelegate, UICollectionViewDataSource {
|
||||||
@@ -136,4 +189,23 @@ extension GIFCollectionViewController: UICollectionViewDelegate, UICollectionVie
|
|||||||
let gif = gifs[indexPath.item]
|
let gif = gifs[indexPath.item]
|
||||||
onSelectGIF?(gif)
|
onSelectGIF?(gif)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Context Menu Support (iOS 14+)
|
||||||
|
|
||||||
|
@available(iOS 14.0, *)
|
||||||
|
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
|
||||||
|
let gif = gifs[indexPath.item]
|
||||||
|
|
||||||
|
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
|
||||||
|
let deleteAction = UIAction(
|
||||||
|
title: "Delete",
|
||||||
|
image: UIImage(systemName: "trash"),
|
||||||
|
attributes: .destructive
|
||||||
|
) { [weak self] _ in
|
||||||
|
self?.deleteGIF(at: indexPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return UIMenu(title: "", children: [deleteAction])
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user