InsertCannulaViewModel.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // InsertCannulaViewModel.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 3/10/20.
  6. // Copyright © 2021 LoopKit Authors. All rights reserved.
  7. //
  8. import LoopKit
  9. import LoopKitUI
  10. import OmniKit
  11. public protocol CannulaInserter {
  12. func insertCannula(completion: @escaping (Result<TimeInterval,OmnipodPumpManagerError>) -> ())
  13. func checkCannulaInsertionFinished(completion: @escaping (OmnipodPumpManagerError?) -> Void)
  14. }
  15. extension OmnipodPumpManager: CannulaInserter {}
  16. class InsertCannulaViewModel: ObservableObject, Identifiable {
  17. enum InsertCannulaViewModelState {
  18. case ready
  19. case startingInsertion
  20. case inserting(finishTime: CFTimeInterval)
  21. case checkingInsertion
  22. case error(OmnipodPumpManagerError)
  23. case finished
  24. var actionButtonAccessibilityLabel: String {
  25. switch self {
  26. case .ready, .startingInsertion:
  27. return LocalizedString("Slide Button to insert Cannula", comment: "Insert cannula slider button accessibility label while ready to pair")
  28. case .inserting:
  29. return LocalizedString("Inserting. Please wait.", comment: "Insert cannula action button accessibility label while pairing")
  30. case .checkingInsertion:
  31. return LocalizedString("Checking Insertion", comment: "Insert cannula action button accessibility label checking insertion")
  32. case .error(let error):
  33. return String(format: "%@ %@", error.errorDescription ?? "", error.recoverySuggestion ?? "")
  34. case .finished:
  35. return LocalizedString("Cannula inserted successfully. Continue.", comment: "Insert cannula action button accessibility label when cannula insertion succeeded")
  36. }
  37. }
  38. var instructionsDisabled: Bool {
  39. switch self {
  40. case .ready, .error:
  41. return false
  42. default:
  43. return true
  44. }
  45. }
  46. var nextActionButtonDescription: String {
  47. switch self {
  48. case .ready:
  49. return LocalizedString("Slide to Insert Cannula", comment: "Cannula insertion button text while ready to insert")
  50. case .error:
  51. return LocalizedString("Retry", comment: "Cannula insertion button text while showing error")
  52. case .inserting, .startingInsertion:
  53. return LocalizedString("Inserting...", comment: "Cannula insertion button text while inserting")
  54. case .checkingInsertion:
  55. return LocalizedString("Checking...", comment: "Cannula insertion button text while checking insertion")
  56. case .finished:
  57. return LocalizedString("Continue", comment: "Cannula insertion button text when inserted")
  58. }
  59. }
  60. var nextActionButtonStyle: ActionButton.ButtonType {
  61. switch self {
  62. case .error(let error):
  63. if !error.recoverable {
  64. return .destructive
  65. }
  66. default:
  67. break
  68. }
  69. return .primary
  70. }
  71. var progressState: ProgressIndicatorState {
  72. switch self {
  73. case .ready, .error:
  74. return .hidden
  75. case .startingInsertion, .checkingInsertion:
  76. return .indeterminantProgress
  77. case .inserting(let finishTime):
  78. return .timedProgress(finishTime: finishTime)
  79. case .finished:
  80. return .completed
  81. }
  82. }
  83. var showProgressDetail: Bool {
  84. switch self {
  85. case .ready:
  86. return false
  87. default:
  88. return true
  89. }
  90. }
  91. var isProcessing: Bool {
  92. switch self {
  93. case .startingInsertion, .inserting:
  94. return true
  95. default:
  96. return false
  97. }
  98. }
  99. var isFinished: Bool {
  100. if case .finished = self {
  101. return true
  102. }
  103. return false
  104. }
  105. }
  106. var error: OmnipodPumpManagerError? {
  107. if case .error(let error) = self.state {
  108. return error
  109. }
  110. return nil
  111. }
  112. @Published var state: InsertCannulaViewModelState = .ready
  113. public var stateNeedsDeliberateUserAcceptance : Bool {
  114. switch state {
  115. case .ready:
  116. true
  117. default:
  118. false
  119. }
  120. }
  121. var didFinish: (() -> Void)?
  122. var didRequestDeactivation: (() -> Void)?
  123. var cannulaInserter: CannulaInserter
  124. init(cannulaInserter: CannulaInserter) {
  125. self.cannulaInserter = cannulaInserter
  126. }
  127. // private func handleEvent(_ event: ActivationStep2Event) {
  128. // switch event {
  129. // case .insertingCannula:
  130. // let finishTime = TimeInterval(Pod.estimatedCannulaInsertionDuration)
  131. // state = .inserting(finishTime: CACurrentMediaTime() + finishTime)
  132. // case .step2Completed:
  133. // state = .finished
  134. // default:
  135. // break
  136. // }
  137. // }
  138. private func checkCannulaInsertionFinished() {
  139. state = .startingInsertion
  140. cannulaInserter.checkCannulaInsertionFinished() { (error) in
  141. DispatchQueue.main.async {
  142. if let error = error {
  143. self.state = .error(error)
  144. } else {
  145. self.state = .finished
  146. }
  147. }
  148. }
  149. }
  150. private func insertCannula() {
  151. state = .startingInsertion
  152. cannulaInserter.insertCannula { (result) in
  153. DispatchQueue.main.async {
  154. switch(result) {
  155. case .success(let finishTime):
  156. self.state = .inserting(finishTime: CACurrentMediaTime() + finishTime)
  157. let delay = finishTime
  158. if delay > 0 {
  159. DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
  160. self.checkCannulaInsertionFinished() // now check if actually ready
  161. }
  162. } else {
  163. self.state = .finished
  164. }
  165. case .failure(let error):
  166. self.state = .error(error)
  167. }
  168. }
  169. // switch status {
  170. // case .error(let error):
  171. // self.state = .error(error)
  172. // case .event(let event):
  173. // self.handleEvent(event)
  174. // }
  175. }
  176. }
  177. public func continueButtonTapped() {
  178. switch state {
  179. case .finished:
  180. didFinish?()
  181. case .error(let error):
  182. if error.recoverable {
  183. insertCannula()
  184. } else {
  185. didRequestDeactivation?()
  186. }
  187. default:
  188. insertCannula()
  189. }
  190. }
  191. }
  192. public extension OmnipodPumpManagerError {
  193. var recoverable: Bool {
  194. //TODO
  195. return true
  196. // switch self {
  197. // case .podIsInAlarm:
  198. // return false
  199. // case .activationError(let activationErrorCode):
  200. // switch activationErrorCode {
  201. // case .podIsLumpOfCoal1Hour, .podIsLumpOfCoal2Hours:
  202. // return false
  203. // default:
  204. // return true
  205. // }
  206. // case .internalError(.incompatibleProductId):
  207. // return false
  208. // case .systemError:
  209. // return false
  210. // default:
  211. // return true
  212. // }
  213. }
  214. }