WatchStateModel.swift 3.4 KB

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