瀏覽代碼

VLCMediaViewController: Hide edit and sortbutton when viewcontroller is empty

(fixes #478)
Carola Nitz 6 年之前
父節點
當前提交
771df44b94

+ 5 - 1
Sources/MediaCategories/MediaCategoryViewController.swift

@@ -138,8 +138,12 @@ class VLCMediaCategoryViewController: UICollectionViewController, UICollectionVi
         reloadData()
     }
 
+    func isEmptyCollectionView() -> Bool {
+        return collectionView?.numberOfItems(inSection: 0) == 0
+    }
+
     func updateUIForContent() {
-        let isEmpty = collectionView?.numberOfItems(inSection: 0) == 0
+        let isEmpty = isEmptyCollectionView()
 
         if isEmpty {
             collectionView?.setContentOffset(.zero, animated: false)

+ 21 - 7
Sources/MediaViewControllers/MediaViewController.swift

@@ -16,11 +16,17 @@ class VLCMediaViewController: VLCPagingViewController<VLCLabelCell> {
     var services: Services
     private var rendererButton: UIButton
     private var sortButton: UIBarButtonItem?
+    private var rigthBarButtons: [UIBarButtonItem]?
 
     init(services: Services) {
         self.services = services
         rendererButton = services.rendererDiscovererManager.setupRendererButton()
         super.init(nibName: nil, bundle: nil)
+        rigthBarButtons = [editButtonItem, UIBarButtonItem(customView: rendererButton)]
+        sortButton = UIBarButtonItem(title: NSLocalizedString("SORT", comment: ""),
+                                     style: .plain,
+                                     target: self,
+                                     action: #selector(handleSort))
     }
 
     override func viewDidLoad() {
@@ -30,8 +36,8 @@ class VLCMediaViewController: VLCPagingViewController<VLCLabelCell> {
             oldCell?.iconLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor
             newCell?.iconLabel.textColor = PresentationTheme.current.colors.orangeUI
         }
-        setupNavigationBar()
         super.viewDidLoad()
+        setupNavigationBar()
     }
 
     private func setupNavigationBar() {
@@ -39,12 +45,7 @@ class VLCMediaViewController: VLCPagingViewController<VLCLabelCell> {
             navigationController?.navigationBar.prefersLargeTitles = false
         }
         navigationController?.navigationBar.isTranslucent = false
-        navigationItem.rightBarButtonItems = [editButtonItem, UIBarButtonItem(customView: rendererButton)]
-        sortButton = UIBarButtonItem(title: NSLocalizedString("SORT", comment: ""),
-                                     style: .plain,
-                                     target: self,
-                                     action: #selector(handleSort))
-        navigationItem.leftBarButtonItem = sortButton
+        updateButtonsFor(viewControllers[currentIndex])
     }
 
     // MARK: - PagerTabStripDataSource
@@ -53,11 +54,24 @@ class VLCMediaViewController: VLCPagingViewController<VLCLabelCell> {
         fatalError("this should only be used as subclass")
     }
 
+    func updateButtonsFor(_ viewController: UIViewController) {
+        var showButtons = false
+        if let mediaCategoryViewController = viewController as? VLCMediaCategoryViewController,
+            !mediaCategoryViewController.isEmptyCollectionView() {
+            showButtons = true
+        }
+        navigationItem.rightBarButtonItems = showButtons ? rigthBarButtons : nil
+        navigationItem.leftBarButtonItem = showButtons ? sortButton : nil
+    }
+
     override func configure(cell: VLCLabelCell, for indicatorInfo: IndicatorInfo) {
         cell.iconLabel.text = indicatorInfo.title
     }
 
     override func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) {
+        if indexWasChanged {
+            updateButtonsFor(viewControllers[toIndex])
+        }
         super.updateIndicator(for: viewController, fromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: progressPercentage, indexWasChanged: indexWasChanged)
     }