Skip to content

Add delegate method for delete confirmation #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ViewController: UIViewController {
@IBOutlet weak var image7: UIImageView!

var items: [DataItem] = []

var galleryViewController: GalleryViewController?

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -93,6 +95,7 @@ class ViewController: UIViewController {
footerView.currentIndex = index
}

self.galleryViewController = galleryViewController
self.presentImageGallery(galleryViewController)
}

Expand Down Expand Up @@ -179,6 +182,21 @@ extension ViewController: GalleryItemsDelegate {
imageView.removeFromSuperview()
items.remove(at: index)
}

func shouldRemoveGalleryItem(at index: Int, block: @escaping (Bool) -> Void) {
let alert = UIAlertController(
title: "Delete?",
message: "Are you sure you want to delete this image?",
preferredStyle: .alert
)
alert.addAction(.init(title: "Delete", style: .destructive) { _ in
block(true)
})
alert.addAction(.init(title: "Cancel", style: .cancel) { _ in
block(false)
})
galleryViewController?.present(alert, animated: true)
}
}

// Some external custom UIImageView we want to show in the gallery
Expand Down
7 changes: 7 additions & 0 deletions ImageViewer/Source/GalleryItemsDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ import Foundation
public protocol GalleryItemsDelegate: class {

func removeGalleryItem(at index: Int)
func shouldRemoveGalleryItem(at index: Int, block: @escaping (Bool) -> Void)
}

extension GalleryItemsDelegate {
func shouldRemoveGalleryItem(at index: Int, block: @escaping (Bool) -> Void) {
block(true)
}
}
19 changes: 12 additions & 7 deletions ImageViewer/Source/GalleryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -424,16 +424,21 @@ open class GalleryViewController: UIPageViewController, ItemControllerDelegate {
}

@objc fileprivate func deleteItem() {

itemsDelegate?.shouldRemoveGalleryItem(at: currentIndex) {
[weak self] shouldRemove in
guard let self = self, shouldRemove else { return }

deleteButton?.isEnabled = false
view.isUserInteractionEnabled = false
self.deleteButton?.isEnabled = false
self.view.isUserInteractionEnabled = false

itemsDelegate?.removeGalleryItem(at: currentIndex)
removePage(atIndex: currentIndex) {
self.itemsDelegate?.removeGalleryItem(at: self.currentIndex)
self.removePage(atIndex: self.currentIndex) {

[weak self] in
self?.deleteButton?.isEnabled = true
self?.view.isUserInteractionEnabled = true
[weak self] in
self?.deleteButton?.isEnabled = true
self?.view.isUserInteractionEnabled = true
}
}
}

Expand Down