فهرست منبع

EditController: Make it a ViewController

This enables us to move the edittoolbar back into the editcontroller by setting the Edittoolbar as it's view
Removed the resetAllvisiblecells because on prepare for reuse all cells are unchecked
Removed the reference to the collectionview and added a tiny delegate call instead
Removed restriction for adding to playlist by model and add in services
Carola Nitz 6 سال پیش
والد
کامیت
b772cb0088
2فایلهای تغییر یافته به همراه60 افزوده شده و 52 حذف شده
  1. 18 12
      Sources/MediaCategories/MediaCategoryViewController.swift
  2. 42 40
      Sources/VLCEditController.swift

+ 18 - 12
Sources/MediaCategories/MediaCategoryViewController.swift

@@ -20,12 +20,12 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
     private var services: Services
     private var searchController: UISearchController?
     private let searchDataSource = VLCLibrarySearchDisplayDataSource()
-    private lazy var editController = VLCEditController(collectionView: self.collectionView!, model: self.model)
-    private lazy var editToolbar: VLCEditToolbar = {
-        let editToolbar = VLCEditToolbar(category: model)
-        editToolbar.delegate = editController
-        return editToolbar
+    private lazy var editController: VLCEditController = {
+        let editController = VLCEditController(mediaLibraryManager:services.medialibraryManager, model: model)
+        editController.delegate = self
+        return editController
     }()
+
     private var editToolbarConstraint: NSLayoutConstraint?
     private var cachedCellSize = CGSize.zero
 
@@ -97,19 +97,19 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
 
     @objc func themeDidChange() {
         collectionView?.backgroundColor = PresentationTheme.current.colors.background
-        editToolbar.backgroundColor = PresentationTheme.current.colors.background
+        editController.view.backgroundColor = PresentationTheme.current.colors.background
         setNeedsStatusBarAppearanceUpdate()
     }
 
     func setupEditToolbar() {
-        editToolbar.translatesAutoresizingMaskIntoConstraints = false
-        view.addSubview(editToolbar)
-        editToolbarConstraint = editToolbar.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 60)
+        editController.view.translatesAutoresizingMaskIntoConstraints = false
+        view.addSubview(editController.view)
+        editToolbarConstraint = editController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 60)
         NSLayoutConstraint.activate([
             editToolbarConstraint!,
-            editToolbar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
-            editToolbar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
-            editToolbar.heightAnchor.constraint(equalToConstant: 50)
+            editController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
+            editController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
+            editController.view.heightAnchor.constraint(equalToConstant: 50)
         ])
     }
 
@@ -267,6 +267,12 @@ extension VLCMediaCategoryViewController {
     }
 }
 
+extension VLCMediaCategoryViewController: VLCEditControllerDelegate {
+    func editController(editController: VLCEditController, cellforItemAt indexPath: IndexPath) -> MediaEditCell? {
+        return collectionView.cellForItem(at: indexPath) as? MediaEditCell
+    }
+}
+
 private extension VLCMediaCategoryViewController {
     func setupCollectionView() {
         let cellNib = UINib(nibName: model.cellType.nibName, bundle: nil)

+ 42 - 40
Sources/VLCEditController.swift

@@ -9,15 +9,30 @@
  * Refer to the COPYING file of the official project for license.
  *****************************************************************************/
 
-class VLCEditController: NSObject {
+protocol VLCEditControllerDelegate: class {
+    func editController(editController: VLCEditController, cellforItemAt indexPath: IndexPath) -> MediaEditCell?
+}
+
+class VLCEditController: UIViewController {
     private var selectedCellIndexPaths = Set<IndexPath>()
-    private let collectionView: UICollectionView
     private let model: MediaLibraryBaseModel
+    private let mediaLibraryManager: VLCMediaLibraryManager
+    weak var delegate: VLCEditControllerDelegate?
+
+    override func loadView() {
+        let editToolbar = VLCEditToolbar(category: model)
+        editToolbar.delegate = self
+        self.view = editToolbar
+    }
 
-    init(collectionView: UICollectionView, model: MediaLibraryBaseModel) {
-        self.collectionView = collectionView
+    init(mediaLibraryManager: VLCMediaLibraryManager, model: MediaLibraryBaseModel) {
+        self.mediaLibraryManager = mediaLibraryManager
         self.model = model
-        super.init()
+        super.init(nibName: nil, bundle: nil)
+    }
+
+    required init?(coder aDecoder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
     }
 
     func resetSelections() {
@@ -28,17 +43,6 @@ class VLCEditController: NSObject {
 // MARK: - Helpers
 
 private extension VLCEditController {
-    private func resetCell(at indexPath: IndexPath) {
-        if let cell = collectionView.cellForItem(at: indexPath) as? MediaEditCell {
-            cell.isChecked = false
-        }
-    }
-
-    private func resetAllVisibleCell() {
-        for case let cell as MediaEditCell in collectionView.visibleCells {
-            cell.isChecked = false
-        }
-    }
 
     private struct TextFieldAlertInfo {
         var alertTitle: String
@@ -82,7 +86,7 @@ private extension VLCEditController {
         alertController.addAction(cancelButton)
         alertController.addAction(confirmAction)
 
-        UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
+        present(alertController, animated: true, completion: nil)
     }
 }
 
@@ -91,29 +95,26 @@ private extension VLCEditController {
 extension VLCEditController: VLCEditToolbarDelegate {
 
     func editToolbarDidAddToPlaylist(_ editToolbar: VLCEditToolbar) {
-        if let model = model as? PlaylistModel {
-            let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
-                placeHolder: "NEW_PLAYLIST")
-
-            presentTextFieldAlert(with: alertInfo, completionHandler: {
-                text -> Void in
-                    model.create(name: text)
-                })
-
-        } else if let model = model as? VideoModel {
-            let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
-                                               placeHolder: "NEW_PLAYLIST")
+        //Todo: replace with Viewcontroller that shows existing Playlists
+        let alertInfo = TextFieldAlertInfo(alertTitle: NSLocalizedString("PLAYLISTS", comment: ""),
+                                           alertDescription: NSLocalizedString("PLAYLIST_DESCRIPTION", comment: ""),
+                                           placeHolder: NSLocalizedString("PLAYLIST_PLACEHOLDER", comment:""))
+
+        presentTextFieldAlert(with: alertInfo, completionHandler: {
+            [weak self] text -> Void in
+            guard let strongSelf = self else {
+                return
+            }
+            let playlist = strongSelf.mediaLibraryManager.createPlaylist(with: text)
 
-            presentTextFieldAlert(with: alertInfo, completionHandler: {
-                [selectedCellIndexPaths, model] text -> Void in
-                let playlist = model.medialibrary.createPlaylist(with: text)
-                for indexPath in selectedCellIndexPaths {
-                    if let media = model.anyfiles[indexPath.row] as? VLCMLMedia {
-                        playlist.appendMedia(withIdentifier: media.identifier())
-                    }
+            for indexPath in strongSelf.selectedCellIndexPaths {
+                guard let media = strongSelf.model.anyfiles[indexPath.row] as? VLCMLMedia else {
+                    assertionFailure("we're not handling collections yet")
+                    return
                 }
-            })
-        }
+                playlist.appendMedia(withIdentifier: media.identifier())
+            }
+        })
     }
 
     func editToolbarDidDelete(_ editToolbar: VLCEditToolbar) {
@@ -130,7 +131,6 @@ extension VLCEditController: VLCEditToolbarDelegate {
                                             [weak self] action in
                                             self?.model.delete(objectsToDelete)
                                             self?.selectedCellIndexPaths.removeAll()
-                                            self?.resetAllVisibleCell()
         })
 
         VLCAlertViewController.alertViewManager(title: NSLocalizedString("DELETE_TITLE", comment: ""),
@@ -182,7 +182,9 @@ extension VLCEditController: VLCEditToolbarDelegate {
                         return
                     }
                     media.updateTitle(text)
-                    self?.resetCell(at: indexPath)
+                    if let strongself = self {
+                        strongself.delegate?.editController(editController: strongself, cellforItemAt: indexPath)?.isChecked = false
+                    }
                 })
             }
         }