Add ChooseTriggerDialog

This commit is contained in:
Nico Schmitz 2018-10-16 00:07:13 +02:00
parent f98d40f71b
commit 2e4f18794f
4 changed files with 159 additions and 1 deletions

View file

@ -31,6 +31,12 @@ 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;
@ -39,6 +45,9 @@ public class AutomationFragment extends SubscriberFragment {
@Override
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);

View file

@ -0,0 +1,114 @@
package info.nightscout.androidaps.plugins.general.automation.dialogs;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBg;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime;
public class ChooseTriggerDialog extends DialogFragment {
public interface OnClickListener {
void onClick(Trigger newTriggerObject);
}
private static final List<Trigger> triggerDummyObjects = new ArrayList<Trigger>() {{
add(new TriggerBg());
add(new TriggerTime());
}};
private Unbinder mUnbinder;
private OnClickListener mClickListener = null;
@BindView(R.id.radioGroup)
RadioGroup mRadioGroup;
public static ChooseTriggerDialog newInstance() {
Bundle args = new Bundle();
ChooseTriggerDialog fragment = new ChooseTriggerDialog();
fragment.setArguments(args);
return fragment;
}
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.automation_dialog_trigger, container, false);
mUnbinder = ButterKnife.bind(this, view);
for(Trigger t : triggerDummyObjects) {
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText(t.friendlyName());
radioButton.setTag(t);
mRadioGroup.addView(radioButton);
}
((RadioButton)mRadioGroup.getChildAt(0)).setChecked(true);
return view;
}
private Class getTriggerClass() {
int radioButtonID = mRadioGroup.getCheckedRadioButtonId();
RadioButton radioButton = mRadioGroup.findViewById(radioButtonID);
if (radioButton != null) {
Object tag = radioButton.getTag();
if (tag instanceof Trigger)
return tag.getClass();
}
return null;
}
private Trigger instantiateTrigger() {
Class triggerClass = getTriggerClass();
if (triggerClass != null) {
try {
return (Trigger) triggerClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public void setOnClickListener(OnClickListener clickListener) {
mClickListener = clickListener;
}
@Override
public void onDestroyView() {
mUnbinder.unbind();
super.onDestroyView();
}
@OnClick(R.id.ok)
public void onButtonOk(View view) {
if (mClickListener != null)
mClickListener.onClick(instantiateTrigger());
dismiss();
}
@OnClick(R.id.cancel)
public void onButtonCancel(View view) {
dismiss();
}
}

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.general.automation.triggers;
import android.content.Context;
import android.support.annotation.StringRes;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
@ -17,6 +18,7 @@ import java.util.List;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.general.automation.AutomationFragment;
import info.nightscout.androidaps.plugins.general.automation.dialogs.ChooseTriggerDialog;
import info.nightscout.utils.JsonHelper;
public class TriggerConnector extends Trigger {
@ -207,7 +209,10 @@ public class TriggerConnector extends Trigger {
Button buttonAdd = new Button(context);
buttonAdd.setText("+");
buttonAdd.setOnClickListener(v -> {
addTrigger(adapter, new TriggerTime(), getConnectorType());
ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance();
FragmentManager manager = AutomationFragment.fragmentManager();
dialog.show(manager, "ChooseTriggerDialog");
dialog.setOnClickListener(newTriggerObject -> addTrigger(adapter, newTriggerObject, getConnectorType()));
});
buttonLayout.addView(buttonAdd);

View file

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="10dp">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include layout="@layout/mdtp_done_button" />
</LinearLayout>
</ScrollView>
</FrameLayout>