WatchStateModel.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 isConfirmationViewActive = false
  25. @Published var confirmationSuccess: Bool?
  26. private var lifetime = Set<AnyCancellable>()
  27. init(session: WCSession = .default) {
  28. self.session = session
  29. super.init()
  30. session.delegate = self
  31. session.activate()
  32. }
  33. func addCarbs(_ carbs: Int) {
  34. confirmationSuccess = nil
  35. isConfirmationViewActive = true
  36. isCarbsViewActive = false
  37. session.sendMessage(["carbs": carbs], replyHandler: { reply in
  38. self.completionHandler(reply)
  39. if let ok = reply["confirmation"] as? Bool, ok, self.bolusAfterCarbs {
  40. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  41. self.isBolusViewActive = true
  42. }
  43. }
  44. }) { error in
  45. print(error.localizedDescription)
  46. DispatchQueue.main.async {
  47. self.confirmation(false)
  48. }
  49. }
  50. }
  51. func enactTempTarget(id: String) {
  52. confirmationSuccess = nil
  53. isConfirmationViewActive = true
  54. isTempTargetViewActive = false
  55. session.sendMessage(["tempTarget": id], replyHandler: completionHandler) { error in
  56. print(error.localizedDescription)
  57. DispatchQueue.main.async {
  58. self.confirmation(false)
  59. }
  60. }
  61. }
  62. func enactBolus(amount: Double) {
  63. confirmationSuccess = nil
  64. isConfirmationViewActive = true
  65. isBolusViewActive = false
  66. session.sendMessage(["bolus": amount], replyHandler: completionHandler) { error in
  67. print(error.localizedDescription)
  68. DispatchQueue.main.async {
  69. self.confirmation(false)
  70. }
  71. }
  72. }
  73. func requestState() {
  74. session.sendMessage(["stateRequest": true], replyHandler: nil) { error in
  75. print("WatchStateModel error: " + error.localizedDescription)
  76. }
  77. }
  78. private func completionHandler(_ reply: [String: Any]) {
  79. if let ok = reply["confirmation"] as? Bool {
  80. DispatchQueue.main.async {
  81. self.confirmation(ok)
  82. }
  83. } else {
  84. DispatchQueue.main.async {
  85. self.confirmation(false)
  86. }
  87. }
  88. }
  89. private func confirmation(_ ok: Bool) {
  90. WKInterfaceDevice.current().play(ok ? .success : .failure)
  91. withAnimation {
  92. confirmationSuccess = ok
  93. }
  94. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  95. withAnimation {
  96. self.isConfirmationViewActive = false
  97. }
  98. }
  99. }
  100. private func processState(_ state: WatchState) {
  101. glucose = state.glucose ?? "?"
  102. trend = state.trend ?? "?"
  103. delta = state.delta ?? "?"
  104. glucoseDate = state.glucoseDate
  105. lastLoopDate = state.lastLoopDate
  106. bolusIncrement = state.bolusIncrement
  107. maxCOB = state.maxCOB
  108. maxBolus = state.maxBolus
  109. bolusRecommended = state.bolusRecommended
  110. carbsRequired = state.carbsRequired
  111. iob = state.iob
  112. cob = state.cob
  113. tempTargets = state.tempTargets
  114. bolusAfterCarbs = state.bolusAfterCarbs ?? true
  115. }
  116. }
  117. extension WatchStateModel: WCSessionDelegate {
  118. func session(_: WCSession, activationDidCompleteWith state: WCSessionActivationState, error _: Error?) {
  119. print("WCSession activated: \(state == .activated)")
  120. requestState()
  121. }
  122. func session(_: WCSession, didReceiveMessage _: [String: Any]) {}
  123. func sessionReachabilityDidChange(_ session: WCSession) {
  124. print("WCSession Reachability: \(session.isReachable)")
  125. }
  126. func session(_: WCSession, didReceiveMessageData messageData: Data) {
  127. if let state = try? JSONDecoder().decode(WatchState.self, from: messageData) {
  128. DispatchQueue.main.async {
  129. // WKInterfaceDevice.current().play(.click)
  130. self.processState(state)
  131. }
  132. }
  133. }
  134. }