MediaViewController.swift 6.83 KB
Newer Older
1 2 3 4 5 6 7
/*****************************************************************************
 * MediaViewController.swift
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2018 VideoLAN. All rights reserved.
 * $Id$
 *
Carola Nitz's avatar
Carola Nitz committed
8
 * Authors: Carola Nitz <caro # videolan.org>
9 10 11 12
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

Carola Nitz's avatar
Carola Nitz committed
13
import UIKit
14

15 16
class VLCMediaViewController: VLCPagingViewController<VLCLabelCell>, MediaCategoryViewControllerDelegate {

Carola Nitz's avatar
Carola Nitz committed
17 18
    var services: Services
    private var rendererButton: UIButton
19
    private lazy var sortButton: UIBarButtonItem = {
20 21 22 23 24 25 26 27 28
        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)
        }
        sortButton.addTarget(self, action: #selector(handleSort), for: .touchUpInside)
29 30 31
        sortButton.tintColor = PresentationTheme.current.colors.orangeUI
        sortButton.accessibilityLabel = NSLocalizedString("BUTTON_SORT", comment: "")
        sortButton.accessibilityHint = NSLocalizedString("BUTTON_SORT_HINT", comment: "")
32 33 34
        sortButton.addGestureRecognizer(UILongPressGestureRecognizer(target: self,
                                                                     action: #selector(handleSortShortcut(sender:))))
        return UIBarButtonItem(customView: sortButton)
35 36
    }()

37 38 39 40 41 42 43 44 45 46
    private lazy var editButton: UIBarButtonItem = {
        var editButton = UIBarButtonItem(image: UIImage(named: "edit"),
                                     style: .plain, target: self,
                                     action: #selector(customSetEditing(button:)))
        editButton.tintColor = PresentationTheme.current.colors.orangeUI
        editButton.accessibilityLabel = NSLocalizedString("BUTTON_EDIT", comment: "")
        editButton.accessibilityHint = NSLocalizedString("BUTTON_EDIT_HINT", comment: "")
        return editButton
    }()

47 48 49 50
    private lazy var doneButton: UIBarButtonItem = {
        return UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(customSetEditing(button:)))
    }()

51
    private var rigthBarButtons: [UIBarButtonItem]?
52
    private var leftBarButton: UIBarButtonItem?
53

Carola Nitz's avatar
Carola Nitz committed
54 55 56 57
    init(services: Services) {
        self.services = services
        rendererButton = services.rendererDiscovererManager.setupRendererButton()
        super.init(nibName: nil, bundle: nil)
58
        rigthBarButtons = [editButton, UIBarButtonItem(customView: rendererButton)]
59
        leftBarButton = sortButton
60 61
    }

Soomin Lee's avatar
Soomin Lee committed
62
    override func viewDidLoad() {
63

Carola Nitz's avatar
Carola Nitz committed
64 65 66 67
        changeCurrentIndexProgressive = { (oldCell: VLCLabelCell?, newCell: VLCLabelCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) in
            guard changeCurrentIndex == true else { return }
            oldCell?.iconLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor
            newCell?.iconLabel.textColor = PresentationTheme.current.colors.orangeUI
Soomin Lee's avatar
Soomin Lee committed
68
        }
Carola Nitz's avatar
Carola Nitz committed
69
        super.viewDidLoad()
70 71 72
        viewControllers.forEach {
            ($0 as? VLCMediaCategoryViewController)?.delegate = self
        }
73
        setupNavigationBar()
74 75
    }

Carola Nitz's avatar
Carola Nitz committed
76
    private func setupNavigationBar() {
77
        if #available(iOS 11.0, *) {
Carola Nitz's avatar
Carola Nitz committed
78
            navigationController?.navigationBar.prefersLargeTitles = false
79
        }
80
        navigationController?.navigationBar.isTranslucent = false
81
        updateButtonsFor(viewControllers[currentIndex])
82
    }
83
    // MARK: - MediaCatgoryViewControllerDelegate
84

85
    func needsToUpdateNavigationbarIfNeeded(_ viewController: VLCMediaCategoryViewController) {
86 87
        if viewController == viewControllers[currentIndex] {
            updateButtonsFor(viewController)
88 89
        }
    }
Carola Nitz's avatar
Carola Nitz committed
90
    // MARK: - PagerTabStripDataSource
91

Carola Nitz's avatar
Carola Nitz committed
92 93
    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
        fatalError("this should only be used as subclass")
94 95
    }

96 97 98
    func updateButtonsFor(_ viewController: UIViewController) {
        var showButtons = false
        if let mediaCategoryViewController = viewController as? VLCMediaCategoryViewController,
99 100
            !mediaCategoryViewController.isEmptyCollectionView()
                && !mediaCategoryViewController.isSearching {
101 102 103
            showButtons = true
        }
        navigationItem.rightBarButtonItems = showButtons ? rigthBarButtons : nil
104
        navigationItem.leftBarButtonItem = showButtons ? leftBarButton : nil
105 106
    }

Carola Nitz's avatar
Carola Nitz committed
107 108
    override func configure(cell: VLCLabelCell, for indicatorInfo: IndicatorInfo) {
        cell.iconLabel.text = indicatorInfo.title
109 110
    }

Carola Nitz's avatar
Carola Nitz committed
111
    override func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) {
112 113 114
        if indexWasChanged {
            updateButtonsFor(viewControllers[toIndex])
        }
Carola Nitz's avatar
Carola Nitz committed
115
        super.updateIndicator(for: viewController, fromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: progressPercentage, indexWasChanged: indexWasChanged)
116
    }
117

Carola Nitz's avatar
Carola Nitz committed
118 119
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return PresentationTheme.current.colors.statusBarStyle
120
    }
121 122 123 124 125 126 127
}

// MARK: - Edit

extension VLCMediaViewController {
    @objc private func customSetEditing(button: UIButton) {
        isEditing = !isEditing
128 129
        rigthBarButtons = isEditing ? [doneButton] : [editButton, UIBarButtonItem(customView: rendererButton)]
        leftBarButton = isEditing ? nil : sortButton
130 131
    }

Soomin Lee's avatar
Soomin Lee committed
132 133
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
134 135

        scrollingEnabled(!editing)
Soomin Lee's avatar
Soomin Lee committed
136 137
        viewControllers[currentIndex].setEditing(editing, animated: animated)
    }
Soomin Lee's avatar
Soomin Lee committed
138
}
139 140 141 142 143 144 145 146 147

// MARK: - Sort

extension VLCMediaViewController {
    @objc func handleSort() {
        if let mediaCategoryViewController = viewControllers[currentIndex] as? VLCMediaCategoryViewController {
            mediaCategoryViewController.handleSort()
        }
    }
148 149 150 151 152 153 154 155 156 157 158

    @objc func handleSortShortcut(sender: UILongPressGestureRecognizer) {
        if sender.state == .began {
            if #available(iOS 10.0, *) {
                UIImpactFeedbackGenerator(style: .light).impactOccurred()
            }
            if let mediaCategoryViewController = viewControllers[currentIndex] as? VLCMediaCategoryViewController {
                mediaCategoryViewController.handleSortShortcut()
            }
        }
    }
159
}