From 1aa8771f4d617869247839cd91d665e04d9db71f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 14 Oct 2019 23:49:53 +0200 Subject: [PATCH] Events to RxBus --- .../nightscout/androidaps/MainActivity.java | 8 +---- .../nightscout/androidaps/events/Event.java | 16 --------- .../nightscout/androidaps/events/Event.kt | 18 ++++++++++ .../events/EventAppInitialized.java | 8 ----- .../androidaps/events/EventAppInitialized.kt | 3 ++ .../events/EventBolusRequested.java | 21 ----------- .../androidaps/events/EventBolusRequested.kt | 3 ++ .../EventCustomCalculationFinished.java | 8 ----- .../events/EventCustomCalculationFinished.kt | 3 ++ .../events/EventFeatureRunning.java | 36 ------------------- .../configBuilder/ConfigBuilderPlugin.java | 3 +- .../general/overview/dialogs/WizardDialog.kt | 6 ---- .../plugins/general/wear/WearPlugin.java | 27 +++++++------- .../xdripStatusline/StatuslinePlugin.java | 17 ++++----- .../IobCobCalculatorPlugin.java | 34 +++++++++--------- .../androidaps/queue/CommandQueue.java | 2 +- 16 files changed, 68 insertions(+), 145 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/events/Event.java create mode 100644 app/src/main/java/info/nightscout/androidaps/events/Event.kt delete mode 100644 app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.java create mode 100644 app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.kt delete mode 100644 app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.java create mode 100644 app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.kt delete mode 100644 app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.java create mode 100644 app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.kt delete mode 100644 app/src/main/java/info/nightscout/androidaps/events/EventFeatureRunning.java diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index cca6fb3100..f3df901e2e 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -33,7 +33,6 @@ import com.google.android.material.navigation.NavigationView; import com.google.android.material.tabs.TabLayout; import com.joanzapata.iconify.Iconify; import com.joanzapata.iconify.fonts.FontAwesomeModule; -import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,7 +44,6 @@ import info.nightscout.androidaps.activities.PreferencesActivity; import info.nightscout.androidaps.activities.SingleFragmentActivity; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.events.EventAppExit; -import info.nightscout.androidaps.events.EventFeatureRunning; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.interfaces.PluginBase; @@ -192,11 +190,7 @@ public class MainActivity extends NoSplashAppCompatActivity { AndroidPermission.notifyForLocationPermissions(this); AndroidPermission.notifyForSMSPermissions(this); } - - MainApp.bus(). - - post(new EventFeatureRunning(EventFeatureRunning.Feature.MAIN)); - } + } @Override public void onDestroy() { diff --git a/app/src/main/java/info/nightscout/androidaps/events/Event.java b/app/src/main/java/info/nightscout/androidaps/events/Event.java deleted file mode 100644 index 864d55d6f7..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/Event.java +++ /dev/null @@ -1,16 +0,0 @@ -package info.nightscout.androidaps.events; - -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; - -/** Base class for all events posted on the event bus. */ -public abstract class Event { - static { - ReflectionToStringBuilder.setDefaultStyle(ToStringStyle.SHORT_PREFIX_STYLE); - } - - @Override - public String toString() { - return ReflectionToStringBuilder.toString(this); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/Event.kt b/app/src/main/java/info/nightscout/androidaps/events/Event.kt new file mode 100644 index 0000000000..a44f65e836 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/Event.kt @@ -0,0 +1,18 @@ +package info.nightscout.androidaps.events + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder +import org.apache.commons.lang3.builder.ToStringStyle + +/** Base class for all events posted on the event bus. */ +abstract class Event { + + override fun toString(): String { + return ReflectionToStringBuilder.toString(this) + } + + companion object { + init { + ReflectionToStringBuilder.setDefaultStyle(ToStringStyle.SHORT_PREFIX_STYLE) + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.java b/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.java deleted file mode 100644 index 17262cfb85..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 23.01.2018. - */ - -public class EventAppInitialized extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.kt b/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.kt new file mode 100644 index 0000000000..293f9698f2 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventAppInitialized.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventAppInitialized : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.java b/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.java deleted file mode 100644 index cb727758bb..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.java +++ /dev/null @@ -1,21 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by adrian on 07/02/17. - */ - -public class EventBolusRequested extends Event { - private double amount; - - public EventBolusRequested (double amount){ - this.amount = amount; - } - - public double getAmount() { - return amount; - } - - public void setAmount(double amount) { - this.amount = amount; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.kt b/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.kt new file mode 100644 index 0000000000..a528ef1656 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventBolusRequested.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventBolusRequested(var amount: Double) : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.java b/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.java deleted file mode 100644 index e52761dc58..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 13.02.2018. - */ - -public class EventCustomCalculationFinished extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.kt b/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.kt new file mode 100644 index 0000000000..f6092b395d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventCustomCalculationFinished.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventCustomCalculationFinished : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventFeatureRunning.java b/app/src/main/java/info/nightscout/androidaps/events/EventFeatureRunning.java deleted file mode 100644 index 0d07cd6c61..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventFeatureRunning.java +++ /dev/null @@ -1,36 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by jamorham on 07/02/2018. - * - * Event to indicate that an app feature is being used, for example bolus wizard being opened - * - * The purpose this has been created for is to enable opportunistic connection to the pump - * so that it is already connected before the user wishes to enact a pump function - * - */ - -public class EventFeatureRunning extends Event { - - private Feature feature = Feature.UNKNOWN; - - public EventFeatureRunning() { - } - - public EventFeatureRunning(Feature feature) { - this.feature = feature; - } - - public Feature getFeature() { - return feature; - } - - public enum Feature { - UNKNOWN, - MAIN, - WIZARD, - - JUST_ADD_MORE_HERE - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java index b73c535147..32ece49d49 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java @@ -20,6 +20,7 @@ import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.SensitivityInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.insulin.InsulinOrefRapidActingPlugin; import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin; import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin; @@ -83,7 +84,7 @@ public class ConfigBuilderPlugin extends PluginBase { upgradeSettings(); loadSettings(); setAlwaysEnabledPluginsEnabled(); - MainApp.bus().post(new EventAppInitialized()); + RxBus.INSTANCE.send(new EventAppInitialized()); } private void setAlwaysEnabledPluginsEnabled() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt index 34bbcf6fc3..9471747d7c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/dialogs/WizardDialog.kt @@ -16,7 +16,6 @@ import info.nightscout.androidaps.R import info.nightscout.androidaps.data.Profile import info.nightscout.androidaps.db.BgReading import info.nightscout.androidaps.db.DatabaseHelper -import info.nightscout.androidaps.events.EventFeatureRunning import info.nightscout.androidaps.interfaces.Constraint import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin @@ -68,11 +67,6 @@ class WizardDialog : DialogFragment() { this.parentContext = null } - override fun onResume() { - super.onResume() - MainApp.bus().post(EventFeatureRunning(EventFeatureRunning.Feature.WIZARD)) - } - override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) savedInstanceState.putDouble("treatments_wizard_bginput", treatments_wizard_bginput.value) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/WearPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/WearPlugin.java index 8816cf58cd..cf3cc6b783 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/WearPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/wear/WearPlugin.java @@ -120,8 +120,7 @@ public class WearPlugin extends PluginBase { resendDataToWatch(); // status may be formated differently sendDataToWatch(true, false, false); - }, - FabricPrivacy::logException + }, FabricPrivacy::logException )); disposable.add(RxBus.INSTANCE .toObservable(EventRefreshOverview.class) @@ -129,8 +128,18 @@ public class WearPlugin extends PluginBase { .subscribe(event -> { if (WatchUpdaterService.shouldReportLoopStatus(LoopPlugin.getPlugin().isEnabled(PluginType.LOOP))) sendDataToWatch(true, false, false); - }, - FabricPrivacy::logException + }, FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventBolusRequested.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + String status = String.format(MainApp.gs(R.string.bolusrequested), event.getAmount()); + Intent intent = new Intent(ctx, WatchUpdaterService.class).setAction(WatchUpdaterService.ACTION_SEND_BOLUSPROGRESS); + intent.putExtra("progresspercent", 0); + intent.putExtra("progressstatus", status); + ctx.startService(intent); + }, FabricPrivacy::logException )); } @@ -192,16 +201,6 @@ public class WearPlugin extends PluginBase { } } - @Subscribe - public void onStatusEvent(final EventBolusRequested ev) { - String status = String.format(MainApp.gs(R.string.bolusrequested), ev.getAmount()); - Intent intent = new Intent(ctx, WatchUpdaterService.class).setAction(WatchUpdaterService.ACTION_SEND_BOLUSPROGRESS); - intent.putExtra("progresspercent", 0); - intent.putExtra("progressstatus", status); - ctx.startService(intent); - - } - @Subscribe public void onStatusEvent(final EventDismissBolusprogressIfRunning ev) { if (ev.result == null) return; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/xdripStatusline/StatuslinePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/xdripStatusline/StatuslinePlugin.java index 2713a0ef08..da61b83b98 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/xdripStatusline/StatuslinePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/xdripStatusline/StatuslinePlugin.java @@ -8,8 +8,6 @@ import android.preference.PreferenceManager; import androidx.annotation.NonNull; -import com.squareup.otto.Subscribe; - import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.IobTotal; @@ -88,7 +86,6 @@ public class StatuslinePlugin extends PluginBase { @Override protected void onStart() { super.onStart(); - MainApp.bus().register(this); disposable.add(RxBus.INSTANCE .toObservable(EventRefreshOverview.class) .observeOn(Schedulers.io()) @@ -134,12 +131,17 @@ public class StatuslinePlugin extends PluginBase { .subscribe(event -> sendStatus(), FabricPrivacy::logException )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAppInitialized.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendStatus(), + FabricPrivacy::logException + )); } @Override protected void onStop() { super.onStop(); - MainApp.bus().unregister(this); disposable.clear(); sendStatus(); } @@ -211,11 +213,4 @@ public class StatuslinePlugin extends PluginBase { return status; } - - - @Subscribe - public void onStatusEvent(final EventAppInitialized ev) { - sendStatus(); - } - } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java index e5aec47b43..16df35c75d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobCalculatorPlugin.java @@ -135,7 +135,7 @@ public class IobCobCalculatorPlugin extends PluginBase { basalDataTable = new LongSparseArray<>(); } runCalculation("onNewProfile", System.currentTimeMillis(), false, true, event); - }, FabricPrivacy::logException) + }, FabricPrivacy::logException) ); // EventNewBG disposable.add(RxBus.INSTANCE @@ -149,9 +149,9 @@ public class IobCobCalculatorPlugin extends PluginBase { } stopCalculation("onEventNewBG"); runCalculation("onEventNewBG", System.currentTimeMillis(), true, true, event); - }, FabricPrivacy::logException) + }, FabricPrivacy::logException) ); - // EventPreferenceChange + // EventPreferenceChange disposable.add(RxBus.INSTANCE .toObservable(EventPreferenceChange.class) .observeOn(Schedulers.io()) @@ -179,7 +179,20 @@ public class IobCobCalculatorPlugin extends PluginBase { } runCalculation("onEventPreferenceChange", System.currentTimeMillis(), false, true, event); } - }, FabricPrivacy::logException) + }, FabricPrivacy::logException) + ); + // EventAppInitialized + disposable.add(RxBus.INSTANCE + .toObservable(EventAppInitialized.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (this != getPlugin()) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Ignoring event for non default instance"); + return; + } + runCalculation("onEventAppInitialized", System.currentTimeMillis(), true, true, event); + }, FabricPrivacy::logException) ); } @@ -724,18 +737,7 @@ public class IobCobCalculatorPlugin extends PluginBase { return array; } - @Subscribe - @SuppressWarnings("unused") - public void onEventAppInitialized(EventAppInitialized ev) { - if (this != getPlugin()) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Ignoring event for non default instance"); - return; - } - runCalculation("onEventAppInitialized", System.currentTimeMillis(), true, true, ev); - } - - public void stopCalculation(String from) { + public void stopCalculation(String from) { if (thread != null && thread.getState() != Thread.State.TERMINATED) { stopCalculationTrigger = true; if (L.isEnabled(L.AUTOSENS)) diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java index 415af062fd..cd0b666119 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueue.java @@ -249,7 +249,7 @@ public class CommandQueue { // not when the Bolus command is starting. The command closes the dialog upon completion). showBolusProgressDialog(detailedBolusInfo.insulin, detailedBolusInfo.context); // Notify Wear about upcoming bolus - MainApp.bus().post(new EventBolusRequested(detailedBolusInfo.insulin)); + RxBus.INSTANCE.send(new EventBolusRequested(detailedBolusInfo.insulin)); } }