handle default TT valuses in current units

This commit is contained in:
Milos Kozak 2019-11-27 23:26:29 +01:00
parent 0f631b1d47
commit 3ee9836e66
7 changed files with 108 additions and 77 deletions

View file

@ -10,9 +10,12 @@ import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup; import android.preference.PreferenceGroup;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import java.util.Arrays;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventRebuildTabs; import info.nightscout.androidaps.events.EventRebuildTabs;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
@ -46,6 +49,7 @@ import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAverage
import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin; import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin;
import info.nightscout.androidaps.utils.OKDialog; import info.nightscout.androidaps.utils.OKDialog;
import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.SafeParse;
public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
MyPreferenceFragment myPreferenceFragment; MyPreferenceFragment myPreferenceFragment;
@ -65,14 +69,18 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
RxBus.INSTANCE.send(new EventPreferenceChange(key)); RxBus.INSTANCE.send(new EventPreferenceChange(key));
if (key.equals("language")) { if (key.equals(MainApp.gs(R.string.key_language))) {
RxBus.INSTANCE.send(new EventRebuildTabs(true)); RxBus.INSTANCE.send(new EventRebuildTabs(true));
//recreate() does not update language so better close settings //recreate() does not update language so better close settings
finish(); finish();
} }
if (key.equals("short_tabtitles")) { if (key.equals(MainApp.gs(R.string.key_short_tabtitles))) {
RxBus.INSTANCE.send(new EventRebuildTabs()); RxBus.INSTANCE.send(new EventRebuildTabs());
} }
if (key.equals(MainApp.gs(R.string.key_units))) {
recreate();
return;
}
if (key.equals(MainApp.gs(R.string.key_openapsama_useautosens)) && SP.getBoolean(R.string.key_openapsama_useautosens, false)) { if (key.equals(MainApp.gs(R.string.key_openapsama_useautosens)) && SP.getBoolean(R.string.key_openapsama_useautosens, false)) {
OKDialog.show(this, MainApp.gs(R.string.configbuilder_sensitivity), MainApp.gs(R.string.sensitivity_warning), null); OKDialog.show(this, MainApp.gs(R.string.configbuilder_sensitivity), MainApp.gs(R.string.sensitivity_warning), null);
} }
@ -96,6 +104,14 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
plugin.updatePreferenceSummary(pref); plugin.updatePreferenceSummary(pref);
} }
} }
// convert preferences values to current units
String[] unitDependent = new String[]{MainApp.gs(R.string.key_hypo_target), MainApp.gs(R.string.key_activity_target), MainApp.gs(R.string.key_eatingsoon_target)};
if (Arrays.asList(unitDependent).contains(pref.getKey())) {
editTextPref = (EditTextPreference) pref;
String converted = Profile.toCurrentUnitsString(SafeParse.stringToDouble(editTextPref.getText()));
editTextPref.setSummary(converted);
editTextPref.setText(converted);
}
} }
} }

View file

@ -19,6 +19,7 @@ import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
@ -617,22 +618,39 @@ public class Profile {
else return value * Constants.MGDL_TO_MMOLL; else return value * Constants.MGDL_TO_MMOLL;
} }
public static double toUnits(Double valueInMgdl, Double valueInMmol, String units) { public static double fromMmolToUnits(double value, String units) {
if (units.equals(Constants.MMOL)) return value;
else return value * Constants.MMOLL_TO_MGDL;
}
public static double toUnits(double valueInMgdl, double valueInMmol, String units) {
if (units.equals(Constants.MGDL)) return valueInMgdl; if (units.equals(Constants.MGDL)) return valueInMgdl;
else return valueInMmol; else return valueInMmol;
} }
public static String toUnitsString(Double valueInMgdl, Double valueInMmol, String units) { public static String toUnitsString(double valueInMgdl, double valueInMmol, String units) {
if (units.equals(Constants.MGDL)) return DecimalFormatter.to0Decimal(valueInMgdl); if (units.equals(Constants.MGDL)) return DecimalFormatter.to0Decimal(valueInMgdl);
else return DecimalFormatter.to1Decimal(valueInMmol); else return DecimalFormatter.to1Decimal(valueInMmol);
} }
public static String toSignedUnitsString(Double valueInMgdl, Double valueInMmol, String units) { public static String toSignedUnitsString(double valueInMgdl, double valueInMmol, String units) {
if (units.equals(Constants.MGDL)) if (units.equals(Constants.MGDL))
return (valueInMgdl > 0 ? "+" : "") + DecimalFormatter.to0Decimal(valueInMgdl); return (valueInMgdl > 0 ? "+" : "") + DecimalFormatter.to0Decimal(valueInMgdl);
else return (valueInMmol > 0 ? "+" : "") + DecimalFormatter.to1Decimal(valueInMmol); else return (valueInMmol > 0 ? "+" : "") + DecimalFormatter.to1Decimal(valueInMmol);
} }
public static double toCurrentUnits(double anyBg) {
if (anyBg < 32) return fromMmolToUnits(anyBg, ProfileFunctions.getSystemUnits());
else return fromMgdlToUnits(anyBg, ProfileFunctions.getSystemUnits());
}
public static String toCurrentUnitsString(double anyBg) {
if (anyBg < 32)
return toUnitsString(anyBg * Constants.MMOLL_TO_MGDL, anyBg, ProfileFunctions.getSystemUnits());
else
return toUnitsString(anyBg, anyBg * Constants.MGDL_TO_MMOLL, ProfileFunctions.getSystemUnits());
}
// targets are stored in mg/dl but profile vary // targets are stored in mg/dl but profile vary
public static String toTargetRangeString(double low, double high, String sourceUnits, String units) { public static String toTargetRangeString(double low, double high, String sourceUnits, String units) {
double lowMgdl = toMgdl(low, sourceUnits); double lowMgdl = toMgdl(low, sourceUnits);

View file

@ -216,13 +216,13 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
DefaultValueHelper helper = new DefaultValueHelper(); DefaultValueHelper helper = new DefaultValueHelper();
if (MainApp.gs(R.string.eatingsoon).equals(reasonList.get(position))) { if (MainApp.gs(R.string.eatingsoon).equals(reasonList.get(position))) {
defaultDuration = helper.determineEatingSoonTTDuration(); defaultDuration = helper.determineEatingSoonTTDuration();
defaultTarget = helper.determineEatingSoonTT(units); defaultTarget = helper.determineEatingSoonTT();
} else if (MainApp.gs(R.string.activity).equals(reasonList.get(position))) { } else if (MainApp.gs(R.string.activity).equals(reasonList.get(position))) {
defaultDuration = helper.determineActivityTTDuration(); defaultDuration = helper.determineActivityTTDuration();
defaultTarget = helper.determineActivityTT(units); defaultTarget = helper.determineActivityTT();
} else if (MainApp.gs(R.string.hypo).equals(reasonList.get(position))) { } else if (MainApp.gs(R.string.hypo).equals(reasonList.get(position))) {
defaultDuration = helper.determineHypoTTDuration(); defaultDuration = helper.determineHypoTTDuration();
defaultTarget = helper.determineHypoTT(units); defaultTarget = helper.determineHypoTT();
} else if (editDuration.getValue() != 0) { } else if (editDuration.getValue() != 0) {
defaultDuration = editDuration.getValue(); defaultDuration = editDuration.getValue();
} else { } else {

View file

@ -760,7 +760,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
pvd.show(manager, "ProfileViewDialog"); pvd.show(manager, "ProfileViewDialog");
} else if (item.getTitle().equals(MainApp.gs(R.string.eatingsoon))) { } else if (item.getTitle().equals(MainApp.gs(R.string.eatingsoon))) {
DefaultValueHelper defHelper = new DefaultValueHelper(); DefaultValueHelper defHelper = new DefaultValueHelper();
double target = defHelper.determineEatingSoonTT(Constants.MGDL); double target = Profile.toMgdl(defHelper.determineEatingSoonTT(), ProfileFunctions.getSystemUnits());
TempTarget tempTarget = new TempTarget() TempTarget tempTarget = new TempTarget()
.date(System.currentTimeMillis()) .date(System.currentTimeMillis())
.duration(defHelper.determineEatingSoonTTDuration()) .duration(defHelper.determineEatingSoonTTDuration())
@ -771,7 +771,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
} else if (item.getTitle().equals(MainApp.gs(R.string.activity))) { } else if (item.getTitle().equals(MainApp.gs(R.string.activity))) {
DefaultValueHelper defHelper = new DefaultValueHelper(); DefaultValueHelper defHelper = new DefaultValueHelper();
double target = defHelper.determineActivityTT(Constants.MGDL); double target = Profile.toMgdl(defHelper.determineActivityTT(), ProfileFunctions.getSystemUnits());
TempTarget tempTarget = new TempTarget() TempTarget tempTarget = new TempTarget()
.date(now()) .date(now())
.duration(defHelper.determineActivityTTDuration()) .duration(defHelper.determineActivityTTDuration())
@ -782,7 +782,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
} else if (item.getTitle().equals(MainApp.gs(R.string.hypo))) { } else if (item.getTitle().equals(MainApp.gs(R.string.hypo))) {
DefaultValueHelper defHelper = new DefaultValueHelper(); DefaultValueHelper defHelper = new DefaultValueHelper();
double target = defHelper.determineHypoTT(Constants.MGDL); double target = Profile.toMgdl(defHelper.determineHypoTT(), ProfileFunctions.getSystemUnits());
TempTarget tempTarget = new TempTarget() TempTarget tempTarget = new TempTarget()
.date(now()) .date(now())
.duration(defHelper.determineHypoTTDuration()) .duration(defHelper.determineHypoTTDuration())

View file

@ -319,13 +319,13 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, C
DefaultValueHelper helper = new DefaultValueHelper(); DefaultValueHelper helper = new DefaultValueHelper();
int activityTTDuration = helper.determineActivityTTDuration(); int activityTTDuration = helper.determineActivityTTDuration();
double activityTT = helper.determineActivityTT(units); double activityTT = helper.determineActivityTT();
int eatingSoonTTDuration = helper.determineEatingSoonTTDuration(); int eatingSoonTTDuration = helper.determineEatingSoonTTDuration();
double eatingSoonTT = helper.determineEatingSoonTT(units); double eatingSoonTT = helper.determineEatingSoonTT();
int hypoTTDuration = helper.determineHypoTTDuration(); int hypoTTDuration = helper.determineHypoTTDuration();
double hypoTT = helper.determineHypoTT(units); double hypoTT = helper.determineHypoTT();
List<String> actions = new LinkedList<>(); List<String> actions = new LinkedList<>();

View file

@ -735,8 +735,7 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
receivedSms.processed = true receivedSms.processed = true
messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction() { messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction() {
override fun run() { override fun run() {
val currentProfile = ProfileFunctions.getInstance().profile val units = ProfileFunctions.getSystemUnits()
if (currentProfile != null) {
var keyDuration = 0 var keyDuration = 0
var defaultTargetDuration = 0 var defaultTargetDuration = 0
var keyTarget = 0 var keyTarget = 0
@ -763,22 +762,20 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
} }
var ttDuration = SP.getInt(keyDuration, defaultTargetDuration) var ttDuration = SP.getInt(keyDuration, defaultTargetDuration)
ttDuration = if (ttDuration > 0) ttDuration else defaultTargetDuration ttDuration = if (ttDuration > 0) ttDuration else defaultTargetDuration
var tt = SP.getDouble(keyTarget, if (currentProfile.units == Constants.MMOL) defaultTargetMMOL else defaultTargetMGDL) var tt = SP.getDouble(keyTarget, if (units == Constants.MMOL) defaultTargetMMOL else defaultTargetMGDL)
tt = if (tt > 0) tt else if (currentProfile.units == Constants.MMOL) defaultTargetMMOL else defaultTargetMGDL tt = Profile.toCurrentUnits(tt)
tt = if (tt > 0) tt else if (units == Constants.MMOL) defaultTargetMMOL else defaultTargetMGDL
val tempTarget = TempTarget() val tempTarget = TempTarget()
.date(System.currentTimeMillis()) .date(System.currentTimeMillis())
.duration(ttDuration) .duration(ttDuration)
.reason(MainApp.gs(R.string.eatingsoon)) .reason(MainApp.gs(R.string.eatingsoon))
.source(Source.USER) .source(Source.USER)
.low(Profile.toMgdl(tt, currentProfile.units)) .low(Profile.toMgdl(tt, units))
.high(Profile.toMgdl(tt, currentProfile.units)) .high(Profile.toMgdl(tt, units))
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget) TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget)
val ttString = if (currentProfile.units == Constants.MMOL) DecimalFormatter.to1Decimal(tt) else DecimalFormatter.to0Decimal(tt) val ttString = if (units == Constants.MMOL) DecimalFormatter.to1Decimal(tt) else DecimalFormatter.to0Decimal(tt)
val replyText = String.format(MainApp.gs(R.string.smscommunicator_tt_set), ttString, ttDuration) val replyText = String.format(MainApp.gs(R.string.smscommunicator_tt_set), ttString, ttDuration)
sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText))
} else {
sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_unknowncommand))
}
} }
}) })
} else if (isStop) { } else if (isStop) {
@ -787,8 +784,6 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
receivedSms.processed = true receivedSms.processed = true
messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction() { messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction() {
override fun run() { override fun run() {
val currentProfile = ProfileFunctions.getInstance().profile
if (currentProfile != null) {
val tempTarget = TempTarget() val tempTarget = TempTarget()
.source(Source.USER) .source(Source.USER)
.date(DateUtil.now()) .date(DateUtil.now())
@ -798,9 +793,6 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget) TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget)
val replyText = String.format(MainApp.gs(R.string.smscommunicator_tt_canceled)) val replyText = String.format(MainApp.gs(R.string.smscommunicator_tt_canceled))
sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText)) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText))
} else {
sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_unknowncommand))
}
} }
}) })
} else } else

View file

@ -2,6 +2,8 @@ package info.nightscout.androidaps.utils;
import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
public class DefaultValueHelper { public class DefaultValueHelper {
@ -41,11 +43,12 @@ public class DefaultValueHelper {
/** /**
* returns the configured EatingSoon TempTarget, if this is set to 0, the Default-Value is returned. * returns the configured EatingSoon TempTarget, if this is set to 0, the Default-Value is returned.
* *
* @param units
* @return * @return
*/ */
public double determineEatingSoonTT(String units) { public double determineEatingSoonTT() {
String units = ProfileFunctions.getSystemUnits();
double value = SP.getDouble(R.string.key_eatingsoon_target, this.getDefaultEatingSoonTT(units)); double value = SP.getDouble(R.string.key_eatingsoon_target, this.getDefaultEatingSoonTT(units));
value = Profile.toCurrentUnits(value);
return value > 0 ? value : this.getDefaultEatingSoonTT(units); return value > 0 ? value : this.getDefaultEatingSoonTT(units);
} }
@ -58,11 +61,12 @@ public class DefaultValueHelper {
/** /**
* returns the configured Activity TempTarget, if this is set to 0, the Default-Value is returned. * returns the configured Activity TempTarget, if this is set to 0, the Default-Value is returned.
* *
* @param units
* @return * @return
*/ */
public double determineActivityTT(String units) { public double determineActivityTT() {
String units = ProfileFunctions.getSystemUnits();
double value = SP.getDouble(R.string.key_activity_target, this.getDefaultActivityTT(units)); double value = SP.getDouble(R.string.key_activity_target, this.getDefaultActivityTT(units));
value = Profile.toCurrentUnits(value);
return value > 0 ? value : this.getDefaultActivityTT(units); return value > 0 ? value : this.getDefaultActivityTT(units);
} }
@ -74,11 +78,12 @@ public class DefaultValueHelper {
/** /**
* returns the configured Hypo TempTarget, if this is set to 0, the Default-Value is returned. * returns the configured Hypo TempTarget, if this is set to 0, the Default-Value is returned.
* *
* @param units
* @return * @return
*/ */
public double determineHypoTT(String units) { public double determineHypoTT() {
String units = ProfileFunctions.getSystemUnits();
double value = SP.getDouble(R.string.key_hypo_target, this.getDefaultHypoTT(units)); double value = SP.getDouble(R.string.key_hypo_target, this.getDefaultHypoTT(units));
value = Profile.toCurrentUnits(value);
return value > 0 ? value : this.getDefaultHypoTT(units); return value > 0 ? value : this.getDefaultHypoTT(units);
} }