Selaa lähdekoodia

CollectionModel: Handle sort inside VideoGroups

Soomin Lee 5 vuotta sitten
vanhempi
commit
d92b3234d7

+ 5 - 0
SharedSources/MediaLibraryModel/AlbumModel.swift

@@ -119,6 +119,11 @@ extension VLCMLAlbum: MediaCollectionModel {
     func title() -> String {
         return title
     }
+
+    func sortFilesInCollection(with criteria: VLCMLSortingCriteria,
+                               desc: Bool) -> [VLCMLMedia]? {
+        return nil
+    }
 }
 
 extension VLCMLAlbum {

+ 5 - 0
SharedSources/MediaLibraryModel/ArtistModel.swift

@@ -105,6 +105,11 @@ extension VLCMLArtist: MediaCollectionModel {
     func title() -> String {
         return name
     }
+
+    func sortFilesInCollection(with criteria: VLCMLSortingCriteria,
+                               desc: Bool) -> [VLCMLMedia]? {
+        return nil
+    }
 }
 
 extension VLCMLArtist {

+ 8 - 2
SharedSources/MediaLibraryModel/CollectionModel.swift

@@ -13,7 +13,7 @@ import Foundation
 class CollectionModel: MLBaseModel {
     typealias MLType = VLCMLMedia
 
-    var sortModel: SortModel
+    var sortModel = SortModel([.alpha, .duration, .insertionDate, .releaseDate, .fileSize])
 
     var mediaCollection: MediaCollectionModel
 
@@ -36,7 +36,6 @@ class CollectionModel: MLBaseModel {
         self.medialibrary = mediaService
         self.mediaCollection = mediaCollection
         files = mediaCollection.files() ?? []
-        sortModel = mediaCollection.sortModel() ?? SortModel([.default])
         medialibrary.addObserver(self)
     }
 
@@ -65,6 +64,13 @@ class CollectionModel: MLBaseModel {
             }
         }
     }
+
+    func sort(by criteria: VLCMLSortingCriteria, desc: Bool) {
+        files = mediaCollection.sortFilesInCollection(with: criteria, desc: desc) ?? []
+        sortModel.currentSort = criteria
+        sortModel.desc = desc
+        updateView?()
+    }
 }
 
 // MARK: - Edit

+ 5 - 0
SharedSources/MediaLibraryModel/GenreModel.swift

@@ -130,4 +130,9 @@ extension VLCMLGenre: MediaCollectionModel {
     func title() -> String {
         return name
     }
+
+    func sortFilesInCollection(with criteria: VLCMLSortingCriteria,
+                               desc: Bool) -> [VLCMLMedia]? {
+        return nil
+    }
 }

+ 2 - 0
SharedSources/MediaLibraryModel/MediaLibraryBaseModel.swift

@@ -76,6 +76,8 @@ protocol MediaCollectionModel {
     func files() -> [VLCMLMedia]?
     func sortModel() -> SortModel?
     func title() -> String
+    func sortFilesInCollection(with criteria: VLCMLSortingCriteria,
+                               desc: Bool) -> [VLCMLMedia]?
 }
 
 // MARK: - Helper methods

+ 5 - 0
SharedSources/MediaLibraryModel/PlaylistModel.swift

@@ -157,4 +157,9 @@ extension VLCMLPlaylist: MediaCollectionModel {
     func title() -> String {
         return name
     }
+
+    func sortFilesInCollection(with criteria: VLCMLSortingCriteria,
+                               desc: Bool) -> [VLCMLMedia]? {
+        return nil
+    }
 }

+ 5 - 0
SharedSources/MediaLibraryModel/VideoGroupViewModel.swift

@@ -87,6 +87,11 @@ extension VLCMLVideoGroup: MediaCollectionModel {
     func title() -> String {
         return name()
     }
+
+    func sortFilesInCollection(with criteria: VLCMLSortingCriteria,
+                               desc: Bool) -> [VLCMLMedia]? {
+        return media(with: criteria, desc: desc)
+    }
 }
 
 // MARK: - VLCMLVideoGroup - Helpers

+ 90 - 23
Sources/MediaCategories/MediaCategoryViewController.swift

@@ -13,7 +13,7 @@
 
 import Foundation
 
-protocol MediaCategoryViewControllerDelegate: NSObjectProtocol {
+@objc protocol MediaCategoryViewControllerDelegate: NSObjectProtocol {
     func needsToUpdateNavigationbarIfNeeded(_ viewController: MediaCategoryViewController)
     func enableCategorySwitching(for viewController: MediaCategoryViewController,
                                  enable: Bool)
@@ -64,6 +64,17 @@ class MediaCategoryViewController: UICollectionViewController, UISearchBarDelega
         return actionSheet
     }()
 
+    private lazy var sortBarButton: UIBarButtonItem = {
+        return UIBarButtonItem(customView: setupSortButton())
+    }()
+
+    private lazy var editBarButton: UIBarButtonItem = {
+        return setupEditBarButton()
+    }()
+
+    private lazy var rendererBarButton: UIBarButtonItem = {
+        return UIBarButtonItem(customView: rendererButton)
+    }()
 
     lazy var emptyView: VLCEmptyLibraryView = {
         let name = String(describing: VLCEmptyLibraryView.self)
@@ -101,7 +112,6 @@ class MediaCategoryViewController: UICollectionViewController, UISearchBarDelega
         if let collection = model as? CollectionModel {
             title = collection.mediaCollection.title()
         }
-        navigationItem.rightBarButtonItems = [editButtonItem, UIBarButtonItem(customView: rendererButton)]
         NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange),
                                                name: .VLCThemeDidChangeNotification, object: nil)
     }
@@ -346,7 +356,11 @@ class MediaCategoryViewController: UICollectionViewController, UISearchBarDelega
             play(media: media, at: indexPath)
             createSpotlightItem(media: media)
         } else if let mediaCollection = modelContent as? MediaCollectionModel {
-            let collectionViewController = CollectionCategoryViewController(services, mediaCollection: mediaCollection)
+            let collectionViewController = CollectionCategoryViewController(services,
+                                                                            mediaCollection: mediaCollection)
+
+            collectionViewController.navigationItem.rightBarButtonItems = collectionViewController.rightBarButtonItems()
+
             navigationController?.pushViewController(collectionViewController, animated: true)
         }
     }
@@ -365,6 +379,79 @@ class MediaCategoryViewController: UICollectionViewController, UISearchBarDelega
     }
 }
 
+// MARK: - NavigationItem
+
+extension MediaCategoryViewController {
+    private func setupEditBarButton() -> UIBarButtonItem {
+        let editButton = UIBarButtonItem(image: UIImage(named: "edit"),
+                                         style: .plain, target: self,
+                                         action: #selector(handleEditing))
+        editButton.tintColor = PresentationTheme.current.colors.orangeUI
+        editButton.accessibilityLabel = NSLocalizedString("BUTTON_EDIT", comment: "")
+        editButton.accessibilityHint = NSLocalizedString("BUTTON_EDIT_HINT", comment: "")
+        return editButton
+    }
+
+    private func setupSortButton() -> UIButton {
+        // Fetch sortButton configuration from MediaVC
+        let sortButton = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
+        sortButton.setImage(UIImage(named: "sort"), for: .normal)
+        sortButton.addTarget(self,
+                             action: #selector(handleSort),
+                             for: .touchUpInside)
+        sortButton
+            .addGestureRecognizer(UILongPressGestureRecognizer(target: self,
+                                                               action: #selector(handleSortShortcut)))
+
+        sortButton.tintColor = PresentationTheme.current.colors.orangeUI
+        sortButton.accessibilityLabel = NSLocalizedString("BUTTON_SORT", comment: "")
+        sortButton.accessibilityHint = NSLocalizedString("BUTTON_SORT_HINT", comment: "")
+        return sortButton
+    }
+
+    private func rightBarButtonItems() -> [UIBarButtonItem] {
+        var rightBarButtonItems = [UIBarButtonItem]()
+
+        // Sort is only available for VideoGroups
+        if let model = model as? CollectionModel, model.mediaCollection is VLCMLVideoGroup {
+            rightBarButtonItems.append(sortBarButton)
+        }
+        rightBarButtonItems.append(editBarButton)
+        rightBarButtonItems.append(rendererBarButton)
+        return rightBarButtonItems
+    }
+
+    @objc func handleSort() {
+        var currentSortIndex: Int = 0
+        for (index, criteria) in
+            model.sortModel.sortingCriteria.enumerated()
+            where criteria == model.sortModel.currentSort {
+                currentSortIndex = index
+                break
+        }
+        present(sortActionSheet, animated: false) {
+            [sortActionSheet, currentSortIndex] in
+            sortActionSheet.collectionView.selectItem(at:
+                IndexPath(row: currentSortIndex, section: 0), animated: false,
+                                                              scrollPosition: .centeredVertically)
+        }
+    }
+
+    @objc func handleSortShortcut() {
+        model.sort(by: model.sortModel.currentSort, desc: !model.sortModel.desc)
+    }
+
+    @objc func handleEditing() {
+        isEditing = !isEditing
+        setEditing(isEditing, animated: true)
+        navigationItem.rightBarButtonItems = isEditing ? [UIBarButtonItem(barButtonSystemItem: .done,
+                                                                          target: self,
+                                                                          action: #selector(handleEditing))]
+            : rightBarButtonItems()
+        navigationItem.setHidesBackButton(isEditing, animated: true)
+    }
+}
+
 // MARK: - UISearchBarDelegate
 
 extension MediaCategoryViewController {
@@ -433,26 +520,6 @@ extension MediaCategoryViewController: UICollectionViewDelegateFlowLayout {
     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
         return model.cellType.interItemPadding
     }
-
-    func handleSort() {
-        var currentSortIndex: Int = 0
-        for (index, criteria) in
-            model.sortModel.sortingCriteria.enumerated()
-            where criteria == model.sortModel.currentSort {
-                currentSortIndex = index
-                break
-        }
-        present(sortActionSheet, animated: false) {
-            [sortActionSheet, currentSortIndex] in
-            sortActionSheet.collectionView.selectItem(at:
-                IndexPath(row: currentSortIndex, section: 0), animated: false,
-                                                    scrollPosition: .centeredVertically)
-        }
-    }
-
-    func handleSortShortcut() {
-        model.sort(by: model.sortModel.currentSort, desc: !model.sortModel.desc)
-    }
 }
 
 // MARK: VLCActionSheetDelegate

+ 19 - 12
Sources/MediaViewControllers/MediaViewController.swift

@@ -16,19 +16,10 @@ class VLCMediaViewController: VLCPagingViewController<VLCLabelCell>, MediaCatego
 
     var services: Services
     private var rendererButton: UIButton
-    private lazy var sortButton: UIBarButtonItem = {
-        var sortButton = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
-        sortButton.setImage(UIImage(named: "sort"), for: .normal)
-        // It seems that using a custom view, UIBarButtonItem have a offset of 16, therefore adding a large margin
-        if UIView.userInterfaceLayoutDirection(for: sortButton.semanticContentAttribute) == .rightToLeft {
-            sortButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -16)
-        } else {
-            sortButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
-        }
+    private(set) lazy var sortButton: UIBarButtonItem = {
+        let sortButton = setupSortbutton()
+
         sortButton.addTarget(self, action: #selector(handleSort), for: .touchUpInside)
-        sortButton.tintColor = PresentationTheme.current.colors.orangeUI
-        sortButton.accessibilityLabel = NSLocalizedString("BUTTON_SORT", comment: "")
-        sortButton.accessibilityHint = NSLocalizedString("BUTTON_SORT_HINT", comment: "")
         sortButton.addGestureRecognizer(UILongPressGestureRecognizer(target: self,
                                                                      action: #selector(handleSortShortcut(sender:))))
         return UIBarButtonItem(customView: sortButton)
@@ -73,6 +64,22 @@ class VLCMediaViewController: VLCPagingViewController<VLCLabelCell>, MediaCatego
         setupNavigationBar()
     }
 
+    private func setupSortbutton() -> UIButton {
+        let sortButton = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
+
+        sortButton.setImage(UIImage(named: "sort"), for: .normal)
+        // It seems that using a custom view, UIBarButtonItem have a offset of 16, therefore adding a large margin
+        if UIView.userInterfaceLayoutDirection(for: sortButton.semanticContentAttribute) == .rightToLeft {
+            sortButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -16)
+        } else {
+            sortButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
+        }
+        sortButton.tintColor = PresentationTheme.current.colors.orangeUI
+        sortButton.accessibilityLabel = NSLocalizedString("BUTTON_SORT", comment: "")
+        sortButton.accessibilityHint = NSLocalizedString("BUTTON_SORT_HINT", comment: "")
+        return sortButton
+    }
+
     private func setupNavigationBar() {
         if #available(iOS 11.0, *) {
             navigationController?.navigationBar.prefersLargeTitles = false