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.PreferenceManager;
import java.util.Arrays;
import info.nightscout.androidaps.Config;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventRebuildTabs;
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.utils.OKDialog;
import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.SafeParse;
public class PreferencesActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
MyPreferenceFragment myPreferenceFragment;
@ -65,14 +69,18 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String 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));
//recreate() does not update language so better close settings
finish();
}
if (key.equals("short_tabtitles")) {
if (key.equals(MainApp.gs(R.string.key_short_tabtitles))) {
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)) {
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);
}
}
// 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.plugins.bus.RxBus;
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.notifications.Notification;
import info.nightscout.androidaps.utils.DateUtil;
@ -617,22 +618,39 @@ public class Profile {
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;
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);
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))
return (valueInMgdl > 0 ? "+" : "") + DecimalFormatter.to0Decimal(valueInMgdl);
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
public static String toTargetRangeString(double low, double high, String sourceUnits, String units) {
double lowMgdl = toMgdl(low, sourceUnits);

View file

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

View file

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

View file

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

View file

@ -735,50 +735,47 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
receivedSms.processed = true
messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction() {
override fun run() {
val currentProfile = ProfileFunctions.getInstance().profile
if (currentProfile != null) {
var keyDuration = 0
var defaultTargetDuration = 0
var keyTarget = 0
var defaultTargetMMOL = 0.0
var defaultTargetMGDL = 0.0
if (isMeal) {
keyDuration = R.string.key_eatingsoon_duration
defaultTargetDuration = Constants.defaultEatingSoonTTDuration
keyTarget = R.string.key_eatingsoon_target
defaultTargetMMOL = Constants.defaultEatingSoonTTmmol
defaultTargetMGDL = Constants.defaultEatingSoonTTmgdl
} else if (isActivity) {
keyDuration = R.string.key_activity_duration
defaultTargetDuration = Constants.defaultActivityTTDuration
keyTarget = R.string.key_activity_target
defaultTargetMMOL = Constants.defaultActivityTTmmol
defaultTargetMGDL = Constants.defaultActivityTTmgdl
} else if (isHypo) {
keyDuration = R.string.key_hypo_duration
defaultTargetDuration = Constants.defaultHypoTTDuration
keyTarget = R.string.key_hypo_target
defaultTargetMMOL = Constants.defaultHypoTTmmol
defaultTargetMGDL = Constants.defaultHypoTTmgdl
}
var ttDuration = SP.getInt(keyDuration, defaultTargetDuration)
ttDuration = if (ttDuration > 0) ttDuration else defaultTargetDuration
var tt = SP.getDouble(keyTarget, if (currentProfile.units == Constants.MMOL) defaultTargetMMOL else defaultTargetMGDL)
tt = if (tt > 0) tt else if (currentProfile.units == Constants.MMOL) defaultTargetMMOL else defaultTargetMGDL
val tempTarget = TempTarget()
.date(System.currentTimeMillis())
.duration(ttDuration)
.reason(MainApp.gs(R.string.eatingsoon))
.source(Source.USER)
.low(Profile.toMgdl(tt, currentProfile.units))
.high(Profile.toMgdl(tt, currentProfile.units))
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget)
val ttString = if (currentProfile.units == Constants.MMOL) DecimalFormatter.to1Decimal(tt) else DecimalFormatter.to0Decimal(tt)
val replyText = String.format(MainApp.gs(R.string.smscommunicator_tt_set), ttString, ttDuration)
sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText))
} else {
sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_unknowncommand))
val units = ProfileFunctions.getSystemUnits()
var keyDuration = 0
var defaultTargetDuration = 0
var keyTarget = 0
var defaultTargetMMOL = 0.0
var defaultTargetMGDL = 0.0
if (isMeal) {
keyDuration = R.string.key_eatingsoon_duration
defaultTargetDuration = Constants.defaultEatingSoonTTDuration
keyTarget = R.string.key_eatingsoon_target
defaultTargetMMOL = Constants.defaultEatingSoonTTmmol
defaultTargetMGDL = Constants.defaultEatingSoonTTmgdl
} else if (isActivity) {
keyDuration = R.string.key_activity_duration
defaultTargetDuration = Constants.defaultActivityTTDuration
keyTarget = R.string.key_activity_target
defaultTargetMMOL = Constants.defaultActivityTTmmol
defaultTargetMGDL = Constants.defaultActivityTTmgdl
} else if (isHypo) {
keyDuration = R.string.key_hypo_duration
defaultTargetDuration = Constants.defaultHypoTTDuration
keyTarget = R.string.key_hypo_target
defaultTargetMMOL = Constants.defaultHypoTTmmol
defaultTargetMGDL = Constants.defaultHypoTTmgdl
}
var ttDuration = SP.getInt(keyDuration, defaultTargetDuration)
ttDuration = if (ttDuration > 0) ttDuration else defaultTargetDuration
var tt = SP.getDouble(keyTarget, if (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()
.date(System.currentTimeMillis())
.duration(ttDuration)
.reason(MainApp.gs(R.string.eatingsoon))
.source(Source.USER)
.low(Profile.toMgdl(tt, units))
.high(Profile.toMgdl(tt, units))
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget)
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)
sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText))
}
})
} else if (isStop) {
@ -787,20 +784,15 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
receivedSms.processed = true
messageToConfirm = AuthRequest(this, receivedSms, reply, passCode, object : SmsAction() {
override fun run() {
val currentProfile = ProfileFunctions.getInstance().profile
if (currentProfile != null) {
val tempTarget = TempTarget()
.source(Source.USER)
.date(DateUtil.now())
.duration(0)
.low(0.0)
.high(0.0)
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget)
val replyText = String.format(MainApp.gs(R.string.smscommunicator_tt_canceled))
sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText))
} else {
sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_unknowncommand))
}
val tempTarget = TempTarget()
.source(Source.USER)
.date(DateUtil.now())
.duration(0)
.low(0.0)
.high(0.0)
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget)
val replyText = String.format(MainApp.gs(R.string.smscommunicator_tt_canceled))
sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, replyText))
}
})
} else

View file

@ -2,6 +2,8 @@ package info.nightscout.androidaps.utils;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
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.
*
* @param units
* @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));
value = Profile.toCurrentUnits(value);
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.
*
* @param units
* @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));
value = Profile.toCurrentUnits(value);
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.
*
* @param units
* @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));
value = Profile.toCurrentUnits(value);
return value > 0 ? value : this.getDefaultHypoTT(units);
}