From 0364d3c039558b4aef6a15886390db144cc543c2 Mon Sep 17 00:00:00 2001 From: Nico Schmitz Date: Thu, 25 Oct 2018 22:08:04 +0200 Subject: [PATCH] Refactor code. Some minor fixes. --- .../automation/AutomationFragment.java | 27 ++++++------------- .../automation/dialogs/EditEventDialog.java | 3 +-- .../general/automation/triggers/Trigger.java | 3 ++- .../automation/triggers/TriggerBg.java | 27 ++++++++++++++----- .../automation/triggers/TriggerConnector.java | 7 ++--- .../automation/triggers/TriggerTime.java | 5 ++-- .../info/nightscout/utils/NumberPicker.java | 19 +++++++++++++ 7 files changed, 57 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.java index 62956cbd87..a0ddba7684 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.java @@ -32,12 +32,6 @@ import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerCon public class AutomationFragment extends SubscriberFragment { - public static FragmentManager fragmentManager() { - return mFragmentManager; - } - - private static FragmentManager mFragmentManager = null; - @BindView(R.id.eventListView) RecyclerView mEventListView; @@ -47,8 +41,6 @@ public class AutomationFragment extends SubscriberFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - mFragmentManager = getFragmentManager(); - View view = inflater.inflate(R.layout.automation_fragment, container, false); unbinder = ButterKnife.bind(this, view); @@ -66,16 +58,13 @@ public class AutomationFragment extends SubscriberFragment { public void updateGUI() { Activity activity = getActivity(); if (activity != null) - activity.runOnUiThread(() -> { - mEventListAdapter.notifyDataSetChanged(); - }); + activity.runOnUiThread(() -> mEventListAdapter.notifyDataSetChanged()); } @OnClick(R.id.fabAddEvent) void onClickAddEvent(View v) { EditEventDialog dialog = EditEventDialog.newInstance(new AutomationEvent()); - FragmentManager manager = getFragmentManager(); - dialog.show(manager, "EditEventDialog"); + dialog.show(getFragmentManager(), "EditEventDialog"); } /** @@ -128,10 +117,12 @@ public class AutomationFragment extends SubscriberFragment { private final LinearLayout mRootLayout; private final Context mContext; private final TriggerConnector mRootConnector; + private final FragmentManager mFragmentManager; - public TriggerListAdapter(Context context, LinearLayout rootLayout, TriggerConnector rootTrigger) { + public TriggerListAdapter(Context context, FragmentManager fragmentManager, LinearLayout rootLayout, TriggerConnector rootTrigger) { mRootLayout = rootLayout; mContext = context; + mFragmentManager = fragmentManager; mRootConnector = rootTrigger; build(); } @@ -151,7 +142,7 @@ public class AutomationFragment extends SubscriberFragment { } // trigger layout - mRootLayout.addView(trigger.createView(mContext)); + mRootLayout.addView(trigger.createView(mContext, mFragmentManager)); // buttons createButtons(trigger); @@ -162,8 +153,7 @@ public class AutomationFragment extends SubscriberFragment { buttonAdd.setText("Add New"); buttonAdd.setOnClickListener(v -> { ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance(); - FragmentManager manager = AutomationFragment.fragmentManager(); - dialog.show(manager, "ChooseTriggerDialog"); + dialog.show(mFragmentManager, "ChooseTriggerDialog"); dialog.setOnClickListener(newTriggerObject -> { mRootConnector.add(newTriggerObject); rebuild(); @@ -235,8 +225,7 @@ public class AutomationFragment extends SubscriberFragment { buttonAdd.setText("add"); buttonAdd.setOnClickListener(v -> { ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance(); - FragmentManager manager = AutomationFragment.fragmentManager(); - dialog.show(manager, "ChooseTriggerDialog"); + dialog.show(mFragmentManager, "ChooseTriggerDialog"); dialog.setOnClickListener(newTriggerObject -> { TriggerConnector connector = trigger.getConnector(); connector.add(connector.pos(trigger)+1, newTriggerObject); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.java index a9c6d6ed0e..a2d5304ce0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.java @@ -52,7 +52,7 @@ public class EditEventDialog extends DialogFragment { mEvent.setTrigger(rootTrigger); // display root trigger - mLayoutTrigger.addView(rootTrigger.createView(getContext())); + mLayoutTrigger.addView(rootTrigger.createView(getContext(), getFragmentManager())); return view; } @@ -71,7 +71,6 @@ public class EditEventDialog extends DialogFragment { mEvent.setTitle(title); - final AutomationPlugin plugin = AutomationPlugin.getPlugin(); plugin.getAutomationEvents().add(mEvent); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java index 32f758222c..cfc3774d21 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.automation.triggers; +import android.support.v4.app.FragmentManager; import android.content.Context; import android.support.annotation.StringRes; import android.view.View; @@ -111,7 +112,7 @@ public abstract class Trigger { return null; } - public View createView(Context context) { + public View createView(Context context, FragmentManager fragmentManager) { final int padding = MainApp.dpToPx(4); LinearLayout root = new LinearLayout(context); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java index f82b1d9333..44697c1b19 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java @@ -1,11 +1,13 @@ package info.nightscout.androidaps.plugins.general.automation.triggers; +import android.support.v4.app.FragmentManager; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.Spinner; @@ -26,9 +28,9 @@ import info.nightscout.utils.NumberPicker; public class TriggerBg extends Trigger { - protected double threshold; - protected Comparator comparator = Comparator.IS_EQUAL; - protected String units = ProfileFunctions.getInstance().getProfileUnits(); + private double threshold = 100.0; // FIXME + private Comparator comparator = Comparator.IS_EQUAL; + private String units = ProfileFunctions.getInstance().getProfileUnits(); final private TextWatcher textWatcher = new TextWatcher() { @Override @@ -107,7 +109,7 @@ public class TriggerBg extends Trigger { if (comparator.equals(Comparator.IS_NOT_AVAILABLE)) return MainApp.gs(R.string.glucoseisnotavailable); else - return MainApp.gs(R.string.glucosecompared, comparator.getStringRes(), threshold, units); + return MainApp.gs(R.string.glucosecompared, MainApp.gs(comparator.getStringRes()), threshold, units); } @Override @@ -131,8 +133,8 @@ public class TriggerBg extends Trigger { } @Override - public View createView(Context context) { - LinearLayout root = (LinearLayout) super.createView(context); + public View createView(Context context, FragmentManager fragmentManager) { + LinearLayout root = (LinearLayout) super.createView(context, fragmentManager); // spinner for comparator Spinner spinner = new Spinner(context); @@ -145,6 +147,16 @@ public class TriggerBg extends Trigger { ); spinnerParams.setMargins(0, MainApp.dpToPx(4), 0, MainApp.dpToPx(4)); spinner.setLayoutParams(spinnerParams); + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + comparator = Comparator.values()[position]; + } + + @Override + public void onNothingSelected(AdapterView parent) { } + }); + spinner.setSelection(comparator.ordinal()); root.addView(spinner); // horizontal layout @@ -156,7 +168,8 @@ public class TriggerBg extends Trigger { // input filed for threshold NumberPicker numberPicker = new NumberPicker(context, null); numberPicker.setParams(0d, 0d, (double) 500, 1d, new DecimalFormat("0"), false, textWatcher); - numberPicker.setValue(100.0); + numberPicker.setValue(threshold); + numberPicker.setOnValueChangedListener(value -> threshold = value); layout.addView(numberPicker); // text view for unit diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java index 184f38c36b..b4268c868e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.automation.triggers; +import android.support.v4.app.FragmentManager; import android.content.Context; import android.support.annotation.StringRes; import android.view.View; @@ -163,7 +164,7 @@ public class TriggerConnector extends Trigger { int counter = 0; StringBuilder result = new StringBuilder(); for (Trigger t : list) { - if (counter++ > 0) result.append(friendlyName()); + if (counter++ > 0) result.append(" " + MainApp.gs(friendlyName()) + " "); result.append(t.friendlyDescription()); } return result.toString(); @@ -182,7 +183,7 @@ public class TriggerConnector extends Trigger { } @Override - public View createView(Context context) { + public View createView(Context context, FragmentManager fragmentManager) { final int padding = MainApp.dpToPx(5); LinearLayout root = new LinearLayout(context); @@ -196,7 +197,7 @@ public class TriggerConnector extends Trigger { triggerListLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); root.addView(triggerListLayout); - adapter = new AutomationFragment.TriggerListAdapter(context, triggerListLayout, this); + adapter = new AutomationFragment.TriggerListAdapter(context, fragmentManager, triggerListLayout, this); return root; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java index 1bb928fa6f..6fc34d489a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.automation.triggers; +import android.support.v4.app.FragmentManager; import android.content.Context; import android.support.annotation.StringRes; import android.view.View; @@ -268,8 +269,8 @@ public class TriggerTime extends Trigger { } @Override - public View createView(Context context) { - LinearLayout root = (LinearLayout) super.createView(context); + public View createView(Context context, FragmentManager fragmentManager) { + LinearLayout root = (LinearLayout) super.createView(context, fragmentManager); // TODO: Replace external tool WeekdaysPicker with a self-made GUI element WeekdaysPicker weekdaysPicker = new WeekdaysPicker(context); diff --git a/app/src/main/java/info/nightscout/utils/NumberPicker.java b/app/src/main/java/info/nightscout/utils/NumberPicker.java index 102abedc7d..e85a712625 100644 --- a/app/src/main/java/info/nightscout/utils/NumberPicker.java +++ b/app/src/main/java/info/nightscout/utils/NumberPicker.java @@ -34,6 +34,10 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, View.OnTouchListener, View.OnClickListener { private static Logger log = LoggerFactory.getLogger(NumberPicker.class); + public interface OnValueChangedListener { + void onValueChanged(double value); + } + TextView editText; Button minusButton; Button plusButton; @@ -48,6 +52,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, private Handler mHandler; private ScheduledExecutorService mUpdater; + private OnValueChangedListener mOnValueChangedListener; private class UpdateCounterTask implements Runnable { private boolean mInc; @@ -139,10 +144,15 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, @Override public void afterTextChanged(Editable s) { value = SafeParse.stringToDouble(editText.getText().toString()); + callValueChangedListener(); } }); } + public void setOnValueChangedListener(OnValueChangedListener onValueChangedListener) { + mOnValueChangedListener = onValueChangedListener; + } + public void setTextWatcher(TextWatcher textWatcher) { this.textWatcher = textWatcher; editText.addTextChangedListener(textWatcher); @@ -164,6 +174,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, this.step = step; this.formater = formater; this.allowZero = allowZero; + callValueChangedListener(); editText.setKeyListener(DigitsKeyListener.getInstance(minValue < 0, step != Math.rint(step))); @@ -178,6 +189,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, if (textWatcher != null) editText.removeTextChangedListener(textWatcher); this.value = value; + callValueChangedListener(); updateEditText(); if (textWatcher != null) editText.addTextChangedListener(textWatcher); @@ -199,6 +211,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, value += step * multiplier; if (value > maxValue) { value = maxValue; + callValueChangedListener(); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.youareonallowedlimit)); stopUpdating(); } @@ -209,6 +222,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, value -= step * multiplier; if (value < minValue) { value = minValue; + callValueChangedListener(); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.youareonallowedlimit)); stopUpdating(); } @@ -222,6 +236,11 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener, editText.setText(formater.format(value)); } + private void callValueChangedListener() { + if (mOnValueChangedListener != null) + mOnValueChangedListener.onValueChanged(value); + } + private void startUpdating(boolean inc) { if (mUpdater != null) { log.debug("Another executor is still active");