diff --git a/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java index cd969a53a1..b16b48b149 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/HistoryBrowseActivity.java @@ -30,6 +30,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.events.EventCustomCalculationFinished; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.overview.OverviewFragment; @@ -39,12 +40,15 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorP import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventIobCalculationProgress; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; public class HistoryBrowseActivity extends NoSplashActivity { private static Logger log = LoggerFactory.getLogger(HistoryBrowseActivity.class); - + private CompositeDisposable disposable = new CompositeDisposable(); ImageButton chartButton; @@ -166,6 +170,7 @@ public class HistoryBrowseActivity extends NoSplashActivity { public void onPause() { super.onPause(); MainApp.bus().unregister(this); + disposable.clear(); iobCobCalculatorPlugin.stopCalculation("onPause"); } @@ -173,6 +178,18 @@ public class HistoryBrowseActivity extends NoSplashActivity { public void onResume() { super.onResume(); MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> { + if (event.getCause() == eventCustomCalculationFinished) { + log.debug("EventAutosensCalculationFinished"); + synchronized (HistoryBrowseActivity.this) { + updateGUI("EventAutosensCalculationFinished"); + } + } + }, FabricPrivacy::logException) + ); // set start of current day Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); @@ -193,18 +210,6 @@ public class HistoryBrowseActivity extends NoSplashActivity { iobCobCalculatorPlugin.runCalculation(from, end, true, false, eventCustomCalculationFinished); } - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished e) { - if (e.getCause() == eventCustomCalculationFinished) { - log.debug("EventAutosensCalculationFinished"); - runOnUiThread(() -> { - synchronized (HistoryBrowseActivity.this) { - updateGUI("EventAutosensCalculationFinished"); - } - }); - } - } - @Subscribe public void onStatusEvent(final EventIobCalculationProgress e) { runOnUiThread(() -> { diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 691d97947d..f1763e8aa8 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -412,7 +412,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void run() { if (L.isEnabled(L.DATABASE)) log.debug("Firing EventNewBg"); - MainApp.bus().post(new EventNewBG(bgReading)); RxBus.INSTANCE.send(new EventNewBG(bgReading)); scheduledBgPost = null; } diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.java b/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.java deleted file mode 100644 index db3498bed7..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.java +++ /dev/null @@ -1,17 +0,0 @@ -package info.nightscout.androidaps.events; - -import androidx.annotation.Nullable; - -import info.nightscout.androidaps.db.BgReading; - -/** - * Created by mike on 05.06.2016. - */ -public class EventNewBG extends EventLoop { - @Nullable - public final BgReading bgReading; - - public EventNewBG(BgReading bgReading) { - this.bgReading = bgReading; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.kt b/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.kt new file mode 100644 index 0000000000..08c05407c9 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventNewBG.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.events + +import info.nightscout.androidaps.db.BgReading + +class EventNewBG(val bgReading: BgReading?) : EventLoop() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java b/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java deleted file mode 100644 index f26a310b6b..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.java +++ /dev/null @@ -1,7 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 04.06.2016. - */ -public class EventNewBasalProfile extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.kt b/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.kt new file mode 100644 index 0000000000..2ffa5a9724 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventNewBasalProfile.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventNewBasalProfile : Event() \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java index d555ed3615..e6767a3067 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java @@ -129,6 +129,30 @@ public class LoopPlugin extends PluginBase { invoke("EventTempTargetChange", true); }, FabricPrivacy::logException) ); + /** + * This method is triggered once autosens calculation has completed, so the LoopPlugin + * has current data to work with. However, autosens calculation can be triggered by multiple + * sources and currently only a new BG should trigger a loop run. Hence we return early if + * the event causing the calculation is not EventNewBg. + *

+ */ + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + // Autosens calculation not triggered by a new BG + if (!(event.getCause() instanceof EventNewBG)) return; + + BgReading bgReading = DatabaseHelper.actualBg(); + // BG outdated + if (bgReading == null) return; + // already looped with that value + if (bgReading.date <= lastBgTriggeredRun) return; + + lastBgTriggeredRun = bgReading.date; + invoke("AutosenseCalculation for " + bgReading, true); + }, FabricPrivacy::logException) + ); } private void createNotificationChannel() { @@ -156,33 +180,6 @@ public class LoopPlugin extends PluginBase { return pump == null || pump.getPumpDescription().isTempBasalCapable; } - /** - * This method is triggered once autosens calculation has completed, so the LoopPlugin - * has current data to work with. However, autosens calculation can be triggered by multiple - * sources and currently only a new BG should trigger a loop run. Hence we return early if - * the event causing the calculation is not EventNewBg. - *

- */ - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - if (!(ev.getCause() instanceof EventNewBG)) { - // Autosens calculation not triggered by a new BG - return; - } - BgReading bgReading = DatabaseHelper.actualBg(); - if (bgReading == null) { - // BG outdated - return; - } - if (bgReading.date <= lastBgTriggeredRun) { - // already looped with that value - return; - } - - lastBgTriggeredRun = bgReading.date; - invoke("AutosenseCalculation for " + bgReading, true); - } - public long suspendedTo() { return loopSuspendedTill; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java index b9276a5ac3..e9bd53bcf3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctions.java @@ -23,6 +23,7 @@ import info.nightscout.androidaps.events.EventProfileNeedsUpdate; import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.overview.dialogs.ErrorHelperActivity; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.queue.Callback; @@ -64,7 +65,7 @@ public class ProfileFunctions { MainApp.instance().startActivity(i); } if (result.enacted) - MainApp.bus().post(new EventNewBasalProfile()); + RxBus.INSTANCE.send(new EventNewBasalProfile()); } }); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java index 78be541095..e88fe59c80 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.java @@ -918,6 +918,12 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, .subscribe(event -> scheduleUpdateGUI("EventInitializationChanged"), FabricPrivacy::logException )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventAutosensCalculationFinished"), + FabricPrivacy::logException + )); disposable.add(RxBus.INSTANCE .toObservable(EventPumpStatusChanged.class) .observeOn(AndroidSchedulers.mainThread()) @@ -940,11 +946,6 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, scheduleUpdateGUI("EventPreferenceChange"); } - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - scheduleUpdateGUI("EventAutosensCalculationFinished"); - } - @Subscribe public void onStatusEvent(final EventNewOpenLoopNotification ev) { scheduleUpdateGUI("EventNewOpenLoopNotification"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java index 1d26a0ec16..c6b24d6438 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/persistentNotification/PersistentNotificationPlugin.java @@ -126,6 +126,18 @@ public class PersistentNotificationPlugin extends PluginBase { .subscribe(event -> triggerNotificationUpdate(false), FabricPrivacy::logException )); + disposable.add(RxBus.INSTANCE + .toObservable(EventNewBasalProfile.class) + .observeOn(Schedulers.io()) + .subscribe(event -> triggerNotificationUpdate(false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(Schedulers.io()) + .subscribe(event -> triggerNotificationUpdate(false), + FabricPrivacy::logException + )); triggerNotificationUpdate(true); } @@ -327,14 +339,4 @@ public class PersistentNotificationPlugin extends PluginBase { triggerNotificationUpdate(false); } - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - triggerNotificationUpdate(false); - } - - @Subscribe - public void onStatusEvent(final EventNewBasalProfile ev) { - triggerNotificationUpdate(false); - } - } 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 d74643cd14..f207bd2c84 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 @@ -81,25 +81,37 @@ public class WearPlugin extends PluginBase { disposable.add(RxBus.INSTANCE .toObservable(EventOpenAPSUpdateGui.class) .observeOn(Schedulers.io()) - .subscribe(eventOpenAPSUpdateGui -> sendDataToWatch(true, true, false), + .subscribe(event -> sendDataToWatch(true, true, false), FabricPrivacy::logException )); disposable.add(RxBus.INSTANCE .toObservable(EventExtendedBolusChange.class) .observeOn(Schedulers.io()) - .subscribe(eventOpenAPSUpdateGui -> sendDataToWatch(true, true, false), + .subscribe(event -> sendDataToWatch(true, true, false), FabricPrivacy::logException )); disposable.add(RxBus.INSTANCE .toObservable(EventTempBasalChange.class) .observeOn(Schedulers.io()) - .subscribe(eventOpenAPSUpdateGui -> sendDataToWatch(true, true, false), + .subscribe(event -> sendDataToWatch(true, true, false), FabricPrivacy::logException )); disposable.add(RxBus.INSTANCE .toObservable(EventTreatmentChange.class) .observeOn(Schedulers.io()) - .subscribe(eventOpenAPSUpdateGui -> sendDataToWatch(true, true, false), + .subscribe(event -> sendDataToWatch(true, true, false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventNewBasalProfile.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendDataToWatch(false, true, false), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendDataToWatch(true, true, true), FabricPrivacy::logException )); disposable.add(RxBus.INSTANCE @@ -169,16 +181,6 @@ public class WearPlugin extends PluginBase { sendDataToWatch(true, false, false); } - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - sendDataToWatch(true, true, true); - } - - @Subscribe - public void onStatusEvent(final EventNewBasalProfile ev) { - sendDataToWatch(false, true, false); - } - @Subscribe public void onStatusEvent(final EventOverviewBolusProgress ev) { if (!ev.isSMB() || SP.getBoolean("wear_notifySMB", true)) { 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 2d8736351b..fec423ea66 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 @@ -122,6 +122,12 @@ public class StatuslinePlugin extends PluginBase { .subscribe(event -> sendStatus(), FabricPrivacy::logException )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendStatus(), + FabricPrivacy::logException + )); } @Override @@ -207,11 +213,6 @@ public class StatuslinePlugin extends PluginBase { sendStatus(); } - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - sendStatus(); - } - @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 8ede44ce9e..2db1f2dc35 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 @@ -91,6 +91,7 @@ public class IobCobCalculatorPlugin extends PluginBase { protected void onStart() { super.onStart(); MainApp.bus().register(this); + // EventConfigBuilderChange disposable.add(RxBus.INSTANCE .toObservable(EventConfigBuilderChange.class) .observeOn(Schedulers.io()) @@ -110,6 +111,46 @@ public class IobCobCalculatorPlugin extends PluginBase { runCalculation("onEventConfigBuilderChange", System.currentTimeMillis(), false, true, event); }, FabricPrivacy::logException) ); + // EventNewBasalProfile + disposable.add(RxBus.INSTANCE + .toObservable(EventNewBasalProfile.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (this != getPlugin()) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Ignoring event for non default instance"); + return; + } + if (ConfigBuilderPlugin.getPlugin() == null) + return; // app still initializing + if (event == null) { // on init no need of reset + return; + } + stopCalculation("onNewProfile"); + synchronized (dataLock) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Invalidating cached data because of new profile. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records"); + iobTable = new LongSparseArray<>(); + autosensDataTable = new LongSparseArray<>(); + basalDataTable = new LongSparseArray<>(); + } + runCalculation("onNewProfile", System.currentTimeMillis(), false, true, event); + }, FabricPrivacy::logException) + ); + // EventNewBG + disposable.add(RxBus.INSTANCE + .toObservable(EventNewBG.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (this != getPlugin()) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Ignoring event for non default instance"); + return; + } + stopCalculation("onEventNewBG"); + runCalculation("onEventNewBG", System.currentTimeMillis(), true, true, event); + }, FabricPrivacy::logException) + ); } @Override @@ -664,19 +705,7 @@ public class IobCobCalculatorPlugin extends PluginBase { runCalculation("onEventAppInitialized", System.currentTimeMillis(), true, true, ev); } - @Subscribe - @SuppressWarnings("unused") - public void onEventNewBG(EventNewBG ev) { - if (this != getPlugin()) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Ignoring event for non default instance"); - return; - } - stopCalculation("onEventNewBG"); - runCalculation("onEventNewBG", 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)) @@ -701,29 +730,6 @@ public class IobCobCalculatorPlugin extends PluginBase { } } - @Subscribe - public void onNewProfile(EventNewBasalProfile ev) { - if (this != getPlugin()) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Ignoring event for non default instance"); - return; - } - if (ConfigBuilderPlugin.getPlugin() == null) - return; // app still initializing - if (ev == null) { // on init no need of reset - return; - } - stopCalculation("onNewProfile"); - synchronized (dataLock) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Invalidating cached data because of new profile. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records"); - iobTable = new LongSparseArray<>(); - autosensDataTable = new LongSparseArray<>(); - basalDataTable = new LongSparseArray<>(); - } - runCalculation("onNewProfile", System.currentTimeMillis(), false, true, ev); - } - @Subscribe public void onEventPreferenceChange(EventPreferenceChange ev) { if (this != getPlugin()) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java index 2b427602f5..84cca4e90f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobOref1Thread.java @@ -387,7 +387,7 @@ public class IobCobOref1Thread extends Thread { } new Thread(() -> { SystemClock.sleep(1000); - MainApp.bus().post(new EventAutosensCalculationFinished(cause)); + RxBus.INSTANCE.send(new EventAutosensCalculationFinished(cause)); }).start(); } finally { if (mWakeLock != null) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java index 756cee9853..65495b1fd2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/iob/iobCobCalculator/IobCobThread.java @@ -312,7 +312,6 @@ public class IobCobThread extends Thread { } new Thread(() -> { SystemClock.sleep(1000); - MainApp.bus().post(new EventAutosensCalculationFinished(cause)); RxBus.INSTANCE.send(new EventAutosensCalculationFinished(cause)); }).start(); } finally { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.java index 269ee2b3f5..6023d70d12 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/source/BGSourceFragment.java @@ -1,19 +1,18 @@ package info.nightscout.androidaps.plugins.source; -import android.app.Activity; import android.content.DialogInterface; import android.graphics.Paint; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import java.util.List; @@ -21,19 +20,22 @@ import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.BgReading; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.T; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; /** * Created by mike on 16.10.2017. */ -public class BGSourceFragment extends SubscriberFragment { +public class BGSourceFragment extends Fragment { + private CompositeDisposable disposable = new CompositeDisposable(); RecyclerView recyclerView; String units = Constants.MGDL; @@ -66,19 +68,25 @@ public class BGSourceFragment extends SubscriberFragment { return null; } - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished unused) { - updateGUI(); + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGUI(), FabricPrivacy::logException) + ); } @Override + public synchronized void onPause() { + disposable.clear(); + super.onPause(); + } + protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - long now = System.currentTimeMillis(); - recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getAllBgreadingsDataFromTime(now - MILLS_TO_THE_PAST, false)), true); - }); + long now = System.currentTimeMillis(); + recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getAllBgreadingsDataFromTime(now - MILLS_TO_THE_PAST, false)), true); } public class RecyclerViewAdapter extends RecyclerView.Adapter { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.java index 81cfcd16a9..abce919233 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsBolusFragment.java @@ -269,6 +269,11 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis .observeOn(AndroidSchedulers.mainThread()) .subscribe(event -> updateGui(), FabricPrivacy::logException) ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); updateGui(); } @@ -278,26 +283,17 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis disposable.clear(); } - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - updateGui(); - } - private void updateGui() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTreatmentsFromHistory()), false); - if (TreatmentsPlugin.getPlugin().getLastCalculationTreatments() != null) { - iobTotal.setText(DecimalFormatter.to2Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().iob) + " " + MainApp.gs(R.string.insulin_unit_shortname)); - activityTotal.setText(DecimalFormatter.to3Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().activity) + " " + MainApp.gs(R.string.insulin_unit_shortname)); - } - if (!TreatmentsPlugin.getPlugin().getService().getTreatmentDataFromTime(now() + 1000, true).isEmpty()) { - deleteFutureTreatments.setVisibility(View.VISIBLE); - } else { - deleteFutureTreatments.setVisibility(View.GONE); - } - }); + recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTreatmentsFromHistory()), false); + if (TreatmentsPlugin.getPlugin().getLastCalculationTreatments() != null) { + iobTotal.setText(DecimalFormatter.to2Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().iob) + " " + MainApp.gs(R.string.insulin_unit_shortname)); + activityTotal.setText(DecimalFormatter.to3Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().activity) + " " + MainApp.gs(R.string.insulin_unit_shortname)); + } + if (!TreatmentsPlugin.getPlugin().getService().getTreatmentDataFromTime(now() + 1000, true).isEmpty()) { + deleteFutureTreatments.setVisibility(View.VISIBLE); + } else { + deleteFutureTreatments.setVisibility(View.GONE); + } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.java index e148ab7cad..297146d999 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsExtendedBolusesFragment.java @@ -191,6 +191,11 @@ public class TreatmentsExtendedBolusesFragment extends Fragment { .observeOn(AndroidSchedulers.mainThread()) .subscribe(event -> updateGui(), FabricPrivacy::logException) ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); updateGui(); } @@ -200,15 +205,8 @@ public class TreatmentsExtendedBolusesFragment extends Fragment { disposable.clear(); } - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ev) { - updateGui(); - } - private void updateGui() { - Activity activity = getActivity(); - if (activity != null && recyclerView != null) - activity.runOnUiThread(() -> recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getExtendedBolusesFromHistory()), false)); + recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getExtendedBolusesFromHistory()), false); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.java index 59f2fa69c9..35fb692c64 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsTemporaryBasalsFragment.java @@ -217,6 +217,11 @@ public class TreatmentsTemporaryBasalsFragment extends Fragment { .observeOn(AndroidSchedulers.mainThread()) .subscribe(event -> updateGui(), FabricPrivacy::logException) ); + disposable.add(RxBus.INSTANCE + .toObservable(EventAutosensCalculationFinished.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); updateGui(); } @@ -226,20 +231,11 @@ public class TreatmentsTemporaryBasalsFragment extends Fragment { disposable.clear(); } - @Subscribe - public void onStatusEvent(final EventAutosensCalculationFinished ignored) { - updateGui(); - } - private void updateGui() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()), false); - IobTotal tempBasalsCalculation = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals(); - if (tempBasalsCalculation != null) - tempBasalTotalView.setText(DecimalFormatter.to2Decimal(tempBasalsCalculation.basaliob, " U")); - }); + recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()), false); + IobTotal tempBasalsCalculation = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals(); + if (tempBasalsCalculation != null) + tempBasalTotalView.setText(DecimalFormatter.to2Decimal(tempBasalsCalculation.basaliob, " U")); } }