From 0060a2ba7f5676ddc76e501b5663cfebd05d50be Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 9 May 2018 15:10:47 +0200 Subject: [PATCH] SW show pump status --- .../startupwizard/SWDefinition.java | 36 ++++++++---- .../startupwizard/SWEventListener.java | 55 +++++++++++++++++++ .../androidaps/startupwizard/SWItem.java | 1 + .../startupwizard/SetupWizardActivity.java | 10 ++-- .../startupwizard/events/EventSWLabel.java | 11 ++++ 5 files changed, 96 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/startupwizard/SWEventListener.java create mode 100644 app/src/main/java/info/nightscout/androidaps/startupwizard/events/EventSWLabel.java diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java index 256b7db870..69e799671b 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java @@ -1,8 +1,10 @@ package info.nightscout.androidaps.startupwizard; -import android.content.Context; +import android.app.Activity; import android.content.Intent; +import com.squareup.otto.Subscribe; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,13 +15,13 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.PreferencesActivity; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventConfigBuilderChange; -import info.nightscout.androidaps.events.EventRefreshGui; +import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin; +import info.nightscout.androidaps.startupwizard.events.EventSWLabel; import info.nightscout.androidaps.startupwizard.events.EventSWUpdate; import info.nightscout.utils.LocaleHelper; import info.nightscout.utils.PasswordProtection; @@ -28,14 +30,18 @@ import info.nightscout.utils.SP; public class SWDefinition { private static Logger log = LoggerFactory.getLogger(SWDefinition.class); - private Context context; - static List screens = new ArrayList<>(); + private Activity activity; + private List screens = new ArrayList<>(); - public void setContext(Context context) { - this.context = context; + public void setActivity(Activity activity) { + this.activity = activity; } - public static List getScreens() { + public Activity getActivity() { + return activity; + } + + public List getScreens() { return screens; } @@ -118,10 +124,10 @@ public class SWDefinition { .text(R.string.pumpsetup) .action(() -> { final PluginBase plugin = (PluginBase) MainApp.getConfigBuilder().getActivePump(); - PasswordProtection.QueryPassword(context, R.string.settings_password, "settings_password", () -> { - Intent i = new Intent(context, PreferencesActivity.class); + PasswordProtection.QueryPassword(activity, R.string.settings_password, "settings_password", () -> { + Intent i = new Intent(activity, PreferencesActivity.class); i.putExtra("id", plugin.getPreferencesId()); - context.startActivity(i); + activity.startActivity(i); }, null); }) .visibility(() -> ((PluginBase) MainApp.getConfigBuilder().getActivePump()).getPreferencesId() > 0)) @@ -129,6 +135,14 @@ public class SWDefinition { .text(R.string.readstatus) .action(() -> ConfigBuilderPlugin.getCommandQueue().readStatus("Clicked connect to pump", null)) .visibility(() -> MainApp.getSpecificPluginsList(PluginType.PUMP) != null)) + .add(new SWEventListener(this) + .listener(new Object() { + @Subscribe + public void onEventPumpStatusChanged(EventPumpStatusChanged event) { + MainApp.bus().post(new EventSWLabel(event.textStatus())); + } + }) + ) .validator(() -> MainApp.getSpecificPluginsList(PluginType.PUMP) != null && MainApp.getConfigBuilder().getActivePump().isInitialized()) ) .add(new SWScreen(R.string.configbuilder_aps) diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWEventListener.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWEventListener.java new file mode 100644 index 0000000000..b5694ebe84 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWEventListener.java @@ -0,0 +1,55 @@ +package info.nightscout.androidaps.startupwizard; + +import android.content.Context; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.squareup.otto.Subscribe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.startupwizard.events.EventSWLabel; + + +public class SWEventListener extends SWItem { + private static Logger log = LoggerFactory.getLogger(SWEventListener.class); + + TextView textView; + Object listener; + SWDefinition definition; + + SWEventListener(SWDefinition definition) { + super(Type.LISTENER); + this.definition = definition; + MainApp.bus().register(this); + } + + public SWEventListener listener(Object listener) { + this.listener = listener; + return this; + } + + @Override + public void generateDialog(View view, LinearLayout layout) { + Context context = view.getContext(); + + textView = new TextView(context); + textView.setId(view.generateViewId()); + layout.addView(textView); + if (listener != null) + MainApp.bus().register(listener); + } + + @Subscribe + public void onEventSWLabel(final EventSWLabel l) { + if (definition != null && definition.getActivity() != null) + definition.getActivity().runOnUiThread(() -> { + if (textView != null) + textView.setText(l.label); + }); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java index 7d6efd8d59..5741de25b7 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java @@ -17,6 +17,7 @@ public class SWItem { enum Type { NONE, TEXT, + LISTENER, URL, STRING, NUMBER, diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java index c2336bfcc4..e41a465c73 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java @@ -26,10 +26,8 @@ public class SetupWizardActivity extends AppCompatActivity { //logging private static Logger log = LoggerFactory.getLogger(SetupWizardActivity.class); - private TextView screenName; - - SWDefinition swDefinition = new SWDefinition(); - List screens = swDefinition.getScreens(); + private SWDefinition swDefinition = new SWDefinition(); + private List screens = swDefinition.getScreens(); private int currentWizardPage = 0; public static final String INTENT_MESSAGE = "WIZZARDPAGE"; @@ -45,7 +43,7 @@ public class SetupWizardActivity extends AppCompatActivity { SWScreen currentScreen = screens.get(currentWizardPage); //Set screen name - screenName = (TextView) findViewById(R.id.sw_content); + TextView screenName = (TextView) findViewById(R.id.sw_content); screenName.setText(currentScreen.getHeader()); //Generate layout first @@ -64,7 +62,7 @@ public class SetupWizardActivity extends AppCompatActivity { protected void onResume() { super.onResume(); MainApp.bus().register(this); - swDefinition.setContext(this); + swDefinition.setActivity(this); } @Subscribe diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/events/EventSWLabel.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/events/EventSWLabel.java new file mode 100644 index 0000000000..3d9bd86cc8 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/events/EventSWLabel.java @@ -0,0 +1,11 @@ +package info.nightscout.androidaps.startupwizard.events; + +import info.nightscout.androidaps.events.Event; + +public class EventSWLabel extends Event { + public String label; + + public EventSWLabel(String label) { + this.label = label; + } +}