diff --git a/app/src/main/java/info/nightscout/androidaps/RxBus.kt b/app/src/main/java/info/nightscout/androidaps/RxBus.kt index ae370e05f2..489f349ef8 100644 --- a/app/src/main/java/info/nightscout/androidaps/RxBus.kt +++ b/app/src/main/java/info/nightscout/androidaps/RxBus.kt @@ -12,8 +12,6 @@ object RxBus { publisher.onNext(event) } - fun toObservable(): Observable = publisher - // Listen should return an Observable and not the publisher // Using ofType we filter only events that match that class type fun toObservable(eventType: Class): Observable = publisher.ofType(eventType) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt index 3cd3199331..7458aa98c5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt @@ -23,10 +23,12 @@ import info.nightscout.androidaps.receivers.NetworkChangeReceiver import info.nightscout.androidaps.utils.SP import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.ToastUtils +import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.Disposable import org.slf4j.LoggerFactory import java.util.* + object TidepoolPlugin : PluginBase(PluginDescription() .mainType(PluginType.GENERAL) .pluginName(R.string.tidepool) @@ -36,21 +38,63 @@ object TidepoolPlugin : PluginBase(PluginDescription() .description(R.string.description_tidepool) ) { private val log = LoggerFactory.getLogger(L.TIDEPOOL) - private var disposable: Disposable? = null + private var disposable: CompositeDisposable = CompositeDisposable() private val listLog = ArrayList() @Suppress("DEPRECATION") // API level 24 to replace call var textLog = Html.fromHtml("") + operator fun CompositeDisposable.plusAssign(disposable: Disposable) { + add(disposable) + } + override fun onStart() { - disposable = RxBus - .toObservable() - .subscribe { event: Any -> onEvent(event) } + disposable += RxBus + .toObservable(EventTidepoolDoUpload::class.java) + .subscribe({ event -> doUpload() }, {}) + disposable += RxBus + .toObservable(EventTidepoolResetData::class.java) + .subscribe({ + if (TidepoolUploader.connectionStatus != TidepoolUploader.ConnectionStatus.CONNECTED) { + log.debug("Not connected for deleteDataset") + } else { + TidepoolUploader.deleteDataSet() + SP.putLong(R.string.key_tidepool_last_end, 0) + TidepoolUploader.doLogin() + } + }, {}) + disposable += RxBus + .toObservable(EventTidepoolStatus::class.java) + .subscribe({ event -> addToLog(event) }, {}) + disposable += RxBus + .toObservable(EventNewBG::class.java) + .subscribe({ event -> + if (event.bgReading!!.date!! < TidepoolUploader.getLastEnd()) + TidepoolUploader.setLastEnd(event.bgReading!!.date!!) + if (isEnabled(PluginType.GENERAL) + && (!SP.getBoolean(R.string.key_tidepool_only_while_charging, false) || ChargingStateReceiver.isCharging()) + && (!SP.getBoolean(R.string.key_tidepool_only_while_unmetered, false) || NetworkChangeReceiver.isWifiConnected()) + && RateLimit.ratelimit("tidepool-new-data-upload", T.mins(4).secs().toInt())) + doUpload() + }, {}) + disposable += RxBus + .toObservable(EventPreferenceChange::class.java) + .subscribe({ event -> + if (event.isChanged(R.string.key_tidepool_dev_servers) + || event.isChanged(R.string.key_tidepool_username) + || event.isChanged(R.string.key_tidepool_password) + ) + TidepoolUploader.resetInstance() + }, {}) + disposable += RxBus + .toObservable(EventNetworkChange::class.java) + .subscribe({}, {}) // TODO start upload on wifi connect + super.onStart() } override fun onStop() { - disposable?.dispose() + disposable.clear() super.onStop() } @@ -61,40 +105,6 @@ object TidepoolPlugin : PluginBase(PluginDescription() TidepoolUploader.doUpload() } - private fun onEvent(event: Any) { - when (event) { - is EventTidepoolDoUpload -> doUpload() - is EventTidepoolResetData -> { - if (TidepoolUploader.connectionStatus != TidepoolUploader.ConnectionStatus.CONNECTED) { - log.debug("Not connected for deleteDataset") - return - } - TidepoolUploader.deleteDataSet() - SP.putLong(R.string.key_tidepool_last_end, 0) - TidepoolUploader.doLogin() - } - is EventTidepoolStatus -> addToLog(event) - is EventNewBG -> { - if (event.bgReading!!.date!! < TidepoolUploader.getLastEnd()) - TidepoolUploader.setLastEnd(event.bgReading!!.date!!) - if (isEnabled(PluginType.GENERAL) - && (!SP.getBoolean(R.string.key_tidepool_only_while_charging, false) || ChargingStateReceiver.isCharging()) - && (!SP.getBoolean(R.string.key_tidepool_only_while_unmetered, false) || NetworkChangeReceiver.isWifiConnected()) - && RateLimit.ratelimit("tidepool-new-data-upload", T.mins(4).secs().toInt())) - doUpload() - } - is EventPreferenceChange -> { - if (event.isChanged(R.string.key_tidepool_dev_servers) - || event.isChanged(R.string.key_tidepool_username) - || event.isChanged(R.string.key_tidepool_password) - ) - TidepoolUploader.resetInstance() - } - is EventNetworkChange -> { - } // TODO start upload on wifi connect - } - } - @Synchronized private fun addToLog(ev: EventTidepoolStatus) { synchronized(listLog) {