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 fd9af55eca..691d97947d 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -1577,7 +1577,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { public void run() { if (L.isEnabled(L.DATABASE)) log.debug("Firing scheduleCareportalEventChange"); - MainApp.bus().post(new EventCareportalEventChange()); + RxBus.INSTANCE.send(new EventCareportalEventChange()); scheduledCareportalEventPost = null; } } diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.java deleted file mode 100644 index 2dfbf9ae35..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.java +++ /dev/null @@ -1,5 +0,0 @@ -package info.nightscout.androidaps.events; - -/** Base class for events to update the UI, mostly a specific tab. */ -public class EventAcceptOpenLoopChange extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.kt new file mode 100644 index 0000000000..552564edfc --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventAcceptOpenLoopChange.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventAcceptOpenLoopChange : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.java deleted file mode 100644 index 9b47ed39cb..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 25.05.2017. - */ - -public class EventCareportalEventChange extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.kt new file mode 100644 index 0000000000..e7d52d86c0 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventCareportalEventChange.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventCareportalEventChange : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.java deleted file mode 100644 index ad5f558fe8..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 17.02.2017. - */ - -public class EventConfigBuilderChange extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.kt new file mode 100644 index 0000000000..b674374fad --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventConfigBuilderChange.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventConfigBuilderChange : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.java b/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.java deleted file mode 100644 index f2bef1d3d0..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.java +++ /dev/null @@ -1,8 +0,0 @@ -package info.nightscout.androidaps.events; - -/** - * Created by mike on 13.12.2016. - */ - -public class EventInitializationChanged extends Event { -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.kt b/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.kt new file mode 100644 index 0000000000..33ab0062c5 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventInitializationChanged.kt @@ -0,0 +1,3 @@ +package info.nightscout.androidaps.events + +class EventInitializationChanged : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.java deleted file mode 100644 index 265ce2998c..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.java +++ /dev/null @@ -1,11 +0,0 @@ -package info.nightscout.androidaps.events; - -import android.location.Location; - -public class EventLocationChange extends Event { - public Location location; - - public EventLocationChange(Location location) { - this.location = location; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.kt new file mode 100644 index 0000000000..fee6c9f800 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventLocationChange.kt @@ -0,0 +1,5 @@ +package info.nightscout.androidaps.events + +import android.location.Location + +class EventLocationChange(var location: Location) : Event() diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.java b/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.java deleted file mode 100644 index 546d6f8624..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.java +++ /dev/null @@ -1,17 +0,0 @@ -package info.nightscout.androidaps.events; - - -import info.nightscout.androidaps.utils.StringUtils; - -public class EventNetworkChange extends Event { - - public boolean mobileConnected = false; - public boolean wifiConnected = false; - - public String ssid = ""; - public boolean roaming = false; - - public String getSsid() { - return StringUtils.removeSurroundingQuotes(ssid); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.kt b/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.kt new file mode 100644 index 0000000000..62c8bdd13e --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/events/EventNetworkChange.kt @@ -0,0 +1,16 @@ +package info.nightscout.androidaps.events + +import info.nightscout.androidaps.utils.StringUtils + +class EventNetworkChange : Event() { + + var mobileConnected = false + var wifiConnected = false + + var ssid = "" + var roaming = false + + fun connectedSsid(): String { + return StringUtils.removeSurroundingQuotes(ssid) + } +} 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 c87e938d8a..d555ed3615 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 @@ -477,7 +477,7 @@ public class LoopPlugin extends PluginBase { NSUpload.uploadDeviceStatus(); SP.incInt(R.string.key_ObjectivesmanualEnacts); } - MainApp.bus().post(new EventAcceptOpenLoopChange()); + RxBus.INSTANCE.send(new EventAcceptOpenLoopChange()); } }); FabricPrivacy.getInstance().logCustom("AcceptTemp"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java index 84094095d5..ba10e81583 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsid.java @@ -59,13 +59,13 @@ public class TriggerWifiSsid extends Trigger { if (lastRun > DateUtil.now() - T.mins(5).msecs()) return false; - if (!eventNetworkChange.wifiConnected && comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) { + if (!eventNetworkChange.getWifiConnected() && comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); return true; } - boolean doRun = eventNetworkChange.wifiConnected && comparator.getValue().check(eventNetworkChange.getSsid(), getValue()); + boolean doRun = eventNetworkChange.getWifiConnected() && comparator.getValue().check(eventNetworkChange.connectedSsid(), getValue()); if (doRun) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalFragment.java index c7f6e1bf64..42ae7fd6fc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/careportal/CareportalFragment.java @@ -4,14 +4,14 @@ package info.nightscout.androidaps.plugins.general.careportal; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; -import androidx.fragment.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,19 +22,18 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.events.EventCareportalEventChange; -import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; -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.careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.general.nsclient.data.NSSettingsStatus; import info.nightscout.androidaps.plugins.general.overview.OverviewFragment; -import info.nightscout.androidaps.utils.DecimalFormatter; import info.nightscout.androidaps.utils.FabricPrivacy; -import info.nightscout.androidaps.utils.SP; -import info.nightscout.androidaps.utils.SetWarnColor; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; -public class CareportalFragment extends SubscriberFragment implements View.OnClickListener { +public class CareportalFragment extends Fragment implements View.OnClickListener { private static Logger log = LoggerFactory.getLogger(CareportalFragment.class); + private CompositeDisposable disposable = new CompositeDisposable(); TextView iage; TextView cage; @@ -71,59 +70,69 @@ public class CareportalFragment extends SubscriberFragment implements View.OnCli @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - try { - View view = inflater.inflate(R.layout.careportal_fragment, container, false); + View view = inflater.inflate(R.layout.careportal_fragment, container, false); - view.findViewById(R.id.careportal_bgcheck).setOnClickListener(this); - view.findViewById(R.id.careportal_announcement).setOnClickListener(this); - view.findViewById(R.id.careportal_cgmsensorinsert).setOnClickListener(this); - view.findViewById(R.id.careportal_cgmsensorstart).setOnClickListener(this); - view.findViewById(R.id.careportal_combobolus).setOnClickListener(this); - view.findViewById(R.id.careportal_correctionbolus).setOnClickListener(this); - view.findViewById(R.id.careportal_carbscorrection).setOnClickListener(this); - view.findViewById(R.id.careportal_exercise).setOnClickListener(this); - view.findViewById(R.id.careportal_insulincartridgechange).setOnClickListener(this); - view.findViewById(R.id.careportal_pumpbatterychange).setOnClickListener(this); - view.findViewById(R.id.careportal_mealbolus).setOnClickListener(this); - view.findViewById(R.id.careportal_note).setOnClickListener(this); - view.findViewById(R.id.careportal_profileswitch).setOnClickListener(this); - view.findViewById(R.id.careportal_pumpsitechange).setOnClickListener(this); - view.findViewById(R.id.careportal_question).setOnClickListener(this); - view.findViewById(R.id.careportal_snackbolus).setOnClickListener(this); - view.findViewById(R.id.careportal_tempbasalend).setOnClickListener(this); - view.findViewById(R.id.careportal_tempbasalstart).setOnClickListener(this); - view.findViewById(R.id.careportal_openapsoffline).setOnClickListener(this); - view.findViewById(R.id.careportal_temporarytarget).setOnClickListener(this); + view.findViewById(R.id.careportal_bgcheck).setOnClickListener(this); + view.findViewById(R.id.careportal_announcement).setOnClickListener(this); + view.findViewById(R.id.careportal_cgmsensorinsert).setOnClickListener(this); + view.findViewById(R.id.careportal_cgmsensorstart).setOnClickListener(this); + view.findViewById(R.id.careportal_combobolus).setOnClickListener(this); + view.findViewById(R.id.careportal_correctionbolus).setOnClickListener(this); + view.findViewById(R.id.careportal_carbscorrection).setOnClickListener(this); + view.findViewById(R.id.careportal_exercise).setOnClickListener(this); + view.findViewById(R.id.careportal_insulincartridgechange).setOnClickListener(this); + view.findViewById(R.id.careportal_pumpbatterychange).setOnClickListener(this); + view.findViewById(R.id.careportal_mealbolus).setOnClickListener(this); + view.findViewById(R.id.careportal_note).setOnClickListener(this); + view.findViewById(R.id.careportal_profileswitch).setOnClickListener(this); + view.findViewById(R.id.careportal_pumpsitechange).setOnClickListener(this); + view.findViewById(R.id.careportal_question).setOnClickListener(this); + view.findViewById(R.id.careportal_snackbolus).setOnClickListener(this); + view.findViewById(R.id.careportal_tempbasalend).setOnClickListener(this); + view.findViewById(R.id.careportal_tempbasalstart).setOnClickListener(this); + view.findViewById(R.id.careportal_openapsoffline).setOnClickListener(this); + view.findViewById(R.id.careportal_temporarytarget).setOnClickListener(this); - iage = view.findViewById(R.id.careportal_insulinage); - cage = view.findViewById(R.id.careportal_canulaage); - sage = view.findViewById(R.id.careportal_sensorage); - pbage = view.findViewById(R.id.careportal_pbage); + iage = view.findViewById(R.id.careportal_insulinage); + cage = view.findViewById(R.id.careportal_canulaage); + sage = view.findViewById(R.id.careportal_sensorage); + pbage = view.findViewById(R.id.careportal_pbage); - statsLayout = view.findViewById(R.id.careportal_stats); + statsLayout = view.findViewById(R.id.careportal_stats); - noProfileView = view.findViewById(R.id.profileview_noprofile); - butonsLayout = view.findViewById(R.id.careportal_buttons); + noProfileView = view.findViewById(R.id.profileview_noprofile); + butonsLayout = view.findViewById(R.id.careportal_buttons); - ProfileStore profileStore = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null ? ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getProfile() : null; - if (profileStore == null) { - noProfileView.setVisibility(View.VISIBLE); - butonsLayout.setVisibility(View.GONE); - } else { - noProfileView.setVisibility(View.GONE); - butonsLayout.setVisibility(View.VISIBLE); - } - - if (Config.NSCLIENT) - statsLayout.setVisibility(View.GONE); // visible on overview - - updateGUI(); - return view; - } catch (Exception e) { - FabricPrivacy.logException(e); + ProfileStore profileStore = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface() != null ? ConfigBuilderPlugin.getPlugin().getActiveProfileInterface().getProfile() : null; + if (profileStore == null) { + noProfileView.setVisibility(View.VISIBLE); + butonsLayout.setVisibility(View.GONE); + } else { + noProfileView.setVisibility(View.GONE); + butonsLayout.setVisibility(View.VISIBLE); } - return null; + if (Config.NSCLIENT) + statsLayout.setVisibility(View.GONE); // visible on overview + + return view; + } + + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventCareportalEventChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGUI(), FabricPrivacy::logException) + ); + updateGUI(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); } @Override @@ -203,12 +212,6 @@ public class CareportalFragment extends SubscriberFragment implements View.OnCli newDialog.show(manager, "NewNSTreatmentDialog"); } - @Subscribe - public void onStatusEvent(final EventCareportalEventChange c) { - updateGUI(); - } - - @Override protected void updateGUI() { Activity activity = getActivity(); updateAge(activity, sage, iage, cage, pbage); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientPlugin.java index e7cfc7a0dc..cb069bf58d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientPlugin.java @@ -116,6 +116,11 @@ public class NSClientPlugin extends PluginBase { MainApp.bus().post(new EventNSClientUpdateGUI()); }, FabricPrivacy::logException) ); + disposable.add(RxBus.INSTANCE + .toObservable(EventNetworkChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> nsClientReceiverDelegate.onStatusEvent(event), FabricPrivacy::logException) + ); } @Override @@ -139,12 +144,6 @@ public class NSClientPlugin extends PluginBase { nsClientReceiverDelegate.onStatusEvent(ev); } - @Subscribe - public void onStatusEvent(final EventNetworkChange ev) { - nsClientReceiverDelegate.onStatusEvent(ev); - } - - private ServiceConnection mConnection = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java index 072ad6cd56..f7df37aa0f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java @@ -46,7 +46,7 @@ class NsClientReceiverDelegate { EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(context); if (event != null) - bus.post(event); + RxBus.INSTANCE.send(event); context.registerReceiver(chargingStateReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); @@ -69,7 +69,7 @@ class NsClientReceiverDelegate { ) { EventNetworkChange event = networkChangeReceiver.grabNetworkStatus(MainApp.instance().getApplicationContext()); if (event != null) - bus.post(event); + RxBus.INSTANCE.send(event); } else if (ev.isChanged(R.string.key_ns_chargingonly)) { EventChargingState event = chargingStateReceiver.grabChargingState(MainApp.instance().getApplicationContext()); if (event != null) @@ -123,13 +123,13 @@ class NsClientReceiverDelegate { boolean newAllowedState = true; - if (ev.wifiConnected) { + if (ev.getWifiConnected()) { if (!allowedSSIDs.trim().isEmpty() && - (!allowedSSIDs.contains(ev.getSsid()) && !allowedSSIDs.contains(ev.ssid))) { + (!allowedSSIDs.contains(ev.connectedSsid()) && !allowedSSIDs.contains(ev.getSsid()))) { newAllowedState = false; } } else { - if ((!allowRoaming && ev.roaming) || wifiOnly) { + if ((!allowRoaming && ev.getRoaming()) || wifiOnly) { newAllowedState = false; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java index b3e14a7669..d360acd9c3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.java @@ -69,12 +69,15 @@ import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.T; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; import io.socket.client.IO; import io.socket.client.Socket; import io.socket.emitter.Emitter; public class NSClientService extends Service { private static Logger log = LoggerFactory.getLogger(L.NSCLIENT); + private CompositeDisposable disposable = new CompositeDisposable(); static public PowerManager.WakeLock mWakeLock; private IBinder mBinder = new NSClientService.LocalBinder(); @@ -114,6 +117,17 @@ public class NSClientService extends Service { public NSClientService() { registerBus(); + disposable.add(RxBus.INSTANCE + .toObservable(EventConfigBuilderChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (nsEnabled != NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL)) { + latestDateInReceivedData = 0; + destroy(); + initialize(); + } + }, FabricPrivacy::logException) + ); if (handler == null) { HandlerThread handlerThread = new HandlerThread(NSClientService.class.getSimpleName() + "Handler"); handlerThread.start(); @@ -185,15 +199,6 @@ public class NSClientService extends Service { } } - @Subscribe - public void onStatusEvent(EventConfigBuilderChange ev) { - if (nsEnabled != MainApp.getSpecificPlugin(NSClientPlugin.class).isEnabled(PluginType.GENERAL)) { - latestDateInReceivedData = 0; - destroy(); - initialize(); - } - } - @Subscribe public void onStatusEvent(final EventNSClientRestart ev) { latestDateInReceivedData = 0; 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 f16bd1c383..78be541095 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 @@ -900,6 +900,24 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, .subscribe(event -> scheduleUpdateGUI("EventTempTargetChange"), FabricPrivacy::logException )); + disposable.add(RxBus.INSTANCE + .toObservable(EventAcceptOpenLoopChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventAcceptOpenLoopChange"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventCareportalEventChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventCareportalEventChange"), + FabricPrivacy::logException + )); + disposable.add(RxBus.INSTANCE + .toObservable(EventInitializationChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> scheduleUpdateGUI("EventInitializationChanged"), + FabricPrivacy::logException + )); disposable.add(RxBus.INSTANCE .toObservable(EventPumpStatusChanged.class) .observeOn(AndroidSchedulers.mainThread()) @@ -917,11 +935,6 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, updateGUI("onResume"); } - @Subscribe - public void onStatusEvent(final EventInitializationChanged ev) { - scheduleUpdateGUI("EventInitializationChanged"); - } - @Subscribe public void onStatusEvent(final EventPreferenceChange ev) { scheduleUpdateGUI("EventPreferenceChange"); @@ -932,21 +945,11 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, scheduleUpdateGUI("EventAutosensCalculationFinished"); } - @Subscribe - public void onStatusEvent(final EventCareportalEventChange ev) { - scheduleUpdateGUI("EventCareportalEventChange"); - } - @Subscribe public void onStatusEvent(final EventNewOpenLoopNotification ev) { scheduleUpdateGUI("EventNewOpenLoopNotification"); } - @Subscribe - public void onStatusEvent(final EventAcceptOpenLoopChange ev) { - scheduleUpdateGUI("EventAcceptOpenLoopChange"); - } - @Subscribe public void onStatusEvent(final EventProfileNeedsUpdate ev) { scheduleUpdateGUI("EventProfileNeedsUpdate"); 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 dd9ff00c4c..1d26a0ec16 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 @@ -120,6 +120,12 @@ public class PersistentNotificationPlugin extends PluginBase { .subscribe(event -> triggerNotificationUpdate(false), FabricPrivacy::logException )); + disposable.add(RxBus.INSTANCE + .toObservable(EventInitializationChanged.class) + .observeOn(Schedulers.io()) + .subscribe(event -> triggerNotificationUpdate(false), + FabricPrivacy::logException + )); triggerNotificationUpdate(true); } @@ -331,9 +337,4 @@ public class PersistentNotificationPlugin extends PluginBase { triggerNotificationUpdate(false); } - @Subscribe - public void onStatusEvent(final EventInitializationChanged ev) { - triggerNotificationUpdate(false); - } - } 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 2f219de651..2d8736351b 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 @@ -116,6 +116,12 @@ public class StatuslinePlugin extends PluginBase { .subscribe(event -> sendStatus(), FabricPrivacy::logException )); + disposable.add(RxBus.INSTANCE + .toObservable(EventConfigBuilderChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> sendStatus(), + FabricPrivacy::logException + )); } @Override @@ -211,9 +217,4 @@ public class StatuslinePlugin extends PluginBase { sendStatus(); } - @Subscribe - public void onStatusEvent(final EventConfigBuilderChange 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 444b2db67a..8ede44ce9e 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 @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.iob.iobCobCalculator; import android.os.SystemClock; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.collection.LongSparseArray; @@ -31,16 +32,20 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin; +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.iob.iobCobCalculator.events.EventNewHistoryData; -import info.nightscout.androidaps.plugins.aps.openAPSSMB.OpenAPSSMBPlugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin; import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.T; +import io.reactivex.disposables.CompositeDisposable; +import io.reactivex.schedulers.Schedulers; import static info.nightscout.androidaps.utils.DateUtil.now; @@ -50,6 +55,7 @@ import static info.nightscout.androidaps.utils.DateUtil.now; public class IobCobCalculatorPlugin extends PluginBase { private Logger log = LoggerFactory.getLogger(L.AUTOSENS); + private CompositeDisposable disposable = new CompositeDisposable(); private static IobCobCalculatorPlugin plugin = null; @@ -83,14 +89,34 @@ public class IobCobCalculatorPlugin extends PluginBase { @Override protected void onStart() { - MainApp.bus().register(this); super.onStart(); + MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventConfigBuilderChange.class) + .observeOn(Schedulers.io()) + .subscribe(event -> { + if (this != getPlugin()) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Ignoring event for non default instance"); + return; + } + stopCalculation("onEventConfigBuilderChange"); + synchronized (dataLock) { + if (L.isEnabled(L.AUTOSENS)) + log.debug("Invalidating cached data because of configuration change. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records"); + iobTable = new LongSparseArray<>(); + autosensDataTable = new LongSparseArray<>(); + } + runCalculation("onEventConfigBuilderChange", System.currentTimeMillis(), false, true, event); + }, FabricPrivacy::logException) + ); } @Override protected void onStop() { - super.onStop(); + disposable.clear(); MainApp.bus().unregister(this); + super.onStop(); } public LongSparseArray getAutosensDataTable() { @@ -529,7 +555,7 @@ public class IobCobCalculatorPlugin extends PluginBase { count++; } } - return sum /count; + return sum / count; } @Nullable @@ -712,7 +738,7 @@ public class IobCobCalculatorPlugin extends PluginBase { ev.isChanged(R.string.key_absorption_cutoff) || ev.isChanged(R.string.key_openapsama_autosens_max) || ev.isChanged(R.string.key_openapsama_autosens_min) - ) { + ) { stopCalculation("onEventPreferenceChange"); synchronized (dataLock) { if (L.isEnabled(L.AUTOSENS)) @@ -725,23 +751,6 @@ public class IobCobCalculatorPlugin extends PluginBase { } } - @Subscribe - public void onEventConfigBuilderChange(EventConfigBuilderChange ev) { - if (this != getPlugin()) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Ignoring event for non default instance"); - return; - } - stopCalculation("onEventConfigBuilderChange"); - synchronized (dataLock) { - if (L.isEnabled(L.AUTOSENS)) - log.debug("Invalidating cached data because of configuration change. IOB: " + iobTable.size() + " Autosens: " + autosensDataTable.size() + " records"); - iobTable = new LongSparseArray<>(); - autosensDataTable = new LongSparseArray<>(); - } - runCalculation("onEventConfigBuilderChange", System.currentTimeMillis(), false, true, ev); - } - // When historical data is changed (comming from NS etc) finished calculations after this date must be invalidated @Subscribe public void onEventNewHistoryData(EventNewHistoryData ev) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java index a8f77ea54c..f8969e9594 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.java @@ -13,8 +13,7 @@ import android.widget.RadioButton; import android.widget.TextView; import androidx.annotation.NonNull; - -import com.squareup.otto.Subscribe; +import androidx.fragment.app.Fragment; import java.text.DecimalFormat; @@ -23,19 +22,24 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.ProfileStore; import info.nightscout.androidaps.events.EventInitializationChanged; import info.nightscout.androidaps.interfaces.PumpDescription; -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.careportal.CareportalFragment; import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.general.careportal.OptionsToShow; import info.nightscout.androidaps.utils.DecimalFormatter; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.NumberPicker; import info.nightscout.androidaps.utils.SafeParse; import info.nightscout.androidaps.utils.TimeListEdit; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; import static info.nightscout.androidaps.plugins.insulin.InsulinOrefBasePlugin.MIN_DIA; -public class LocalProfileFragment extends SubscriberFragment { +public class LocalProfileFragment extends Fragment { + private CompositeDisposable disposable = new CompositeDisposable(); + private NumberPicker diaView; private RadioButton mgdlView; private RadioButton mmolView; @@ -143,6 +147,23 @@ public class LocalProfileFragment extends SubscriberFragment { return layout; } + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventInitializationChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGUI(), FabricPrivacy::logException) + ); + updateGUI(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + public void doEdit() { LocalProfilePlugin.getPlugin().setEdited(true); updateGUI(); @@ -157,12 +178,6 @@ public class LocalProfileFragment extends SubscriberFragment { return MainApp.gs(R.string.localprofile); } - @Subscribe - public void onStatusEvent(final EventInitializationChanged e) { - updateGUI(); - } - - @Override protected void updateGUI() { Activity activity = getActivity(); if (activity != null) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java index b8a7d16be5..ecf4fc423b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/simple/SimpleProfileFragment.java @@ -1,7 +1,6 @@ package info.nightscout.androidaps.plugins.profile.simple; -import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; @@ -13,19 +12,24 @@ import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; -import com.squareup.otto.Subscribe; +import androidx.fragment.app.Fragment; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventInitializationChanged; +import info.nightscout.androidaps.plugins.bus.RxBus; +import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.general.careportal.CareportalFragment; import info.nightscout.androidaps.plugins.general.careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.general.careportal.OptionsToShow; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SafeParse; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; + +public class SimpleProfileFragment extends Fragment { + private CompositeDisposable disposable = new CompositeDisposable(); -public class SimpleProfileFragment extends SubscriberFragment { EditText diaView; RadioButton mgdlView; RadioButton mmolView; @@ -122,27 +126,34 @@ public class SimpleProfileFragment extends SubscriberFragment { return layout; } - @Subscribe - public void onStatusEvent(final EventInitializationChanged e) { + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventInitializationChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGUI(), FabricPrivacy::logException) + ); updateGUI(); } @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - boolean isValid = SimpleProfilePlugin.getPlugin().getProfile() != null && SimpleProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid(MainApp.gs(R.string.simpleprofile)); - if (!ConfigBuilderPlugin.getPlugin().getActivePump().isInitialized() || ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended() || !isValid) { - profileswitchButton.setVisibility(View.GONE); - } else { - profileswitchButton.setVisibility(View.VISIBLE); - } - if (isValid) - invalidProfile.setVisibility(View.GONE); - else - invalidProfile.setVisibility(View.VISIBLE); - }); + boolean isValid = SimpleProfilePlugin.getPlugin().getProfile() != null && SimpleProfilePlugin.getPlugin().getProfile().getDefaultProfile().isValid(MainApp.gs(R.string.simpleprofile)); + if (!ConfigBuilderPlugin.getPlugin().getActivePump().isInitialized() || ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended() || !isValid) { + profileswitchButton.setVisibility(View.GONE); + } else { + profileswitchButton.setVisibility(View.VISIBLE); + } + if (isValid) + invalidProfile.setVisibility(View.GONE); + else + invalidProfile.setVisibility(View.VISIBLE); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java index affbf49465..e867189a80 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/combo/ComboPlugin.java @@ -375,7 +375,6 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint setValidBasalRateProfileSelectedOnPump(true); pump.initialized = true; - MainApp.bus().post(new EventInitializationChanged()); RxBus.INSTANCE.send(new EventInitializationChanged()); // show notification to check pump date if last bolus is older than 24 hours diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.java index d5b4fd3158..23520f3491 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/activities/DanaRUserOptionsActivity.java @@ -1,14 +1,11 @@ package info.nightscout.androidaps.plugins.pump.danaR.activities; -import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Switch; -import com.squareup.otto.Subscribe; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,12 +18,16 @@ import info.nightscout.androidaps.activities.NoSplashActivity; import info.nightscout.androidaps.events.EventInitializationChanged; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPlugin; import info.nightscout.androidaps.plugins.pump.danaR.DanaRPump; import info.nightscout.androidaps.plugins.pump.danaRS.DanaRSPlugin; import info.nightscout.androidaps.plugins.pump.danaRv2.DanaRv2Plugin; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.NumberPicker; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; /** * Created by Rumen Georgiev on 5/31/2018. @@ -34,6 +35,7 @@ import info.nightscout.androidaps.utils.NumberPicker; public class DanaRUserOptionsActivity extends NoSplashActivity { private static Logger log = LoggerFactory.getLogger(L.PUMP); + private CompositeDisposable disposable = new CompositeDisposable(); Switch timeFormat; Switch buttonScroll; @@ -54,15 +56,19 @@ public class DanaRUserOptionsActivity extends NoSplashActivity { boolean isDanaRv2 = MainApp.getSpecificPlugin(DanaRv2Plugin.class) != null && MainApp.getSpecificPlugin(DanaRv2Plugin.class).isEnabled(PluginType.PUMP); @Override - protected void onResume() { + protected synchronized void onResume() { super.onResume(); - MainApp.bus().register(this); + disposable.add(RxBus.INSTANCE + .toObservable(EventInitializationChanged.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> setData(), FabricPrivacy::logException) + ); } @Override - protected void onPause() { + protected synchronized void onPause() { + disposable.clear(); super.onPause(); - MainApp.bus().unregister(this); } @Override @@ -144,11 +150,6 @@ public class DanaRUserOptionsActivity extends NoSplashActivity { lowReservoir.setValue((double) pump.lowReservoirRate); } - @Subscribe - public void onEventInitializationChanged(EventInitializationChanged ignored) { - runOnUiThread(this::setData); - } - public void onSaveClick() { if (!isRS && !isDanaR && !isDanaRv2) { //exit if pump is not DanaRS, Dana!, or DanaR with upgraded firmware diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/DanaRExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/DanaRExecutionService.java index b30da3f004..49e79acd1d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/DanaRExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaR/services/DanaRExecutionService.java @@ -193,7 +193,6 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { danaRPump.lastConnection = 0; danaRPump.lastSettingsRead = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); RxBus.INSTANCE.send(new EventInitializationChanged()); return; } @@ -211,7 +210,6 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService { } RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); RxBus.INSTANCE.send(new EventInitializationChanged()); NSUpload.uploadDeviceStatus(); if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/services/DanaRKoreanExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/services/DanaRKoreanExecutionService.java index 9f59beb096..aed225b68d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/services/DanaRKoreanExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRKorean/services/DanaRKoreanExecutionService.java @@ -194,7 +194,6 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { danaRPump.lastConnection = 0; danaRPump.lastSettingsRead = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); RxBus.INSTANCE.send(new EventInitializationChanged()); return; } @@ -214,7 +213,6 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService { } RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); RxBus.INSTANCE.send(new EventInitializationChanged()); NSUpload.uploadDeviceStatus(); if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/DanaRSService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/DanaRSService.java index 8da728692b..eb8ece1c06 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/DanaRSService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/DanaRSService.java @@ -163,7 +163,6 @@ public class DanaRSService extends Service { // deinitialize pump danaRPump.lastConnection = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); RxBus.INSTANCE.send(new EventInitializationChanged()); return; } @@ -184,7 +183,6 @@ public class DanaRSService extends Service { //deinitialize pump danaRPump.lastConnection = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); RxBus.INSTANCE.send(new EventInitializationChanged()); return; } else { @@ -214,8 +212,7 @@ public class DanaRSService extends Service { loadEvents(); - MainApp.bus().post(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); + RxBus.INSTANCE.send(new EventDanaRNewStatus()); RxBus.INSTANCE.send(new EventInitializationChanged()); NSUpload.uploadDeviceStatus(); if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/services/DanaRv2ExecutionService.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/services/DanaRv2ExecutionService.java index 3be2f16c73..b4f43f01e7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/services/DanaRv2ExecutionService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRv2/services/DanaRv2ExecutionService.java @@ -202,7 +202,6 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { danaRPump.lastConnection = 0; danaRPump.lastSettingsRead = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); RxBus.INSTANCE.send(new EventInitializationChanged()); return; } @@ -224,7 +223,6 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { //deinitialize pump danaRPump.lastConnection = 0; RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); RxBus.INSTANCE.send(new EventInitializationChanged()); return; } else { @@ -258,7 +256,6 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService { loadEvents(); RxBus.INSTANCE.send(new EventDanaRNewStatus()); - MainApp.bus().post(new EventInitializationChanged()); RxBus.INSTANCE.send(new EventInitializationChanged()); NSUpload.uploadDeviceStatus(); if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java index 2977b306f4..99118549a2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java @@ -148,7 +148,6 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con alertService = ((InsightAlertService.LocalBinder) binder).getService(); } if (connectionService != null && alertService != null) { - MainApp.bus().post(new EventInitializationChanged()); RxBus.INSTANCE.send(new EventInitializationChanged()); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsCareportalFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsCareportalFragment.java index 4a0e767cbb..6dcedd7f3e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsCareportalFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/fragments/TreatmentsCareportalFragment.java @@ -1,6 +1,5 @@ package info.nightscout.androidaps.plugins.treatments.fragments; -import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.graphics.Paint; @@ -13,30 +12,33 @@ import android.widget.TextView; import androidx.appcompat.app.AlertDialog; import androidx.cardview.widget.CardView; +import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import com.squareup.otto.Subscribe; - import java.util.List; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.events.EventCareportalEventChange; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue; import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart; import info.nightscout.androidaps.utils.DateUtil; +import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.SP; import info.nightscout.androidaps.utils.Translator; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.CompositeDisposable; /** * Created by mike on 13/01/17. */ -public class TreatmentsCareportalFragment extends SubscriberFragment implements View.OnClickListener { +public class TreatmentsCareportalFragment extends Fragment implements View.OnClickListener { + private CompositeDisposable disposable = new CompositeDisposable(); RecyclerView recyclerView; LinearLayoutManager llm; @@ -148,10 +150,26 @@ public class TreatmentsCareportalFragment extends SubscriberFragment implements if (nsUploadOnly) refreshFromNS.setVisibility(View.GONE); - updateGUI(); return view; } + @Override + public synchronized void onResume() { + super.onResume(); + disposable.add(RxBus.INSTANCE + .toObservable(EventCareportalEventChange.class) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(event -> updateGui(), FabricPrivacy::logException) + ); + updateGui(); + } + + @Override + public synchronized void onPause() { + super.onPause(); + disposable.clear(); + } + @Override public void onClick(View view) { switch (view.getId()) { @@ -182,21 +200,8 @@ public class TreatmentsCareportalFragment extends SubscriberFragment implements } - @Subscribe - public void onStatusEvent(final EventCareportalEventChange ev) { - updateGUI(); - } - - @Override - protected void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)), false); - } - }); + private void updateGui() { + recyclerView.swapAdapter(new RecyclerViewAdapter(MainApp.getDbHelper().getCareportalEvents(false)), false); } private void removeAndroidAPSStatedEvents() { diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java index 2695ad64f2..a920d13698 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/NetworkChangeReceiver.java @@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.EventNetworkChange; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.bus.RxBus; public class NetworkChangeReceiver extends BroadcastReceiver { @@ -27,7 +28,7 @@ public class NetworkChangeReceiver extends BroadcastReceiver { public void onReceive(final Context context, final Intent intent) { EventNetworkChange event = grabNetworkStatus(context); if (event != null) - MainApp.bus().post(event); + RxBus.INSTANCE.send(event); } @Nullable @@ -40,23 +41,23 @@ public class NetworkChangeReceiver extends BroadcastReceiver { if (activeNetwork != null) { if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI && activeNetwork.isConnected()) { - event.wifiConnected = true; + event.setWifiConnected(true); WifiManager wifiManager = (WifiManager) MainApp.instance().getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (wifiManager != null) { WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) { - event.ssid = wifiInfo.getSSID(); + event.setSsid(wifiInfo.getSSID()); } if (L.isEnabled(L.CORE)) - log.debug("NETCHANGE: Wifi connected. SSID: " + event.ssid); + log.debug("NETCHANGE: Wifi connected. SSID: " + event.connectedSsid()); } } if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { - event.mobileConnected = true; - event.roaming = activeNetwork.isRoaming(); + event.setMobileConnected(true); + event.setRoaming(activeNetwork.isRoaming()); if (L.isEnabled(L.CORE)) - log.debug("NETCHANGE: Mobile connected. Roaming: " + event.roaming); + log.debug("NETCHANGE: Mobile connected. Roaming: " + event.getRoaming()); } } else { if (L.isEnabled(L.CORE)) @@ -68,11 +69,11 @@ public class NetworkChangeReceiver extends BroadcastReceiver { } public static boolean isWifiConnected() { - return lastEvent != null && lastEvent.wifiConnected; + return lastEvent != null && lastEvent.getWifiConnected(); } public static boolean isConnected() { - return lastEvent != null && (lastEvent.wifiConnected || lastEvent.mobileConnected); + return lastEvent != null && (lastEvent.getWifiConnected() || lastEvent.getMobileConnected()); } public static EventNetworkChange getLastEvent() { diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java index 8cc02bf687..718e21b099 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java @@ -19,6 +19,7 @@ import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; +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.constraints.objectives.ObjectivesFragment; @@ -170,7 +171,7 @@ public class SWDefinition { NSClientPlugin.getPlugin().setFragmentVisible(PluginType.GENERAL, true); ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(NSClientPlugin.getPlugin(), PluginType.GENERAL); ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard"); - MainApp.bus().post(new EventConfigBuilderChange()); + RxBus.INSTANCE.send(new EventConfigBuilderChange()); MainApp.bus().post(new EventSWUpdate(true)); }) .visibility(() -> !NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL))) @@ -372,7 +373,7 @@ public class SWDefinition { LoopPlugin.getPlugin().setFragmentVisible(PluginType.LOOP, true); ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(LoopPlugin.getPlugin(), PluginType.LOOP); ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard"); - MainApp.bus().post(new EventConfigBuilderChange()); + RxBus.INSTANCE.send(new EventConfigBuilderChange()); MainApp.bus().post(new EventSWUpdate(true)); }) .visibility(() -> !LoopPlugin.getPlugin().isEnabled(PluginType.LOOP))) diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java index 8c0be61598..e09cbb6e17 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java @@ -17,7 +17,7 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.EventConfigBuilderChange; import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderFragment; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.setupwizard.events.EventSWUpdate; @@ -88,7 +88,7 @@ public class SWPlugin extends SWItem { plugin.setFragmentVisible(pType, rb.isChecked() && makeVisible); ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(plugin, pType); ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard"); - MainApp.bus().post(new EventConfigBuilderChange()); + RxBus.INSTANCE.send(new EventConfigBuilderChange()); MainApp.bus().post(new EventSWUpdate()); }); layout.addView(radioGroup); diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.java index 8156e483cc..6c5a359ec7 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerWifiSsidTest.java @@ -36,22 +36,22 @@ public class TriggerWifiSsidTest { TriggerWifiSsid t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL); - e.wifiConnected = false; + e.setWifiConnected(false); Assert.assertFalse(t.shouldRun()); - e.wifiConnected = true; - e.ssid = "otherSSID"; + e.setWifiConnected(true); + e.setSsid("otherSSID"); Assert.assertFalse(t.shouldRun()); - e.wifiConnected = true; - e.ssid = "aSSID"; + e.setWifiConnected(true); + e.setSsid("aSSID"); Assert.assertTrue(t.shouldRun()); t.lastRun(now - 1); Assert.assertFalse(t.shouldRun()); t = new TriggerWifiSsid().setValue("aSSID").comparator(Comparator.Compare.IS_NOT_AVAILABLE); - e.wifiConnected = false; + e.setWifiConnected(false); Assert.assertTrue(t.shouldRun()); // no network data diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.java index 0400532e9d..0abbf50399 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegateTest.java @@ -64,67 +64,67 @@ public class NsClientReceiverDelegateTest { when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(false); when(SP.getString(anyInt(), anyString())).thenReturn(""); EventNetworkChange ev = new EventNetworkChange(); - ev.ssid = ""; + ev.setSsid(""); - ev.mobileConnected = true; - ev.wifiConnected = true; + ev.setMobileConnected(true); + ev.setWifiConnected(true); assertTrue(sut.calculateStatus(ev)); - ev.ssid = "test"; + ev.setSsid("test"); when(SP.getString(anyInt(), anyString())).thenReturn("\"test\""); assertTrue(sut.calculateStatus(ev)); - ev.ssid = "\"test\""; + ev.setSsid("\"test\""); assertTrue(sut.calculateStatus(ev)); - ev.wifiConnected = false; + ev.setWifiConnected(false); assertTrue(sut.calculateStatus(ev)); // wifiOnly = true // allowRoaming = true as well when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(true); - ev.wifiConnected = true; + ev.setWifiConnected(true); assertTrue(sut.calculateStatus(ev)); - ev.wifiConnected = false; + ev.setWifiConnected(false); assertTrue(!sut.calculateStatus(ev)); // wifiOnly = false // allowRoaming = false as well when(SP.getBoolean(anyInt(), anyBoolean())).thenReturn(false); - ev.wifiConnected = false; - ev.roaming = true; + ev.setWifiConnected(false); + ev.setRoaming(true); assertTrue(!sut.calculateStatus(ev)); // wifiOnly = false // allowRoaming = true when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(false); when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true); - ev.wifiConnected = false; - ev.roaming = true; + ev.setWifiConnected(false); + ev.setRoaming(true); assertTrue(sut.calculateStatus(ev)); // wifiOnly = true // allowRoaming = true when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(true); when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true); - ev.wifiConnected = false; - ev.roaming = true; + ev.setWifiConnected(false); + ev.setRoaming(true); assertTrue(!sut.calculateStatus(ev)); // wifiOnly = true // allowRoaming = true when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(true); when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true); - ev.wifiConnected = true; - ev.roaming = true; + ev.setWifiConnected(true); + ev.setRoaming(true); assertTrue(sut.calculateStatus(ev)); // wifiOnly = false // allowRoaming = false when(SP.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(false); when(SP.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(false); - ev.wifiConnected = true; - ev.roaming = true; + ev.setWifiConnected(true); + ev.setRoaming(true); assertTrue(sut.calculateStatus(ev)); } }