Skip to content

Commit

Permalink
Fix for invalid profile preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Oct 31, 2016
1 parent 8a0112b commit b3a0ff8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
69 changes: 35 additions & 34 deletions app/src/main/java/com/eveningoutpost/dexdrip/Models/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public class Profile {
private static List<ProfileItem> profileItemList;

public static double getSensitivity(double when) {
final double sensitivity = findItemListElementForTime(when).sensitivity;
// Log.d(TAG,"sensitivity: "+sensitivity);
final double sensitivity = findItemListElementForTime(when).sensitivity;
// Log.d(TAG,"sensitivity: "+sensitivity);
return sensitivity;
// expressed in native units lowering effect of 1U
// expressed in native units lowering effect of 1U
}

public static void setSensitivityDefault(double value) {
Expand Down Expand Up @@ -82,46 +82,41 @@ static double getCarbRatio(double when) {
}


private static void populateProfile()
{
if (profileItemList==null)
{
private static void populateProfile() {
if (profileItemList == null) {
profileItemList = ProfileEditor.loadData(false);
Log.d(TAG,"Loaded profile data, blocks: "+profileItemList.size());
Log.d(TAG, "Loaded profile data, blocks: " + profileItemList.size());
}
}

public static void invalidateProfile()
{
public static void invalidateProfile() {
profileItemList = null;
}

private static ProfileItem findItemListElementForTime(double when)
{
private static ProfileItem findItemListElementForTime(double when) {
populateProfile();
// TODO does this want/need a hash table lookup cache?
if (profileItemList.size()==1) profileItemList.get(0); // always will be first/only element.
if (profileItemList.size() == 1)
profileItemList.get(0); // always will be first/only element.
// get time of day
final int min = ProfileItem.timeStampToMin(when);
// find element
for (ProfileItem item : profileItemList)
{
for (ProfileItem item : profileItemList) {
if (item.start_min < item.end_min) {
// regular
if ((item.start_min <= min) && (item.end_min >= min)) {
// Log.d(TAG, "Match on item " + item.getTimePeriod() + " " + profileItemList.indexOf(item));
// Log.d(TAG, "Match on item " + item.getTimePeriod() + " " + profileItemList.indexOf(item));
return item;
}
} else {
// item spans midnight
if ((min >= item.start_min) || (min <= item.end_min))
{
// Log.d(TAG, "midnight span Match on item " + item.getTimePeriod() + " " + profileItemList.indexOf(item));
if ((min >= item.start_min) || (min <= item.end_min)) {
// Log.d(TAG, "midnight span Match on item " + item.getTimePeriod() + " " + profileItemList.indexOf(item));
return item;
}
}
}
Log.wtf(TAG,"Null return from findItemListElementForTime");
Log.wtf(TAG, "Null return from findItemListElementForTime");
return null; // should be impossible
}

Expand All @@ -138,23 +133,21 @@ static double getLiverSensRatio(double when) {
return 2.0;
}

public static void validateTargetRange()
{
final double default_target_glucose = tolerantParseDouble(Home.getPreferencesStringWithDefault("plus_target_range",Double.toString(5.5 / scale_factor)));
if (default_target_glucose > tolerantParseDouble(Home.getPreferencesStringWithDefault("highValue",Double.toString(5.5 / scale_factor))))
{
Home.setPreferencesString("plus_target_range", JoH.qs(default_target_glucose * Constants.MGDL_TO_MMOLL,1));
UserError.Log.i(TAG,"Converted initial value of target glucose to mmol");
public static void validateTargetRange() {
final double default_target_glucose = tolerantParseDouble(Home.getPreferencesStringWithDefault("plus_target_range", Double.toString(5.5 / scale_factor)));
if (default_target_glucose > tolerantParseDouble(Home.getPreferencesStringWithDefault("highValue", Double.toString(5.5 / scale_factor)))) {
Home.setPreferencesString("plus_target_range", JoH.qs(default_target_glucose * Constants.MGDL_TO_MMOLL, 1));
UserError.Log.i(TAG, "Converted initial value of target glucose to mmol");
}
}

static double getTargetRangeInMmol(double when) {
// return tolerantParseDouble(Home.getPreferencesStringWithDefault("plus_target_range",Double.toString(5.5 / scale_factor)));
// return tolerantParseDouble(Home.getPreferencesStringWithDefault("plus_target_range",Double.toString(5.5 / scale_factor)));
return getTargetRangeInUnits(when) / scale_factor;
}

public static double getTargetRangeInUnits(double when) {
return tolerantParseDouble(Home.getPreferencesStringWithDefault("plus_target_range",Double.toString(5.5 / scale_factor)));
return tolerantParseDouble(Home.getPreferencesStringWithDefault("plus_target_range", Double.toString(5.5 / scale_factor)));
//return getTargetRangeInMmol(when) * scale_factor; // TODO deal with rounding errors here? (3 decimal places?)
}

Expand Down Expand Up @@ -203,24 +196,32 @@ public static void reloadPreferences(SharedPreferences prefs) {
try {
Profile.setSensitivityDefault(tolerantParseDouble(prefs.getString("profile_insulin_sensitivity_default", "0")));
} catch (Exception e) {
Home.toaststatic("Invalid insulin sensitivity");
if (JoH.ratelimit("invalid-insulin-profile", 60)) {
Home.toaststatic("Invalid insulin sensitivity");
}
}
try {
Profile.setDefaultCarbRatio(tolerantParseDouble(prefs.getString("profile_carb_ratio_default", "0")));
} catch (Exception e) {
Home.toaststatic("Invalid default carb ratio!");
if (JoH.ratelimit("invalid-insulin-profile", 60)) {
Home.toaststatic("Invalid default carb ratio!");
}
}
try {
Profile.setCarbAbsorptionDefault(tolerantParseDouble(prefs.getString("profile_carb_absorption_default", "0")));
} catch (Exception e) {
Home.toaststatic("Invalid carb absorption rate");
if (JoH.ratelimit("invalid-insulin-profile", 60)) {
Home.toaststatic("Invalid carb absorption rate");
}
}
try {
Profile.setInsulinActionTimeDefault(tolerantParseDouble(prefs.getString("xplus_insulin_dia", "3.0")));
} catch (Exception e) {
Home.toaststatic("Invalid insulin action time");
if (JoH.ratelimit("invalid-insulin-profile", 60)) {
Home.toaststatic("Invalid insulin action time");
}
}
profileItemList=null;
profileItemList = null;
populateProfile();
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/xml/xdrip_plus_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -369,22 +369,26 @@
android:defaultValue="100"
android:key="plus_target_range"
android:summary=""
android:inputType="numberDecimal"
android:title="Target Glucose Default" />
<EditTextPreference
android:defaultValue="3.0"
android:key="xplus_insulin_dia"
android:summary=""
android:inputType="numberDecimal"
android:title="@string/insulin_duration_hours" />

<EditTextPreference
android:defaultValue="2"
android:key="xplus_liver_sensitivity"
android:summary=""
android:inputType="numberDecimal"
android:title="@string/default_liver_sensitivity_ratio" />
<EditTextPreference
android:defaultValue="0.8"
android:key="xplus_liver_maximpact"
android:summary=""
android:inputType="numberDecimal"
android:title="Default Liver Maximum Impact (0-1)" />
</PreferenceCategory>
</PreferenceScreen>
Expand Down

0 comments on commit b3a0ff8

Please sign in to comment.