From 35c6ee38e5c8d55afd01be20e1999de939da4194 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 4 Nov 2021 13:30:31 +0100 Subject: [PATCH] allow profilestore units as a fallback --- .../androidaps/extensions/ProfileSwitchExtension.kt | 4 ++-- .../info/nightscout/androidaps/interfaces/ProfileStore.kt | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/info/nightscout/androidaps/extensions/ProfileSwitchExtension.kt b/core/src/main/java/info/nightscout/androidaps/extensions/ProfileSwitchExtension.kt index fcb480a87c..876a544300 100644 --- a/core/src/main/java/info/nightscout/androidaps/extensions/ProfileSwitchExtension.kt +++ b/core/src/main/java/info/nightscout/androidaps/extensions/ProfileSwitchExtension.kt @@ -97,8 +97,8 @@ fun profileSwitchFromJson(jsonObject: JSONObject, dateUtil: DateUtil, activePlug */ fun pureProfileFromJson(jsonObject: JSONObject, dateUtil: DateUtil, defaultUnits: String? = null): PureProfile? { try { - JsonHelper.safeGetStringAllowNull(jsonObject, "units", defaultUnits) ?: return null - val units = GlucoseUnit.fromText(JsonHelper.safeGetString(jsonObject, "units", Constants.MGDL)) + val txtUnits = JsonHelper.safeGetStringAllowNull(jsonObject, "units", defaultUnits) ?: return null + val units = GlucoseUnit.fromText(txtUnits) val dia = JsonHelper.safeGetDoubleAllowNull(jsonObject, "dia") ?: return null val timezone = TimeZone.getTimeZone(JsonHelper.safeGetString(jsonObject, "timezone", "UTC")) diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/ProfileStore.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/ProfileStore.kt index bc821120d6..c3a9e7e8df 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/ProfileStore.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/ProfileStore.kt @@ -22,6 +22,8 @@ class ProfileStore(val injector: HasAndroidInjector, val data: JSONObject, val d private val cachedObjects = ArrayMap() + private fun storeUnits() : String? = JsonHelper.safeGetStringAllowNull(data, "units", null) + private fun getStore(): JSONObject? { try { if (data.has("store")) return data.getJSONObject("store") @@ -61,13 +63,13 @@ class ProfileStore(val injector: HasAndroidInjector, val data: JSONObject, val d fun getSpecificProfile(profileName: String): PureProfile? { var profile: PureProfile? = null - val defaultUnits = JsonHelper.safeGetStringAllowNull(data, "units", null) + val units = JsonHelper.safeGetStringAllowNull(data, "units", storeUnits()) getStore()?.let { store -> if (store.has(profileName)) { profile = cachedObjects[profileName] if (profile == null) { JsonHelper.safeGetJSONObject(store, profileName, null)?.let { profileObject -> - profile = pureProfileFromJson(profileObject, dateUtil, defaultUnits) + profile = pureProfileFromJson(profileObject, dateUtil, units) profile?.let { cachedObjects[profileName] = profile } } }