RileyLinkSetupTableViewController.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // RileyLinkSetupTableViewController.swift
  3. // Loop
  4. //
  5. // Copyright © 2018 LoopKit Authors. All rights reserved.
  6. //
  7. import UIKit
  8. import LoopKit
  9. import LoopKitUI
  10. import RileyLinkKit
  11. import RileyLinkBLEKit
  12. public class RileyLinkSetupTableViewController: SetupTableViewController {
  13. let rileyLinkPumpManager: RileyLinkPumpManager
  14. private lazy var devicesDataSource: RileyLinkDevicesTableViewDataSource = {
  15. return RileyLinkDevicesTableViewDataSource(
  16. rileyLinkPumpManager: rileyLinkPumpManager,
  17. devicesSectionIndex: Section.devices.rawValue
  18. )
  19. }()
  20. public required init?(coder aDecoder: NSCoder) {
  21. let deviceProvider = RileyLinkBluetoothDeviceProvider(autoConnectIDs: [])
  22. rileyLinkPumpManager = RileyLinkPumpManager(rileyLinkDeviceProvider: deviceProvider)
  23. super.init(coder: aDecoder)
  24. }
  25. public override func viewDidLoad() {
  26. super.viewDidLoad()
  27. devicesDataSource.tableView = tableView
  28. tableView.register(SetupImageTableViewCell.nib(), forCellReuseIdentifier: SetupImageTableViewCell.className)
  29. NotificationCenter.default.addObserver(self, selector: #selector(deviceConnectionStateDidChange), name: .DeviceConnectionStateDidChange, object: nil)
  30. updateContinueButtonState()
  31. }
  32. public override func viewWillAppear(_ animated: Bool) {
  33. super.viewWillAppear(animated)
  34. devicesDataSource.isScanningEnabled = true
  35. }
  36. public override func viewDidDisappear(_ animated: Bool) {
  37. super.viewDidDisappear(animated)
  38. devicesDataSource.isScanningEnabled = false
  39. }
  40. // MARK: - Table view data source
  41. private enum Section: Int {
  42. case info
  43. case devices
  44. static let count = 2
  45. }
  46. private enum InfoRow: Int {
  47. case image
  48. case description
  49. static let count = 2
  50. }
  51. public override func numberOfSections(in tableView: UITableView) -> Int {
  52. return Section.count
  53. }
  54. public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  55. switch Section(rawValue: section)! {
  56. case .info:
  57. return InfoRow.count
  58. case .devices:
  59. return devicesDataSource.tableView(tableView, numberOfRowsInSection: section)
  60. }
  61. }
  62. public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  63. switch Section(rawValue: indexPath.section)! {
  64. case .info:
  65. switch InfoRow(rawValue: indexPath.row)! {
  66. case .image:
  67. let cell = tableView.dequeueReusableCell(withIdentifier: SetupImageTableViewCell.className, for: indexPath) as! SetupImageTableViewCell
  68. let bundle = Bundle(for: type(of: self))
  69. cell.mainImageView?.image = UIImage(named: "RileyLink", in: bundle, compatibleWith: cell.traitCollection)
  70. cell.mainImageView?.tintColor = UIColor(named: "RileyLink Tint", in: bundle, compatibleWith: cell.traitCollection)
  71. if #available(iOSApplicationExtension 13.0, *) {
  72. cell.backgroundColor = .systemBackground
  73. }
  74. return cell
  75. case .description:
  76. var cell = tableView.dequeueReusableCell(withIdentifier: "DescriptionCell")
  77. if cell == nil {
  78. cell = UITableViewCell(style: .default, reuseIdentifier: "DescriptionCell")
  79. cell?.selectionStyle = .none
  80. cell?.textLabel?.text = LocalizedString("RileyLink allows for communication with the pump over Bluetooth Low Energy.", comment: "RileyLink setup description")
  81. cell?.textLabel?.numberOfLines = 0
  82. if #available(iOSApplicationExtension 13.0, *) {
  83. cell?.backgroundColor = .systemBackground
  84. }
  85. }
  86. return cell!
  87. }
  88. case .devices:
  89. return devicesDataSource.tableView(tableView, cellForRowAt: indexPath)
  90. }
  91. }
  92. public override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  93. switch Section(rawValue: section)! {
  94. case .info:
  95. return nil
  96. case .devices:
  97. return devicesDataSource.tableView(tableView, titleForHeaderInSection: section)
  98. }
  99. }
  100. public override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  101. switch Section(rawValue: section)! {
  102. case .info:
  103. return nil
  104. case .devices:
  105. return devicesDataSource.tableView(tableView, viewForHeaderInSection: section)
  106. }
  107. }
  108. public override func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
  109. return devicesDataSource.tableView(tableView, estimatedHeightForHeaderInSection: section)
  110. }
  111. public override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
  112. return false
  113. }
  114. // MARK: - Navigation
  115. private var shouldContinue: Bool {
  116. #if targetEnvironment(simulator)
  117. return true
  118. #else
  119. return rileyLinkPumpManager.rileyLinkDeviceProvider.connectingCount > 0
  120. #endif
  121. }
  122. @objc private func deviceConnectionStateDidChange() {
  123. DispatchQueue.main.async {
  124. self.updateContinueButtonState()
  125. }
  126. }
  127. private func updateContinueButtonState() {
  128. footerView.primaryButton.isEnabled = shouldContinue
  129. }
  130. public override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
  131. return shouldContinue
  132. }
  133. }