|
|
@@ -52,6 +52,8 @@ final class BaseGarminManager: NSObject, GarminManager, Injectable {
|
|
|
|
|
|
/// Stores, retrieves, and updates insulin dose determinations in CoreData.
|
|
|
@Injected() private var determinationStorage: DeterminationStorage!
|
|
|
+
|
|
|
+ @Injected() private var iobService: IOBService!
|
|
|
|
|
|
/// Persists the user's device list between app launches.
|
|
|
@Persisted(key: "BaseGarminManager.persistedDevices") private var persistedDevices: [GarminDevice] = []
|
|
|
@@ -147,6 +149,25 @@ final class BaseGarminManager: NSObject, GarminManager, Injectable {
|
|
|
}
|
|
|
}
|
|
|
.store(in: &subscriptions)
|
|
|
+
|
|
|
+ iobService.iobPublisher
|
|
|
+ .receive(on: DispatchQueue.global(qos: .background))
|
|
|
+ .sink { [weak self] _ in
|
|
|
+ guard let self = self else { return }
|
|
|
+ Task {
|
|
|
+ do {
|
|
|
+ let watchState = try await self.setupGarminWatchState()
|
|
|
+ let watchStateData = try JSONEncoder().encode(watchState)
|
|
|
+ self.sendWatchStateData(watchStateData)
|
|
|
+ } catch {
|
|
|
+ debug(
|
|
|
+ .watchManager,
|
|
|
+ "\(DebuggingIdentifiers.failed) Error updating watch state: \(error)"
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ .store(in: &subscriptions)
|
|
|
|
|
|
registerHandlers()
|
|
|
}
|
|
|
@@ -239,15 +260,15 @@ final class BaseGarminManager: NSObject, GarminManager, Injectable {
|
|
|
var watchState = GarminWatchState()
|
|
|
|
|
|
/// Pull `glucose`, `trendRaw`, `delta`, `lastLoopDateInterval`, `iob`, `cob`, `isf`, and `eventualBGRaw` from the latest determination.
|
|
|
+ let iobValue = self.iobService.currentIOB ?? 0
|
|
|
+ watchState.iob = self.iobFormatterWithOneFractionDigit(iobValue)
|
|
|
+
|
|
|
if let latestDetermination = determinationObjects.first {
|
|
|
watchState.lastLoopDateInterval = latestDetermination.timestamp.map {
|
|
|
guard $0.timeIntervalSince1970 > 0 else { return 0 }
|
|
|
return UInt64($0.timeIntervalSince1970)
|
|
|
}
|
|
|
|
|
|
- let iobValue = latestDetermination.iob ?? 0
|
|
|
- watchState.iob = self.iobFormatterWithOneFractionDigit(iobValue as Decimal)
|
|
|
-
|
|
|
let cobNumber = NSNumber(value: latestDetermination.cob)
|
|
|
watchState.cob = Formatter.integerFormatter.string(from: cobNumber)
|
|
|
|