123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- /*****************************************************************************
- * 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
- enum RemoteNetworkCellType: Int {
- case cloud
- case streaming
- case download
- case wifi
- static let count: Int = {
- var max: Int = 0
- while let _ = RemoteNetworkCellType(rawValue: max) { max += 1 }
- return max
- }()
- }
- @objc(VLCRemoteNetworkDataSourceDelegate)
- protocol RemoteNetworkDataSourceDelegate {
- func showViewController(_ viewController: UIViewController)
- }
- @objc(VLCRemoteNetworkDataSourceAndDelegate)
- class RemoteNetworkDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
- let cloudVC = VLCCloudServicesTableViewController(nibName: "VLCCloudServicesTableViewController", bundle: Bundle.main)
- let streamingVC = VLCOpenNetworkStreamViewController(nibName: "VLCOpenNetworkStreamViewController", bundle: Bundle.main)
- let downloadVC = VLCDownloadViewController(nibName: "VLCDownloadViewController", bundle: Bundle.main)
- @objc weak var delegate: RemoteNetworkDataSourceDelegate?
- // MARK: - DataSource
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return RemoteNetworkCellType.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- 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
- networkCell.accessibilityIdentifier = VLCAccessibilityIdentifier.cloud
- return networkCell
- }
- 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
- networkCell.accessibilityIdentifier = VLCAccessibilityIdentifier.stream
- return networkCell
- }
- 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
- networkCell.accessibilityIdentifier = VLCAccessibilityIdentifier.downloads
- return networkCell
- }
- case .wifi:
- if let wifiCell = tableView.dequeueReusableCell(withIdentifier: VLCWiFiUploadTableViewCell.cellIdentifier()) {
- return wifiCell
- }
- }
- assertionFailure("Cell is nil, did you forget to register the identifier?")
- return UITableViewCell()
- }
- // MARK: - Delegate
- func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
- return RemoteNetworkCellType(rawValue: indexPath.row) == .wifi ? nil : indexPath
- }
- 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
- case .streaming:
- return streamingVC
- case .download:
- return downloadVC
- case .wifi:
- assertionFailure("We shouldn't get in here since we return nil in willSelect")
- return nil
- }
- }
- }
|