Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Nov 26, 2024
2 parents ff1dbd7 + d8e96f9 commit a2bbca9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 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,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 @@ -192,6 +197,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 @@ -749,16 +756,21 @@ public boolean onPreferenceChange(Preference preference, Object value) {
}
};

private static Preference.OnPreferenceChangeListener sBindNumericUnitizedPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { // This listener adds glucose unit in addition to the value to the summary
private static Preference.OnPreferenceChangeListener sBindNumericUnitizedPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { // This listener adds glucose unit in addition to the value to the summary and rejects out-of-range inputs
@Override
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 ? tolerantParseDouble(stringValue) : tolerantParseDouble(stringValue) * Constants.MMOLL_TO_MGDL;
if (submissionMgdl > MAX_GLUCOSE_INPUT || submissionMgdl < MIN_GLUCOSE_INPUT) {
JoH.static_toast_long(xdrip.gs(R.string.the_value_must_be_between_min_and_max, unitsConvert2Disp(domgdl, MIN_GLUCOSE_INPUT), unitsConvert2Disp(domgdl, MAX_GLUCOSE_INPUT)));
return false; // Reject input if out of range
}
preference.setSummary(stringValue + " " + (domgdl ? "mg/dl" : "mmol/l")); // Set the summary to show the value followed by the chosen unit
return true;
return true; // Accept input as it is numeric and in range
}
return false;
return false; // Reject input if not numeric
}
};
private static Preference.OnPreferenceChangeListener sBindPreferenceTitleAppendToValueListenerUpdateChannel = new Preference.OnPreferenceChangeListener() {
Expand Down Expand Up @@ -1009,14 +1021,41 @@ 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 show the value as well as the corresponding glucose unit as the summary, and reject out-of-range inputs
preference.setOnPreferenceChangeListener(sBindNumericUnitizedPreferenceSummaryToValueListener);
sBindNumericUnitizedPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.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
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@
<string name="persistent_high_alert">Persistent High Alert</string>
<string name="persistent_high_alert_enable">Enable</string>
<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 a2bbca9

Please sign in to comment.