Skip to content

Commit

Permalink
Merge pull request #429 from dabear/dev+glucose-source-unit-change
Browse files Browse the repository at this point in the history
Glucose source unit change update cgmmanagers
  • Loading branch information
Sjoerd-Bo3 authored Oct 18, 2024
2 parents eccd566 + f797d6f commit 51f1e1b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion FreeAPS/Sources/APS/FetchGlucoseManager.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Combine
import Foundation
import HealthKit
import LoopKit
import LoopKitUI
import SwiftDate
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
}
Expand Down Expand Up @@ -151,7 +162,6 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
else {
return nil
}

return Manager.init(rawState: rawState)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ protocol PreferencesEditorProvider: Provider {
var preferences: Preferences { get }
func savePreferences(_ preferences: Preferences)
func migrateUnits()
func updateManagerUnits()
}

protocol PreferencesSettable: AnyObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit 51f1e1b

Please sign in to comment.