From ec1c43b905f17468d1c8035ed834a676685c2745 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 10 Mar 2018 19:22:31 +0100 Subject: [PATCH] carbs dialog: defaults for mmol, i18n --- .../info/nightscout/androidaps/Constants.java | 8 ++ .../info/nightscout/androidaps/MainApp.java | 4 + .../Overview/Dialogs/NewCarbsDialog.java | 97 ++++++++++--------- .../res/layout/overview_newcarbs_dialog.xml | 16 +-- app/src/main/res/values/strings.xml | 4 + 5 files changed, 74 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index e0549ab9bb..d933018790 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -38,6 +38,14 @@ public class Constants { //DanaR public static final double dailyLimitWarning = 0.95d; + // Temp targets + public static final int defaultActivityTTDuration = 90; // min + public static final double defaultActivityTTmgdl = 90d; + public static final double defaultActivityTTmmol = 5d; + public static final int defaultEatingSoonTTDuration = 45; // min + public static final double defaultEatingSoonTTmgdl = 140d; + public static final double defaultEatingSoonTTmmol = 8d; + //NSClientInternal public static final int MAX_LOG_LINES = 100; diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index bfa801cbdb..2573b57975 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -247,6 +247,10 @@ public class MainApp extends Application { return sResources.getString(id, args); } + public static int gc(int id) { + return sResources.getColor(id); + } + public static MainApp instance() { return sInstance; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java index 8a2d7b4eed..6f46a5db8a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/NewCarbsDialog.java @@ -44,6 +44,7 @@ import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.utils.DateUtil; +import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.NumberPicker; import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; @@ -70,7 +71,7 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D private static final double FAV3_DEFAULT = 20; private CheckBox suspendLoopCheckbox; private CheckBox startActivityTTCheckbox; - private CheckBox startEsTTCheckbox; + private CheckBox startEatingSoonTTCheckbox; private Integer maxCarbs; @@ -102,7 +103,7 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D Integer carbs = SafeParse.stringToInt(editCarbs.getText()); if (carbs > maxCarbs) { editCarbs.setValue(0d); - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.carbsconstraintapplied)); + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.carbsconstraintapplied)); } } @@ -126,7 +127,7 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D editCarbs.setParams(0d, 0d, (double) maxCarbs, 1d, new DecimalFormat("0"), false, textWatcher); startActivityTTCheckbox = view.findViewById(R.id.newcarbs_activity_tt); - startEsTTCheckbox = view.findViewById(R.id.carbs_eating_soon_tt); + startEatingSoonTTCheckbox = view.findViewById(R.id.carbs_eating_soon_tt); dateButton = view.findViewById(R.id.newcarbs_eventdate); timeButton = view.findViewById(R.id.newcarb_eventtime); @@ -139,7 +140,7 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D timeButton.setOnClickListener(this); //To be able to select only one TT at a time - startEsTTCheckbox.setOnClickListener(this); + startEatingSoonTTCheckbox.setOnClickListener(this); startActivityTTCheckbox.setOnClickListener(this); // TODO prefilling carbs, maybe @@ -151,13 +152,13 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D fav1Button = view.findViewById(R.id.newcarbs_plus1); fav1Button.setOnClickListener(this); - fav1Button.setText("+" + SP.getString(MainApp.gs(R.string.key_carbs_button_increment_1), String.valueOf(FAV1_DEFAULT))); + fav1Button.setText("+" + SP.getString(R.string.key_carbs_button_increment_1, String.valueOf(FAV1_DEFAULT))); fav2Button = view.findViewById(R.id.newcarbs_plus2); fav2Button.setOnClickListener(this); - fav2Button.setText("+" + SP.getString(MainApp.gs(R.string.key_carbs_button_increment_2), String.valueOf(FAV2_DEFAULT))); + fav2Button.setText("+" + SP.getString(R.string.key_carbs_button_increment_2, String.valueOf(FAV2_DEFAULT))); fav3Button = view.findViewById(R.id.newcarbs_plus3); fav3Button.setOnClickListener(this); - fav3Button.setText("+" + SP.getString(MainApp.gs(R.string.key_carbs_button_increment_3), String.valueOf(FAV3_DEFAULT))); + fav3Button.setText("+" + SP.getString(R.string.key_carbs_button_increment_3, String.valueOf(FAV3_DEFAULT))); suspendLoopCheckbox = view.findViewById(R.id.newcarbs_suspend_loop); @@ -201,21 +202,21 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D break; case R.id.newcarbs_plus1: editCarbs.setValue(editCarbs.getValue() - + SP.getDouble(MainApp.gs(R.string.key_carbs_button_increment_1), FAV1_DEFAULT)); + + SP.getDouble(R.string.key_carbs_button_increment_1, FAV1_DEFAULT)); validateInputs(); break; case R.id.newcarbs_plus2: editCarbs.setValue(editCarbs.getValue() - + SP.getDouble(MainApp.gs(R.string.key_carbs_button_increment_2), FAV2_DEFAULT)); + + SP.getDouble(R.string.key_carbs_button_increment_2, FAV2_DEFAULT)); validateInputs(); break; case R.id.newcarbs_plus3: editCarbs.setValue(editCarbs.getValue() - + SP.getDouble(MainApp.gs(R.string.key_carbs_button_increment_3), FAV3_DEFAULT)); + + SP.getDouble(R.string.key_carbs_button_increment_3, FAV3_DEFAULT)); validateInputs(); break; case R.id.newcarbs_activity_tt: - startEsTTCheckbox.setChecked(false); + startEatingSoonTTCheckbox.setChecked(false); break; case R.id.carbs_eating_soon_tt: startActivityTTCheckbox.setChecked(false); @@ -238,45 +239,47 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D String confirmMessage = ""; if (carbs > 0) - confirmMessage += getString(R.string.carbs) + ": " + "" + carbsAfterConstraints + "g" + ""; + confirmMessage += MainApp.gs(R.string.carbs) + ": " + "" + carbsAfterConstraints + "g" + ""; if (!carbsAfterConstraints.equals(carbs)) - confirmMessage += "
" + getString(R.string.carbsconstraintapplied) + ""; + confirmMessage += "
" + MainApp.gs(R.string.carbsconstraintapplied) + ""; if (suspendLoopCheckbox.isChecked()) { - confirmMessage += "
" + "Loop: " + "" + "Suspend for 30 min"; + confirmMessage += "
" + MainApp.gs(R.string.loop) + ": " + "" + MainApp.gs(R.string.suspendloopfor30min) + ""; } - double activityTTDurationSettings = SP.getDouble(R.string.key_activity_duration, 90d); - double eatingSoonTTDuration = SP.getDouble(R.string.key_eatingsoon_duration, 45d); - double eatingSoonTT = SP.getDouble(R.string.key_eatingsoon_target, 90d); - double activityTTDuration = activityTTDurationSettings > 0 ? activityTTDurationSettings : 90d; - final double esTTDuration = eatingSoonTTDuration > 0 ? eatingSoonTTDuration : 45d; - double prefTT = SP.getDouble(R.string.key_activity_target, 140d); - - double activityTT = 140d; - double esTT = 90d; - Profile currentProfile = MainApp.getConfigBuilder().getProfile(); + final Profile currentProfile = MainApp.getConfigBuilder().getProfile(); if (currentProfile == null) return; - esTT = eatingSoonTT > 0 ? Profile.toMgdl(eatingSoonTT, currentProfile.getUnits()) : 90d; - activityTT = prefTT > 0 ? Profile.toMgdl(prefTT, currentProfile.getUnits()) : 140d; + int activityTTDuration = SP.getInt(R.string.key_activity_duration, Constants.defaultActivityTTDuration); + activityTTDuration = activityTTDuration > 0 ? activityTTDuration : Constants.defaultActivityTTDuration; + double activityTT = SP.getDouble(R.string.key_activity_target, currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultActivityTTmmol : Constants.defaultActivityTTmgdl); + activityTT = activityTT > 0 ? activityTT : currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultActivityTTmmol : Constants.defaultActivityTTmgdl; + + int eatingSoonTTDuration = SP.getInt(R.string.key_eatingsoon_duration, Constants.defaultEatingSoonTTDuration); + eatingSoonTTDuration = eatingSoonTTDuration > 0 ? eatingSoonTTDuration : Constants.defaultEatingSoonTTDuration; + double eatingSoonTT = SP.getDouble(R.string.key_eatingsoon_target, currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultEatingSoonTTmmol : Constants.defaultEatingSoonTTmgdl); + eatingSoonTT = eatingSoonTT > 0 ? Profile.toMgdl(eatingSoonTT, currentProfile.getUnits()) : currentProfile.getUnits().equals(Constants.MMOL) ? Constants.defaultEatingSoonTTmmol : Constants.defaultEatingSoonTTmgdl; + if (startActivityTTCheckbox.isChecked()) { if (currentProfile.getUnits().equals(Constants.MMOL)) { - confirmMessage += "
" + "TT: " + "" + Profile.toMmol(activityTT, Constants.MGDL) + " mmol/l for " + ((int) activityTTDuration) + " min "; + confirmMessage += "
" + MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(activityTT) + " mmol/l (" + ((int) activityTTDuration) + " min)"; } else - confirmMessage += "
" + "TT: " + "" + ((int) activityTT) + " mg/dl for " + ((int) activityTTDuration) + " min "; + confirmMessage += "
" + MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to0Decimal(activityTT) + " mg/dl (" + ((int) activityTTDuration) + " min)"; } - if (startEsTTCheckbox.isChecked() && !startActivityTTCheckbox.isChecked()) { + if (startEatingSoonTTCheckbox.isChecked() && !startActivityTTCheckbox.isChecked()) { if (currentProfile.getUnits().equals(Constants.MMOL)) { - confirmMessage += "
" + "TT: " + "" + Profile.toMmol(esTT, Constants.MGDL) + " mmol/l for " + ((int) esTTDuration) + " min "; + confirmMessage += "
" + MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to1Decimal(eatingSoonTT) + " mmol/l (" + eatingSoonTTDuration + " min)"; } else - confirmMessage += "
" + "TT: " + "" + ((int) esTT) + " mg/dl for " + ((int) esTTDuration) + " min "; + confirmMessage += "
" + MainApp.gs(R.string.temptargetshort) + ": " + "" + DecimalFormatter.to0Decimal(eatingSoonTT) + " mg/dl (" + eatingSoonTTDuration + " min)"; } - final double finalTT = activityTT; - final double finalEsTT = esTT; + final double finalActivityTT = activityTT; + final double finalEatigSoonTT = eatingSoonTT; + final int finalActivityTTDuration = activityTTDuration; + final int finalEatingSoonTTDuration = eatingSoonTTDuration; + if (StringUtils.isNoneEmpty(food)) { confirmMessage += "
" + "Food: " + food; } @@ -291,12 +294,12 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D final Context context = getContext(); final AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(this.getContext().getString(R.string.confirmation)); + builder.setTitle(MainApp.gs(R.string.confirmation)); if (confirmMessage.startsWith("
")) confirmMessage = confirmMessage.substring("
".length()); builder.setMessage(Html.fromHtml(confirmMessage)); - builder.setPositiveButton(getString(R.string.ok), (dialog, id) -> { + builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { synchronized (builder) { if (accepted) { log.debug("guarding: already accepted"); @@ -311,29 +314,29 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D @Override public void run() { if (!result.success) { - ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.sResources.getString(R.string.tempbasaldeliveryerror)); + ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.tempbasaldeliveryerror)); } } }); } - if (startActivityTTCheckbox.isChecked() || (startActivityTTCheckbox.isChecked() && startEsTTCheckbox.isChecked())) { + if (startActivityTTCheckbox.isChecked()) { TempTarget tempTarget = new TempTarget(); tempTarget.date = System.currentTimeMillis(); - tempTarget.durationInMinutes = (int) activityTTDuration; - tempTarget.reason = "Activity"; + tempTarget.durationInMinutes = finalActivityTTDuration; + tempTarget.reason = MainApp.gs(R.string.activity); tempTarget.source = Source.USER; - tempTarget.low = (double) finalTT; - tempTarget.high = (double) finalTT; + tempTarget.low = Profile.toMgdl(finalActivityTT, currentProfile.getUnits()); + tempTarget.high = Profile.toMgdl(finalActivityTT, currentProfile.getUnits()); MainApp.getDbHelper().createOrUpdate(tempTarget); - } else if (startEsTTCheckbox.isChecked()) { + } else if (startEatingSoonTTCheckbox.isChecked()) { TempTarget tempTarget = new TempTarget(); tempTarget.date = System.currentTimeMillis(); - tempTarget.durationInMinutes = (int) esTTDuration; - tempTarget.reason = "Eating soon"; + tempTarget.durationInMinutes = finalEatingSoonTTDuration; + tempTarget.reason = MainApp.gs(R.string.eatingsoon); tempTarget.source = Source.USER; - tempTarget.low = (double) finalEsTT; - tempTarget.high = (double) finalEsTT; + tempTarget.low = Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits()); + tempTarget.high = Profile.toMgdl(finalEatigSoonTT, currentProfile.getUnits()); MainApp.getDbHelper().createOrUpdate(tempTarget); } @@ -349,7 +352,7 @@ public class NewCarbsDialog extends DialogFragment implements OnClickListener, D } } }); - builder.setNegativeButton(getString(R.string.cancel), null); + builder.setNegativeButton(MainApp.gs(R.string.cancel), null); builder.show(); dismiss(); } else diff --git a/app/src/main/res/layout/overview_newcarbs_dialog.xml b/app/src/main/res/layout/overview_newcarbs_dialog.xml index 7f00891e40..74416d7b8a 100644 --- a/app/src/main/res/layout/overview_newcarbs_dialog.xml +++ b/app/src/main/res/layout/overview_newcarbs_dialog.xml @@ -37,27 +37,27 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp" - android:text="Start activity TT"/> + android:text="@string/start_activity_tt" /> + android:text="@string/start_eating_soon_tt" /> + android:text="@string/suspendloopfor30min" /> + android:layout_gravity="center_horizontal" + android:padding="5dp"> + android:visibility="gone" /> + android:layout_marginBottom="5dp" + android:orientation="horizontal">