RemoteNetworkDataSource.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 download
  17. case wifi
  18. static let count: Int = {
  19. var max: Int = 0
  20. while let _ = RemoteNetworkCellType(rawValue: max) { max += 1 }
  21. return max
  22. }()
  23. }
  24. @objc(VLCRemoteNetworkDataSourceDelegate)
  25. protocol RemoteNetworkDataSourceDelegate {
  26. func showViewController(_ viewController: UIViewController)
  27. }
  28. @objc(VLCRemoteNetworkDataSourceAndDelegate)
  29. class RemoteNetworkDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
  30. let cloudVC = VLCCloudServicesTableViewController(nibName: "VLCCloudServicesTableViewController", bundle: Bundle.main)
  31. let streamingVC = VLCOpenNetworkStreamViewController(nibName: "VLCOpenNetworkStreamViewController", bundle: Bundle.main)
  32. let downloadVC = VLCDownloadViewController(nibName: "VLCDownloadViewController", bundle: Bundle.main)
  33. @objc weak var delegate: RemoteNetworkDataSourceDelegate?
  34. // MARK: - DataSource
  35. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  36. return RemoteNetworkCellType.count
  37. }
  38. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  39. guard let cellType = RemoteNetworkCellType(rawValue: indexPath.row) else {
  40. assertionFailure("We're having more rows than types of cells that should never happen")
  41. return UITableViewCell()
  42. }
  43. switch cellType {
  44. case .cloud:
  45. if let networkCell = tableView.dequeueReusableCell(withIdentifier: VLCRemoteNetworkCell.cellIdentifier) {
  46. networkCell.textLabel?.text = cloudVC.title
  47. networkCell.detailTextLabel?.text = cloudVC.detailText
  48. networkCell.imageView?.image = cloudVC.cellImage
  49. networkCell.accessibilityIdentifier = VLCAccessibilityIdentifier.cloud
  50. return networkCell
  51. }
  52. case .streaming:
  53. if let networkCell = tableView.dequeueReusableCell(withIdentifier: VLCRemoteNetworkCell.cellIdentifier) {
  54. networkCell.textLabel?.text = streamingVC.title
  55. networkCell.detailTextLabel?.text = streamingVC.detailText
  56. networkCell.imageView?.image = streamingVC.cellImage
  57. networkCell.accessibilityIdentifier = VLCAccessibilityIdentifier.stream
  58. return networkCell
  59. }
  60. case .download:
  61. if let networkCell = tableView.dequeueReusableCell(withIdentifier: VLCRemoteNetworkCell.cellIdentifier) {
  62. networkCell.textLabel?.text = downloadVC.title
  63. networkCell.detailTextLabel?.text = downloadVC.detailText
  64. networkCell.imageView?.image = downloadVC.cellImage
  65. networkCell.accessibilityIdentifier = VLCAccessibilityIdentifier.downloads
  66. return networkCell
  67. }
  68. case .wifi:
  69. if let wifiCell = tableView.dequeueReusableCell(withIdentifier: VLCWiFiUploadTableViewCell.cellIdentifier()) {
  70. return wifiCell
  71. }
  72. }
  73. assertionFailure("Cell is nil, did you forget to register the identifier?")
  74. return UITableViewCell()
  75. }
  76. // MARK: - Delegate
  77. func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
  78. return RemoteNetworkCellType(rawValue: indexPath.row) == .wifi ? nil : indexPath
  79. }
  80. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  81. tableView.deselectRow(at: indexPath, animated: true)
  82. if let vc = viewController(indexPath: indexPath) {
  83. delegate?.showViewController(vc)
  84. }
  85. }
  86. @objc func viewController(indexPath: IndexPath) -> UIViewController? {
  87. guard let cellType = RemoteNetworkCellType(rawValue: indexPath.row) else {
  88. assertionFailure("We're having more rows than types of cells that should never happen")
  89. return nil
  90. }
  91. switch cellType {
  92. case .cloud:
  93. return cloudVC
  94. case .streaming:
  95. return streamingVC
  96. case .download:
  97. return downloadVC
  98. case .wifi:
  99. assertionFailure("We shouldn't get in here since we return nil in willSelect")
  100. return nil
  101. }
  102. }
  103. }