WatchStateModel.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. private func completionHandler(_ reply: [String: Any]) {
  55. if let ok = reply["confirmation"] as? Bool {
  56. DispatchQueue.main.async {
  57. self.confirmation(ok)
  58. }
  59. } else {
  60. DispatchQueue.main.async {
  61. self.confirmation(false)
  62. }
  63. }
  64. }
  65. private func confirmation(_ ok: Bool) {
  66. WKInterfaceDevice.current().play(ok ? .success : .failure)
  67. withAnimation {
  68. confirmationSuccess = ok
  69. }
  70. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  71. withAnimation {
  72. self.isConfirmationViewActive = false
  73. }
  74. }
  75. }
  76. private func processState(_ state: WatchState) {
  77. glucose = state.glucose ?? "?"
  78. trend = state.trend ?? "?"
  79. delta = state.delta ?? "?"
  80. glucoseDate = state.glucoseDate
  81. lastLoopDate = state.lastLoopDate
  82. bolusIncrement = state.bolusIncrement
  83. maxCOB = state.maxCOB
  84. bolusRecommended = state.bolusRecommended
  85. carbsRequired = state.carbsRequired
  86. iob = state.iob
  87. cob = state.cob
  88. tempTargets = state.tempTargets
  89. }
  90. }
  91. extension WatchStateModel: WCSessionDelegate {
  92. func session(_: WCSession, activationDidCompleteWith state: WCSessionActivationState, error _: Error?) {
  93. print("WCSession activated: \(state == .activated)")
  94. session.sendMessage(["stateRequest": true], replyHandler: nil) { error in
  95. print("WatchStateModel error: " + error.localizedDescription)
  96. }
  97. }
  98. func session(_: WCSession, didReceiveMessage _: [String: Any]) {}
  99. func sessionReachabilityDidChange(_ session: WCSession) {
  100. print("WCSession Reachability: \(session.isReachable)")
  101. }
  102. func session(_: WCSession, didReceiveMessageData messageData: Data) {
  103. if let state = try? JSONDecoder().decode(WatchState.self, from: messageData) {
  104. DispatchQueue.main.async {
  105. // WKInterfaceDevice.current().play(.click)
  106. self.processState(state)
  107. }
  108. }
  109. }
  110. }