Events to RxBus

This commit is contained in:
Milos Kozak 2019-10-14 20:26:33 +02:00
parent 64f16c5a7b
commit 0f792dc092
19 changed files with 190 additions and 195 deletions

View file

@ -30,6 +30,7 @@ import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.events.EventCustomCalculationFinished; import info.nightscout.androidaps.events.EventCustomCalculationFinished;
import info.nightscout.androidaps.interfaces.PumpInterface; 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.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.overview.OverviewFragment; 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.EventAutosensCalculationFinished;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventIobCalculationProgress; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventIobCalculationProgress;
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.SP;
import info.nightscout.androidaps.utils.T; import info.nightscout.androidaps.utils.T;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
public class HistoryBrowseActivity extends NoSplashActivity { public class HistoryBrowseActivity extends NoSplashActivity {
private static Logger log = LoggerFactory.getLogger(HistoryBrowseActivity.class); private static Logger log = LoggerFactory.getLogger(HistoryBrowseActivity.class);
private CompositeDisposable disposable = new CompositeDisposable();
ImageButton chartButton; ImageButton chartButton;
@ -166,6 +170,7 @@ public class HistoryBrowseActivity extends NoSplashActivity {
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
MainApp.bus().unregister(this); MainApp.bus().unregister(this);
disposable.clear();
iobCobCalculatorPlugin.stopCalculation("onPause"); iobCobCalculatorPlugin.stopCalculation("onPause");
} }
@ -173,6 +178,18 @@ public class HistoryBrowseActivity extends NoSplashActivity {
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
MainApp.bus().register(this); 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 // set start of current day
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis()); calendar.setTimeInMillis(System.currentTimeMillis());
@ -193,18 +210,6 @@ public class HistoryBrowseActivity extends NoSplashActivity {
iobCobCalculatorPlugin.runCalculation(from, end, true, false, eventCustomCalculationFinished); 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 @Subscribe
public void onStatusEvent(final EventIobCalculationProgress e) { public void onStatusEvent(final EventIobCalculationProgress e) {
runOnUiThread(() -> { runOnUiThread(() -> {

View file

@ -412,7 +412,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public void run() { public void run() {
if (L.isEnabled(L.DATABASE)) if (L.isEnabled(L.DATABASE))
log.debug("Firing EventNewBg"); log.debug("Firing EventNewBg");
MainApp.bus().post(new EventNewBG(bgReading));
RxBus.INSTANCE.send(new EventNewBG(bgReading)); RxBus.INSTANCE.send(new EventNewBG(bgReading));
scheduledBgPost = null; scheduledBgPost = null;
} }

View file

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

View file

@ -0,0 +1,5 @@
package info.nightscout.androidaps.events
import info.nightscout.androidaps.db.BgReading
class EventNewBG(val bgReading: BgReading?) : EventLoop()

View file

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

View file

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

View file

@ -129,6 +129,30 @@ public class LoopPlugin extends PluginBase {
invoke("EventTempTargetChange", true); invoke("EventTempTargetChange", true);
}, FabricPrivacy::logException) }, 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.
* <p>
*/
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() { private void createNotificationChannel() {
@ -156,33 +180,6 @@ public class LoopPlugin extends PluginBase {
return pump == null || pump.getPumpDescription().isTempBasalCapable; 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.
* <p>
*/
@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() { public long suspendedTo() {
return loopSuspendedTill; return loopSuspendedTill;
} }

View file

@ -23,6 +23,7 @@ import info.nightscout.androidaps.events.EventProfileNeedsUpdate;
import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.ProfileInterface;
import info.nightscout.androidaps.interfaces.TreatmentsInterface; import info.nightscout.androidaps.interfaces.TreatmentsInterface;
import info.nightscout.androidaps.logging.L; 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.general.overview.dialogs.ErrorHelperActivity;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.Callback;
@ -64,7 +65,7 @@ public class ProfileFunctions {
MainApp.instance().startActivity(i); MainApp.instance().startActivity(i);
} }
if (result.enacted) if (result.enacted)
MainApp.bus().post(new EventNewBasalProfile()); RxBus.INSTANCE.send(new EventNewBasalProfile());
} }
}); });
} }

View file

@ -918,6 +918,12 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
.subscribe(event -> scheduleUpdateGUI("EventInitializationChanged"), .subscribe(event -> scheduleUpdateGUI("EventInitializationChanged"),
FabricPrivacy::logException FabricPrivacy::logException
)); ));
disposable.add(RxBus.INSTANCE
.toObservable(EventAutosensCalculationFinished.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> scheduleUpdateGUI("EventAutosensCalculationFinished"),
FabricPrivacy::logException
));
disposable.add(RxBus.INSTANCE disposable.add(RxBus.INSTANCE
.toObservable(EventPumpStatusChanged.class) .toObservable(EventPumpStatusChanged.class)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
@ -940,11 +946,6 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
scheduleUpdateGUI("EventPreferenceChange"); scheduleUpdateGUI("EventPreferenceChange");
} }
@Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished ev) {
scheduleUpdateGUI("EventAutosensCalculationFinished");
}
@Subscribe @Subscribe
public void onStatusEvent(final EventNewOpenLoopNotification ev) { public void onStatusEvent(final EventNewOpenLoopNotification ev) {
scheduleUpdateGUI("EventNewOpenLoopNotification"); scheduleUpdateGUI("EventNewOpenLoopNotification");

View file

@ -126,6 +126,18 @@ public class PersistentNotificationPlugin extends PluginBase {
.subscribe(event -> triggerNotificationUpdate(false), .subscribe(event -> triggerNotificationUpdate(false),
FabricPrivacy::logException 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); triggerNotificationUpdate(true);
} }
@ -327,14 +339,4 @@ public class PersistentNotificationPlugin extends PluginBase {
triggerNotificationUpdate(false); triggerNotificationUpdate(false);
} }
@Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished ev) {
triggerNotificationUpdate(false);
}
@Subscribe
public void onStatusEvent(final EventNewBasalProfile ev) {
triggerNotificationUpdate(false);
}
} }

View file

@ -81,25 +81,37 @@ public class WearPlugin extends PluginBase {
disposable.add(RxBus.INSTANCE disposable.add(RxBus.INSTANCE
.toObservable(EventOpenAPSUpdateGui.class) .toObservable(EventOpenAPSUpdateGui.class)
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.subscribe(eventOpenAPSUpdateGui -> sendDataToWatch(true, true, false), .subscribe(event -> sendDataToWatch(true, true, false),
FabricPrivacy::logException FabricPrivacy::logException
)); ));
disposable.add(RxBus.INSTANCE disposable.add(RxBus.INSTANCE
.toObservable(EventExtendedBolusChange.class) .toObservable(EventExtendedBolusChange.class)
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.subscribe(eventOpenAPSUpdateGui -> sendDataToWatch(true, true, false), .subscribe(event -> sendDataToWatch(true, true, false),
FabricPrivacy::logException FabricPrivacy::logException
)); ));
disposable.add(RxBus.INSTANCE disposable.add(RxBus.INSTANCE
.toObservable(EventTempBasalChange.class) .toObservable(EventTempBasalChange.class)
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.subscribe(eventOpenAPSUpdateGui -> sendDataToWatch(true, true, false), .subscribe(event -> sendDataToWatch(true, true, false),
FabricPrivacy::logException FabricPrivacy::logException
)); ));
disposable.add(RxBus.INSTANCE disposable.add(RxBus.INSTANCE
.toObservable(EventTreatmentChange.class) .toObservable(EventTreatmentChange.class)
.observeOn(Schedulers.io()) .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 FabricPrivacy::logException
)); ));
disposable.add(RxBus.INSTANCE disposable.add(RxBus.INSTANCE
@ -169,16 +181,6 @@ public class WearPlugin extends PluginBase {
sendDataToWatch(true, false, false); 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 @Subscribe
public void onStatusEvent(final EventOverviewBolusProgress ev) { public void onStatusEvent(final EventOverviewBolusProgress ev) {
if (!ev.isSMB() || SP.getBoolean("wear_notifySMB", true)) { if (!ev.isSMB() || SP.getBoolean("wear_notifySMB", true)) {

View file

@ -122,6 +122,12 @@ public class StatuslinePlugin extends PluginBase {
.subscribe(event -> sendStatus(), .subscribe(event -> sendStatus(),
FabricPrivacy::logException FabricPrivacy::logException
)); ));
disposable.add(RxBus.INSTANCE
.toObservable(EventAutosensCalculationFinished.class)
.observeOn(Schedulers.io())
.subscribe(event -> sendStatus(),
FabricPrivacy::logException
));
} }
@Override @Override
@ -207,11 +213,6 @@ public class StatuslinePlugin extends PluginBase {
sendStatus(); sendStatus();
} }
@Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished ev) {
sendStatus();
}
@Subscribe @Subscribe
public void onStatusEvent(final EventAppInitialized ev) { public void onStatusEvent(final EventAppInitialized ev) {
sendStatus(); sendStatus();

View file

@ -91,6 +91,7 @@ public class IobCobCalculatorPlugin extends PluginBase {
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
MainApp.bus().register(this); MainApp.bus().register(this);
// EventConfigBuilderChange
disposable.add(RxBus.INSTANCE disposable.add(RxBus.INSTANCE
.toObservable(EventConfigBuilderChange.class) .toObservable(EventConfigBuilderChange.class)
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
@ -110,6 +111,46 @@ public class IobCobCalculatorPlugin extends PluginBase {
runCalculation("onEventConfigBuilderChange", System.currentTimeMillis(), false, true, event); runCalculation("onEventConfigBuilderChange", System.currentTimeMillis(), false, true, event);
}, FabricPrivacy::logException) }, 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 @Override
@ -664,18 +705,6 @@ public class IobCobCalculatorPlugin extends PluginBase {
runCalculation("onEventAppInitialized", System.currentTimeMillis(), true, true, ev); 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) { if (thread != null && thread.getState() != Thread.State.TERMINATED) {
stopCalculationTrigger = true; stopCalculationTrigger = true;
@ -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 @Subscribe
public void onEventPreferenceChange(EventPreferenceChange ev) { public void onEventPreferenceChange(EventPreferenceChange ev) {
if (this != getPlugin()) { if (this != getPlugin()) {

View file

@ -387,7 +387,7 @@ public class IobCobOref1Thread extends Thread {
} }
new Thread(() -> { new Thread(() -> {
SystemClock.sleep(1000); SystemClock.sleep(1000);
MainApp.bus().post(new EventAutosensCalculationFinished(cause)); RxBus.INSTANCE.send(new EventAutosensCalculationFinished(cause));
}).start(); }).start();
} finally { } finally {
if (mWakeLock != null) if (mWakeLock != null)

View file

@ -312,7 +312,6 @@ public class IobCobThread extends Thread {
} }
new Thread(() -> { new Thread(() -> {
SystemClock.sleep(1000); SystemClock.sleep(1000);
MainApp.bus().post(new EventAutosensCalculationFinished(cause));
RxBus.INSTANCE.send(new EventAutosensCalculationFinished(cause)); RxBus.INSTANCE.send(new EventAutosensCalculationFinished(cause));
}).start(); }).start();
} finally { } finally {

View file

@ -1,19 +1,18 @@
package info.nightscout.androidaps.plugins.source; package info.nightscout.androidaps.plugins.source;
import android.app.Activity;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.graphics.Paint; import android.graphics.Paint;
import android.os.Bundle; 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.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; 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; import java.util.List;
@ -21,19 +20,22 @@ import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading; 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.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished;
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.T; import info.nightscout.androidaps.utils.T;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
/** /**
* Created by mike on 16.10.2017. * Created by mike on 16.10.2017.
*/ */
public class BGSourceFragment extends SubscriberFragment { public class BGSourceFragment extends Fragment {
private CompositeDisposable disposable = new CompositeDisposable();
RecyclerView recyclerView; RecyclerView recyclerView;
String units = Constants.MGDL; String units = Constants.MGDL;
@ -66,19 +68,25 @@ public class BGSourceFragment extends SubscriberFragment {
return null; return null;
} }
@Subscribe @Override
public void onStatusEvent(final EventAutosensCalculationFinished unused) { public synchronized void onResume() {
updateGUI(); super.onResume();
disposable.add(RxBus.INSTANCE
.toObservable(EventAutosensCalculationFinished.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGUI(), FabricPrivacy::logException)
);
} }
@Override @Override
public synchronized void onPause() {
disposable.clear();
super.onPause();
}
protected void updateGUI() { protected void updateGUI() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(() -> {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getAllBgreadingsDataFromTime(now - MILLS_TO_THE_PAST, false)), true); recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getAllBgreadingsDataFromTime(now - MILLS_TO_THE_PAST, false)), true);
});
} }
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.BgReadingsViewHolder> { public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.BgReadingsViewHolder> {

View file

@ -269,6 +269,11 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), FabricPrivacy::logException) .subscribe(event -> updateGui(), FabricPrivacy::logException)
); );
disposable.add(RxBus.INSTANCE
.toObservable(EventAutosensCalculationFinished.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), FabricPrivacy::logException)
);
updateGui(); updateGui();
} }
@ -278,15 +283,7 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis
disposable.clear(); disposable.clear();
} }
@Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished ev) {
updateGui();
}
private void updateGui() { private void updateGui() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(() -> {
recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTreatmentsFromHistory()), false); recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTreatmentsFromHistory()), false);
if (TreatmentsPlugin.getPlugin().getLastCalculationTreatments() != null) { if (TreatmentsPlugin.getPlugin().getLastCalculationTreatments() != null) {
iobTotal.setText(DecimalFormatter.to2Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().iob) + " " + MainApp.gs(R.string.insulin_unit_shortname)); iobTotal.setText(DecimalFormatter.to2Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTreatments().iob) + " " + MainApp.gs(R.string.insulin_unit_shortname));
@ -297,7 +294,6 @@ public class TreatmentsBolusFragment extends Fragment implements View.OnClickLis
} else { } else {
deleteFutureTreatments.setVisibility(View.GONE); deleteFutureTreatments.setVisibility(View.GONE);
} }
});
} }
} }

View file

@ -191,6 +191,11 @@ public class TreatmentsExtendedBolusesFragment extends Fragment {
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), FabricPrivacy::logException) .subscribe(event -> updateGui(), FabricPrivacy::logException)
); );
disposable.add(RxBus.INSTANCE
.toObservable(EventAutosensCalculationFinished.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), FabricPrivacy::logException)
);
updateGui(); updateGui();
} }
@ -200,15 +205,8 @@ public class TreatmentsExtendedBolusesFragment extends Fragment {
disposable.clear(); disposable.clear();
} }
@Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished ev) {
updateGui();
}
private void updateGui() { private void updateGui() {
Activity activity = getActivity(); recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getExtendedBolusesFromHistory()), false);
if (activity != null && recyclerView != null)
activity.runOnUiThread(() -> recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getExtendedBolusesFromHistory()), false));
} }
} }

View file

@ -217,6 +217,11 @@ public class TreatmentsTemporaryBasalsFragment extends Fragment {
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), FabricPrivacy::logException) .subscribe(event -> updateGui(), FabricPrivacy::logException)
); );
disposable.add(RxBus.INSTANCE
.toObservable(EventAutosensCalculationFinished.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), FabricPrivacy::logException)
);
updateGui(); updateGui();
} }
@ -226,20 +231,11 @@ public class TreatmentsTemporaryBasalsFragment extends Fragment {
disposable.clear(); disposable.clear();
} }
@Subscribe
public void onStatusEvent(final EventAutosensCalculationFinished ignored) {
updateGui();
}
private void updateGui() { private void updateGui() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(() -> {
recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()), false); recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()), false);
IobTotal tempBasalsCalculation = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals(); IobTotal tempBasalsCalculation = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals();
if (tempBasalsCalculation != null) if (tempBasalsCalculation != null)
tempBasalTotalView.setText(DecimalFormatter.to2Decimal(tempBasalsCalculation.basaliob, " U")); tempBasalTotalView.setText(DecimalFormatter.to2Decimal(tempBasalsCalculation.basaliob, " U"));
});
} }
} }