InsertCannulaViewModel.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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("Insert Cannula", comment: "Insert cannula action 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("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. var didFinish: (() -> Void)?
  114. var didRequestDeactivation: (() -> Void)?
  115. var cannulaInserter: CannulaInserter
  116. init(cannulaInserter: CannulaInserter) {
  117. self.cannulaInserter = cannulaInserter
  118. }
  119. // private func handleEvent(_ event: ActivationStep2Event) {
  120. // switch event {
  121. // case .insertingCannula:
  122. // let finishTime = TimeInterval(Pod.estimatedCannulaInsertionDuration)
  123. // state = .inserting(finishTime: CACurrentMediaTime() + finishTime)
  124. // case .step2Completed:
  125. // state = .finished
  126. // default:
  127. // break
  128. // }
  129. // }
  130. private func checkCannulaInsertionFinished() {
  131. state = .startingInsertion
  132. cannulaInserter.checkCannulaInsertionFinished() { (error) in
  133. DispatchQueue.main.async {
  134. if let error = error {
  135. self.state = .error(error)
  136. } else {
  137. self.state = .finished
  138. }
  139. }
  140. }
  141. }
  142. private func insertCannula() {
  143. state = .startingInsertion
  144. cannulaInserter.insertCannula { (result) in
  145. DispatchQueue.main.async {
  146. switch(result) {
  147. case .success(let finishTime):
  148. self.state = .inserting(finishTime: CACurrentMediaTime() + finishTime)
  149. let delay = finishTime
  150. if delay > 0 {
  151. DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
  152. self.checkCannulaInsertionFinished() // now check if actually ready
  153. }
  154. } else {
  155. self.state = .finished
  156. }
  157. case .failure(let error):
  158. self.state = .error(error)
  159. }
  160. }
  161. // switch status {
  162. // case .error(let error):
  163. // self.state = .error(error)
  164. // case .event(let event):
  165. // self.handleEvent(event)
  166. // }
  167. }
  168. }
  169. public func continueButtonTapped() {
  170. switch state {
  171. case .finished:
  172. didFinish?()
  173. case .error(let error):
  174. if error.recoverable {
  175. insertCannula()
  176. } else {
  177. didRequestDeactivation?()
  178. }
  179. default:
  180. insertCannula()
  181. }
  182. }
  183. }
  184. public extension OmnipodPumpManagerError {
  185. var recoverable: Bool {
  186. //TODO
  187. return true
  188. // switch self {
  189. // case .podIsInAlarm:
  190. // return false
  191. // case .activationError(let activationErrorCode):
  192. // switch activationErrorCode {
  193. // case .podIsLumpOfCoal1Hour, .podIsLumpOfCoal2Hours:
  194. // return false
  195. // default:
  196. // return true
  197. // }
  198. // case .internalError(.incompatibleProductId):
  199. // return false
  200. // case .systemError:
  201. // return false
  202. // default:
  203. // return true
  204. // }
  205. }
  206. }