WatchStateModel.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import Combine
  2. import Foundation
  3. import SwiftUI
  4. import WatchConnectivity
  5. class WatchStateModel: NSObject, ObservableObject {
  6. var session: WCSession
  7. @Published var glucose = "00"
  8. @Published var trend = "→"
  9. @Published var delta = "+00"
  10. @Published var lastLoopDate: Date?
  11. @Published var glucoseDate: Date?
  12. @Published var bolusIncrement: Decimal?
  13. @Published var maxCOB: Decimal?
  14. @Published var maxBolus: Decimal?
  15. @Published var bolusRecommended: Decimal?
  16. @Published var carbsRequired: Decimal?
  17. @Published var iob: Decimal?
  18. @Published var cob: Decimal?
  19. @Published var tempTargets: [TempTargetWatchPreset] = []
  20. @Published var bolusAfterCarbs = true
  21. @Published var isCarbsViewActive = false
  22. @Published var isTempTargetViewActive = false
  23. @Published var isBolusViewActive = false
  24. @Published var displayHR = false
  25. @Published var eventualBG = ""
  26. @Published var isConfirmationViewActive = false {
  27. didSet {
  28. confirmationTimeout = nil
  29. if isConfirmationViewActive {
  30. confirmationTimeout = Just(())
  31. .delay(for: 30, scheduler: DispatchQueue.main)
  32. .sink {
  33. WKInterfaceDevice.current().play(.retry)
  34. self.isConfirmationViewActive = false
  35. }
  36. }
  37. }
  38. }
  39. @Published var isConfirmationBolusViewActive = false
  40. @Published var confirmationSuccess: Bool?
  41. @Published var lastUpdate: Date = .distantPast
  42. @Published var timerDate = Date()
  43. @Published var pendingBolus: Double?
  44. private var lifetime = Set<AnyCancellable>()
  45. private var confirmationTimeout: AnyCancellable?
  46. let timer = Timer.publish(every: 10, on: .main, in: .common).autoconnect()
  47. init(session: WCSession = .default) {
  48. self.session = session
  49. super.init()
  50. session.delegate = self
  51. session.activate()
  52. }
  53. func addCarbs(_ carbs: Int) {
  54. confirmationSuccess = nil
  55. isConfirmationViewActive = true
  56. isCarbsViewActive = false
  57. session.sendMessage(["carbs": carbs], replyHandler: { reply in
  58. self.completionHandler(reply)
  59. if let ok = reply["confirmation"] as? Bool, ok, self.bolusAfterCarbs {
  60. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  61. self.isBolusViewActive = true
  62. }
  63. }
  64. }) { error in
  65. print(error.localizedDescription)
  66. DispatchQueue.main.async {
  67. self.confirmation(false)
  68. }
  69. }
  70. }
  71. func enactTempTarget(id: String) {
  72. confirmationSuccess = nil
  73. isConfirmationViewActive = true
  74. isTempTargetViewActive = false
  75. session.sendMessage(["tempTarget": id], replyHandler: completionHandler) { error in
  76. print(error.localizedDescription)
  77. DispatchQueue.main.async {
  78. self.confirmation(false)
  79. }
  80. }
  81. }
  82. func addBolus(amount: Double) {
  83. isBolusViewActive = false
  84. pendingBolus = amount
  85. isConfirmationBolusViewActive = true
  86. }
  87. func enactBolus() {
  88. isConfirmationBolusViewActive = false
  89. guard let amount = pendingBolus else { return }
  90. confirmationSuccess = nil
  91. isConfirmationViewActive = true
  92. session.sendMessage(["bolus": amount], replyHandler: completionHandler) { error in
  93. print(error.localizedDescription)
  94. DispatchQueue.main.async {
  95. self.confirmation(false)
  96. }
  97. }
  98. }
  99. func requestState() {
  100. guard session.activationState == .activated else {
  101. session.activate()
  102. return
  103. }
  104. session.sendMessage(["stateRequest": true], replyHandler: nil) { error in
  105. print("WatchStateModel error: " + error.localizedDescription)
  106. }
  107. }
  108. private func completionHandler(_ reply: [String: Any]) {
  109. if let ok = reply["confirmation"] as? Bool {
  110. DispatchQueue.main.async {
  111. self.confirmation(ok)
  112. }
  113. } else {
  114. DispatchQueue.main.async {
  115. self.confirmation(false)
  116. }
  117. }
  118. }
  119. private func confirmation(_ ok: Bool) {
  120. WKInterfaceDevice.current().play(ok ? .success : .failure)
  121. withAnimation {
  122. confirmationSuccess = ok
  123. }
  124. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  125. withAnimation {
  126. self.isConfirmationViewActive = false
  127. }
  128. }
  129. }
  130. private func processState(_ state: WatchState) {
  131. glucose = state.glucose ?? "?"
  132. trend = state.trend ?? "?"
  133. delta = state.delta ?? "?"
  134. glucoseDate = state.glucoseDate
  135. lastLoopDate = state.lastLoopDate
  136. bolusIncrement = state.bolusIncrement
  137. maxCOB = state.maxCOB
  138. maxBolus = state.maxBolus
  139. bolusRecommended = state.bolusRecommended
  140. carbsRequired = state.carbsRequired
  141. iob = state.iob
  142. cob = state.cob
  143. tempTargets = state.tempTargets
  144. bolusAfterCarbs = state.bolusAfterCarbs ?? true
  145. lastUpdate = Date()
  146. eventualBG = state.eventualBG ?? ""
  147. displayHR = state.displayHR ?? false
  148. }
  149. }
  150. extension WatchStateModel: WCSessionDelegate {
  151. func session(_: WCSession, activationDidCompleteWith state: WCSessionActivationState, error _: Error?) {
  152. print("WCSession activated: \(state == .activated)")
  153. requestState()
  154. }
  155. func session(_: WCSession, didReceiveMessage _: [String: Any]) {}
  156. func sessionReachabilityDidChange(_ session: WCSession) {
  157. print("WCSession Reachability: \(session.isReachable)")
  158. }
  159. func session(_: WCSession, didReceiveMessageData messageData: Data) {
  160. if let state = try? JSONDecoder().decode(WatchState.self, from: messageData) {
  161. DispatchQueue.main.async {
  162. self.processState(state)
  163. }
  164. }
  165. }
  166. }