Events to RxBus

This commit is contained in:
Milos Kozak 2019-10-14 23:49:53 +02:00
parent 4ed8f0a1e1
commit 1aa8771f4d
16 changed files with 68 additions and 145 deletions

View file

@ -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() {

View file

@ -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);
}
}

View file

@ -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)
}
}
}

View file

@ -1,8 +0,0 @@
package info.nightscout.androidaps.events;
/**
* Created by mike on 23.01.2018.
*/
public class EventAppInitialized extends Event {
}

View file

@ -0,0 +1,3 @@
package info.nightscout.androidaps.events
class EventAppInitialized : Event()

View file

@ -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;
}
}

View file

@ -0,0 +1,3 @@
package info.nightscout.androidaps.events
class EventBolusRequested(var amount: Double) : Event()

View file

@ -1,8 +0,0 @@
package info.nightscout.androidaps.events;
/**
* Created by mike on 13.02.2018.
*/
public class EventCustomCalculationFinished extends Event {
}

View file

@ -0,0 +1,3 @@
package info.nightscout.androidaps.events
class EventCustomCalculationFinished : Event()

View file

@ -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
}
}

View file

@ -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() {

View file

@ -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)

View file

@ -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;

View file

@ -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();
}
}

View file

@ -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))

View file

@ -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));
}
}