|
1 | 1 | package com.eveningoutpost.dexdrip.utils; |
2 | 2 |
|
| 3 | + |
3 | 4 | import static com.eveningoutpost.dexdrip.EditAlertActivity.unitsConvert2Disp; |
| 5 | +import static com.eveningoutpost.dexdrip.models.JoH.showNotification; |
4 | 6 | import static com.eveningoutpost.dexdrip.models.JoH.tolerantParseDouble; |
| 7 | +import static com.eveningoutpost.dexdrip.utilitymodels.Constants.OUT_OF_RANGE_GLUCOSE_ENTRY_ID; |
5 | 8 | import static com.eveningoutpost.dexdrip.utils.DexCollectionType.getBestCollectorHardwareName; |
6 | 9 | import static com.eveningoutpost.dexdrip.xdrip.gs; |
7 | 10 |
|
@@ -1026,6 +1029,33 @@ private static void bindPreferenceSummaryToUnitizedValueAndEnsureNumeric(Prefere |
1026 | 1029 | .getString(preference.getKey(), "")); |
1027 | 1030 | } |
1028 | 1031 |
|
| 1032 | + public static void applyPrefSettingRange(String pref_key, String def, Double min, Double max) { // Correct a preference glucose setting if the value is out of range |
| 1033 | + val notificationId = OUT_OF_RANGE_GLUCOSE_ENTRY_ID; |
| 1034 | + String mySettingString = Pref.getString(pref_key, def); |
| 1035 | + final boolean doMgdl = (Pref.getString("units", "mgdl").equals("mgdl")); |
| 1036 | + double mySettingMgdl = doMgdl ? tolerantParseDouble(mySettingString) : tolerantParseDouble(mySettingString) * Constants.MMOLL_TO_MGDL; // The preference value in mg/dL |
| 1037 | + if (mySettingMgdl > max) { // If the preference value is greater than max |
| 1038 | + if (!doMgdl && mySettingString.equals(def)) { // If the setting value in mmol/L is the same as the default, which is in mg/dL, we correct the value next. |
| 1039 | + // This will only happen if user has chosen mmol/L and updates to a version that has a new preference setting with default in mg/dL |
| 1040 | + UserError.Log.d(TAG, "Setting " + pref_key + " to default converted to mmol/L"); |
| 1041 | + Pref.setString(pref_key, JoH.qs(tolerantParseDouble(def) * Constants.MGDL_TO_MMOLL, 1)); // Set the preference to the default value converted to mmol/L |
| 1042 | + } else { // The preference has been set to a value greater than the max allowed. Let's fix it and notify. |
| 1043 | + // This will only happen if user has entered a preference setting value out of range before the listener range limit update has been merged. |
| 1044 | + mySettingString = doMgdl ? max + "" : JoH.qs(max * Constants.MGDL_TO_MMOLL, 1) + ""; |
| 1045 | + Pref.setString(pref_key, mySettingString); // Set the preference to max |
| 1046 | + UserError.Log.uel(TAG, xdrip.gs(R.string.pref_was_greater_than_max, pref_key)); // Inform the user that xDrip is changing the setting value |
| 1047 | + showNotification(pref_key, xdrip.gs(R.string.setting_pref_to_max), null, notificationId, null, false, false, null, null, null, true); |
| 1048 | + } |
| 1049 | + } else if (mySettingMgdl < min) { // If the preference value is less than min, correct it and notify. |
| 1050 | + // This will only happen if user has entered a preference setting value out of range before the listener range limit update has been merged. |
| 1051 | + mySettingString = doMgdl ? min + "" : JoH.qs(min * Constants.MGDL_TO_MMOLL, 1) + ""; |
| 1052 | + Pref.setString(pref_key, mySettingString); // Set the preference to min |
| 1053 | + UserError.Log.uel(TAG, xdrip.gs(R.string.pref_was_less_than_min, pref_key)); // Inform the user that xDrip is changing the setting value |
| 1054 | + showNotification(pref_key, xdrip.gs(R.string.setting_pref_to_min), null, notificationId, null, false, false, null, null, null, true); |
| 1055 | + |
| 1056 | + } |
| 1057 | + } |
| 1058 | + |
1029 | 1059 | @RequiredArgsConstructor |
1030 | 1060 | public static class AllPrefsFragment extends PreferenceFragment { |
1031 | 1061 |
|
|
0 commit comments