From b5f30a0f462c7ed78333a9884a998c8d95722e39 Mon Sep 17 00:00:00 2001 From: Nico Schmitz Date: Wed, 19 Sep 2018 22:29:35 +0200 Subject: [PATCH] Add some content to automation plugin fragment --- .../automation/AutomationFragment.java | 78 ++++++++++++++++++- .../general/automation/AutomationPlugin.java | 24 +++++- .../automation/dialogs/EditEventDialog.java | 16 ++++ .../main/res/drawable/ic_add_black_24dp.xml | 9 +++ .../main/res/layout/automation_event_item.xml | 20 +++++ .../main/res/layout/automation_fragment.xml | 25 +++++- 6 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.java create mode 100644 app/src/main/res/drawable/ic_add_black_24dp.xml create mode 100644 app/src/main/res/layout/automation_event_item.xml 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 0e9eccd84e..7dce538747 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 @@ -1,24 +1,100 @@ package info.nightscout.androidaps.plugins.general.automation; +import android.app.Activity; import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.design.widget.FloatingActionButton; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.TextView; + +import java.util.List; import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.Common.SubscriberFragment; +import info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog; public class AutomationFragment extends SubscriberFragment { + private RecyclerView mEventListView; + private FloatingActionButton mFabAddEvent; + private EventListAdapter mEventListAdapter; + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.automation_fragment, container, false); + + mEventListView = view.findViewById(R.id.eventListView); + mFabAddEvent = view.findViewById(R.id.fabAddEvent); + + mFabAddEvent.setOnClickListener(this::onClickAddEvent); + + final AutomationPlugin plugin = AutomationPlugin.getPlugin(); + mEventListAdapter = new EventListAdapter(plugin.getAutomationEvents()); + mEventListView.setLayoutManager(new LinearLayoutManager(getContext())); + mEventListView.setAdapter(mEventListAdapter); + + updateGUI(); + return view; } @Override protected void updateGUI() { - + Activity activity = getActivity(); + if (activity != null) + activity.runOnUiThread(() -> { + mEventListAdapter.notifyDataSetChanged(); + }); } + + private void onClickAddEvent(View v) { + /*EditEventDialog dialog = EditEventDialog.newInstance(); + FragmentManager manager = getFragmentManager(); + dialog.show(manager, "EditEventDialog");*/ + + final AutomationPlugin plugin = AutomationPlugin.getPlugin(); + plugin.getAutomationEvents().add(new AutomationEvent("Test")); + updateGUI(); + } + + public static class EventListAdapter extends RecyclerView.Adapter { + private final List mEvents; + + EventListAdapter(List events) { + this.mEvents = events; + } + + @NonNull + @Override + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.automation_event_item, parent, false); + return new ViewHolder(v); + } + + @Override + public void onBindViewHolder(@NonNull ViewHolder holder, int position) { + final AutomationEvent event = mEvents.get(position); + holder.eventTitle.setText(event.getTitle()); + } + + @Override + public int getItemCount() { + return mEvents.size(); + } + + static class ViewHolder extends RecyclerView.ViewHolder { + TextView eventTitle; + + public ViewHolder(View itemView) { + super(itemView); + eventTitle = itemView.findViewById(R.id.viewEventTitle); + } + } + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.java index 22526a50df..aef640129c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.java @@ -31,10 +31,10 @@ public class AutomationPlugin extends PluginBase { return plugin; } - List automationEvents = new ArrayList<>(); - EventLocationChange eventLocationChange; - EventChargingState eventChargingState; - EventNetworkChange eventNetworkChange; + private final List automationEvents = new ArrayList<>(); + private EventLocationChange eventLocationChange; + private EventChargingState eventChargingState; + private EventNetworkChange eventNetworkChange; private AutomationPlugin() { super(new PluginDescription() @@ -64,6 +64,22 @@ public class AutomationPlugin extends PluginBase { MainApp.bus().unregister(this); } + public List getAutomationEvents() { + return automationEvents; + } + + public EventLocationChange getEventLocationChange() { + return eventLocationChange; + } + + public EventChargingState getEventChargingState() { + return eventChargingState; + } + + public EventNetworkChange getEventNetworkChange() { + return eventNetworkChange; + } + @Subscribe public void onEventPreferenceChange(EventPreferenceChange e) { if (e.isChanged(R.string.key_location)) { 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 new file mode 100644 index 0000000000..c7ab2dbf7b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.java @@ -0,0 +1,16 @@ +package info.nightscout.androidaps.plugins.general.automation.dialogs; + +import android.os.Bundle; +import android.support.v4.app.DialogFragment; + +public class EditEventDialog extends DialogFragment { + public static EditEventDialog newInstance() { + + Bundle args = new Bundle(); + + EditEventDialog fragment = new EditEventDialog(); + fragment.setArguments(args); + + return fragment; + } +} diff --git a/app/src/main/res/drawable/ic_add_black_24dp.xml b/app/src/main/res/drawable/ic_add_black_24dp.xml new file mode 100644 index 0000000000..0258249cc4 --- /dev/null +++ b/app/src/main/res/drawable/ic_add_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/automation_event_item.xml b/app/src/main/res/layout/automation_event_item.xml new file mode 100644 index 0000000000..92c9ba93bc --- /dev/null +++ b/app/src/main/res/layout/automation_event_item.xml @@ -0,0 +1,20 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/automation_fragment.xml b/app/src/main/res/layout/automation_fragment.xml index 64cdf916fb..4b6ee5c71d 100644 --- a/app/src/main/res/layout/automation_fragment.xml +++ b/app/src/main/res/layout/automation_fragment.xml @@ -1,9 +1,32 @@ + + + + + + +