allow disable automation event

This commit is contained in:
Milos Kozak 2019-08-23 21:48:00 +02:00
parent ac8dffe44c
commit 6d91938b3f
4 changed files with 50 additions and 22 deletions

View file

@ -16,6 +16,7 @@ public class AutomationEvent {
private Trigger trigger = new TriggerConnector(); private Trigger trigger = new TriggerConnector();
private List<Action> actions = new ArrayList<>(); private List<Action> actions = new ArrayList<>();
private String title; private String title;
private boolean enabled = true;
public void setTitle(String title) { public void setTitle(String title) {
this.title = title; this.title = title;
@ -33,6 +34,14 @@ public class AutomationEvent {
return actions; return actions;
} }
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean newState) {
enabled = newState;
}
public TriggerConnector getPreconditions() { public TriggerConnector getPreconditions() {
TriggerConnector trigger = new TriggerConnector(TriggerConnector.Type.AND); TriggerConnector trigger = new TriggerConnector(TriggerConnector.Type.AND);
for (Action action : actions) { for (Action action : actions) {
@ -55,6 +64,7 @@ public class AutomationEvent {
try { try {
// title // title
o.put("title", title); o.put("title", title);
o.put("enabled", enabled);
// trigger // trigger
o.put("trigger", trigger.toJSON()); o.put("trigger", trigger.toJSON());
// actions // actions
@ -72,11 +82,9 @@ public class AutomationEvent {
public AutomationEvent fromJSON(String data) { public AutomationEvent fromJSON(String data) {
try { try {
JSONObject d = new JSONObject(data); JSONObject d = new JSONObject(data);
// title
title = d.optString("title", ""); title = d.optString("title", "");
// trigger enabled = d.optBoolean("enabled", true);
trigger = Trigger.instantiate(d.getString("trigger")); trigger = Trigger.instantiate(d.getString("trigger"));
// actions
JSONArray array = d.getJSONArray("actions"); JSONArray array = d.getJSONArray("actions");
actions.clear(); actions.clear();
for (int i = 0; i < array.length(); i++) { for (int i = 0; i < array.length(); i++) {

View file

@ -171,7 +171,7 @@ object AutomationPlugin : PluginBase(PluginDescription()
if (L.isEnabled(L.AUTOMATION)) if (L.isEnabled(L.AUTOMATION))
log.debug("processActions") log.debug("processActions")
for (event in automationEvents) { for (event in automationEvents) {
if (event.trigger.shouldRun() && event.preconditions.shouldRun()) { if (event.isEnabled() && event.trigger.shouldRun() && event.preconditions.shouldRun()) {
val actions = event.actions val actions = event.actions
for (action in actions) { for (action in actions) {
action.doAction(object : Callback() { action.doAction(object : Callback() {

View file

@ -5,6 +5,7 @@ import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
@ -20,8 +21,10 @@ import java.util.List;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.general.automation.actions.Action; import info.nightscout.androidaps.plugins.general.automation.actions.Action;
import info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog; import info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog;
import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationDataChanged;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector;
class EventListAdapter extends RecyclerView.Adapter<EventListAdapter.ViewHolder> { class EventListAdapter extends RecyclerView.Adapter<EventListAdapter.ViewHolder> {
@ -51,6 +54,7 @@ class EventListAdapter extends RecyclerView.Adapter<EventListAdapter.ViewHolder>
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final AutomationEvent event = mEventList.get(position); final AutomationEvent event = mEventList.get(position);
holder.eventTitle.setText(event.getTitle()); holder.eventTitle.setText(event.getTitle());
holder.enabled.setChecked(event.isEnabled());
holder.iconLayout.removeAllViews(); holder.iconLayout.removeAllViews();
// trigger icons // trigger icons
@ -77,6 +81,13 @@ class EventListAdapter extends RecyclerView.Adapter<EventListAdapter.ViewHolder>
addImage(res, holder.context, holder.iconLayout); addImage(res, holder.context, holder.iconLayout);
} }
// enabled event
holder.enabled.setOnCheckedChangeListener((buttonView, isChecked) -> {
event.setEnabled(isChecked);
notifyDataSetChanged();
RxBus.INSTANCE.send(new EventAutomationDataChanged());
});
// remove event // remove event
holder.iconTrash.setOnClickListener(v -> { holder.iconTrash.setOnClickListener(v -> {
mEventList.remove(event); mEventList.remove(event);
@ -107,6 +118,7 @@ class EventListAdapter extends RecyclerView.Adapter<EventListAdapter.ViewHolder>
final TextView eventTitle; final TextView eventTitle;
final Context context; final Context context;
final ImageView iconTrash; final ImageView iconTrash;
final CheckBox enabled;
ViewHolder(View view, Context context) { ViewHolder(View view, Context context) {
super(view); super(view);
@ -115,6 +127,7 @@ class EventListAdapter extends RecyclerView.Adapter<EventListAdapter.ViewHolder>
rootLayout = view.findViewById(R.id.rootLayout); rootLayout = view.findViewById(R.id.rootLayout);
iconLayout = view.findViewById(R.id.iconLayout); iconLayout = view.findViewById(R.id.iconLayout);
iconTrash = view.findViewById(R.id.iconTrash); iconTrash = view.findViewById(R.id.iconTrash);
enabled = view.findViewById(R.id.automation_enabled);
} }
} }
} }

View file

@ -11,6 +11,27 @@
android:background="@color/ribbonDefault" android:background="@color/ribbonDefault"
android:padding="8dp"> android:padding="8dp">
<CheckBox
android:id="@+id/automation_enabled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/viewEventTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/automation_enabled"
android:layout_alignBottom="@+id/automation_enabled"
android:layout_centerVertical="true"
android:layout_marginTop="6dp"
android:layout_toStartOf="@+id/iconTrash"
android:layout_toEndOf="@id/automation_enabled"
android:text="Title"
android:textAlignment="viewStart"
android:textStyle="bold" />
<ImageView <ImageView
android:id="@+id/iconTrash" android:id="@+id/iconTrash"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -22,25 +43,11 @@
android:src="@drawable/ic_trash_outline" /> android:src="@drawable/ic_trash_outline" />
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:id="@+id/iconLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentStart="true" android:layout_below="@id/automation_enabled"
android:layout_alignParentTop="true" android:orientation="horizontal" />
android:layout_toStartOf="@id/iconTrash"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/viewEventTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/iconLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</LinearLayout>
</RelativeLayout> </RelativeLayout>