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

14
enum RemoteNetworkCellType: Int {
15 16
    case cloud
    case streaming
17
    case download
18 19 20 21 22 23
    case wifi
    static let count: Int = {
        var max: Int = 0
        while let _ = RemoteNetworkCellType(rawValue: max) { max += 1 }
        return max
    }()
24 25 26 27 28 29 30 31 32
}

@objc(VLCRemoteNetworkDataSourceDelegate)
protocol RemoteNetworkDataSourceDelegate {
    func showViewController(_ viewController: UIViewController)
}
@objc(VLCRemoteNetworkDataSourceAndDelegate)
public class RemoteNetworkDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
    let cloudVC = VLCCloudServicesTableViewController(nibName: "VLCCloudServicesTableViewController", bundle: Bundle.main)
33
    let streamingVC = VLCOpenNetworkStreamViewController(nibName: "VLCOpenNetworkStreamViewController", bundle: Bundle.main)
34
    let downloadVC = VLCDownloadViewController(nibName: "VLCDownloadViewController", bundle: Bundle.main)
35

36
    @objc weak var delegate: RemoteNetworkDataSourceDelegate?
37

38
    // MARK: - DataSource
39
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
40
        return RemoteNetworkCellType.count
41
    }
42

43
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
44 45 46 47 48 49 50 51 52 53
        guard let cellType = RemoteNetworkCellType(rawValue: indexPath.row) else {
            assertionFailure("We're having more rows than types of cells that should never happen")
            return UITableViewCell()
        }
        switch cellType {
        case .cloud:
            if let networkCell = tableView.dequeueReusableCell(withIdentifier: VLCRemoteNetworkCell.cellIdentifier) {
                networkCell.textLabel?.text = cloudVC.title
                networkCell.detailTextLabel?.text = cloudVC.detailText
                networkCell.imageView?.image = cloudVC.cellImage
Soomin Lee's avatar
Soomin Lee committed
54
                networkCell.accessibilityIdentifier = VLCAccessibilityIdentifier.cloud
55 56
                return networkCell
            }
57 58 59 60 61
        case .streaming:
            if let networkCell = tableView.dequeueReusableCell(withIdentifier: VLCRemoteNetworkCell.cellIdentifier) {
                networkCell.textLabel?.text = streamingVC.title
                networkCell.detailTextLabel?.text = streamingVC.detailText
                networkCell.imageView?.image = streamingVC.cellImage
Soomin Lee's avatar
Soomin Lee committed
62
                networkCell.accessibilityIdentifier = VLCAccessibilityIdentifier.stream
63 64
                return networkCell
            }
65 66 67 68 69
        case .download:
            if let networkCell = tableView.dequeueReusableCell(withIdentifier: VLCRemoteNetworkCell.cellIdentifier) {
                networkCell.textLabel?.text = downloadVC.title
                networkCell.detailTextLabel?.text = downloadVC.detailText
                networkCell.imageView?.image = downloadVC.cellImage
Soomin Lee's avatar
Soomin Lee committed
70
                networkCell.accessibilityIdentifier = VLCAccessibilityIdentifier.downloads
71 72
                return networkCell
            }
73 74 75 76
        case .wifi:
            if let wifiCell = tableView.dequeueReusableCell(withIdentifier: VLCWiFiUploadTableViewCell.cellIdentifier()) {
                return wifiCell
            }
77
        }
78
        assertionFailure("Cell is nil, did you forget to register the identifier?")
79 80
        return UITableViewCell()
    }
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    // MARK: - Delegate
    public func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        return RemoteNetworkCellType(rawValue: indexPath.row) == .wifi ? nil : indexPath
    }

    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if let vc = viewController(indexPath: indexPath) {
            delegate?.showViewController(vc)
        }
    }
    
    @objc func viewController(indexPath: IndexPath) -> UIViewController? {
        guard let cellType = RemoteNetworkCellType(rawValue: indexPath.row) else {
            assertionFailure("We're having more rows than types of cells that should never happen")
            return nil
        }
        switch cellType {
        case .cloud:
            return cloudVC
102 103
        case .streaming:
            return streamingVC
104 105
        case .download:
            return downloadVC
106 107 108 109 110
        case .wifi:
            assertionFailure("We shouldn't get in here since we return nil in willSelect")
            return nil
        }
    }
111
}