VLCTabBarCoordinator.swift 7.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*****************************************************************************
 * VLCTabbarCooordinator.swift
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2018 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Carola Nitz <nitz.carola # gmail.com>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

import Foundation

class VLCTabbarCooordinator: NSObject, VLCMediaViewControllerDelegate, UITabBarControllerDelegate {

    private var childCoordinators: [NSObject] = []
18 19
    private var tabBarController: UITabBarController
    private var services: Services
20 21
    private let displayController = VLCPlayerDisplayController()

22
    public init(tabBarController: UITabBarController, services: Services) {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        self.tabBarController = tabBarController
        self.services = services
        super.init()
        self.tabBarController.delegate = self
        NotificationCenter.default.addObserver(self, selector: #selector(updateTheme), name: .VLCThemeDidChangeNotification, object: nil)
    }

    @objc public func start() {
        setupViewControllers()
        updateTheme()
    }

    @objc func updateTheme() {
        tabBarController.tabBar.barTintColor = PresentationTheme.current.colors.tabBarColor
        customizeMoreViewController()
    }

    func customizeMoreViewController() {
        if let UITabBarCustomizeViewClass = NSClassFromString("UITabBarCustomizeView") {
            for subview in tabBarController.view.subviews where subview.isKind(of: UITabBarCustomizeViewClass) {
                subview.backgroundColor = PresentationTheme.current.colors.background
                subview.tintColor = PresentationTheme.current.colors.orangeUI
            }
        }
    }

    func tabBarController(_ tabBarController: UITabBarController, willBeginCustomizing viewControllers: [UIViewController]) {
       customizeMoreViewController()
    }

    func setupViewControllers() {

        tabBarController.addChildViewController(displayController)
        tabBarController.view.addSubview(displayController.view)
        displayController.view.layoutMargins = UIEdgeInsets(top:0, left:0, bottom:tabBarController.tabBar.frame.size.height, right:0)
        displayController.didMove(toParentViewController: tabBarController)

        let videoVC = VLCMediaViewController(services: services)
        //this should probably not be the delegate
        videoVC.delegate = self
63
        videoVC.title = NSLocalizedString("Video", comment: "")
64
        videoVC.tabBarItem = UITabBarItem(
65
            title: NSLocalizedString("Video", comment: ""),
66 67
            image: UIImage(named: "TVShowsIcon"),
            selectedImage: UIImage(named: "TVShowsIcon"))
68
        videoVC.tabBarItem.accessibilityIdentifier = "Video"
69 70 71 72 73

        // Audio
        let audioVC = VLCMediaViewController(services: services)
        //this should probably not be the delegate
        audioVC.delegate = self
74
        audioVC.title = NSLocalizedString("Audio", comment: "")
75
        audioVC.tabBarItem = UITabBarItem(
76
            title: NSLocalizedString("Audio", comment: ""),
77 78
            image: UIImage(named: "MusicAlbums"),
            selectedImage:UIImage(named: "MusicAlbums"))
79
        audioVC.tabBarItem.accessibilityIdentifier = "Audio"
80 81 82 83 84

        //Serverlist
        let serverVC = VLCServerListViewController(nibName: nil, bundle: nil)
        serverVC.title = NSLocalizedString("LOCAL_NETWORK", comment: "")
        serverVC.tabBarItem = UITabBarItem(
85
            title: NSLocalizedString("LOCAL_NETWORK", comment: ""),
86 87
            image: UIImage(named: "Local"),
            selectedImage: UIImage(named: "Local"))
88
        serverVC.tabBarItem.accessibilityIdentifier = "Server"
89 90 91

        //CloudServices
        let cloudVC = VLCCloudServicesTableViewController(nibName: "VLCCloudServicesTableViewController", bundle: Bundle.main)
92
        cloudVC.title = NSLocalizedString("CLOUD_SERVICES", comment: "")
93
        cloudVC.tabBarItem = UITabBarItem(
94
            title: NSLocalizedString("CLOUD_SERVICES", comment: ""),
95 96
            image: UIImage(named: "iCloudIcon"),
            selectedImage: UIImage(named: "iCloudIcon"))
97
        cloudVC.tabBarItem.accessibilityIdentifier = "Cloud"
98 99 100

        //Settings
        let settingsVC = VLCSettingsController()
101
        settingsVC.title = NSLocalizedString("Settings", comment: "")
102
        settingsVC.tabBarItem = UITabBarItem(
103
            title: NSLocalizedString("Settings", comment: ""),
104 105
            image: UIImage(named: "Settings"),
            selectedImage: UIImage(named: "Settings"))
106
        settingsVC.tabBarItem.accessibilityIdentifier = "Settings"
107 108 109 110 111

        //Download
        let downloadVC = VLCDownloadViewController()
        downloadVC.title = NSLocalizedString("DOWNLOAD_FROM_HTTP", comment:"")
        downloadVC.tabBarItem = UITabBarItem(
112
            title: NSLocalizedString("DOWNLOAD_FROM_HTTP", comment: ""),
113 114
            image: UIImage(named: "Downloads"),
            selectedImage:  UIImage(named: "Downloads"))
115
        downloadVC.tabBarItem.accessibilityIdentifier = "Downloads"
116 117 118 119 120 121 122 123

        //Streaming
        let streamVC = VLCOpenNetworkStreamViewController(nibName: "VLCOpenNetworkStreamViewController", bundle: Bundle.main)
        streamVC.title = NSLocalizedString("OPEN_NETWORK", comment: "")
        streamVC.tabBarItem = UITabBarItem(
            title:  NSLocalizedString("OPEN_NETWORK", comment: ""),
            image: UIImage(named: "OpenNetStream"),
            selectedImage: UIImage(named: "OpenNetStream"))
124
        streamVC.tabBarItem.accessibilityIdentifier = "Stream"
125

126
        let controllers = [audioVC, serverVC, videoVC, settingsVC, cloudVC, downloadVC, streamVC]
127 128 129
        tabBarController.viewControllers = controllers.map { UINavigationController(rootViewController: $0)}
    }

130
    // MARK: - VLCMediaViewControllerDelegate
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    func mediaViewControllerDidSelectMediaObject(_ VLCMediaViewController: VLCMediaViewController, mediaObject: NSManagedObject) {
        playMedia(media:mediaObject)
    }

    func mediaViewControllerDidSelectSort(_ VLCMediaViewController: VLCMediaViewController) {
        showSortOptions()
    }

    func playMedia(media: NSManagedObject) {
        //that should go into a Coordinator itself
        let vpc = VLCPlaybackController.sharedInstance()
        vpc?.playMediaLibraryObject(media)
    }

    func showSortOptions() {
        //This should be in a subclass
147
        let sortOptionsAlertController = UIAlertController(title: NSLocalizedString("Sort by", comment: ""), message: nil, preferredStyle: .actionSheet)
148 149 150 151 152 153
        let sortByNameAction = UIAlertAction(title: SortOption.alphabetically.localizedDescription, style: .default) { action in
        }
        let sortBySizeAction = UIAlertAction(title: SortOption.size.localizedDescription, style: .default) { action in
        }
        let sortbyDateAction = UIAlertAction(title: SortOption.insertonDate.localizedDescription, style: .default) { action in
        }
154
        let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)
155 156 157 158 159 160 161 162
        sortOptionsAlertController.addAction(sortByNameAction)
        sortOptionsAlertController.addAction(sortbyDateAction)
        sortOptionsAlertController.addAction(sortBySizeAction)
        sortOptionsAlertController.addAction(cancelAction)
        sortOptionsAlertController.view.tintColor = UIColor.vlcOrangeTint()
        tabBarController.present(sortOptionsAlertController, animated: true)
    }
}