EOPatch: Elimiminate timber dependency

This commit is contained in:
Milos Kozak 2022-02-15 19:23:08 +01:00
parent 070e95ef62
commit e4858e34bf
16 changed files with 170 additions and 219 deletions

View file

@ -35,7 +35,6 @@ buildscript {
androidx_junit_version = '1.1.2'
androidx_rules_version = '1.4.0'
timber_version = "4.7.1"
rxandroidble_version = '1.12.1'
replayshare_version = '2.2.0'
}

View file

@ -42,7 +42,4 @@ dependencies {
//RxAndroidBle
implementation "com.polidea.rxandroidble2:rxandroidble:$rxandroidble_version"
implementation "com.jakewharton.rx2:replaying-share:$replayshare_version"
// Log
implementation "com.jakewharton.timber:timber:$timber_version"
}

View file

@ -25,7 +25,6 @@ import info.nightscout.androidaps.plugins.pump.eopatch.ble.IPreferenceManager
import info.nightscout.androidaps.plugins.pump.eopatch.code.BolusExDuration
import info.nightscout.androidaps.plugins.pump.eopatch.code.SettingKeys
import info.nightscout.androidaps.plugins.pump.eopatch.extension.takeOne
import info.nightscout.androidaps.plugins.pump.eopatch.extension.with
import info.nightscout.androidaps.plugins.pump.eopatch.ui.EopatchOverviewFragment
import info.nightscout.androidaps.plugins.pump.eopatch.vo.TempBasal
import info.nightscout.androidaps.queue.commands.CustomCommand
@ -34,6 +33,7 @@ import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.TimeChangeType
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.shared.sharedPreferences.SP
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
@ -41,7 +41,6 @@ import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.BehaviorSubject
import org.json.JSONObject
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.math.min
@ -53,11 +52,8 @@ class EopatchPumpPlugin @Inject constructor(
aapsLogger: AAPSLogger,
rh: ResourceHelper,
commandQueue: CommandQueue,
private val context: Context,
private val aapsSchedulers: AapsSchedulers,
private val rxBus: RxBus,
private val sp: SP,
private val profileFunction: ProfileFunction,
private val activePlugin: ActivePlugin,
private val fabricPrivacy: FabricPrivacy,
private val dateUtil: DateUtil,
private val pumpSync: PumpSync,
@ -81,12 +77,6 @@ class EopatchPumpPlugin @Inject constructor(
private var mLastDataTime: Long = 0
private val mPumpDescription = PumpDescription(mPumpType)
init {
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
}
override fun onStart() {
super.onStart()
mDisposables.add(rxBus
@ -128,7 +118,7 @@ class EopatchPumpPlugin @Inject constructor(
override fun onStop() {
super.onStop()
aapsLogger.debug(LTag.PUMP, "EOPatchPumpPlugin onStop()");
aapsLogger.debug(LTag.PUMP, "EOPatchPumpPlugin onStop()")
}
override fun onStateChange(type: PluginType?, oldState: State?, newState: State?) {
@ -348,7 +338,10 @@ class EopatchPumpPlugin @Inject constructor(
override fun stopBolusDelivering() {
patchmanager.stopNowBolus()
.with()
.subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.main)
.subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.main)
.subscribe { it ->
rxBus.send(EventOverviewBolusProgress.apply {
status = rh.gs(R.string.bolusdelivered, (it.injectedBolusAmount * 0.05f)) //todo stoped 메세지로 변경 필요
@ -362,7 +355,8 @@ class EopatchPumpPlugin @Inject constructor(
mLastDataTime = System.currentTimeMillis()
val tb = TempBasal.createAbsolute(durationInMinutes.toLong(), absoluteRate.toFloat())
return patchmanager.startTempBasal(tb)
.with()
.subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.main)
.doOnSuccess {
preferenceManager.getTempBasalManager().startedBasal = tb
preferenceManager.getTempBasalManager().startedBasal?.startTimestamp = System.currentTimeMillis()
@ -394,7 +388,8 @@ class EopatchPumpPlugin @Inject constructor(
mLastDataTime = System.currentTimeMillis()
val tb = TempBasal.createPercent(durationInMinutes.toLong(), percent)
return patchmanager.startTempBasal(tb)
.with()
.subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.main)
.doOnSuccess {
preferenceManager.getTempBasalManager().startedBasal = tb
preferenceManager.getTempBasalManager().startedBasal?.startTimestamp = System.currentTimeMillis()

View file

@ -1,35 +1,41 @@
@file:Suppress("unused")
package info.nightscout.androidaps.plugins.pump.eopatch
import android.os.SystemClock
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.shared.logging.AAPSLogger
import io.reactivex.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import org.reactivestreams.Subscription
import timber.log.Timber
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Singleton
enum class RxVoid {
@Singleton
class RxAction @Inject constructor(
private val aapsSchedulers: AapsSchedulers,
private val aapsLogger: AAPSLogger
) {
enum class RxVoid {
INSTANCE
}
}
class SilentObserver<T>(private val aapsLogger: AAPSLogger) : MaybeObserver<T>, SingleObserver<T>, Observer<T>, FlowableSubscriber<T> {
class SilentObserver<T> : MaybeObserver<T>, SingleObserver<T>, Observer<T>, FlowableSubscriber<T> {
override fun onSubscribe(d: Disposable) {}
override fun onSuccess(t: T) {}
override fun onError(e: Throwable) = Timber.d(e, "SilentObserver.onError() ignore")
override fun onError(e: Throwable) = aapsLogger.error("SilentObserver.onError() ignore", e)
override fun onComplete() {}
override fun onNext(t: T) {}
override fun onSubscribe(s: Subscription) {}
}
object RxAction {
private fun msleep(millis: Long) {
if (millis <= 0)
return
try {
Thread.sleep(millis)
} catch (e: InterruptedException) {
}
private fun sleep(millis: Long) {
if (millis <= 0)
return
SystemClock.sleep(millis)
}
private fun delay(delayMs: Long): Single<*> {
@ -42,7 +48,7 @@ object RxAction {
fun single(action: Runnable, delayMs: Long, scheduler: Scheduler): Single<*> {
return delay(delayMs)
.observeOn(scheduler)
.flatMap { o ->
.flatMap {
Single.fromCallable {
action.run()
RxVoid.INSTANCE
@ -50,64 +56,63 @@ object RxAction {
}
}
fun safeSingle(action: Runnable, delayMs: Long, scheduler: Scheduler): Single<*> {
private fun safeSingle(action: Runnable, delayMs: Long, scheduler: Scheduler): Single<*> {
return single(action, delayMs, scheduler)
}
@JvmOverloads
fun runOnComputationThread(action: Runnable, delayMs: Long = 0) {
single(action, delayMs, Schedulers.computation()).subscribe(SilentObserver())
single(action, delayMs, aapsSchedulers.cpu).subscribe(SilentObserver(aapsLogger))
}
@JvmOverloads
fun runOnIoThread(action: Runnable, delayMs: Long = 0) {
single(action, delayMs, Schedulers.io()).subscribe(SilentObserver())
single(action, delayMs, aapsSchedulers.io).subscribe(SilentObserver(aapsLogger))
}
@JvmOverloads
fun runOnNewThread(action: Runnable, delayMs: Long = 0) {
single(action, delayMs, Schedulers.newThread()).subscribe(SilentObserver())
single(action, delayMs, aapsSchedulers.newThread).subscribe(SilentObserver(aapsLogger))
}
@JvmOverloads
fun runOnMainThread(action: Runnable, delayMs: Long = 0) {
single(action, delayMs, AndroidSchedulers.mainThread()).subscribe(SilentObserver())
single(action, delayMs, aapsSchedulers.main).subscribe(SilentObserver(aapsLogger))
}
@JvmOverloads
fun safeRunOnComputationThread(action: Runnable, delayMs: Long = 0) {
safeSingle(action, delayMs, Schedulers.computation()).subscribe(SilentObserver())
safeSingle(action, delayMs, aapsSchedulers.cpu).subscribe(SilentObserver(aapsLogger))
}
@JvmOverloads
fun safeRunOnIoThread(action: Runnable, delayMs: Long = 0) {
safeSingle(action, delayMs, Schedulers.io()).subscribe(SilentObserver())
safeSingle(action, delayMs, aapsSchedulers.io).subscribe(SilentObserver(aapsLogger))
}
@JvmOverloads
fun safeRunOnNewThread(action: Runnable, delayMs: Long = 0) {
safeSingle(action, delayMs, Schedulers.newThread()).subscribe(SilentObserver())
safeSingle(action, delayMs, aapsSchedulers.newThread).subscribe(SilentObserver(aapsLogger))
}
@JvmOverloads
fun safeRunOnMainThread(action: Runnable, delayMs: Long = 0) {
safeSingle(action, delayMs, AndroidSchedulers.mainThread()).subscribe(SilentObserver())
safeSingle(action, delayMs, aapsSchedulers.main).subscribe(SilentObserver(aapsLogger))
}
fun singleOnMainThread(action: Runnable, delayMs: Long): Single<*> {
return single(action, delayMs, AndroidSchedulers.mainThread())
return single(action, delayMs, aapsSchedulers.main)
}
fun singleOnComputationThread(action: Runnable, delayMs: Long): Single<*> {
return single(action, delayMs, Schedulers.computation())
return single(action, delayMs, aapsSchedulers.cpu)
}
fun singleOnIoThread(action: Runnable, delayMs: Long): Single<*> {
return single(action, delayMs, Schedulers.io())
return single(action, delayMs, aapsSchedulers.io)
}
fun singleOnNewThread(action: Runnable, delayMs: Long): Single<*> {
return single(action, delayMs, Schedulers.newThread())
return single(action, delayMs, aapsSchedulers.newThread)
}
}

View file

@ -16,7 +16,7 @@ import info.nightscout.androidaps.plugins.pump.eopatch.EoPatchRxBus
import info.nightscout.androidaps.plugins.pump.eopatch.OsAlarmReceiver
import info.nightscout.androidaps.plugins.pump.eopatch.code.PatchLifecycle
import info.nightscout.androidaps.plugins.pump.eopatch.core.code.PatchAeCode
import info.nightscout.androidaps.plugins.pump.eopatch.extension.observeOnMainThread
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
@ -38,6 +38,7 @@ class AlarmRegistry @Inject constructor() : IAlarmRegistry {
@Inject lateinit var pm: IPreferenceManager
@Inject lateinit var rxBus: RxBus
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var aapsSchedulers: AapsSchedulers
private lateinit var mOsAlarmManager: AlarmManager
private var mDisposable: Disposable? = null
@ -46,7 +47,7 @@ class AlarmRegistry @Inject constructor() : IAlarmRegistry {
@Inject fun onInit() {
mOsAlarmManager = mContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
mDisposable = pm.observePatchLifeCycle()
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.subscribe {
when(it){
PatchLifecycle.REMOVE_NEEDLE_CAP -> {
@ -64,7 +65,7 @@ class AlarmRegistry @Inject constructor() : IAlarmRegistry {
it.keys.forEach {
sources.add(
Maybe.just(it)
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.doOnSuccess { rxBus.send(EventDismissNotification(Notification.EOELOW_PATCH_ALERTS + (it.aeCode + 10000))) }
)
}
@ -107,7 +108,7 @@ class AlarmRegistry @Inject constructor() : IAlarmRegistry {
override fun add(patchAeCodes: Set<PatchAeCode>) {
compositeDisposable.add(
Observable.fromIterable(patchAeCodes)
.filter{patchAeCodeItem -> AlarmCode.Companion.findByPatchAeCode(patchAeCodeItem.getAeValue()) != null}
.filter{patchAeCodeItem -> AlarmCode.findByPatchAeCode(patchAeCodeItem.getAeValue()) != null}
.observeOn(AndroidSchedulers.mainThread())
.filter { patchAeCodes -> AlarmCode.findByPatchAeCode(patchAeCodes.getAeValue()) != null }
.flatMapMaybe{aeCodeResponse -> add(AlarmCode.findByPatchAeCode(aeCodeResponse.getAeValue())!!,0L, true)}

View file

@ -3,6 +3,13 @@ package info.nightscout.androidaps.plugins.pump.eopatch.ble;
import android.content.Context;
import android.content.Intent;
import com.polidea.rxandroidble2.exceptions.BleException;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Singleton;
import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.events.EventCustomActionsChanged;
import info.nightscout.androidaps.events.EventPumpStatusChanged;
@ -11,42 +18,28 @@ import info.nightscout.androidaps.interfaces.ActivePlugin;
import info.nightscout.androidaps.interfaces.CommandQueue;
import info.nightscout.androidaps.interfaces.ProfileFunction;
import info.nightscout.androidaps.interfaces.PumpSync;
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.shared.logging.AAPSLogger;
import info.nightscout.shared.logging.LTag;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.pump.eopatch.CommonUtils;
import info.nightscout.androidaps.plugins.pump.common.defs.PumpType;
import info.nightscout.androidaps.plugins.pump.eopatch.R;
import info.nightscout.androidaps.plugins.pump.eopatch.RxAction;
import info.nightscout.androidaps.plugins.pump.eopatch.alarm.AlarmCode;
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.BleConnectionState;
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.IPatchScanner;
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.PatchScanner;
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.PatchSelfTestResult;
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.ScanList;
import info.nightscout.androidaps.plugins.pump.eopatch.code.BolusExDuration;
import info.nightscout.androidaps.plugins.pump.eopatch.code.DeactivationStatus;
import info.nightscout.androidaps.plugins.pump.eopatch.code.PatchLifecycle;
import com.polidea.rxandroidble2.RxBleClient;
import com.polidea.rxandroidble2.exceptions.BleException;
import com.polidea.rxandroidble2.internal.RxBleLog;
import java.util.concurrent.TimeUnit;
import info.nightscout.androidaps.plugins.pump.eopatch.code.SettingKeys;
import info.nightscout.androidaps.plugins.pump.eopatch.core.response.BasalScheduleSetResponse;
import info.nightscout.androidaps.plugins.pump.eopatch.core.response.BaseResponse;
import javax.inject.Inject;
import javax.inject.Singleton;
import info.nightscout.androidaps.plugins.pump.eopatch.core.response.BolusResponse;
import info.nightscout.androidaps.plugins.pump.eopatch.core.response.BolusStopResponse;
import info.nightscout.androidaps.plugins.pump.eopatch.core.response.ComboBolusStopResponse;
import info.nightscout.androidaps.plugins.pump.eopatch.core.response.PatchBooleanResponse;
import info.nightscout.androidaps.plugins.pump.eopatch.core.response.TempBasalScheduleSetResponse;
import info.nightscout.androidaps.plugins.pump.eopatch.core.response.TemperatureResponse;
import info.nightscout.androidaps.plugins.pump.eopatch.code.SettingKeys;
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.BleConnectionState;
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.IPatchScanner;
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.PatchScanner;
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.PatchSelfTestResult;
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.ScanList;
import info.nightscout.androidaps.plugins.pump.eopatch.event.EventPatchActivationNotComplete;
import info.nightscout.androidaps.plugins.pump.eopatch.ui.DialogHelperActivity;
import info.nightscout.androidaps.plugins.pump.eopatch.vo.BolusCurrent;
@ -55,8 +48,10 @@ import info.nightscout.androidaps.plugins.pump.eopatch.vo.PatchConfig;
import info.nightscout.androidaps.plugins.pump.eopatch.vo.PatchLifecycleEvent;
import info.nightscout.androidaps.plugins.pump.eopatch.vo.PatchState;
import info.nightscout.androidaps.plugins.pump.eopatch.vo.TempBasal;
import info.nightscout.androidaps.queue.commands.Command;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.resources.ResourceHelper;
import info.nightscout.shared.logging.AAPSLogger;
import info.nightscout.shared.logging.LTag;
import info.nightscout.shared.sharedPreferences.SP;
import io.reactivex.Observable;
import io.reactivex.Single;
@ -83,6 +78,7 @@ public class PatchManager implements IPatchManager {
@Inject SP sp;
@Inject PumpSync pumpSync;
@Inject DateUtil dateUtil;
@Inject RxAction rxAction;
private IPatchScanner patchScanner;
private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
@ -443,7 +439,7 @@ public class PatchManager implements IPatchManager {
if(getPatchConfig().getLifecycleEvent().isSubStepRunning()
&& !pm.getAlarms().isOccuring(AlarmCode.A005)
&& !pm.getAlarms().isOccuring(AlarmCode.A020)) {
RxAction.INSTANCE.runOnMainThread(() -> {
rxAction.runOnMainThread(() -> {
rxBus.send(new EventPatchActivationNotComplete());
});
}

View file

@ -1,29 +0,0 @@
package info.nightscout.androidaps.plugins.pump.eopatch.extension
import io.reactivex.Completable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
fun Completable.observeOnMainThread(): Completable = observeOn(AndroidSchedulers.mainThread())
fun Completable.observeOnComputation(): Completable = observeOn(Schedulers.computation())
fun Completable.observeOnIo(): Completable = observeOn(Schedulers.io())
fun Completable.subscribeEmpty(): Disposable {
return subscribe({}, {})
}
fun Completable.subscribeEmpty(onComplete: () -> Unit, onError: (Throwable) -> Unit): Disposable {
return subscribe(onComplete, onError)
}
fun Completable.subscribeDefault(): Disposable {
return subscribe({ Timber.d("onComplete") }, { Timber.e(it, "onError") })
}
fun Completable.subscribeDefault(onComplete: () -> Unit): Disposable {
return subscribe(onComplete, { Timber.e(it, "onError") })
}

View file

@ -1,27 +0,0 @@
package info.nightscout.androidaps.plugins.pump.eopatch.extension
import io.reactivex.Maybe
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
fun <T> Maybe<T>.observeOnMainThread(): Maybe<T> = observeOn(AndroidSchedulers.mainThread())
fun <T> Maybe<T>.observeOnComputation(): Maybe<T> = observeOn(Schedulers.computation())
fun <T> Maybe<T>.observeOnIo(): Maybe<T> = observeOn(Schedulers.io())
fun <T> Maybe<T>.subscribeEmpty(): Disposable = subscribe({}, {}, {})
fun <T> Maybe<T>.subscribeEmpty(onSuccess: (T) -> Unit): Disposable = subscribe(onSuccess, {}, {})
fun <T> Maybe<T>.subscribeEmpty(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit): Disposable = subscribe(onSuccess, onError, {})
fun <T> Maybe<T>.subscribeDefault(): Disposable = subscribe({ Timber.d("onSuccess") }, { Timber.e(it, "onError") }, { Timber.d("onComplete") })
fun <T> Maybe<T>.subscribeDefault(onSuccess: (T) -> Unit): Disposable = subscribe(onSuccess, { Timber.e(it, "onError") }, { Timber.d("onComplete") })
fun <T> Maybe<T>.subscribeDefault(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit): Disposable = subscribe(onSuccess, onError, { Timber.d("onComplete") })
fun <T> Maybe<T>.with(): Maybe<T> = subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

View file

@ -1,10 +1,11 @@
package info.nightscout.androidaps.plugins.pump.eopatch.extension
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
fun <T> Observable<T>.observeOnMainThread(): Observable<T> = observeOn(AndroidSchedulers.mainThread())
@ -18,10 +19,14 @@ fun <T> Observable<T>.subscribeEmpty(onSuccess: (T) -> Unit): Disposable = subsc
fun <T> Observable<T>.subscribeEmpty(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit): Disposable = subscribe(onSuccess, onError, {})
fun <T> Observable<T>.subscribeDefault(): Disposable = subscribe({ Timber.d("onSuccess") }, { Timber.e(it, "onError") }, { Timber.d("onComplete") })
fun <T> Observable<T>.subscribeDefault(aapsLogger: AAPSLogger): Disposable = subscribe({ aapsLogger.debug(LTag.PUMP, "onSuccess") }, { aapsLogger.error(LTag.PUMP, "onError", it) }, {
aapsLogger.debug(LTag.PUMP, "onComplete")
})
fun <T> Observable<T>.subscribeDefault(onSuccess: (T) -> Unit): Disposable = subscribe(onSuccess, { Timber.e(it, "onError") }, { Timber.d("onComplete") })
fun <T> Observable<T>.subscribeDefault(aapsLogger: AAPSLogger, onSuccess: (T) -> Unit): Disposable =
subscribe(onSuccess, { aapsLogger.error(LTag.PUMP, "onError", it) }, { aapsLogger.debug(LTag.PUMP, "onComplete") })
fun <T> Observable<T>.subscribeDefault(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit): Disposable = subscribe(onSuccess, onError, { Timber.d("onComplete") })
fun <T> Observable<T>.subscribeDefault(aapsLogger: AAPSLogger, onSuccess: (T) -> Unit, onError: (Throwable) -> Unit): Disposable =
subscribe(onSuccess, onError, { aapsLogger.debug(LTag.PUMP, "onComplete") })
fun <T> Observable<T>.with(): Observable<T> = subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

View file

@ -1,15 +1,16 @@
package info.nightscout.androidaps.plugins.pump.eopatch.extension
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import timber.log.Timber
fun <T> Single<T>.observeOnMainThread(): Single<T> = observeOn(AndroidSchedulers.mainThread())
fun <T> Single<T>.subscribeDefault(onSuccess: (T) -> Unit): Disposable = subscribe(onSuccess, {
Timber.e(it, "onError")
fun <T> Single<T>.subscribeDefault(aapsLogger: AAPSLogger, onSuccess: (T) -> Unit): Disposable = subscribe(onSuccess, {
aapsLogger.error(LTag.PUMP, "onError", it)
})
fun <T> Single<T>.with(): Single<T> = subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())

View file

@ -16,9 +16,9 @@ import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.plugins.pump.eopatch.EoPatchRxBus
import info.nightscout.androidaps.plugins.pump.eopatch.dagger.EopatchPluginQualifier
import info.nightscout.androidaps.plugins.pump.eopatch.extension.fillExtras
import info.nightscout.androidaps.plugins.pump.eopatch.extension.observeOnMainThread
import info.nightscout.androidaps.plugins.pump.eopatch.extension.subscribeDefault
import info.nightscout.androidaps.plugins.pump.eopatch.vo.ActivityResultEvent
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import javax.inject.Inject
@ -29,6 +29,8 @@ abstract class EoBaseActivity<B : ViewDataBinding> : NoSplashAppCompatActivity()
@EopatchPluginQualifier
lateinit var viewModelFactory: ViewModelProvider.Factory
@Inject lateinit var aapsSchedulers: AapsSchedulers
protected lateinit var binding: B
private val compositeDisposable = CompositeDisposable()
@ -90,8 +92,8 @@ abstract class EoBaseActivity<B : ViewDataBinding> : NoSplashAppCompatActivity()
override fun checkCommunication(onSuccess: () -> Unit, onCancel: (() -> Unit)?, onDiscard: (() -> Unit)?, goHomeAfterDiscard: Boolean) {
EoPatchRxBus.listen(ActivityResultEvent::class.java)
.doOnSubscribe { startActivityForResult({ EopatchActivity.createIntentForCheckConnection(this, goHomeAfterDiscard) }, 10001) }
.observeOnMainThread()
.subscribeDefault {
.observeOn(aapsSchedulers.main)
.subscribeDefault(aapsLogger) {
if (it.requestCode == 10001) {
when (it.resultCode) {
RESULT_OK -> onSuccess.invoke()

View file

@ -19,21 +19,22 @@ import info.nightscout.androidaps.plugins.pump.eopatch.code.PatchStep
import info.nightscout.androidaps.plugins.pump.eopatch.code.EventType
import info.nightscout.androidaps.plugins.pump.eopatch.databinding.FragmentEopatchOverviewBinding
import info.nightscout.androidaps.plugins.pump.eopatch.extension.fillExtras
import info.nightscout.androidaps.plugins.pump.eopatch.extension.observeOnMainThread
import info.nightscout.androidaps.plugins.pump.eopatch.extension.subscribeDefault
import info.nightscout.androidaps.plugins.pump.eopatch.extension.takeOne
import info.nightscout.androidaps.plugins.pump.eopatch.ui.viewmodel.EopatchOverviewViewModel
import info.nightscout.androidaps.plugins.pump.eopatch.vo.ActivityResultEvent
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.shared.logging.AAPSLogger
import io.reactivex.disposables.CompositeDisposable
import javax.inject.Inject
class EopatchOverviewFragment: EoBaseFragment<FragmentEopatchOverviewBinding>() {
@Inject lateinit var rxBus: RxBus
@Inject lateinit var aapsSchedulers: AapsSchedulers
@Inject lateinit var aapsLogger: AAPSLogger
private var disposable: CompositeDisposable = CompositeDisposable()
private var mProgressDialog: ProgressDialog? = null
override fun getLayoutId(): Int = R.layout.fragment_eopatch_overview
override fun onDestroy() {
@ -167,8 +168,8 @@ class EopatchOverviewFragment: EoBaseFragment<FragmentEopatchOverviewBinding>()
override fun checkCommunication(onSuccess: () -> Unit, onCancel: (() -> Unit)?, onDiscard: (() -> Unit)?, goHomeAfterDiscard: Boolean) {
EoPatchRxBus.listen(ActivityResultEvent::class.java)
.doOnSubscribe { startActivityForResult({ EopatchActivity.createIntentForCheckConnection(this, goHomeAfterDiscard) }, 10001) }
.observeOnMainThread()
.subscribeDefault {
.observeOn(aapsSchedulers.main)
.subscribeDefault(aapsLogger) {
if (it.requestCode == 10001) {
when (it.resultCode) {
DaggerAppCompatActivity.RESULT_OK -> onSuccess.invoke()

View file

@ -14,10 +14,10 @@ import info.nightscout.androidaps.plugins.pump.eopatch.alarm.IAlarmProcess
import info.nightscout.androidaps.plugins.pump.eopatch.bindingadapters.setOnSafeClickListener
import info.nightscout.androidaps.plugins.pump.eopatch.ble.IPatchManager
import info.nightscout.androidaps.plugins.pump.eopatch.databinding.DialogAlarmBinding
import info.nightscout.androidaps.plugins.pump.eopatch.extension.observeOnMainThread
import info.nightscout.androidaps.plugins.pump.eopatch.ui.AlarmHelperActivity
import info.nightscout.androidaps.services.AlarmSoundServiceHelper
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject
@ -28,6 +28,7 @@ class AlarmDialog : DaggerDialogFragment() {
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var patchManager: IPatchManager
@Inject lateinit var rxBus: RxBus
@Inject lateinit var aapsSchedulers: AapsSchedulers
var helperActivity: AlarmHelperActivity? = null
var alarmCode: AlarmCode? = null
@ -111,7 +112,7 @@ class AlarmDialog : DaggerDialogFragment() {
startAlarm()
disposable = patchManager.observePatchLifeCycle()
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.subscribe {
if(it.isShutdown) {
activity?.finish()

View file

@ -1,24 +1,22 @@
package info.nightscout.androidaps.plugins.pump.eopatch.ui.viewmodel
import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import info.nightscout.androidaps.interfaces.ActivePlugin
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.plugins.pump.eopatch.R
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.BleConnectionState
import info.nightscout.androidaps.plugins.pump.eopatch.ble.IPatchManager
import info.nightscout.androidaps.plugins.pump.eopatch.ble.IPreferenceManager
import info.nightscout.androidaps.plugins.pump.eopatch.code.EventType
import info.nightscout.androidaps.plugins.pump.eopatch.extension.observeOnMainThread
import info.nightscout.androidaps.plugins.pump.eopatch.extension.with
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.BleConnectionState
import info.nightscout.androidaps.plugins.pump.eopatch.ui.EoBaseNavigator
import info.nightscout.androidaps.plugins.pump.eopatch.ui.event.UIEvent
import info.nightscout.androidaps.plugins.pump.eopatch.ui.event.SingleLiveEvent
import info.nightscout.androidaps.plugins.pump.eopatch.ui.event.UIEvent
import info.nightscout.androidaps.plugins.pump.eopatch.vo.Alarms
import info.nightscout.androidaps.plugins.pump.eopatch.vo.PatchConfig
import info.nightscout.androidaps.plugins.pump.eopatch.vo.PatchState
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit
@ -26,11 +24,11 @@ import javax.inject.Inject
import kotlin.math.roundToInt
class EopatchOverviewViewModel @Inject constructor(
private val context: Context,
private val rh: ResourceHelper,
val patchManager: IPatchManager,
val preferenceManager: IPreferenceManager,
val profileFunction: ProfileFunction,
val activePlugin: ActivePlugin
private val preferenceManager: IPreferenceManager,
private val profileFunction: ProfileFunction,
private val aapsSchedulers: AapsSchedulers
) : EoBaseViewModel<EoBaseNavigator>() {
private val _eventHandler = SingleLiveEvent<UIEvent<EventType>>()
val UIEventTypeHandler : LiveData<UIEvent<EventType>>
@ -82,12 +80,12 @@ class EopatchOverviewViewModel @Inject constructor(
init {
preferenceManager.observePatchConfig()
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.subscribe { _patchConfig.value = it }
.addTo()
preferenceManager.observePatchState()
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.subscribe {
_patchState.value = it
_patchRemainingInsulin.value = it.remainedInsulin
@ -97,25 +95,25 @@ class EopatchOverviewViewModel @Inject constructor(
.addTo()
patchManager.observePatchConnectionState()
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.subscribe {
_bleStatus.value = when(it){
BleConnectionState.CONNECTED -> "{fa-bluetooth}"
BleConnectionState.DISCONNECTED -> "{fa-bluetooth-b}"
else -> "{fa-bluetooth-b spin} ${context.getString(R.string.string_connecting)}"
else -> "{fa-bluetooth-b spin} ${rh.gs(R.string.string_connecting)}"
}
}
.addTo()
patchManager.observePatchLifeCycle()
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.subscribe {
updatePatchStatus()
}
.addTo()
preferenceManager.observeAlarm()
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.subscribe {
_alarms.value = it
}
@ -135,10 +133,10 @@ class EopatchOverviewViewModel @Inject constructor(
val h = TimeUnit.MILLISECONDS.toHours(remainTimeMillis)
val m = TimeUnit.MILLISECONDS.toMinutes(remainTimeMillis - TimeUnit.HOURS.toMillis(h))
_status.value = if(patchManager.patchState.isNormalBasalPaused)
"${context.getString(R.string.string_suspended)}\n" +
"${context.getString(R.string.string_temp_basal_remained_hhmm, h.toString(), m.toString())}"
"${rh.gs(R.string.string_suspended)}\n" +
"${rh.gs(R.string.string_temp_basal_remained_hhmm, h.toString(), m.toString())}"
else
context.getString(R.string.string_running)
rh.gs(R.string.string_running)
}else{
_status.value = ""
}
@ -190,7 +188,8 @@ class EopatchOverviewViewModel @Inject constructor(
fun pauseBasal(pauseDurationHour: Float){
patchManager.pauseBasal(pauseDurationHour)
.with()
.subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.main)
.subscribe({
if (it.isSuccess) {
navigator?.toast(R.string.string_suspended_insulin_delivery_message)
@ -205,7 +204,8 @@ class EopatchOverviewViewModel @Inject constructor(
fun resumeBasal() {
patchManager.resumeBasal()
.with()
.subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.main)
.subscribe({
if (it.isSuccess) {
navigator?.toast(R.string.string_resumed_insulin_delivery_message)
@ -221,7 +221,7 @@ class EopatchOverviewViewModel @Inject constructor(
private fun startPeriodicallyUpdate(){
if(mDisposable == null) {
mDisposable = Observable.interval(30, TimeUnit.SECONDS)
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.subscribe { updatePatchStatus() }
}
}

View file

@ -1,37 +1,40 @@
package info.nightscout.androidaps.plugins.pump.eopatch.ui.viewmodel
import android.content.Context
import android.content.res.Resources
import androidx.annotation.StringRes
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import info.nightscout.androidaps.plugins.pump.eopatch.CommonUtils
import info.nightscout.androidaps.plugins.pump.eopatch.R
import info.nightscout.androidaps.plugins.pump.eopatch.RxAction
import info.nightscout.androidaps.plugins.pump.eopatch.alarm.AlarmCode
import info.nightscout.androidaps.plugins.pump.eopatch.alarm.IAlarmRegistry
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.BleConnectionState
import info.nightscout.androidaps.plugins.pump.eopatch.core.define.IPatchConstant
import info.nightscout.androidaps.plugins.pump.eopatch.ble.IPatchManager
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.PatchSelfTestResult.*
import info.nightscout.androidaps.plugins.pump.eopatch.code.EventType
import info.nightscout.androidaps.plugins.pump.eopatch.code.PatchLifecycle
import info.nightscout.androidaps.plugins.pump.eopatch.code.PatchStep
import info.nightscout.androidaps.plugins.pump.eopatch.code.EventType
import info.nightscout.androidaps.plugins.pump.eopatch.extension.*
import info.nightscout.androidaps.plugins.pump.eopatch.core.define.IPatchConstant
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.BleConnectionState
import info.nightscout.androidaps.plugins.pump.eopatch.core.scan.PatchSelfTestResult.TEST_SUCCESS
import info.nightscout.androidaps.plugins.pump.eopatch.extension.getDiffDays
import info.nightscout.androidaps.plugins.pump.eopatch.extension.subscribeDefault
import info.nightscout.androidaps.plugins.pump.eopatch.extension.subscribeEmpty
import info.nightscout.androidaps.plugins.pump.eopatch.extension.takeOne
import info.nightscout.androidaps.plugins.pump.eopatch.ui.EoBaseNavigator
import info.nightscout.androidaps.plugins.pump.eopatch.ui.event.UIEvent
import info.nightscout.androidaps.plugins.pump.eopatch.ui.event.SingleLiveEvent
import info.nightscout.androidaps.plugins.pump.eopatch.vo.PatchLifecycleEvent
import info.nightscout.androidaps.plugins.pump.eopatch.ui.event.UIEvent
import info.nightscout.androidaps.plugins.pump.eopatch.ui.viewmodel.EopatchViewModel.SetupStep.*
import info.nightscout.androidaps.plugins.pump.eopatch.vo.PatchLifecycleEvent
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.disposables.Disposable
import io.reactivex.subjects.PublishSubject
import java.lang.ref.WeakReference
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import javax.inject.Inject
@ -39,10 +42,12 @@ import kotlin.math.abs
import kotlin.math.roundToInt
class EopatchViewModel @Inject constructor(
private val context: Context,
private val rh: ResourceHelper,
val patchManager: IPatchManager,
private val alarmRegistry: IAlarmRegistry,
private val aapsLogger: AAPSLogger
private val aapsLogger: AAPSLogger,
private val aapsSchedulers: AapsSchedulers,
private val rxAction: RxAction
) : EoBaseViewModel<EoBaseNavigator>() {
companion object {
private const val MAX_ELAPSED_MILLIS_AFTER_EXPIRATION = -12L * 60 * 60 * 1000
@ -64,11 +69,6 @@ class EopatchViewModel @Inject constructor(
_eventHandler.postValue(UIEvent(EventType.ACTIVTION_CLICKED))
}
private val mContentRef = WeakReference(context)
private val mContext: Context?
get() = mContentRef.get()
val patchStep = MutableLiveData<PatchStep>()
val isActivated = MutableLiveData<Boolean>(patchManager.isActivated)
@ -123,7 +123,7 @@ class EopatchViewModel @Inject constructor(
val commCheckCancelLabel: LiveData<String>
get() = Transformations.map(patchStep) {
mContext?.getString(when (it) {
rh.gs(when (it) {
PatchStep.CONNECT_NEW -> {
isBonded.takeOne(R.string.cancel, R.string.patch_cancel_pairing)
}
@ -134,7 +134,7 @@ class EopatchViewModel @Inject constructor(
val programEnabledMessage: String
// get() = """'기초1' program has been enabled."""
get() = mContext?.getString(R.string.patch_basal_schedule_desc_1,"기초1") ?: ""
get() = rh.gs(R.string.patch_basal_schedule_desc_1,"기초1") ?: ""
val patchStepIsSafeDeactivation: Boolean
get() = patchStep.value?.isSafeDeactivation ?: false
@ -181,13 +181,13 @@ class EopatchViewModel @Inject constructor(
.throttleFirst(500, TimeUnit.MILLISECONDS)
.delay(100, TimeUnit.MILLISECONDS)
.filter { isSubStepRunning }
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.flatMapMaybe { alarmRegistry.remove(AlarmCode.B012) }
.flatMapMaybe { alarmRegistry.add(AlarmCode.B012, TimeUnit.MINUTES.toMillis(3)) }
.subscribeDefault {}
.subscribe()
patchManager.observePatchLifeCycle()
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.subscribe {
isActivated.value = patchManager.isActivated
}
@ -213,9 +213,9 @@ class EopatchViewModel @Inject constructor(
CommonUtils.dispose(mUpdateDisposable)
mUpdateDisposable = Observable.interval(0, 1, TimeUnit.SECONDS)
.observeOnMainThread()
.observeOn(aapsSchedulers.main)
.takeUntil { !patchConfig.isActivated }
.subscribeDefault {
.subscribeDefault(aapsLogger) {
_patchExpirationTimestamp.value = patchManager.patchExpiredTime
}
}
@ -275,12 +275,13 @@ class EopatchViewModel @Inject constructor(
}
.retry(1)
}
.with()
.subscribeOn(aapsSchedulers.io)
.observeOn(aapsSchedulers.main)
.onErrorReturnItem(false)
.doOnSubscribe { showPatchCommCheckDialog() }
.doFinally { dismissPatchCommCheckDialog() }
.doOnError { aapsLogger.error(LTag.PUMP, it.message?:"Error") }
.subscribeDefault {
.subscribeDefault(aapsLogger) {
_isCommCheckFailed.value = !it
}
}
@ -623,11 +624,11 @@ class EopatchViewModel @Inject constructor(
.doFinally {
dismissProgressDialog()
}
.subscribeDefault { status ->
.subscribeDefault(aapsLogger) { status ->
if (status.isDeactivated) {
onSuccessListener.invoke()
} else {
RxAction.runOnMainThread({
rxAction.runOnMainThread({
checkCommunication({ deactivate(false, onSuccessListener) },
{ _eventHandler.postValue(UIEvent(EventType.FINISH_ACTIVITY)) })
}, 100)
@ -673,7 +674,7 @@ class EopatchViewModel @Inject constructor(
}
.onErrorReturnItem("")
.doOnSubscribe { updateSetupStep(SCAN_STARTED) }
.subscribeDefault {
.subscribeDefault(aapsLogger) {
if (!it.isNullOrEmpty()) {
startBond(it)
} else {
@ -698,7 +699,7 @@ class EopatchViewModel @Inject constructor(
updateSetupStep(BONDING_FAILED)
}
}
.subscribeDefault {
.subscribeDefault(aapsLogger) {
if (it) {
getPatchInfo()
} else {
@ -712,7 +713,7 @@ class EopatchViewModel @Inject constructor(
patchManager.getPatchInfo(timeout)
.doOnSubscribe { updateSetupStep(GET_PATCH_INFO_STARTED) }
.onErrorReturnItem(false)
.subscribeDefault {
.subscribeDefault(aapsLogger) {
if (it) {
selfTest(delayMs = 1000)
} else {
@ -723,12 +724,12 @@ class EopatchViewModel @Inject constructor(
@Synchronized
fun selfTest(timeout: Long = 20000, delayMs: Long = 0) {
RxAction.runOnMainThread({
rxAction.runOnMainThread({
patchManager.selfTest(timeout)
.doOnSubscribe { updateSetupStep(SELF_TEST_STARTED) }
.map { it == TEST_SUCCESS }
.onErrorReturnItem(false)
.subscribeDefault {
.subscribeDefault(aapsLogger) {
if (it) {
moveStep(PatchStep.REMOVE_NEEDLE_CAP)
} else if (!patchManager.patchConnectionState.isConnected) {
@ -777,7 +778,7 @@ class EopatchViewModel @Inject constructor(
updateSetupStep(NEEDLE_SENSING_STARTED)
}
.onErrorReturnItem(false)
.subscribeDefault {
.subscribeDefault(aapsLogger) {
if (it) {
startActivation()
} else {
@ -798,7 +799,7 @@ class EopatchViewModel @Inject constructor(
}
.doFinally { dismissProgressDialog() }
.onErrorReturnItem(false)
.subscribeDefault {
.subscribeDefault(aapsLogger) {
if (it) {
moveStep(PatchStep.COMPLETE)
} else {

View file

@ -12,16 +12,19 @@ interface AapsSchedulers {
val main: Scheduler
val io: Scheduler
val cpu: Scheduler
val newThread: Scheduler
}
class DefaultAapsSchedulers : AapsSchedulers {
override val main: Scheduler = AndroidSchedulers.mainThread()
override val io: Scheduler = Schedulers.io()
override val cpu: Scheduler = Schedulers.computation()
override val newThread: Scheduler = Schedulers.newThread()
}
class TestAapsSchedulers : AapsSchedulers {
override val main: Scheduler = Schedulers.trampoline()
override val io: Scheduler = Schedulers.trampoline()
override val cpu: Scheduler = Schedulers.trampoline()
override val newThread: Scheduler = Schedulers.trampoline()
}