WatchStateModel.swift 6.6 KB

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