From 420c5584cd7da57412c4de3a04cabb5a651f7195 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 8 Feb 2021 15:15:42 +0100 Subject: [PATCH] reset databases in CompositeDisposable --- .../maintenance/MaintenanceFragment.kt | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt index 94e19ba0cb..e6015b8b65 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt @@ -12,24 +12,33 @@ import info.nightscout.androidaps.database.AppRepository import info.nightscout.androidaps.databinding.MaintenanceFragmentBinding import info.nightscout.androidaps.events.EventNewBG import info.nightscout.androidaps.interfaces.ImportExportPrefsInterface +import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.general.food.FoodPlugin import info.nightscout.androidaps.plugins.general.maintenance.activities.LogSettingActivity import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.utils.alertDialogs.OKDialog import info.nightscout.androidaps.utils.resources.ResourceHelper +import info.nightscout.androidaps.utils.rx.AapsSchedulers +import io.reactivex.Completable.fromAction +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.rxkotlin.subscribeBy import javax.inject.Inject class MaintenanceFragment : DaggerFragment() { + @Inject lateinit var aapsLogger: AAPSLogger @Inject lateinit var maintenancePlugin: MaintenancePlugin @Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var treatmentsPlugin: TreatmentsPlugin @Inject lateinit var foodPlugin: FoodPlugin @Inject lateinit var importExportPrefs: ImportExportPrefsInterface + @Inject lateinit var aapsSchedulers: AapsSchedulers @Inject lateinit var repository: AppRepository + private val compositeDisposable = CompositeDisposable() + private var _binding: MaintenanceFragmentBinding? = null // This property is only valid between onCreateView and @@ -48,15 +57,22 @@ class MaintenanceFragment : DaggerFragment() { binding.navResetdb.setOnClickListener { activity?.let { activity -> OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.maintenance), resourceHelper.gs(R.string.reset_db_confirm), Runnable { - MainApp.getDbHelper().resetDatabases() - Thread { - // should be handled by Plugin-Interface and - // additional service interface and plugin registry - foodPlugin.service?.resetFood() - treatmentsPlugin.service.resetTreatments() - repository.clearDatabases() - rxBus.send(EventNewBG(null)) - } .start() + compositeDisposable.add( + fromAction { + MainApp.getDbHelper().resetDatabases() + // should be handled by Plugin-Interface and + // additional service interface and plugin registry + foodPlugin.service?.resetFood() + treatmentsPlugin.service.resetTreatments() + repository.clearDatabases() + } + .subscribeOn(aapsSchedulers.io) + .observeOn(aapsSchedulers.main) + .subscribeBy( + onError = { aapsLogger.error("Error clearing databases", it) }, + onComplete = { rxBus.send(EventNewBG(null)) } + ) + ) }) } } @@ -78,6 +94,7 @@ class MaintenanceFragment : DaggerFragment() { @Synchronized override fun onDestroyView() { super.onDestroyView() + compositeDisposable.clear() _binding = null } } \ No newline at end of file