DeactivatePodViewModel.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // DeactivatePodViewModel.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 3/9/20.
  6. // Copyright © 2021 LoopKit Authors. All rights reserved.
  7. //
  8. import Foundation
  9. import LoopKitUI
  10. import OmniKit
  11. public protocol PodDeactivater {
  12. func deactivatePod(completion: @escaping (OmnipodPumpManagerError?) -> Void)
  13. func forgetPod(completion: @escaping () -> Void)
  14. }
  15. extension OmnipodPumpManager: PodDeactivater {}
  16. class DeactivatePodViewModel: ObservableObject, Identifiable {
  17. public var podAttachedToBody: Bool
  18. var instructionText: String {
  19. if podAttachedToBody {
  20. return LocalizedString("Please deactivate the pod. When deactivation is complete, you may remove it and pair a new pod.", comment: "Instructions for deactivate pod when pod is on body")
  21. } else {
  22. return LocalizedString("Please deactivate the pod. When deactivation is complete, you may pair a new pod.", comment: "Instructions for deactivate pod when pod not on body")
  23. }
  24. }
  25. enum DeactivatePodViewModelState {
  26. case active
  27. case deactivating
  28. case resultError(DeactivationError)
  29. case finished
  30. var actionButtonAccessibilityLabel: String {
  31. switch self {
  32. case .active:
  33. return LocalizedString("Deactivate Pod", comment: "Deactivate pod action button accessibility label while ready to deactivate")
  34. case .deactivating:
  35. return LocalizedString("Deactivating.", comment: "Deactivate pod action button accessibility label while deactivating")
  36. case .resultError(let error):
  37. return String(format: "%@ %@", error.errorDescription ?? "", error.recoverySuggestion ?? "")
  38. case .finished:
  39. return LocalizedString("Pod deactivated successfully. Continue.", comment: "Deactivate pod action button accessibility label when deactivation complete")
  40. }
  41. }
  42. var actionButtonDescription: String {
  43. switch self {
  44. case .active:
  45. return LocalizedString("Deactivate Pod", comment: "Action button description for deactivate while pod still active")
  46. case .resultError:
  47. return LocalizedString("Retry", comment: "Action button description for deactivate after failed attempt")
  48. case .deactivating:
  49. return LocalizedString("Deactivating...", comment: "Action button description while deactivating")
  50. case .finished:
  51. return LocalizedString("Continue", comment: "Action button description when deactivated")
  52. }
  53. }
  54. var actionButtonStyle: ActionButton.ButtonType {
  55. switch self {
  56. case .active:
  57. return .destructive
  58. default:
  59. return .primary
  60. }
  61. }
  62. var progressState: ProgressIndicatorState {
  63. switch self {
  64. case .active, .resultError:
  65. return .hidden
  66. case .deactivating:
  67. return .indeterminantProgress
  68. case .finished:
  69. return .completed
  70. }
  71. }
  72. var showProgressDetail: Bool {
  73. switch self {
  74. case .active:
  75. return false
  76. default:
  77. return true
  78. }
  79. }
  80. var isProcessing: Bool {
  81. switch self {
  82. case .deactivating:
  83. return true
  84. default:
  85. return false
  86. }
  87. }
  88. var isFinished: Bool {
  89. if case .finished = self {
  90. return true
  91. }
  92. return false
  93. }
  94. }
  95. @Published var state: DeactivatePodViewModelState = .active
  96. var error: DeactivationError? {
  97. if case .resultError(let error) = self.state {
  98. return error
  99. }
  100. return nil
  101. }
  102. var didFinish: (() -> Void)?
  103. var didCancel: (() -> Void)?
  104. var podDeactivator: PodDeactivater
  105. init(podDeactivator: PodDeactivater, podAttachedToBody: Bool) {
  106. self.podDeactivator = podDeactivator
  107. self.podAttachedToBody = podAttachedToBody
  108. }
  109. public func continueButtonTapped() {
  110. if case .finished = state {
  111. didFinish?()
  112. } else {
  113. self.state = .deactivating
  114. podDeactivator.deactivatePod { (error) in
  115. DispatchQueue.main.async {
  116. if let error = error {
  117. self.state = .resultError(DeactivationError.OmnipodPumpManagerError(error))
  118. } else {
  119. self.discardPod(navigateOnCompletion: false)
  120. }
  121. }
  122. }
  123. }
  124. }
  125. public func discardPod(navigateOnCompletion: Bool = true) {
  126. podDeactivator.forgetPod {
  127. DispatchQueue.main.async {
  128. if navigateOnCompletion {
  129. self.didFinish?()
  130. } else {
  131. self.state = .finished
  132. }
  133. }
  134. }
  135. }
  136. }
  137. enum DeactivationError : LocalizedError {
  138. case OmnipodPumpManagerError(OmnipodPumpManagerError)
  139. var recoverySuggestion: String? {
  140. switch self {
  141. case .OmnipodPumpManagerError:
  142. return LocalizedString("There was a problem communicating with the pod. If this problem persists, tap Discard Pod. You can then activate a new Pod.", comment: "Format string for recovery suggestion during deactivate pod.")
  143. }
  144. }
  145. var errorDescription: String? {
  146. switch self {
  147. case .OmnipodPumpManagerError(let error):
  148. return error.errorDescription
  149. }
  150. }
  151. }