RileyLinkSetupTableViewController.swift 6.0 KB

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