From 8cecac0a7f3b468c0ad61f25613864b3d2b359a5 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 2 Oct 2017 19:57:41 +0200 Subject: [PATCH] more LocalProfile work --- .../info/nightscout/androidaps/Config.java | 2 +- .../nightscout/androidaps/data/Profile.java | 2 +- .../Dialogs/NewNSTreatmentDialog.java | 52 +++--- .../Overview/Dialogs/WizardDialog.java | 12 -- .../ProfileLocal/LocalProfileFragment.java | 162 +++++++++--------- .../ProfileLocal/LocalProfilePlugin.java | 26 +-- .../java/info/nightscout/utils/DateUtil.java | 14 ++ .../info/nightscout/utils/NumberPicker.java | 23 +-- .../info/nightscout/utils/TimeListEdit.java | 157 +++++++++-------- .../main/res/layout/localprofile_fragment.xml | 11 +- .../main/res/layout/timelistedit_element.xml | 34 ++-- 11 files changed, 261 insertions(+), 234 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Config.java b/app/src/main/java/info/nightscout/androidaps/Config.java index e6c06583c9..4b19a0e75f 100644 --- a/app/src/main/java/info/nightscout/androidaps/Config.java +++ b/app/src/main/java/info/nightscout/androidaps/Config.java @@ -40,7 +40,7 @@ public class Config { public static final boolean logNSUpload = true; public static final boolean logPumpActions = true; public static final boolean logCongigBuilderActions = true; - public static final boolean logAutosensData = true; + public static final boolean logAutosensData = false; // DanaR specific public static final boolean logDanaBTComm = true; diff --git a/app/src/main/java/info/nightscout/androidaps/data/Profile.java b/app/src/main/java/info/nightscout/androidaps/data/Profile.java index e7e7de8d91..99d416bc6e 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/Profile.java +++ b/app/src/main/java/info/nightscout/androidaps/data/Profile.java @@ -195,7 +195,7 @@ public class Profile { int offset = -(timeshift % 24) + 24; Integer shiftedTime = originalTime + offset * 60 * 60; shiftedTime = shiftedTime % (24 * 60 * 60); - log.debug("(Sec) Original time: " + new Date(originalTime).toLocaleString() + " ShiftedTime: " + new Date(shiftedTime).toLocaleString()); + //log.debug("(Sec) Original time: " + new Date(originalTime).toLocaleString() + " ShiftedTime: " + new Date(shiftedTime).toLocaleString()); return shiftedTime; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index 4afa4c15c6..160a22a1ce 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -190,20 +190,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick // bg bgUnitsView.setText(units); - Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile != null ? profile.getUnits() : Constants.MGDL); - editBg = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_bginput); - editTemptarget = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_temptarget); - if (profile == null) { - editBg.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false); - editTemptarget.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false); - } else if (profile.getUnits().equals(Constants.MMOL)) { - editBg.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false); - editTemptarget.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false); - } else { - editBg.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false); - editTemptarget.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false); - } - editBg.addTextChangedListener(new TextWatcher() { + TextWatcher bgTextWatcher = new TextWatcher() { public void afterTextChanged(Editable s) { } @@ -213,8 +200,21 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick public void onTextChanged(CharSequence s, int start, int before, int count) { if (sensorRadioButton.isChecked()) meterRadioButton.setChecked(true); } - }); + }; + Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile != null ? profile.getUnits() : Constants.MGDL); + editBg = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_bginput); + editTemptarget = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_temptarget); + if (profile == null) { + editBg.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false, bgTextWatcher); + editTemptarget.setParams(bg, 0d, 500d, 0.1d, new DecimalFormat("0.0"), false); + } else if (profile.getUnits().equals(Constants.MMOL)) { + editBg.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false, bgTextWatcher); + editTemptarget.setParams(bg, 0d, 30d, 0.1d, new DecimalFormat("0.0"), false); + } else { + editBg.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false, bgTextWatcher); + editTemptarget.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false); + } sensorRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { @@ -236,10 +236,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick editDuration = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_durationinput); editDuration.setParams(0d, 0d, 24 * 60d, 10d, new DecimalFormat("0"), false); - Integer maxPercent = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalPercentOnlyForCheckLimit); - editPercent = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_percentinput); - editPercent.setParams(0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true); - editPercent.addTextChangedListener(new TextWatcher() { + TextWatcher percentTextWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable s) { } @@ -255,12 +252,13 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick layoutPercent.setVisibility(View.VISIBLE); layoutAbsolute.setVisibility(View.GONE); } - }); + }; - Double maxAbsolute = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit); - editAbsolute = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_absoluteinput); - editAbsolute.setParams(0d, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true); - editAbsolute.addTextChangedListener(new TextWatcher() { + Integer maxPercent = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalPercentOnlyForCheckLimit); + editPercent = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_percentinput); + editPercent.setParams(0d, 0d, (double) maxPercent, 5d, new DecimalFormat("0"), true, percentTextWatcher); + + TextWatcher absoluteTextWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable s) { } @@ -276,7 +274,11 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick layoutPercent.setVisibility(View.GONE); layoutAbsolute.setVisibility(View.VISIBLE); } - }); + }; + + Double maxAbsolute = MainApp.getConfigBuilder().applyBasalConstraints(Constants.basalAbsoluteOnlyForCheckLimit); + editAbsolute = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_absoluteinput); + editAbsolute.setParams(0d, 0d, maxAbsolute, 0.05d, new DecimalFormat("0.00"), true, absoluteTextWatcher); editCarbTime = (NumberPicker) view.findViewById(R.id.careportal_newnstreatment_carbtimeinput); editCarbTime.setParams(0d, -60d, 60d, 5d, new DecimalFormat("0"), false); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java index 54f526288b..b3303a6e23 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Dialogs/WizardDialog.java @@ -420,15 +420,9 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com BgReading lastBg = DatabaseHelper.actualBg(); if (lastBg != null) { - editBg.removeTextChangedListener(textWatcher); - //bgInput.setText(lastBg.valueToUnitsToString(units)); editBg.setValue(lastBg.valueToUnits(units)); - editBg.addTextChangedListener(textWatcher); } else { - editBg.removeTextChangedListener(textWatcher); - //bgInput.setText(""); editBg.setValue(0d); - editBg.addTextChangedListener(textWatcher); } ttCheckbox.setEnabled(MainApp.getConfigBuilder().getTempTargetFromHistory() != null); @@ -465,19 +459,13 @@ public class WizardDialog extends DialogFragment implements OnClickListener, Com Double c_correction = SafeParse.stringToDouble(editCorr.getText()); Double corrAfterConstraint = MainApp.getConfigBuilder().applyBolusConstraints(c_correction); if (c_correction - corrAfterConstraint != 0) { // c_correction != corrAfterConstraint doesn't work - editCorr.removeTextChangedListener(textWatcher); editCorr.setValue(0d); - editCorr.addTextChangedListener(textWatcher); - //wizardDialogDeliverButton.setVisibility(Button.GONE); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.bolusconstraintapplied)); return; } Integer carbsAfterConstraint = MainApp.getConfigBuilder().applyCarbsConstraints(c_carbs); if (c_carbs - carbsAfterConstraint != 0) { - editCarbs.removeTextChangedListener(textWatcher); editCarbs.setValue(0d); - editCarbs.addTextChangedListener(textWatcher); - //wizardDialogDeliverButton.setVisibility(Button.GONE); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), getString(R.string.carbsconstraintapplied)); return; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java index 1495e66f5d..da29008bf1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfileFragment.java @@ -9,7 +9,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; -import android.widget.EditText; import android.widget.RadioButton; import com.squareup.otto.Subscribe; @@ -27,6 +26,7 @@ import info.nightscout.androidaps.plugins.Careportal.CareportalFragment; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.Common.SubscriberFragment; +import info.nightscout.utils.NumberPicker; import info.nightscout.utils.SafeParse; import info.nightscout.utils.TimeListEdit; @@ -39,7 +39,7 @@ public class LocalProfileFragment extends SubscriberFragment { return localProfilePlugin; } - EditText diaView; + NumberPicker diaView; RadioButton mgdlView; RadioButton mmolView; TimeListEdit icView; @@ -51,88 +51,92 @@ public class LocalProfileFragment extends SubscriberFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - Runnable save = new Runnable() { - @Override - public void run() { - localProfilePlugin.storeSettings(); + try { + Runnable save = new Runnable() { + @Override + public void run() { + localProfilePlugin.storeSettings(); + } + }; + + TextWatcher textWatch = new TextWatcher() { + + @Override + public void afterTextChanged(Editable s) { + } + + @Override + public void beforeTextChanged(CharSequence s, int start, + int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, + int before, int count) { + localProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString()); + localProfilePlugin.storeSettings(); + } + }; + + View layout = inflater.inflate(R.layout.localprofile_fragment, container, false); + diaView = (NumberPicker) layout.findViewById(R.id.localprofile_dia); + diaView.setParams(localProfilePlugin.dia, 2d, 48d, 0.1d, new DecimalFormat("0.0"), false, textWatch); + mgdlView = (RadioButton) layout.findViewById(R.id.localprofile_mgdl); + mmolView = (RadioButton) layout.findViewById(R.id.localprofile_mmol); + icView = new TimeListEdit(getContext(), layout, R.id.localprofile_ic, MainApp.sResources.getString(R.string.nsprofileview_ic_label), getPlugin().ic, null, 0.1d, new DecimalFormat("0.0"), save); + isfView = new TimeListEdit(getContext(), layout, R.id.localprofile_isf, MainApp.sResources.getString(R.string.nsprofileview_isf_label), getPlugin().isf, null, 0.1d, new DecimalFormat("0.0"), save); + basalView = new TimeListEdit(getContext(), layout, R.id.localprofile_basal, MainApp.sResources.getString(R.string.nsprofileview_basal_label), getPlugin().basal, null, 0.01d, new DecimalFormat("0.00"), save); + targetView = new TimeListEdit(getContext(), layout, R.id.localprofile_target, MainApp.sResources.getString(R.string.nsprofileview_target_label), getPlugin().targetLow, getPlugin().targetHigh, 0.1d, new DecimalFormat("0.0"), save); + profileswitchButton = (Button) layout.findViewById(R.id.localprofile_profileswitch); + + PumpInterface pump = MainApp.getConfigBuilder(); + if (!pump.getPumpDescription().isTempBasalCapable) { + layout.findViewById(R.id.localprofile_basal).setVisibility(View.GONE); } - }; - View layout = inflater.inflate(R.layout.localprofile_fragment, container, false); - diaView = (EditText) layout.findViewById(R.id.localprofile_dia); - mgdlView = (RadioButton) layout.findViewById(R.id.localprofile_mgdl); - mmolView = (RadioButton) layout.findViewById(R.id.localprofile_mmol); - icView = new TimeListEdit(getContext(), layout, R.id.localprofile_ic, MainApp.sResources.getString(R.string.nsprofileview_ic_label), getPlugin().ic, null, new DecimalFormat("0.0"), save); - isfView = new TimeListEdit(getContext(), layout, R.id.localprofile_isf, MainApp.sResources.getString(R.string.nsprofileview_isf_label), getPlugin().isf, null, new DecimalFormat("0.0"), save); - basalView = new TimeListEdit(getContext(), layout, R.id.localprofile_basal, MainApp.sResources.getString(R.string.nsprofileview_basal_label), getPlugin().basal, null, new DecimalFormat("0.00"), save); - targetView = new TimeListEdit(getContext(), layout, R.id.localprofile_target, MainApp.sResources.getString(R.string.nsprofileview_target_label), getPlugin().targetLow, getPlugin().targetHigh, new DecimalFormat("0.0"), save); - profileswitchButton = (Button) layout.findViewById(R.id.localprofile_profileswitch); + updateGUI(); - PumpInterface pump = MainApp.getConfigBuilder(); - if (!pump.getPumpDescription().isTempBasalCapable) { - layout.findViewById(R.id.localprofile_basal).setVisibility(View.GONE); + mgdlView.setChecked(localProfilePlugin.mgdl); + mmolView.setChecked(localProfilePlugin.mmol); + + mgdlView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + localProfilePlugin.mgdl = mgdlView.isChecked(); + localProfilePlugin.mmol = !localProfilePlugin.mgdl; + mmolView.setChecked(localProfilePlugin.mmol); + localProfilePlugin.storeSettings(); + } + }); + mmolView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + localProfilePlugin.mmol = mmolView.isChecked(); + localProfilePlugin.mgdl = !localProfilePlugin.mmol; + mgdlView.setChecked(localProfilePlugin.mgdl); + localProfilePlugin.storeSettings(); + } + }); + + profileswitchButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); + final OptionsToShow profileswitch = CareportalFragment.PROFILESWITCHDIRECT; + profileswitch.executeProfileSwitch = true; + newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); + newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); + } + }); + + + updateGUI(); + return layout; + } catch (Exception e) { + log.error("Unhandled exception: ", e); } - updateGUI(); - - mgdlView.setChecked(localProfilePlugin.mgdl); - mmolView.setChecked(localProfilePlugin.mmol); - diaView.setText(localProfilePlugin.dia.toString()); - - mgdlView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - localProfilePlugin.mgdl = mgdlView.isChecked(); - localProfilePlugin.mmol = !localProfilePlugin.mgdl; - mmolView.setChecked(localProfilePlugin.mmol); - localProfilePlugin.storeSettings(); - } - }); - mmolView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - localProfilePlugin.mmol = mmolView.isChecked(); - localProfilePlugin.mgdl = !localProfilePlugin.mmol; - mgdlView.setChecked(localProfilePlugin.mgdl); - localProfilePlugin.storeSettings(); - } - }); - - profileswitchButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - NewNSTreatmentDialog newDialog = new NewNSTreatmentDialog(); - final OptionsToShow profileswitch = CareportalFragment.PROFILESWITCHDIRECT; - profileswitch.executeProfileSwitch = true; - newDialog.setOptions(profileswitch, R.string.careportal_profileswitch); - newDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); - } - }); - - TextWatcher textWatch = new TextWatcher() { - - @Override - public void afterTextChanged(Editable s) { - } - - @Override - public void beforeTextChanged(CharSequence s, int start, - int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, - int before, int count) { - localProfilePlugin.dia = SafeParse.stringToDouble(diaView.getText().toString()); - localProfilePlugin.storeSettings(); - } - }; - - diaView.addTextChangedListener(textWatch); - - updateGUI(); - - return layout; + return null; } @Subscribe diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java index 51d231edeb..2a2d45e2ef 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ProfileLocal/LocalProfilePlugin.java @@ -24,11 +24,11 @@ import info.nightscout.utils.SP; public class LocalProfilePlugin implements PluginBase, ProfileInterface { private static Logger log = LoggerFactory.getLogger(LocalProfilePlugin.class); - private static boolean fragmentEnabled = false; - private static boolean fragmentVisible = true; + private boolean fragmentEnabled = false; + private boolean fragmentVisible = true; - private static ProfileStore convertedProfile = null; - private static String convertedProfileName = null; + private ProfileStore convertedProfile = null; + private String convertedProfileName = null; final private String DEFAULTARRAY = "[{\"time\":\"00:00\",\"timeAsSeconds\":0,\"value\":0}]"; @@ -107,8 +107,6 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { } public void storeSettings() { - if (Config.logPrefsChange) - log.debug("Storing settings"); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("LocalProfile" + "mmol", mmol); @@ -120,8 +118,10 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { editor.putString("LocalProfile" + "targetlow", targetLow.toString()); editor.putString("LocalProfile" + "targethigh", targetHigh.toString()); - editor.commit(); + editor.apply(); createConvertedProfile(); + if (Config.logPrefsChange) + log.debug("Storing settings: " + getProfile().getData().toString()); } private void loadSettings() { @@ -136,7 +136,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { } catch (JSONException e1) { try { ic = new JSONArray(DEFAULTARRAY); - } catch (JSONException e2) { + } catch (JSONException ignored) { } } try { @@ -144,7 +144,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { } catch (JSONException e1) { try { isf = new JSONArray(DEFAULTARRAY); - } catch (JSONException e2) { + } catch (JSONException ignored) { } } try { @@ -152,7 +152,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { } catch (JSONException e1) { try { basal = new JSONArray(DEFAULTARRAY); - } catch (JSONException e2) { + } catch (JSONException ignored) { } } try { @@ -160,7 +160,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { } catch (JSONException e1) { try { targetLow = new JSONArray(DEFAULTARRAY); - } catch (JSONException e2) { + } catch (JSONException ignored) { } } try { @@ -168,7 +168,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { } catch (JSONException e1) { try { targetHigh = new JSONArray(DEFAULTARRAY); - } catch (JSONException e2) { + } catch (JSONException ignored) { } } createConvertedProfile(); @@ -212,7 +212,7 @@ public class LocalProfilePlugin implements PluginBase, ProfileInterface { "created_at": "2016-06-16T08:34:41.256Z" } */ - void createConvertedProfile() { + private void createConvertedProfile() { JSONObject json = new JSONObject(); JSONObject store = new JSONObject(); JSONObject profile = new JSONObject(); diff --git a/app/src/main/java/info/nightscout/utils/DateUtil.java b/app/src/main/java/info/nightscout/utils/DateUtil.java index 2d0fbf5a14..3e32557adb 100644 --- a/app/src/main/java/info/nightscout/utils/DateUtil.java +++ b/app/src/main/java/info/nightscout/utils/DateUtil.java @@ -1,6 +1,8 @@ package info.nightscout.utils; +import android.support.v4.util.LongSparseArray; import android.text.format.DateUtils; +import android.util.SparseIntArray; import java.text.DateFormat; import java.text.ParseException; @@ -132,4 +134,16 @@ public class DateUtil { return String.format(MainApp.sResources.getString(R.string.minago), mins); } + private static LongSparseArray timeStrings = new LongSparseArray<>(); + + public static String timeStringFromSeconds(int seconds) { + String cached = timeStrings.get(seconds); + if (cached != null) + return cached; + String t = DateUtils.formatDateTime(MainApp.instance(), toDate(seconds).getTime(), DateUtils.FORMAT_SHOW_TIME); + timeStrings.put(seconds, t); + return t; + } + + } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/utils/NumberPicker.java b/app/src/main/java/info/nightscout/utils/NumberPicker.java index ce960cbe7a..abaede1eaf 100644 --- a/app/src/main/java/info/nightscout/utils/NumberPicker.java +++ b/app/src/main/java/info/nightscout/utils/NumberPicker.java @@ -42,6 +42,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, Double step = 1d; NumberFormat formater; boolean allowZero = false; + TextWatcher textWatcher = null; private Handler mHandler; private ScheduledExecutorService mUpdater; @@ -94,9 +95,12 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, LayoutInflater.from(context).inflate(R.layout.number_picker_layout, this, true); // init ui components - this.minusButton = (Button) findViewById(R.id.decrement); - this.plusButton = (Button) findViewById(R.id.increment); - this.editText = (EditText) findViewById(R.id.display); + minusButton = (Button) findViewById(R.id.decrement); + minusButton.setId(View.generateViewId()); + plusButton = (Button) findViewById(R.id.increment); + plusButton.setId(View.generateViewId()); + editText = (EditText) findViewById(R.id.display); + editText.setId(View.generateViewId()); mHandler = new Handler() { @Override @@ -121,16 +125,9 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, plusButton.setOnClickListener(this); } - public void removeTextChangedListener(TextWatcher textWatcher) { - editText.removeTextChangedListener(textWatcher); - } - - public void addTextChangedListener(TextWatcher textWatcher) { - editText.addTextChangedListener(textWatcher); - } - public void setParams(Double initValue, Double minValue, Double maxValue, Double step, NumberFormat formater, boolean allowZero, TextWatcher textWatcher) { setParams(initValue, minValue, maxValue, step, formater, allowZero); + this.textWatcher = textWatcher; editText.addTextChangedListener(textWatcher); } @@ -146,8 +143,12 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, } public void setValue(Double value) { + if (textWatcher != null) + editText.removeTextChangedListener(textWatcher); this.value = value; updateEditText(); + if (textWatcher != null) + editText.addTextChangedListener(textWatcher); } public Double getValue() { diff --git a/app/src/main/java/info/nightscout/utils/TimeListEdit.java b/app/src/main/java/info/nightscout/utils/TimeListEdit.java index 8a1fac75a3..8e8d4956b3 100644 --- a/app/src/main/java/info/nightscout/utils/TimeListEdit.java +++ b/app/src/main/java/info/nightscout/utils/TimeListEdit.java @@ -49,22 +49,25 @@ public class TimeListEdit { String label; JSONArray data1; JSONArray data2; + double step; NumberFormat formatter; Runnable save; - public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, NumberFormat formatter, Runnable save) { + public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data1, JSONArray data2, double step, NumberFormat formatter, Runnable save) { this.context = context; this.view = view; this.resLayoutId = resLayoutId; this.label = label; this.data1 = data1; this.data2 = data2; + this.step = step; this.formatter = formatter; this.save = save; buildView(); } private void buildView() { + log.debug("buildView start"); LayoutInflater inflater = LayoutInflater.from(context); layout = (LinearLayout) view.findViewById(resLayoutId); @@ -82,42 +85,84 @@ public class TimeListEdit { for (int i = 0; i < itemsCount(); i++) { View childview = inflater.inflate(R.layout.timelistedit_element, layout, false); - childview.setId(View.generateViewId()); + //childview.setId(View.generateViewId()); final Spinner timeSpinner = (Spinner) childview.findViewById(R.id.timelistedit_time); - timeSpinner.setId(View.generateViewId()); + //timeSpinner.setId(View.generateViewId()); int previous = i == 0 ? -1 * ONEHOURINSECONDS : secondFromMidnight(i - 1); int next = i == itemsCount() - 1 ? 24 * ONEHOURINSECONDS : secondFromMidnight(i + 1); if (i == 0) next = ONEHOURINSECONDS; fillSpinner(timeSpinner, secondFromMidnight(i), previous, next); - final EditText editText1 = (EditText) childview.findViewById(R.id.timelistedit_edit1); - editText1.setId(View.generateViewId()); - fillNumber(editText1, value1(i)); - final EditText editText2 = ((EditText) childview.findViewById(R.id.timelistedit_edit2)); - fillNumber(editText2, value2(i)); - editText2.setId(View.generateViewId()); + final int fixedPos = i; + final NumberPicker editText1 = (NumberPicker) childview.findViewById(R.id.timelistedit_edit1); + + TextWatcher tw1 = new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + log.debug("aaaa"); + editItem(fixedPos, secondFromMidnight(fixedPos), SafeParse.stringToDouble(editText1.getText()), value2(fixedPos)); + callSave(); + log(); + } + + @Override + public void beforeTextChanged(CharSequence s, int start, + int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, + int before, int count) { + } + }; + + editText1.setParams(value1(i), 0.1d, 100d, step, formatter, false, tw1); + + final NumberPicker editText2 = (NumberPicker) childview.findViewById(R.id.timelistedit_edit2); + + TextWatcher tw2 = new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + log.debug("bbbbb"); + editItem(fixedPos, secondFromMidnight(fixedPos), value1(fixedPos), SafeParse.stringToDouble(editText2.getText())); + callSave(); + log(); + } + + @Override + public void beforeTextChanged(CharSequence s, int start, + int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, + int before, int count) { + } + }; + editText2.setParams(value2(i), 0.1d, 100d, step, formatter, false, tw2); if (data2 == null) { editText2.setVisibility(View.GONE); } ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add); - addbutton.setId(View.generateViewId()); + //addbutton.setId(View.generateViewId()); ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove); - removebutton.setId(View.generateViewId()); + //removebutton.setId(View.generateViewId()); - if (itemsCount() == 1 && i == 0) { - removebutton.setVisibility(View.GONE); + if (itemsCount() == 1 || i == 0) { + removebutton.setVisibility(View.INVISIBLE); } - if (itemsCount() >= 24) { - addbutton.setVisibility(View.GONE); + if (itemsCount() >= 24 || secondFromMidnight(i) >= 82800) { + addbutton.setVisibility(View.INVISIBLE); } - final int fixedPos = i; addbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + log.debug("ccccc"); + layout.removeAllViews(); int seconds = secondFromMidnight(fixedPos); addItem(fixedPos, seconds, 0, 0); // for here for the rest of values @@ -128,6 +173,7 @@ public class TimeListEdit { } while (itemsCount() > 24 || secondFromMidnight(itemsCount() - 1) > 23 * ONEHOURINSECONDS) removeItem(itemsCount() - 1); + callSave(); log(); buildView(); } @@ -136,7 +182,10 @@ public class TimeListEdit { removebutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + log.debug("dddd"); + layout.removeAllViews(); removeItem(fixedPos); + callSave(); log(); buildView(); } @@ -145,57 +194,26 @@ public class TimeListEdit { timeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { + log.debug("eeeeee"); + layout.removeAllViews(); int seconds = DateUtil.toSeconds(timeSpinner.getSelectedItem().toString()); editItem(fixedPos, seconds, value1(fixedPos), value2(fixedPos)); log(); + callSave(); buildView(); } @Override public void onNothingSelected(AdapterView parent) { + log.debug("fffff"); + layout.removeAllViews(); editItem(fixedPos, 0, value1(fixedPos), value2(fixedPos)); log(); + callSave(); buildView(); } } ); - - editText1.addTextChangedListener(new TextWatcher() { - @Override - public void afterTextChanged(Editable s) { - editItem(fixedPos, secondFromMidnight(fixedPos), SafeParse.stringToDouble(editText1.getText().toString()), value2(fixedPos)); - log(); - } - - @Override - public void beforeTextChanged(CharSequence s, int start, - int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, - int before, int count) { - } - }); - - editText2.addTextChangedListener(new TextWatcher() { - @Override - public void afterTextChanged(Editable s) { - editItem(fixedPos, secondFromMidnight(fixedPos), value1(fixedPos), SafeParse.stringToDouble(editText2.getText().toString())); - log(); - } - - @Override - public void beforeTextChanged(CharSequence s, int start, - int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, - int before, int count) { - } - }); - layout.addView(childview); } @@ -210,13 +228,15 @@ public class TimeListEdit { imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + log.debug("gggggggg"); addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + ONEHOURINSECONDS : 0, 0, 0); + callSave(); log(); buildView(); } }); } - + log.debug("buildView end"); } public void fillSpinner(Spinner spinner, int secondsFromMidnight, int previous, int next) { @@ -224,24 +244,17 @@ public class TimeListEdit { ArrayList timeList = new ArrayList<>(); int pos = 0; for (int t = previous + ONEHOURINSECONDS; t < next; t += ONEHOURINSECONDS) { - timeList.add(DateUtil.timeString(DateUtil.toDate(t))); + timeList.add(DateUtil.timeStringFromSeconds(t)); if (secondsFromMidnight == t) posInList = pos; pos++; } - ArrayAdapter adapter = new ArrayAdapter(context, + ArrayAdapter adapter = new ArrayAdapter<>(context, R.layout.spinner_centered, timeList); spinner.setAdapter(adapter); spinner.setSelection(posInList, false); } - public void fillNumber(EditText edit, Double value) { - if (value.equals(0d)) - edit.setText(""); - else - edit.setText(formatter.format(value)); - } - public int itemsCount() { return data1.length(); } @@ -250,7 +263,13 @@ public class TimeListEdit { try { JSONObject item = (JSONObject) data1.get(index); if (item.has("timeAsSeconds")) { - return item.getInt("timeAsSeconds"); + int time = item.getInt("timeAsSeconds"); + if (index == 0 && time != 0) { + // fix the bug, every array must start with 0 + item.put("timeAsSeconds", 0); + time = 0; + } + return time; } } catch (JSONException e) { log.error("Unhandled exception", e); @@ -303,7 +322,6 @@ public class TimeListEdit { newObject2.put("value", value2); data2.put(index, newObject2); } - if (save != null) save.run(); } catch (JSONException e) { log.error("Unhandled exception", e); } @@ -320,7 +338,6 @@ public class TimeListEdit { } // add new object editItem(index, timeAsSeconds, value1, value2); - if (save != null) save.run(); } catch (JSONException e) { log.error("Unhandled exception", e); } @@ -331,13 +348,15 @@ public class TimeListEdit { data1.remove(index); if (data2 != null) data2.remove(index); - if (save != null) save.run(); } void log() { for (int i = 0; i < data1.length(); i++) { - int pos = 0; - log.debug(i + ": @" + DateUtil.timeString(DateUtil.toDate(secondFromMidnight(i))) + " " + value1(i) + (data2 != null ? " " + value2(i) : "")); + log.debug(i + ": @" + DateUtil.timeStringFromSeconds(secondFromMidnight(i)) + " " + value1(i) + (data2 != null ? " " + value2(i) : "")); } } + + void callSave() { + if (save != null) save.run(); + } } diff --git a/app/src/main/res/layout/localprofile_fragment.xml b/app/src/main/res/layout/localprofile_fragment.xml index 3773b43fbc..0f6ac1fa91 100644 --- a/app/src/main/res/layout/localprofile_fragment.xml +++ b/app/src/main/res/layout/localprofile_fragment.xml @@ -23,6 +23,7 @@ - + android:layout_width="130dp" + android:layout_height="40dp" + android:layout_gravity="center_horizontal" /> + android:layout_gravity="center" + android:layout_marginRight="10dp" /> - + android:orientation="vertical"> - + + + +