Skip to content

Commit

Permalink
Persistent High Threshold range verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Navid200 committed Nov 10, 2024
1 parent eb920d2 commit bc2b918
Show file tree
Hide file tree
Showing 4 changed files with 49 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,5 +1,8 @@
package com.eveningoutpost.dexdrip.utils;

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 @@ -192,6 +195,8 @@ public class Preferences extends BasePreferenceActivity implements SearchPrefere
private void refreshFragments() {
refreshFragments(null);
}
public static final double MIN_GLUCOSE_INPUT = 40; // The smallest acceptable input glucose value in mg/dL
public static final double MAX_GLUCOSE_INPUT = 400; // The largest acceptable input glucose value in mg/dL

private void refreshFragments(final String jumpTo) {
this.preferenceFragment = new AllPrefsFragment(jumpTo);
Expand Down Expand Up @@ -1017,6 +1022,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 @@ -244,6 +244,10 @@
<string name="persistent_high_alert_enable">Enable</string>
<string name="title_persistent_high_threshold">Threshold</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 bc2b918

Please sign in to comment.