RemoteNetworkDataSource.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 = 0, wifi
  15. static let count: Int = 2
  16. }
  17. @objc(VLCRemoteNetworkDataSourceDelegate)
  18. protocol RemoteNetworkDataSourceDelegate {
  19. func showViewController(_ viewController: UIViewController)
  20. }
  21. @objc(VLCRemoteNetworkDataSourceAndDelegate)
  22. public class RemoteNetworkDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
  23. let cloudVC = VLCCloudServicesTableViewController(nibName: "VLCCloudServicesTableViewController", bundle: Bundle.main)
  24. @objc weak var delegate: RemoteNetworkDataSourceDelegate?
  25. @objc public let height = RemoteNetworkCellType.count * 55
  26. // MARK: - DataSource
  27. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  28. return RemoteNetworkCellType.count
  29. }
  30. public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  31. guard let cellType = RemoteNetworkCellType(rawValue: indexPath.row) else {
  32. assertionFailure("We're having more rows than types of cells that should never happen")
  33. return UITableViewCell()
  34. }
  35. switch cellType {
  36. case .cloud:
  37. if let networkCell = tableView.dequeueReusableCell(withIdentifier: VLCRemoteNetworkCell.cellIdentifier) {
  38. networkCell.textLabel?.text = cloudVC.title
  39. networkCell.detailTextLabel?.text = cloudVC.detailText
  40. networkCell.imageView?.image = cloudVC.cellImage
  41. return networkCell
  42. }
  43. case .wifi:
  44. if let wifiCell = tableView.dequeueReusableCell(withIdentifier: VLCWiFiUploadTableViewCell.cellIdentifier()) {
  45. return wifiCell
  46. }
  47. }
  48. assertionFailure("Cell is nil, did you forget to register the identifier?")
  49. return UITableViewCell()
  50. }
  51. // MARK: - Delegate
  52. public func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
  53. return RemoteNetworkCellType(rawValue: indexPath.row) == .wifi ? nil : indexPath
  54. }
  55. public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  56. tableView.deselectRow(at: indexPath, animated: true)
  57. if let vc = viewController(indexPath: indexPath) {
  58. delegate?.showViewController(vc)
  59. }
  60. }
  61. @objc func viewController(indexPath: IndexPath) -> UIViewController? {
  62. guard let cellType = RemoteNetworkCellType(rawValue: indexPath.row) else {
  63. assertionFailure("We're having more rows than types of cells that should never happen")
  64. return nil
  65. }
  66. switch cellType {
  67. case .cloud:
  68. return cloudVC
  69. case .wifi:
  70. assertionFailure("We shouldn't get in here since we return nil in willSelect")
  71. return nil
  72. }
  73. }
  74. }