MWPhotoBrowser - how to implement delete photo functionality?

635 views Asked by At

I am using MWPhotoBrowser for my app. I need to give delete functionality to my users. Is there any way we can implement delete a particular photo or multiple photos functionality?

Quick help needed.

1

There are 1 answers

0
Esqarrouth On

enter image description here

I did this in swift by adding this extension from outside of the library:

extension MWPhotoBrowser {

    public override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        if let _ = valueForKey("_gridController") as? MWGridViewController {
            let leftButtonItem = editButtonItem()
            //leftButtonItem.tintColor = QorumColors.ThemeWhite
            navigationItem.leftBarButtonItem = leftButtonItem
        } else {
            navigationItem.leftBarButtonItem = nil
        }
    }

    public override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)

        if editing {
            navigationItem.leftBarButtonItem?.title = "Delete"
            //navigationItem.leftBarButtonItem?.tintColor = QorumColors.Nickname

            displaySelectionButtons = true
            title = "Delete Photos"

            let gridController = valueForKey("_gridController") as! MWGridViewController
            gridController.selectionMode = displaySelectionButtons
            gridController.collectionView!.reloadData()
        } else {
            let nav = self.navigationController as! TempPresentVC
            let photosToDelete = nav.selectedPhotos

            let afterButtonPress = {
                //self.navigationItem.leftBarButtonItem?.tintColor = QorumColors.ThemeWhite

                self.displaySelectionButtons = false
                self.updateNavigation()

                let gridController = self.valueForKey("_gridController") as! MWGridViewController
                gridController.selectionMode = self.displaySelectionButtons
                gridController.collectionView!.reloadData()
            }

            guard photosToDelete.count > 0 else {
                afterButtonPress()
                return
            }

            let title = "Delete Photo"
            let message = "Are you sure you want to delete these photos?"
            let action = "Delete"
            let cancelAction = "Cancel"

            let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
            let declineButton = UIAlertAction(title: cancelAction, style: .Default, handler: { (action: UIAlertAction) in
                afterButtonPress()
            })
            let acceptButton = UIAlertAction(title: action, style: .Default, handler: { (action: UIAlertAction) in
                afterButtonPress()
            })
            alert.addAction(declineButton)
            alert.addAction(acceptButton)
            UIApplication.topMostController().presentVC(alert) //private lib
        }
    }
}



class TempPresentVC: UINavigationController, MWPhotoBrowserDelegate {
    var selectedPhotos = [Int]()

    func photoBrowser(photoBrowser: MWPhotoBrowser!, photoAtIndex index: UInt, selectedChanged selected: Bool) {
        if selected {
            selectedPhotos.append(index.toInt)
        } else {
            selectedPhotos.removeObject(index.toInt)
        }
    }
}

This doesn't allow you to delete a photo in a single photo view mode, but starting from this code base that part would be easy to implement.

Beware, some of the stuff I use inside are private functions I wrote in my other classes, what they do should be clear though.