Refactor code. Some minor fixes.

This commit is contained in:
Nico Schmitz 2018-10-25 22:08:04 +02:00
parent 7698923fe8
commit 0364d3c039
7 changed files with 57 additions and 34 deletions

View file

@ -32,12 +32,6 @@ import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerCon
public class AutomationFragment extends SubscriberFragment { public class AutomationFragment extends SubscriberFragment {
public static FragmentManager fragmentManager() {
return mFragmentManager;
}
private static FragmentManager mFragmentManager = null;
@BindView(R.id.eventListView) @BindView(R.id.eventListView)
RecyclerView mEventListView; RecyclerView mEventListView;
@ -47,8 +41,6 @@ public class AutomationFragment extends SubscriberFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
mFragmentManager = getFragmentManager();
View view = inflater.inflate(R.layout.automation_fragment, container, false); View view = inflater.inflate(R.layout.automation_fragment, container, false);
unbinder = ButterKnife.bind(this, view); unbinder = ButterKnife.bind(this, view);
@ -66,16 +58,13 @@ public class AutomationFragment extends SubscriberFragment {
public void updateGUI() { public void updateGUI() {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null)
activity.runOnUiThread(() -> { activity.runOnUiThread(() -> mEventListAdapter.notifyDataSetChanged());
mEventListAdapter.notifyDataSetChanged();
});
} }
@OnClick(R.id.fabAddEvent) @OnClick(R.id.fabAddEvent)
void onClickAddEvent(View v) { void onClickAddEvent(View v) {
EditEventDialog dialog = EditEventDialog.newInstance(new AutomationEvent()); EditEventDialog dialog = EditEventDialog.newInstance(new AutomationEvent());
FragmentManager manager = getFragmentManager(); dialog.show(getFragmentManager(), "EditEventDialog");
dialog.show(manager, "EditEventDialog");
} }
/** /**
@ -128,10 +117,12 @@ public class AutomationFragment extends SubscriberFragment {
private final LinearLayout mRootLayout; private final LinearLayout mRootLayout;
private final Context mContext; private final Context mContext;
private final TriggerConnector mRootConnector; 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; mRootLayout = rootLayout;
mContext = context; mContext = context;
mFragmentManager = fragmentManager;
mRootConnector = rootTrigger; mRootConnector = rootTrigger;
build(); build();
} }
@ -151,7 +142,7 @@ public class AutomationFragment extends SubscriberFragment {
} }
// trigger layout // trigger layout
mRootLayout.addView(trigger.createView(mContext)); mRootLayout.addView(trigger.createView(mContext, mFragmentManager));
// buttons // buttons
createButtons(trigger); createButtons(trigger);
@ -162,8 +153,7 @@ public class AutomationFragment extends SubscriberFragment {
buttonAdd.setText("Add New"); buttonAdd.setText("Add New");
buttonAdd.setOnClickListener(v -> { buttonAdd.setOnClickListener(v -> {
ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance(); ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance();
FragmentManager manager = AutomationFragment.fragmentManager(); dialog.show(mFragmentManager, "ChooseTriggerDialog");
dialog.show(manager, "ChooseTriggerDialog");
dialog.setOnClickListener(newTriggerObject -> { dialog.setOnClickListener(newTriggerObject -> {
mRootConnector.add(newTriggerObject); mRootConnector.add(newTriggerObject);
rebuild(); rebuild();
@ -235,8 +225,7 @@ public class AutomationFragment extends SubscriberFragment {
buttonAdd.setText("add"); buttonAdd.setText("add");
buttonAdd.setOnClickListener(v -> { buttonAdd.setOnClickListener(v -> {
ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance(); ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance();
FragmentManager manager = AutomationFragment.fragmentManager(); dialog.show(mFragmentManager, "ChooseTriggerDialog");
dialog.show(manager, "ChooseTriggerDialog");
dialog.setOnClickListener(newTriggerObject -> { dialog.setOnClickListener(newTriggerObject -> {
TriggerConnector connector = trigger.getConnector(); TriggerConnector connector = trigger.getConnector();
connector.add(connector.pos(trigger)+1, newTriggerObject); connector.add(connector.pos(trigger)+1, newTriggerObject);

View file

@ -52,7 +52,7 @@ public class EditEventDialog extends DialogFragment {
mEvent.setTrigger(rootTrigger); mEvent.setTrigger(rootTrigger);
// display root trigger // display root trigger
mLayoutTrigger.addView(rootTrigger.createView(getContext())); mLayoutTrigger.addView(rootTrigger.createView(getContext(), getFragmentManager()));
return view; return view;
} }
@ -71,7 +71,6 @@ public class EditEventDialog extends DialogFragment {
mEvent.setTitle(title); mEvent.setTitle(title);
final AutomationPlugin plugin = AutomationPlugin.getPlugin(); final AutomationPlugin plugin = AutomationPlugin.getPlugin();
plugin.getAutomationEvents().add(mEvent); plugin.getAutomationEvents().add(mEvent);

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.general.automation.triggers; package info.nightscout.androidaps.plugins.general.automation.triggers;
import android.support.v4.app.FragmentManager;
import android.content.Context; import android.content.Context;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.view.View; import android.view.View;
@ -111,7 +112,7 @@ public abstract class Trigger {
return null; return null;
} }
public View createView(Context context) { public View createView(Context context, FragmentManager fragmentManager) {
final int padding = MainApp.dpToPx(4); final int padding = MainApp.dpToPx(4);
LinearLayout root = new LinearLayout(context); LinearLayout root = new LinearLayout(context);

View file

@ -1,11 +1,13 @@
package info.nightscout.androidaps.plugins.general.automation.triggers; package info.nightscout.androidaps.plugins.general.automation.triggers;
import android.support.v4.app.FragmentManager;
import android.content.Context; import android.content.Context;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.Gravity; import android.view.Gravity;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Spinner; import android.widget.Spinner;
@ -26,9 +28,9 @@ import info.nightscout.utils.NumberPicker;
public class TriggerBg extends Trigger { public class TriggerBg extends Trigger {
protected double threshold; private double threshold = 100.0; // FIXME
protected Comparator comparator = Comparator.IS_EQUAL; private Comparator comparator = Comparator.IS_EQUAL;
protected String units = ProfileFunctions.getInstance().getProfileUnits(); private String units = ProfileFunctions.getInstance().getProfileUnits();
final private TextWatcher textWatcher = new TextWatcher() { final private TextWatcher textWatcher = new TextWatcher() {
@Override @Override
@ -107,7 +109,7 @@ public class TriggerBg extends Trigger {
if (comparator.equals(Comparator.IS_NOT_AVAILABLE)) if (comparator.equals(Comparator.IS_NOT_AVAILABLE))
return MainApp.gs(R.string.glucoseisnotavailable); return MainApp.gs(R.string.glucoseisnotavailable);
else else
return MainApp.gs(R.string.glucosecompared, comparator.getStringRes(), threshold, units); return MainApp.gs(R.string.glucosecompared, MainApp.gs(comparator.getStringRes()), threshold, units);
} }
@Override @Override
@ -131,8 +133,8 @@ public class TriggerBg extends Trigger {
} }
@Override @Override
public View createView(Context context) { public View createView(Context context, FragmentManager fragmentManager) {
LinearLayout root = (LinearLayout) super.createView(context); LinearLayout root = (LinearLayout) super.createView(context, fragmentManager);
// spinner for comparator // spinner for comparator
Spinner spinner = new Spinner(context); Spinner spinner = new Spinner(context);
@ -145,6 +147,16 @@ public class TriggerBg extends Trigger {
); );
spinnerParams.setMargins(0, MainApp.dpToPx(4), 0, MainApp.dpToPx(4)); spinnerParams.setMargins(0, MainApp.dpToPx(4), 0, MainApp.dpToPx(4));
spinner.setLayoutParams(spinnerParams); 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); root.addView(spinner);
// horizontal layout // horizontal layout
@ -156,7 +168,8 @@ public class TriggerBg extends Trigger {
// input filed for threshold // input filed for threshold
NumberPicker numberPicker = new NumberPicker(context, null); NumberPicker numberPicker = new NumberPicker(context, null);
numberPicker.setParams(0d, 0d, (double) 500, 1d, new DecimalFormat("0"), false, textWatcher); 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); layout.addView(numberPicker);
// text view for unit // text view for unit

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.general.automation.triggers; package info.nightscout.androidaps.plugins.general.automation.triggers;
import android.support.v4.app.FragmentManager;
import android.content.Context; import android.content.Context;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.view.View; import android.view.View;
@ -163,7 +164,7 @@ public class TriggerConnector extends Trigger {
int counter = 0; int counter = 0;
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
for (Trigger t : list) { for (Trigger t : list) {
if (counter++ > 0) result.append(friendlyName()); if (counter++ > 0) result.append(" " + MainApp.gs(friendlyName()) + " ");
result.append(t.friendlyDescription()); result.append(t.friendlyDescription());
} }
return result.toString(); return result.toString();
@ -182,7 +183,7 @@ public class TriggerConnector extends Trigger {
} }
@Override @Override
public View createView(Context context) { public View createView(Context context, FragmentManager fragmentManager) {
final int padding = MainApp.dpToPx(5); final int padding = MainApp.dpToPx(5);
LinearLayout root = new LinearLayout(context); 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)); triggerListLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
root.addView(triggerListLayout); root.addView(triggerListLayout);
adapter = new AutomationFragment.TriggerListAdapter(context, triggerListLayout, this); adapter = new AutomationFragment.TriggerListAdapter(context, fragmentManager, triggerListLayout, this);
return root; return root;
} }

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.plugins.general.automation.triggers; package info.nightscout.androidaps.plugins.general.automation.triggers;
import android.support.v4.app.FragmentManager;
import android.content.Context; import android.content.Context;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.view.View; import android.view.View;
@ -268,8 +269,8 @@ public class TriggerTime extends Trigger {
} }
@Override @Override
public View createView(Context context) { public View createView(Context context, FragmentManager fragmentManager) {
LinearLayout root = (LinearLayout) super.createView(context); LinearLayout root = (LinearLayout) super.createView(context, fragmentManager);
// TODO: Replace external tool WeekdaysPicker with a self-made GUI element // TODO: Replace external tool WeekdaysPicker with a self-made GUI element
WeekdaysPicker weekdaysPicker = new WeekdaysPicker(context); WeekdaysPicker weekdaysPicker = new WeekdaysPicker(context);

View file

@ -34,6 +34,10 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
View.OnTouchListener, View.OnClickListener { View.OnTouchListener, View.OnClickListener {
private static Logger log = LoggerFactory.getLogger(NumberPicker.class); private static Logger log = LoggerFactory.getLogger(NumberPicker.class);
public interface OnValueChangedListener {
void onValueChanged(double value);
}
TextView editText; TextView editText;
Button minusButton; Button minusButton;
Button plusButton; Button plusButton;
@ -48,6 +52,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
private Handler mHandler; private Handler mHandler;
private ScheduledExecutorService mUpdater; private ScheduledExecutorService mUpdater;
private OnValueChangedListener mOnValueChangedListener;
private class UpdateCounterTask implements Runnable { private class UpdateCounterTask implements Runnable {
private boolean mInc; private boolean mInc;
@ -139,10 +144,15 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
value = SafeParse.stringToDouble(editText.getText().toString()); value = SafeParse.stringToDouble(editText.getText().toString());
callValueChangedListener();
} }
}); });
} }
public void setOnValueChangedListener(OnValueChangedListener onValueChangedListener) {
mOnValueChangedListener = onValueChangedListener;
}
public void setTextWatcher(TextWatcher textWatcher) { public void setTextWatcher(TextWatcher textWatcher) {
this.textWatcher = textWatcher; this.textWatcher = textWatcher;
editText.addTextChangedListener(textWatcher); editText.addTextChangedListener(textWatcher);
@ -164,6 +174,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
this.step = step; this.step = step;
this.formater = formater; this.formater = formater;
this.allowZero = allowZero; this.allowZero = allowZero;
callValueChangedListener();
editText.setKeyListener(DigitsKeyListener.getInstance(minValue < 0, step != Math.rint(step))); 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) if (textWatcher != null)
editText.removeTextChangedListener(textWatcher); editText.removeTextChangedListener(textWatcher);
this.value = value; this.value = value;
callValueChangedListener();
updateEditText(); updateEditText();
if (textWatcher != null) if (textWatcher != null)
editText.addTextChangedListener(textWatcher); editText.addTextChangedListener(textWatcher);
@ -199,6 +211,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
value += step * multiplier; value += step * multiplier;
if (value > maxValue) { if (value > maxValue) {
value = maxValue; value = maxValue;
callValueChangedListener();
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.youareonallowedlimit)); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.youareonallowedlimit));
stopUpdating(); stopUpdating();
} }
@ -209,6 +222,7 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
value -= step * multiplier; value -= step * multiplier;
if (value < minValue) { if (value < minValue) {
value = minValue; value = minValue;
callValueChangedListener();
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.youareonallowedlimit)); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.youareonallowedlimit));
stopUpdating(); stopUpdating();
} }
@ -222,6 +236,11 @@ public class NumberPicker extends LinearLayout implements View.OnKeyListener,
editText.setText(formater.format(value)); editText.setText(formater.format(value));
} }
private void callValueChangedListener() {
if (mOnValueChangedListener != null)
mOnValueChangedListener.onValueChanged(value);
}
private void startUpdating(boolean inc) { private void startUpdating(boolean inc) {
if (mUpdater != null) { if (mUpdater != null) {
log.debug("Another executor is still active"); log.debug("Another executor is still active");