CompositeDisposable
This commit is contained in:
parent
ccf49b5e5a
commit
a7451c9b16
2 changed files with 49 additions and 41 deletions
|
@ -12,8 +12,6 @@ object RxBus {
|
||||||
publisher.onNext(event)
|
publisher.onNext(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toObservable(): Observable<Any> = publisher
|
|
||||||
|
|
||||||
// Listen should return an Observable and not the publisher
|
// Listen should return an Observable and not the publisher
|
||||||
// Using ofType we filter only events that match that class type
|
// Using ofType we filter only events that match that class type
|
||||||
fun <T> toObservable(eventType: Class<T>): Observable<T> = publisher.ofType(eventType)
|
fun <T> toObservable(eventType: Class<T>): Observable<T> = publisher.ofType(eventType)
|
||||||
|
|
|
@ -23,10 +23,12 @@ import info.nightscout.androidaps.receivers.NetworkChangeReceiver
|
||||||
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 info.nightscout.androidaps.utils.ToastUtils
|
import info.nightscout.androidaps.utils.ToastUtils
|
||||||
|
import io.reactivex.disposables.CompositeDisposable
|
||||||
import io.reactivex.disposables.Disposable
|
import io.reactivex.disposables.Disposable
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
object TidepoolPlugin : PluginBase(PluginDescription()
|
object TidepoolPlugin : PluginBase(PluginDescription()
|
||||||
.mainType(PluginType.GENERAL)
|
.mainType(PluginType.GENERAL)
|
||||||
.pluginName(R.string.tidepool)
|
.pluginName(R.string.tidepool)
|
||||||
|
@ -36,21 +38,63 @@ object TidepoolPlugin : PluginBase(PluginDescription()
|
||||||
.description(R.string.description_tidepool)
|
.description(R.string.description_tidepool)
|
||||||
) {
|
) {
|
||||||
private val log = LoggerFactory.getLogger(L.TIDEPOOL)
|
private val log = LoggerFactory.getLogger(L.TIDEPOOL)
|
||||||
private var disposable: Disposable? = null
|
private var disposable: CompositeDisposable = CompositeDisposable()
|
||||||
|
|
||||||
private val listLog = ArrayList<EventTidepoolStatus>()
|
private val listLog = ArrayList<EventTidepoolStatus>()
|
||||||
@Suppress("DEPRECATION") // API level 24 to replace call
|
@Suppress("DEPRECATION") // API level 24 to replace call
|
||||||
var textLog = Html.fromHtml("")
|
var textLog = Html.fromHtml("")
|
||||||
|
|
||||||
|
operator fun CompositeDisposable.plusAssign(disposable: Disposable) {
|
||||||
|
add(disposable)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
disposable = RxBus
|
disposable += RxBus
|
||||||
.toObservable()
|
.toObservable(EventTidepoolDoUpload::class.java)
|
||||||
.subscribe { event: Any -> onEvent(event) }
|
.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()
|
super.onStart()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStop() {
|
override fun onStop() {
|
||||||
disposable?.dispose()
|
disposable.clear()
|
||||||
super.onStop()
|
super.onStop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,40 +105,6 @@ object TidepoolPlugin : PluginBase(PluginDescription()
|
||||||
TidepoolUploader.doUpload()
|
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
|
@Synchronized
|
||||||
private fun addToLog(ev: EventTidepoolStatus) {
|
private fun addToLog(ev: EventTidepoolStatus) {
|
||||||
synchronized(listLog) {
|
synchronized(listLog) {
|
||||||
|
|
Loading…
Reference in a new issue