ReplacePodViewController.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // ReplacePodViewController.swift
  3. // OmniKitUI
  4. //
  5. // Created by Pete Schwamb on 11/28/18.
  6. // Copyright © 2018 Pete Schwamb. All rights reserved.
  7. //
  8. import UIKit
  9. import LoopKitUI
  10. import OmniKit
  11. class ReplacePodViewController: SetupTableViewController {
  12. enum PodReplacementReason {
  13. case normal
  14. case activationTimeout
  15. case fault(_ podFault: DetailedStatus)
  16. case canceledPairingBeforeApplication
  17. case canceledPairing
  18. }
  19. var replacementReason: PodReplacementReason = .normal {
  20. didSet {
  21. updateButtonTint()
  22. switch replacementReason {
  23. case .normal:
  24. break // Text set in interface builder
  25. case .activationTimeout:
  26. instructionsLabel.text = LocalizedString("Activation time exceeded. The pod must be deactivated before pairing with a new one. Please deactivate and discard pod.", comment: "Instructions when deactivating pod that didn't complete activation in time.")
  27. case .fault(let podFault):
  28. var faultDescription = podFault.faultEventCode.localizedDescription
  29. if let refString = podFault.pdmRef {
  30. faultDescription += String(format: " (%@)", refString)
  31. }
  32. instructionsLabel.text = String(format: LocalizedString("%1$@. Insulin delivery has stopped. Please deactivate and remove pod.", comment: "Format string providing instructions for replacing pod due to a fault. (1: The fault description)"), faultDescription)
  33. case .canceledPairingBeforeApplication:
  34. instructionsLabel.text = LocalizedString("Incompletely set up pod must be deactivated before pairing with a new one. Please deactivate and discard pod.", comment: "Instructions when deactivating pod that has been paired, but not attached.")
  35. case .canceledPairing:
  36. instructionsLabel.text = LocalizedString("Incompletely set up pod must be deactivated before pairing with a new one. Please deactivate and remove pod.", comment: "Instructions when deactivating pod that has been paired and possibly attached.")
  37. }
  38. tableView.reloadData()
  39. }
  40. }
  41. var pumpManager: OmnipodPumpManager! {
  42. didSet {
  43. let podState = pumpManager.state.podState
  44. if let podFault = podState?.fault {
  45. if podFault.podProgressStatus == .activationTimeExceeded {
  46. self.replacementReason = .activationTimeout
  47. } else {
  48. self.replacementReason = .fault(podFault)
  49. }
  50. } else if podState?.setupProgress.primingNeeded == true {
  51. self.replacementReason = .canceledPairingBeforeApplication
  52. } else if podState?.setupProgress.needsCannulaInsertion == true {
  53. self.replacementReason = .canceledPairing
  54. } else {
  55. self.replacementReason = .normal
  56. }
  57. }
  58. }
  59. // MARK: -
  60. @IBOutlet weak var activityIndicator: SetupIndicatorView!
  61. @IBOutlet weak var loadingLabel: UILabel!
  62. @IBOutlet weak var instructionsLabel: UILabel!
  63. private var tryCount: Int = 0
  64. override func viewDidLoad() {
  65. super.viewDidLoad()
  66. continueState = .initial
  67. }
  68. override func setEditing(_ editing: Bool, animated: Bool) {
  69. super.setEditing(editing, animated: animated)
  70. }
  71. // MARK: - UITableViewDelegate
  72. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  73. guard continueState != .deactivating else {
  74. return
  75. }
  76. tableView.deselectRow(at: indexPath, animated: true)
  77. }
  78. // MARK: - Navigation
  79. private enum State {
  80. case initial
  81. case deactivating
  82. case deactivationFailed
  83. case continueAfterFailure
  84. case ready
  85. }
  86. private var continueState: State = .initial {
  87. didSet {
  88. updateButtonTint()
  89. switch continueState {
  90. case .initial:
  91. activityIndicator.state = .hidden
  92. footerView.primaryButton.isEnabled = true
  93. footerView.primaryButton.setDeactivateTitle()
  94. case .deactivating:
  95. activityIndicator.state = .indeterminantProgress
  96. footerView.primaryButton.isEnabled = false
  97. lastError = nil
  98. case .deactivationFailed:
  99. activityIndicator.state = .hidden
  100. footerView.primaryButton.isEnabled = true
  101. footerView.primaryButton.setRetryTitle()
  102. case .continueAfterFailure:
  103. activityIndicator.state = .hidden
  104. footerView.primaryButton.isEnabled = true
  105. footerView.primaryButton.resetTitle()
  106. tableView.beginUpdates()
  107. loadingLabel.text = LocalizedString("Unable to deactivate pod. Please continue and pair a new one.", comment: "Instructions when pod cannot be deactivated")
  108. loadingLabel.isHidden = false
  109. tableView.endUpdates()
  110. case .ready:
  111. navigationItem.rightBarButtonItem = nil
  112. activityIndicator.state = .completed
  113. footerView.primaryButton.isEnabled = true
  114. footerView.primaryButton.resetTitle()
  115. footerView.primaryButton.tintColor = nil
  116. lastError = nil
  117. }
  118. }
  119. }
  120. private var lastError: Error? {
  121. didSet {
  122. guard oldValue != nil || lastError != nil else {
  123. return
  124. }
  125. var errorText = lastError?.localizedDescription
  126. if let error = lastError as? LocalizedError {
  127. let localizedText = [error.errorDescription, error.failureReason, error.recoverySuggestion].compactMap({ $0 }).joined(separator: ". ")
  128. if !localizedText.isEmpty {
  129. errorText = localizedText + "."
  130. }
  131. }
  132. // If we have an error but no error text, generate a string to describe the error
  133. if let error = lastError, (errorText == nil || errorText!.isEmpty) {
  134. errorText = String(describing: error)
  135. }
  136. tableView.beginUpdates()
  137. loadingLabel.text = errorText
  138. let isHidden = (errorText == nil)
  139. loadingLabel.isHidden = isHidden
  140. tableView.endUpdates()
  141. }
  142. }
  143. override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
  144. return continueState == .ready || continueState == .continueAfterFailure
  145. }
  146. override func continueButtonPressed(_ sender: Any) {
  147. switch continueState {
  148. case .ready, .continueAfterFailure:
  149. super.continueButtonPressed(sender)
  150. case .initial, .deactivationFailed:
  151. continueState = .deactivating
  152. deactivate()
  153. case .deactivating:
  154. break
  155. }
  156. }
  157. func deactivate() {
  158. tryCount += 1
  159. let continueAfterFailure = tryCount > 1
  160. pumpManager.deactivatePod(forgetPodOnFail: continueAfterFailure) { (error) in
  161. DispatchQueue.main.async {
  162. if let error = error {
  163. if continueAfterFailure {
  164. self.continueState = .continueAfterFailure
  165. } else {
  166. self.lastError = error
  167. self.continueState = .deactivationFailed
  168. }
  169. } else {
  170. self.continueState = .ready
  171. }
  172. }
  173. }
  174. }
  175. override func cancelButtonPressed(_ sender: Any) {
  176. self.dismiss(animated: true, completion: nil)
  177. }
  178. private func updateButtonTint() {
  179. let buttonTint: UIColor?
  180. if case .normal = replacementReason, case .initial = continueState {
  181. buttonTint = .deleteColor
  182. } else {
  183. buttonTint = nil
  184. }
  185. footerView.primaryButton.tintColor = buttonTint
  186. }
  187. }
  188. private extension SetupButton {
  189. func setDeactivateTitle() {
  190. setTitle(LocalizedString("Deactivate Pod", comment: "Button title for pod deactivation"), for: .normal)
  191. }
  192. func setRetryTitle() {
  193. setTitle(LocalizedString("Retry Pod Deactivation", comment: "Button title for retrying pod deactivation"), for: .normal)
  194. }
  195. }