WatchStateModel.swift 4.0 KB

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