瀏覽代碼

VLCEditController: Separate edit logic

Soomin Lee 7 年之前
父節點
當前提交
b2e3ad49c9

+ 10 - 6
Sources/MediaCategories/MediaCategoryViewController.swift

@@ -19,6 +19,7 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
     private var searchController: UISearchController?
     private let searchDataSource = VLCLibrarySearchDisplayDataSource()
     var category: MediaLibraryBaseModel
+    private lazy var editController = VLCEditController(collectionView: self.collectionView!, category: self.category)
 
 //    @available(iOS 11.0, *)
 //    lazy var dragAndDropManager: VLCDragAndDropManager = { () -> VLCDragAndDropManager<T> in
@@ -142,8 +143,17 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
         collectionView?.collectionViewLayout.invalidateLayout()
     }
 
+    // MARK: - Edit
+
     override func setEditing(_ editing: Bool, animated: Bool) {
         super.setEditing(editing, animated: animated)
+        // might have an issue if the old datasource was search
+        // Most of the edit logic is handled inside editController
+        collectionView?.dataSource = editing ? editController : self
+        collectionView?.delegate = editing ? editController : self
+
+        editController.toolbarNeedsUpdate(editing: editing)
+
         let layoutToBe = editing ? editCollectionViewLayout : UICollectionViewFlowLayout()
         collectionView?.setCollectionViewLayout(layoutToBe, animated: false, completion: {
             [weak self] finished in
@@ -208,12 +218,6 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
     // MARK: - UICollectionViewDelegateFlowLayout
     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
 
-        if isEditing {
-            let contentInset = collectionView.contentInset
-            let insetToRemove = contentInset.left + contentInset.right + (cellPadding * 2)
-            return CGSize(width: collectionView.frame.width - insetToRemove, height: VLCMediaViewEditCell.height)
-        }
-
         let numberOfCells: CGFloat = collectionView.traitCollection.horizontalSizeClass == .regular ? 3.0 : 2.0
         let aspectRatio: CGFloat = 10.0 / 16.0
 

+ 76 - 0
Sources/VLCEditController.swift

@@ -0,0 +1,76 @@
+/*****************************************************************************
+ * VLCEditController.swift
+ *
+ * Copyright © 2018 VLC authors and VideoLAN
+ * Copyright © 2018 Videolabs
+ *
+ * Authors: Soomin Lee <bubu@mikan.io>
+ *
+ * Refer to the COPYING file of the official project for license.
+ *****************************************************************************/
+
+protocol VLCEditControllerDataSource {
+    func toolbarNeedsUpdate(editing: Bool)
+}
+
+class VLCEditController: NSObject {
+    private let collectionView: UICollectionView
+    private let category: MediaLibraryBaseModel
+
+    private lazy var editToolbar: VLCEditToolbar = {
+        let editToolbar = VLCEditToolbar(frame: CGRect(x: 0, y: 550,
+                                                       width: collectionView.frame.width, height: 50))
+        editToolbar.isHidden = true
+        return editToolbar
+    }()
+
+    init(collectionView: UICollectionView, category: MediaLibraryBaseModel) {
+        self.collectionView = collectionView
+        self.category = category
+        super.init()
+
+        collectionView.addSubview(editToolbar)
+        collectionView.bringSubview(toFront: editToolbar)
+    }
+}
+
+extension VLCEditController: VLCEditControllerDataSource {
+    func toolbarNeedsUpdate(editing: Bool) {
+        editToolbar.isHidden = !editing
+    }
+}
+
+extension VLCEditController: UICollectionViewDataSource {
+    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
+        return category.anyfiles.count
+    }
+
+    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
+        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: VLCMediaViewEditCell.identifier,
+                                                         for: indexPath) as? VLCMediaViewEditCell {
+            cell.titleLabel.text = "( `ー´)ノ"
+            cell.subInfoLabel.text = "(-ω-、)"
+            cell.sizeLabel.text = "|ω°•)"
+            cell.thumbnailImageView.image = UIImage(named: "vlc-xmas")
+            return cell
+        }
+        return UICollectionViewCell()
+    }
+}
+
+extension VLCEditController: UICollectionViewDelegate {
+    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
+        if let cell = collectionView.cellForItem(at: indexPath) as? VLCMediaViewEditCell {
+            cell.checkView.isEnabled = !cell.checkView.isEnabled
+        }
+    }
+}
+
+extension VLCEditController: UICollectionViewDelegateFlowLayout {
+    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
+        let contentInset = collectionView.contentInset
+        // FIXME: 5 should be cell padding, but not usable maybe static?
+        let insetToRemove = contentInset.left + contentInset.right + (5 * 2)
+        return CGSize(width: collectionView.frame.width - insetToRemove, height: VLCMediaViewEditCell.height)
+    }
+}

+ 4 - 0
VLC.xcodeproj/project.pbxproj

@@ -264,6 +264,7 @@
 		8DE18894210B5F8200A091D2 /* AlbumModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DE18893210B5F8200A091D2 /* AlbumModel.swift */; };
 		8DE18898210F144B00A091D2 /* GenreModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DE18897210F144B00A091D2 /* GenreModel.swift */; };
 		8DF9669D2113317E00D0FCD6 /* VLCEditToolbar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DF9669C2113317100D0FCD6 /* VLCEditToolbar.swift */; };
+		8DF966B121188BDB00D0FCD6 /* VLCEditController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DF966B021188BDB00D0FCD6 /* VLCEditController.swift */; };
 		8F91EC79195CEC7900F5BCBA /* VLCOpenInActivity.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F91EC78195CEC7900F5BCBA /* VLCOpenInActivity.m */; };
 		8F91EC7F195E1DAB00F5BCBA /* AssetsLibrary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F91EC7E195E1DAB00F5BCBA /* AssetsLibrary.framework */; };
 		9B088308183D7BEC004B5C2A /* VLCCloudStorageTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B088307183D7BEC004B5C2A /* VLCCloudStorageTableViewController.m */; };
@@ -959,6 +960,7 @@
 		8DE18893210B5F8200A091D2 /* AlbumModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlbumModel.swift; sourceTree = "<group>"; };
 		8DE18897210F144B00A091D2 /* GenreModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenreModel.swift; sourceTree = "<group>"; };
 		8DF9669C2113317100D0FCD6 /* VLCEditToolbar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = VLCEditToolbar.swift; path = Sources/VLCEditToolbar.swift; sourceTree = "<group>"; };
+		8DF966B021188BDB00D0FCD6 /* VLCEditController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = VLCEditController.swift; path = Sources/VLCEditController.swift; sourceTree = "<group>"; };
 		8F91EC77195CEC7900F5BCBA /* VLCOpenInActivity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = VLCOpenInActivity.h; path = Sources/VLCOpenInActivity.h; sourceTree = SOURCE_ROOT; };
 		8F91EC78195CEC7900F5BCBA /* VLCOpenInActivity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = VLCOpenInActivity.m; path = Sources/VLCOpenInActivity.m; sourceTree = SOURCE_ROOT; };
 		8F91EC7E195E1DAB00F5BCBA /* AssetsLibrary.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AssetsLibrary.framework; path = System/Library/Frameworks/AssetsLibrary.framework; sourceTree = SDKROOT; };
@@ -1469,6 +1471,7 @@
 				4144156B20ECE6330078EC37 /* VLCFileServerSectionTableHeaderView.swift */,
 				8D222DC120F779F1009C0D34 /* VLCMediaViewEditCell.swift */,
 				8DF9669C2113317100D0FCD6 /* VLCEditToolbar.swift */,
+				8DF966B021188BDB00D0FCD6 /* VLCEditController.swift */,
 			);
 			name = "UI Elements";
 			sourceTree = "<group>";
@@ -3223,6 +3226,7 @@
 				7D9289751877459B009108FD /* VLCFirstStepsThirdPageViewController.m in Sources */,
 				7D95610B1AF3E9E800779745 /* VLCMiniPlaybackView.m in Sources */,
 				DD3EFF451BDEBCE500B68579 /* VLCLocalNetworkServiceBrowserManualConnect.m in Sources */,
+				8DF966B121188BDB00D0FCD6 /* VLCEditController.swift in Sources */,
 				7DC19B051868D1C400810BF7 /* VLCFirstStepsFifthPageViewController.m in Sources */,
 				DD3EFF311BDEBCE500B68579 /* VLCNetworkServerBrowserFTP.m in Sources */,
 				9BADAF45185FBD9D00108BD8 /* VLCFrostedGlasView.m in Sources */,