RemoteNetworkDataSource.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*****************************************************************************
  2. * RemoteNetworkDataSource.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # googlemail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import Foundation
  13. enum RemoteNetworkCellType: Int {
  14. case cloud
  15. case streaming
  16. case wifi
  17. static let count: Int = {
  18. var max: Int = 0
  19. while let _ = RemoteNetworkCellType(rawValue: max) { max += 1 }
  20. return max
  21. }()
  22. }
  23. @objc(VLCRemoteNetworkDataSourceDelegate)
  24. protocol RemoteNetworkDataSourceDelegate {
  25. func showViewController(_ viewController: UIViewController)
  26. }
  27. @objc(VLCRemoteNetworkDataSourceAndDelegate)
  28. public class RemoteNetworkDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
  29. let cloudVC = VLCCloudServicesTableViewController(nibName: "VLCCloudServicesTableViewController", bundle: Bundle.main)
  30. let streamingVC = VLCOpenNetworkStreamViewController(nibName: "VLCOpenNetworkStreamViewController", bundle: Bundle.main)
  31. @objc weak var delegate: RemoteNetworkDataSourceDelegate?
  32. @objc public let height = RemoteNetworkCellType.count * 55
  33. // MARK: - DataSource
  34. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  35. return RemoteNetworkCellType.count
  36. }
  37. public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  38. guard let cellType = RemoteNetworkCellType(rawValue: indexPath.row) else {
  39. assertionFailure("We're having more rows than types of cells that should never happen")
  40. return UITableViewCell()
  41. }
  42. switch cellType {
  43. case .cloud:
  44. if let networkCell = tableView.dequeueReusableCell(withIdentifier: VLCRemoteNetworkCell.cellIdentifier) {
  45. networkCell.textLabel?.text = cloudVC.title
  46. networkCell.detailTextLabel?.text = cloudVC.detailText
  47. networkCell.imageView?.image = cloudVC.cellImage
  48. return networkCell
  49. }
  50. case .streaming:
  51. if let networkCell = tableView.dequeueReusableCell(withIdentifier: VLCRemoteNetworkCell.cellIdentifier) {
  52. networkCell.textLabel?.text = streamingVC.title
  53. networkCell.detailTextLabel?.text = streamingVC.detailText
  54. networkCell.imageView?.image = streamingVC.cellImage
  55. networkCell.accessibilityIdentifier = "Stream"
  56. return networkCell
  57. }
  58. case .wifi:
  59. if let wifiCell = tableView.dequeueReusableCell(withIdentifier: VLCWiFiUploadTableViewCell.cellIdentifier()) {
  60. return wifiCell
  61. }
  62. }
  63. assertionFailure("Cell is nil, did you forget to register the identifier?")
  64. return UITableViewCell()
  65. }
  66. // MARK: - Delegate
  67. public func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
  68. return RemoteNetworkCellType(rawValue: indexPath.row) == .wifi ? nil : indexPath
  69. }
  70. public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  71. tableView.deselectRow(at: indexPath, animated: true)
  72. if let vc = viewController(indexPath: indexPath) {
  73. delegate?.showViewController(vc)
  74. }
  75. }
  76. @objc func viewController(indexPath: IndexPath) -> UIViewController? {
  77. guard let cellType = RemoteNetworkCellType(rawValue: indexPath.row) else {
  78. assertionFailure("We're having more rows than types of cells that should never happen")
  79. return nil
  80. }
  81. switch cellType {
  82. case .cloud:
  83. return cloudVC
  84. case .streaming:
  85. return streamingVC
  86. case .wifi:
  87. assertionFailure("We shouldn't get in here since we return nil in willSelect")
  88. return nil
  89. }
  90. }
  91. }