diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/Home.java b/app/src/main/java/com/eveningoutpost/dexdrip/Home.java index 85193b0887..ceca112c04 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/Home.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/Home.java @@ -6,6 +6,7 @@ import static com.eveningoutpost.dexdrip.models.JoH.quietratelimit; import static com.eveningoutpost.dexdrip.models.JoH.tsl; import static com.eveningoutpost.dexdrip.services.Ob1G5CollectionService.getTransmitterID; +import static com.eveningoutpost.dexdrip.ui.FlipUnits.triggerUnitsChange; import static com.eveningoutpost.dexdrip.utilitymodels.ColorCache.X; import static com.eveningoutpost.dexdrip.utilitymodels.ColorCache.getCol; import static com.eveningoutpost.dexdrip.utilitymodels.Constants.DAY_IN_MS; @@ -385,6 +386,7 @@ protected void onCreate(Bundle savedInstanceState) { setVolumeControlStream(AudioManager.STREAM_MUSIC); checkedeula = checkEula(); + triggerUnitsChange(); // Correct defaults that are in mg/dL if we are using mmol/L binding = ActivityHomeBinding.inflate(getLayoutInflater()); binding.setVs(homeShelf); diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/ui/FlipUnits.java b/app/src/main/java/com/eveningoutpost/dexdrip/ui/FlipUnits.java new file mode 100644 index 0000000000..94e05b76e2 --- /dev/null +++ b/app/src/main/java/com/eveningoutpost/dexdrip/ui/FlipUnits.java @@ -0,0 +1,25 @@ +package com.eveningoutpost.dexdrip.ui; + +import static com.eveningoutpost.dexdrip.utils.Preferences.handleUnitsChange; + +import com.eveningoutpost.dexdrip.Home; +import com.eveningoutpost.dexdrip.utilitymodels.Pref; + +/** + * Created by Navid200 on 25/10/2024. + * + * handleUnitsChange runs only when units are changed. But, even if we don't change units, we may need to trigger that method. + * Triggers handleUnitsChange to correct values that are in mg/dL even though the selected unit is mmol/L. + */ +public class FlipUnits { + + public static void triggerUnitsChange() { + + final boolean domgdl = Pref.getString("units", "mgdl").equals("mgdl"); // Identify which unit is chosen + if (!domgdl) { // Only if the selected unit is mmol/L + handleUnitsChange(null, "mmol", null); + Home.staticRefreshBGCharts(); + } + } +} + diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java b/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java index e892922fc1..3e9b222c32 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java @@ -1,5 +1,6 @@ package com.eveningoutpost.dexdrip.utils; +import static com.eveningoutpost.dexdrip.EditAlertActivity.unitsConvert2Disp; import static com.eveningoutpost.dexdrip.utils.DexCollectionType.getBestCollectorHardwareName; import static com.eveningoutpost.dexdrip.xdrip.gs; @@ -755,6 +756,11 @@ public boolean onPreferenceChange(Preference preference, Object value) { String stringValue = value.toString(); if (isNumeric(stringValue)) { final boolean domgdl = Pref.getString("units", "mgdl").equals("mgdl"); // Identify which unit is chosen + double submissionMgdl = domgdl ? Double.parseDouble(stringValue) : Double.parseDouble(stringValue) * Constants.MMOLL_TO_MGDL; + if (submissionMgdl > 400 || submissionMgdl < 40) { + JoH.static_toast_long("The value must be between " + unitsConvert2Disp(domgdl, 40) + " and " + unitsConvert2Disp(domgdl, 400)); + return false; + } preference.setSummary(stringValue + " " + (domgdl ? "mg/dl" : "mmol/l")); // Set the summary to show the value followed by the chosen unit return true; } @@ -1009,7 +1015,11 @@ private static void bindPreferenceSummaryToValueAndEnsureNumeric(Preference pref .getString(preference.getKey(), "")); } - private static void bindPreferenceSummaryToUnitizedValueAndEnsureNumeric(Preference preference) { // Use this to show the value as well as the corresponding glucose unit as the summary + private static void bindPreferenceSummaryToUnitizedValueAndEnsureNumeric(Preference preference) { // Use this to: + // 1- show the value as summary; + // 2- amend the value in summary with the corresponding glucose unit; + // 3- reject inputs outside the 40-400 mg/dL range. + preference.setOnPreferenceChangeListener(sBindNumericUnitizedPreferenceSummaryToValueListener); sBindNumericUnitizedPreferenceSummaryToValueListener.onPreferenceChange(preference, PreferenceManager