Skip to content

Commit

Permalink
Merge pull request #3767 from Navid200/Navid_2024_11_09b
Browse files Browse the repository at this point in the history
Persistent High Threshold range verification
  • Loading branch information
jamorham authored Nov 26, 2024
2 parents 9ad109f + 4cd5d82 commit d8e96f9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class Constants {
public static final int ZXING_CAM_REQ_CODE = 49374;
public static final int ZXING_FILE_REQ_CODE = 49375; // This is created by just incrementing the existing camera scan code from the zxing package
public static final int SENSORY_EXPIRY_NOTIFICATION_ID = 2003;
public static final int OUT_OF_RANGE_GLUCOSE_ENTRY_ID = 2004; // Preference setting out of range


// increments from this start number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.eveningoutpost.dexdrip.utilitymodels;

import static com.eveningoutpost.dexdrip.utils.Preferences.MAX_GLUCOSE_INPUT;
import static com.eveningoutpost.dexdrip.utils.Preferences.MIN_GLUCOSE_INPUT;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
Expand All @@ -20,6 +23,7 @@
import com.eveningoutpost.dexdrip.models.UserNotification;
import com.eveningoutpost.dexdrip.R;
import com.eveningoutpost.dexdrip.SnoozeActivity;
import com.eveningoutpost.dexdrip.utils.Preferences;

import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -60,6 +64,7 @@ public void performAll() {
IncompatibleApps.notifyAboutIncompatibleApps();
CompatibleApps.notifyAboutCompatibleApps();
legacySettingsMoveLanguageFromNoToNb();
prefSettingRangeVerification();

}

Expand Down Expand Up @@ -160,4 +165,11 @@ private static void legacySettingsMoveLanguageFromNoToNb() {
Pref.setString("forced_language", "nb");
}
}

// Correct preference setting values if the values are out of range.
// Include new preference settings here that represent glucose values.
private static void prefSettingRangeVerification() {
Preferences.applyPrefSettingRange("persistent_high_threshold", "170", MIN_GLUCOSE_INPUT, MAX_GLUCOSE_INPUT);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.eveningoutpost.dexdrip.utils;


import static com.eveningoutpost.dexdrip.EditAlertActivity.unitsConvert2Disp;
import static com.eveningoutpost.dexdrip.models.JoH.showNotification;
import static com.eveningoutpost.dexdrip.models.JoH.tolerantParseDouble;
import static com.eveningoutpost.dexdrip.utilitymodels.Constants.OUT_OF_RANGE_GLUCOSE_ENTRY_ID;
import static com.eveningoutpost.dexdrip.utils.DexCollectionType.getBestCollectorHardwareName;
import static com.eveningoutpost.dexdrip.xdrip.gs;

Expand Down Expand Up @@ -1026,6 +1029,33 @@ private static void bindPreferenceSummaryToUnitizedValueAndEnsureNumeric(Prefere
.getString(preference.getKey(), ""));
}

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
val notificationId = OUT_OF_RANGE_GLUCOSE_ENTRY_ID;
String mySettingString = Pref.getString(pref_key, def);
final boolean doMgdl = (Pref.getString("units", "mgdl").equals("mgdl"));
double mySettingMgdl = doMgdl ? tolerantParseDouble(mySettingString) : tolerantParseDouble(mySettingString) * Constants.MMOLL_TO_MGDL; // The preference value in mg/dL
if (mySettingMgdl > max) { // If the preference value is greater than max
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.
// 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
UserError.Log.d(TAG, "Setting " + pref_key + " to default converted to mmol/L");
Pref.setString(pref_key, JoH.qs(tolerantParseDouble(def) * Constants.MGDL_TO_MMOLL, 1)); // Set the preference to the default value converted to mmol/L
} else { // The preference has been set to a value greater than the max allowed. Let's fix it and notify.
// This will only happen if user has entered a preference setting value out of range before the listener range limit update has been merged.
mySettingString = doMgdl ? max + "" : JoH.qs(max * Constants.MGDL_TO_MMOLL, 1) + "";
Pref.setString(pref_key, mySettingString); // Set the preference to max
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
showNotification(pref_key, xdrip.gs(R.string.setting_pref_to_max), null, notificationId, null, false, false, null, null, null, true);
}
} else if (mySettingMgdl < min) { // If the preference value is less than min, correct it and notify.
// This will only happen if user has entered a preference setting value out of range before the listener range limit update has been merged.
mySettingString = doMgdl ? min + "" : JoH.qs(min * Constants.MGDL_TO_MMOLL, 1) + "";
Pref.setString(pref_key, mySettingString); // Set the preference to min
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
showNotification(pref_key, xdrip.gs(R.string.setting_pref_to_min), null, notificationId, null, false, false, null, null, null, true);

}
}

@RequiredArgsConstructor
public static class AllPrefsFragment extends PreferenceFragment {

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@
<string name="title_persistent_high_threshold">Threshold</string>
<string name="the_value_must_be_between_min_and_max">The value must be between %1$s and %2$s</string>
<string name="forecasted_low_alert">Forecasted Low Alert</string>
<string name="setting_pref_to_max">Set to max</string>
<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>
<string name="setting_pref_to_min">Set to min</string>
<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>
<string name="extrapolate_data_to_try_to_predict_lows">Extrapolate data to try to predict lows</string>
<string name="alarm_at_forecasted_low_mins">Alarm at Forecasted Low mins</string>
<string name="other_xdrip_plus_alerts">Other xDrip+ alerts</string>
Expand Down

0 comments on commit d8e96f9

Please sign in to comment.