Skip to content

Commit d8e96f9

Browse files
authored
Merge pull request #3767 from Navid200/Navid_2024_11_09b
Persistent High Threshold range verification
2 parents 9ad109f + 4cd5d82 commit d8e96f9

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class Constants {
6161
public static final int ZXING_CAM_REQ_CODE = 49374;
6262
public static final int ZXING_FILE_REQ_CODE = 49375; // This is created by just incrementing the existing camera scan code from the zxing package
6363
public static final int SENSORY_EXPIRY_NOTIFICATION_ID = 2003;
64+
public static final int OUT_OF_RANGE_GLUCOSE_ENTRY_ID = 2004; // Preference setting out of range
6465

6566

6667
// increments from this start number

app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/IdempotentMigrations.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.eveningoutpost.dexdrip.utilitymodels;
22

3+
import static com.eveningoutpost.dexdrip.utils.Preferences.MAX_GLUCOSE_INPUT;
4+
import static com.eveningoutpost.dexdrip.utils.Preferences.MIN_GLUCOSE_INPUT;
5+
36
import android.content.Context;
47
import android.content.SharedPreferences;
58
import android.net.Uri;
@@ -20,6 +23,7 @@
2023
import com.eveningoutpost.dexdrip.models.UserNotification;
2124
import com.eveningoutpost.dexdrip.R;
2225
import com.eveningoutpost.dexdrip.SnoozeActivity;
26+
import com.eveningoutpost.dexdrip.utils.Preferences;
2327

2428
import java.util.ArrayList;
2529
import java.util.Iterator;
@@ -60,6 +64,7 @@ public void performAll() {
6064
IncompatibleApps.notifyAboutIncompatibleApps();
6165
CompatibleApps.notifyAboutCompatibleApps();
6266
legacySettingsMoveLanguageFromNoToNb();
67+
prefSettingRangeVerification();
6368

6469
}
6570

@@ -160,4 +165,11 @@ private static void legacySettingsMoveLanguageFromNoToNb() {
160165
Pref.setString("forced_language", "nb");
161166
}
162167
}
168+
169+
// Correct preference setting values if the values are out of range.
170+
// Include new preference settings here that represent glucose values.
171+
private static void prefSettingRangeVerification() {
172+
Preferences.applyPrefSettingRange("persistent_high_threshold", "170", MIN_GLUCOSE_INPUT, MAX_GLUCOSE_INPUT);
173+
}
174+
163175
}

app/src/main/java/com/eveningoutpost/dexdrip/utils/Preferences.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.eveningoutpost.dexdrip.utils;
22

3+
34
import static com.eveningoutpost.dexdrip.EditAlertActivity.unitsConvert2Disp;
5+
import static com.eveningoutpost.dexdrip.models.JoH.showNotification;
46
import static com.eveningoutpost.dexdrip.models.JoH.tolerantParseDouble;
7+
import static com.eveningoutpost.dexdrip.utilitymodels.Constants.OUT_OF_RANGE_GLUCOSE_ENTRY_ID;
58
import static com.eveningoutpost.dexdrip.utils.DexCollectionType.getBestCollectorHardwareName;
69
import static com.eveningoutpost.dexdrip.xdrip.gs;
710

@@ -1026,6 +1029,33 @@ private static void bindPreferenceSummaryToUnitizedValueAndEnsureNumeric(Prefere
10261029
.getString(preference.getKey(), ""));
10271030
}
10281031

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+
10291059
@RequiredArgsConstructor
10301060
public static class AllPrefsFragment extends PreferenceFragment {
10311061

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@
245245
<string name="title_persistent_high_threshold">Threshold</string>
246246
<string name="the_value_must_be_between_min_and_max">The value must be between %1$s and %2$s</string>
247247
<string name="forecasted_low_alert">Forecasted Low Alert</string>
248+
<string name="setting_pref_to_max">Set to max</string>
249+
<string name="pref_was_greater_than_max">%1$s was set to a value greater than maximum allowed. It has been changed to maximum.</string>
250+
<string name="setting_pref_to_min">Set to min</string>
251+
<string name="pref_was_less_than_min">%1$s was set to a value less than minimum allowed. It has been changed to minimum.</string>
248252
<string name="extrapolate_data_to_try_to_predict_lows">Extrapolate data to try to predict lows</string>
249253
<string name="alarm_at_forecasted_low_mins">Alarm at Forecasted Low mins</string>
250254
<string name="other_xdrip_plus_alerts">Other xDrip+ alerts</string>

0 commit comments

Comments
 (0)