Browse Source

EditController: Change editing state upon successful edit action

Soomin Lee 5 years ago
parent
commit
210c64067d

+ 15 - 1
Sources/EditController.swift

@@ -12,6 +12,8 @@
 protocol EditControllerDelegate: class {
     func editController(editController: EditController, cellforItemAt indexPath: IndexPath) -> BaseCollectionViewCell?
     func editController(editController: EditController, present viewController: UIViewController)
+
+    func editControllerDidFinishEditing(editController: EditController?)
 }
 
 class EditController: UIViewController {
@@ -144,6 +146,7 @@ private extension EditController {
             }
         }
         resetSelections(resetUI: true)
+        delegate?.editControllerDidFinishEditing(editController: self)
     }
 }
 
@@ -208,6 +211,7 @@ extension EditController: EditToolbarDelegate {
                                             self?.model.delete(objectsToDelete)
                                             // Update directly the cached indexes since cells will be destroyed
                                             self?.selectedCellIndexPaths.removeAll()
+                                            self?.delegate?.editControllerDidFinishEditing(editController: self)
         })
 
         VLCAlertViewController.alertViewManager(title: NSLocalizedString("DELETE_TITLE", comment: ""),
@@ -220,7 +224,14 @@ extension EditController: EditToolbarDelegate {
     func editToolbarDidShare(_ editToolbar: EditToolbar, presentFrom button: UIButton) {
         UIApplication.shared.beginIgnoringInteractionEvents()
         let rootViewController = UIApplication.shared.keyWindow?.rootViewController
-        guard let controller = VLCActivityViewControllerVendor.activityViewController(forFiles: fileURLsFromSelection(), presenting: button, presenting: rootViewController) else {
+        guard let controller = VLCActivityViewControllerVendor
+            .activityViewController(forFiles: fileURLsFromSelection(),
+                                    presenting: button,
+                                    presenting: rootViewController,
+                                    completionHandler: {
+                                        [weak self] completion in
+            self?.delegate?.editControllerDidFinishEditing(editController: self)
+        }) else {
             UIApplication.shared.endIgnoringInteractionEvents()
             return
         }
@@ -299,6 +310,8 @@ extension EditController: EditToolbarDelegate {
             //call until all items are renamed
             if !strongself.selectedCellIndexPaths.isEmpty {
                 strongself.editToolbarDidRename(editToolbar)
+            } else {
+                strongself.delegate?.editControllerDidFinishEditing(editController: self)
             }
         })
     }
@@ -428,6 +441,7 @@ extension EditController: AddToPlaylistViewControllerDelegate {
         }
         resetSelections(resetUI: false)
         addToPlaylistViewController.dismiss(animated: true, completion: nil)
+        delegate?.editControllerDidFinishEditing(editController: self)
     }
 
     func addToPlaylistViewController(_ addToPlaylistViewController: AddToPlaylistViewController,

+ 14 - 0
Sources/MediaCategories/MediaCategoryViewController.swift

@@ -17,6 +17,7 @@ import Foundation
     func needsToUpdateNavigationbarIfNeeded(_ viewController: MediaCategoryViewController)
     func enableCategorySwitching(for viewController: MediaCategoryViewController,
                                  enable: Bool)
+    func setEditingStateChanged(for viewController: MediaCategoryViewController, editing: Bool)
 }
 
 class MediaCategoryViewController: UICollectionViewController, UISearchBarDelegate, IndicatorInfoProvider {
@@ -283,6 +284,10 @@ class MediaCategoryViewController: UICollectionViewController, UISearchBarDelega
     }
 
     override func setEditing(_ editing: Bool, animated: Bool) {
+        guard editing != isEditing else {
+            // Guard in case where setEditing is called twice with the same state
+            return
+        }
         super.setEditing(editing, animated: animated)
         // might have an issue if the old datasource was search
         // Most of the edit logic is handled inside editController
@@ -576,6 +581,15 @@ extension MediaCategoryViewController: EditControllerDelegate {
         let newNavigationController = UINavigationController(rootViewController: viewController)
         navigationController?.present(newNavigationController, animated: true, completion: nil)
     }
+
+    func editControllerDidFinishEditing(editController: EditController?) {
+        // NavigationItems for Collections are create from the parent, there is no need to propagate the information.
+        if self is CollectionCategoryViewController {
+            handleEditing()
+        } else {
+            delegate?.setEditingStateChanged(for: self, editing: false)
+        }
+    }
 }
 
 private extension MediaCategoryViewController {

+ 4 - 0
Sources/MediaViewControllers/MediaViewController.swift

@@ -133,6 +133,10 @@ extension MediaViewController: MediaCategoryViewControllerDelegate {
     func enableCategorySwitching(for viewController: MediaCategoryViewController, enable: Bool) {
         scrollingEnabled(enable)
     }
+
+    func setEditingStateChanged(for viewController: MediaCategoryViewController, editing: Bool) {
+        customSetEditing()
+    }
 }
 
 // MARK: - Edit