From 3acfe1e2ca2444317923e6fd0834086718f6641b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Inge=20Berg?= Date: Thu, 10 Oct 2024 11:09:28 +0200 Subject: [PATCH 1/2] issue #40 : update glucose units when creating manager --- FreeAPS/Sources/APS/FetchGlucoseManager.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/FreeAPS/Sources/APS/FetchGlucoseManager.swift b/FreeAPS/Sources/APS/FetchGlucoseManager.swift index 333b4ef71..7b6f44c78 100644 --- a/FreeAPS/Sources/APS/FetchGlucoseManager.swift +++ b/FreeAPS/Sources/APS/FetchGlucoseManager.swift @@ -1,5 +1,6 @@ import Combine import Foundation +import HealthKit import LoopKit import LoopKitUI import SwiftDate @@ -98,6 +99,14 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable { settingsManager.settings.uploadGlucose = cgmM.shouldSyncToRemoteService } + private func updateManagerUnits(_ manager: CGMManagerUI?) { + let units = settingsManager.settings.units + let managerName = cgmManager.map { "\(type(of: $0))" } ?? "nil" + let loopkitUnits: HKUnit = units == .mgdL ? .milligramsPerDeciliter : .millimolesPerLiter + print("manager: \(managerName) is changing units to: \(loopkitUnits.description) ") + manager?.unitDidChange(to: loopkitUnits) + } + func updateGlucoseSource(cgmGlucoseSourceType: CGMType, cgmGlucosePluginId: String, newManager: CGMManagerUI?) { // if changed, remove all calibrations if self.cgmGlucoseSourceType != cgmGlucoseSourceType || self.cgmGlucosePluginId != cgmGlucosePluginId { @@ -120,6 +129,8 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable { removeCalibrations() } else if self.cgmGlucoseSourceType == .plugin, cgmManager == nil, let rawCGMManager = rawCGMManager { cgmManager = cgmManagerFromRawValue(rawCGMManager) + updateManagerUnits(cgmManager) + } else { saveConfigManager() } @@ -151,7 +162,6 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable { else { return nil } - return Manager.init(rawState: rawState) } From f797d6f2ff405a33ba4aa81810cb9252d4aaf21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Inge=20Berg?= Date: Thu, 10 Oct 2024 11:11:34 +0200 Subject: [PATCH 2/2] issue #40 : update manager glucose units when changing trio preferences --- .../PreferencesEditor/PreferencesEditorDataFlow.swift | 1 + .../PreferencesEditor/PreferencesEditorProvider.swift | 11 +++++++++++ .../PreferencesEditorStateModel.swift | 1 + 3 files changed, 13 insertions(+) diff --git a/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorDataFlow.swift b/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorDataFlow.swift index 1bbd54d6c..0915350f2 100644 --- a/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorDataFlow.swift +++ b/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorDataFlow.swift @@ -109,6 +109,7 @@ protocol PreferencesEditorProvider: Provider { var preferences: Preferences { get } func savePreferences(_ preferences: Preferences) func migrateUnits() + func updateManagerUnits() } protocol PreferencesSettable: AnyObject { diff --git a/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorProvider.swift b/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorProvider.swift index 7e8c63a04..f2a2765e1 100644 --- a/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorProvider.swift +++ b/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorProvider.swift @@ -1,8 +1,10 @@ import Foundation +import HealthKit extension PreferencesEditor { final class Provider: BaseProvider, PreferencesEditorProvider { @Injected() private var settingsManager: SettingsManager! + @Injected() var fetchGlucoseManager: FetchGlucoseManager! private let processQueue = DispatchQueue(label: "PreferencesEditorProvider.processQueue") var preferences: Preferences { @@ -17,6 +19,15 @@ extension PreferencesEditor { } } + func updateManagerUnits() { + var manager = fetchGlucoseManager.cgmManager + let managerName = manager.map { "\(type(of: $0))" } ?? "nil" + let units = settingsManager.settings.units + let loopkitUnits: HKUnit = units == .mgdL ? .milligramsPerDeciliter : .millimolesPerLiter + print("manager: \(managerName) is changing units to: \(loopkitUnits.description) ") + manager?.unitDidChange(to: loopkitUnits) + } + func migrateUnits() { migrateTargets() migrateISF() diff --git a/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift b/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift index d28869fb5..01175ffb7 100644 --- a/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift +++ b/FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift @@ -16,6 +16,7 @@ extension PreferencesEditor { subscribeSetting(\.units, on: $unitsIndex.map { $0 == 0 ? GlucoseUnits.mgdL : .mmolL }) { unitsIndex = $0 == .mgdL ? 0 : 1 } didSet: { [weak self] _ in + self?.provider.updateManagerUnits() self?.provider.migrateUnits() }