Ability to Delete GIFs

This commit is contained in:
Joshua Higgins
2025-06-03 22:37:09 -04:00
parent 8ce5adc766
commit 36949981b4

View File

@@ -16,6 +16,7 @@ class GIFCollectionViewController: UIViewController {
super.viewDidLoad()
setupCollectionView()
setupUI()
setupGestureRecognizers()
loadGIFs()
}
@@ -41,6 +42,11 @@ class GIFCollectionViewController: UIViewController {
collectionView.dataSource = self
collectionView.register(GIFCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
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() {
@@ -114,6 +120,53 @@ class GIFCollectionViewController: UIViewController {
navController.modalPresentationStyle = .formSheet
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 {
@@ -136,4 +189,23 @@ extension GIFCollectionViewController: UICollectionViewDelegate, UICollectionVie
let gif = gifs[indexPath.item]
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])
}
}
}