From 26c702c7c0c904db52c4d4faa5efc8cfc9597e3f Mon Sep 17 00:00:00 2001 From: Philoul Date: Sun, 17 Oct 2021 20:26:35 +0200 Subject: [PATCH 1/2] Add Worker to launch exportUserEntriesCsv in Background --- .../fragments/TreatmentsUserEntryFragment.kt | 2 +- .../dependencyInjection/WorkersModule.kt | 2 + .../maintenance/ImportExportPrefsImpl.kt | 63 ++++++++++++++----- .../maintenance/MaintenanceFragment.kt | 2 +- .../interfaces/ImportExportPrefs.kt | 2 +- 5 files changed, 54 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsUserEntryFragment.kt b/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsUserEntryFragment.kt index b0ca06cfdd..e5c6b95ad7 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsUserEntryFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsUserEntryFragment.kt @@ -68,7 +68,7 @@ class TreatmentsUserEntryFragment : DaggerFragment() { activity?.let { activity -> OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.ue_export_to_csv) + "?") { uel.log(Action.EXPORT_CSV, Sources.Treatments) - importExportPrefs.exportUserEntriesCsv(activity) + importExportPrefs.exportUserEntriesCsv() } } } diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/WorkersModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/WorkersModule.kt index 5d3f33d2d9..a4ef80ab81 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/WorkersModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/WorkersModule.kt @@ -3,6 +3,7 @@ package info.nightscout.androidaps.dependencyInjection import dagger.Module import dagger.android.ContributesAndroidInjector import info.nightscout.androidaps.plugins.general.food.FoodPlugin +import info.nightscout.androidaps.plugins.general.maintenance.ImportExportPrefsImpl import info.nightscout.androidaps.plugins.general.nsclient.NSClientAddAckWorker import info.nightscout.androidaps.plugins.general.nsclient.NSClientAddUpdateWorker import info.nightscout.androidaps.plugins.general.nsclient.NSClientMbgWorker @@ -30,4 +31,5 @@ abstract class WorkersModule { @ContributesAndroidInjector abstract fun contributesNSClientUpdateRemoveAckWorker(): NSClientUpdateRemoveAckWorker @ContributesAndroidInjector abstract fun contributesNSClientMbgWorker(): NSClientMbgWorker @ContributesAndroidInjector abstract fun contributesFoodWorker(): FoodPlugin.FoodWorker + @ContributesAndroidInjector abstract fun contributesCsvExportWorker(): ImportExportPrefsImpl.CsvExportWorker } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefsImpl.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefsImpl.kt index 90a3edba81..1f48ba24b7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefsImpl.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefsImpl.kt @@ -11,7 +11,13 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity +import androidx.work.OneTimeWorkRequest +import androidx.work.Worker +import androidx.work.WorkerParameters +import androidx.work.workDataOf +import dagger.android.HasAndroidInjector import info.nightscout.androidaps.BuildConfig +import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R import info.nightscout.androidaps.activities.DaggerAppCompatActivityWithResult import info.nightscout.androidaps.activities.PreferencesActivity @@ -26,6 +32,7 @@ import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.maintenance.formats.* +import info.nightscout.androidaps.receivers.DataWorker import info.nightscout.androidaps.utils.AndroidPermission import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.MidnightTime @@ -64,7 +71,7 @@ class ImportExportPrefsImpl @Inject constructor( private val encryptedPrefsFormat: EncryptedPrefsFormat, private val prefFileList: PrefFileListProvider, private val uel: UserEntryLogger, - private val repository: AppRepository, + private val dataWorker: DataWorker, private val dateUtil: DateUtil ) : ImportExportPrefs { @@ -362,20 +369,48 @@ class ImportExportPrefsImpl @Inject constructor( } } - override fun exportUserEntriesCsv(activity: FragmentActivity) { - val entries = repository.getUserEntryFilteredDataFromTime(MidnightTime.calc() - T.days(90).msecs()).blockingGet() - prefFileList.ensureExportDirExists() - val newFile = prefFileList.newExportCsvFile() + override fun exportUserEntriesCsv() { + dataWorker.enqueue( + OneTimeWorkRequest.Builder(CsvExportWorker::class.java) + .build() + ) + } - try { - classicPrefsFormat.saveCsv(newFile, entries) - ToastUtils.okToast(activity, resourceHelper.gs(R.string.ue_exported)) - } catch (e: FileNotFoundException) { - ToastUtils.errorToast(activity, resourceHelper.gs(R.string.filenotfound) + " " + newFile) - log.error(LTag.CORE, "Unhandled exception", e) - } catch (e: IOException) { - ToastUtils.errorToast(activity, e.message) - log.error(LTag.CORE, "Unhandled exception", e) + class CsvExportWorker( + context: Context, + params: WorkerParameters + ) : Worker(context, params) { + + @Inject lateinit var injector: HasAndroidInjector + @Inject lateinit var aapsLogger: AAPSLogger + @Inject lateinit var repository: AppRepository + @Inject lateinit var classicPrefsFormat: ClassicPrefsFormat + @Inject lateinit var resourceHelper: ResourceHelper + @Inject lateinit var prefFileList: PrefFileListProvider + @Inject lateinit var mainApp: MainApp + + init { + (context.applicationContext as HasAndroidInjector).androidInjector().inject(this) + } + + override fun doWork(): Result { + val entries = repository.getUserEntryFilteredDataFromTime(MidnightTime.calc() - T.days(90).msecs()).blockingGet() + prefFileList.ensureExportDirExists() + val newFile = prefFileList.newExportCsvFile() + var ret = Result.success() + try { + classicPrefsFormat.saveCsv(newFile, entries) + ToastUtils.okToast(mainApp, resourceHelper.gs(R.string.ue_exported)) + } catch (e: FileNotFoundException) { + ToastUtils.errorToast(mainApp, resourceHelper.gs(R.string.filenotfound) + " " + newFile) + aapsLogger.error(LTag.CORE, "Unhandled exception", e) + ret = Result.failure(workDataOf("Error" to "Error FileNotFoundException")) + } catch (e: IOException) { + ToastUtils.errorToast(mainApp, e.message) + aapsLogger.error(LTag.CORE, "Unhandled exception", e) + ret = Result.failure(workDataOf("Error" to "Error IOException")) + } + return ret } } } \ No newline at end of file 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 064c3c0998..a6a1c2bf85 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 @@ -118,7 +118,7 @@ class MaintenanceFragment : DaggerFragment() { activity?.let { activity -> OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.ue_export_to_csv) + "?") { uel.log(Action.EXPORT_CSV, Sources.Maintenance) - importExportPrefs.exportUserEntriesCsv(activity) + importExportPrefs.exportUserEntriesCsv() } } } diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/ImportExportPrefs.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/ImportExportPrefs.kt index 95dbb3467f..ef5589c322 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/ImportExportPrefs.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/ImportExportPrefs.kt @@ -14,5 +14,5 @@ interface ImportExportPrefs { fun prefsFileExists(): Boolean fun verifyStoragePermissions(fragment: Fragment, onGranted: Runnable) fun exportSharedPreferences(f: Fragment) - fun exportUserEntriesCsv(activity: FragmentActivity) + fun exportUserEntriesCsv() } \ No newline at end of file From c723058628b9e2d6eee529ca75d6f270a4e3ec82 Mon Sep 17 00:00:00 2001 From: Philoul Date: Mon, 18 Oct 2021 09:59:26 +0200 Subject: [PATCH 2/2] Create a new worker for export --- .../fragments/TreatmentsUserEntryFragment.kt | 2 +- .../general/maintenance/ImportExportPrefsImpl.kt | 16 ++++++---------- .../general/maintenance/MaintenanceFragment.kt | 2 +- .../androidaps/interfaces/ImportExportPrefs.kt | 2 +- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsUserEntryFragment.kt b/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsUserEntryFragment.kt index e5c6b95ad7..b0ca06cfdd 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsUserEntryFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/fragments/TreatmentsUserEntryFragment.kt @@ -68,7 +68,7 @@ class TreatmentsUserEntryFragment : DaggerFragment() { activity?.let { activity -> OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.ue_export_to_csv) + "?") { uel.log(Action.EXPORT_CSV, Sources.Treatments) - importExportPrefs.exportUserEntriesCsv() + importExportPrefs.exportUserEntriesCsv(activity) } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefsImpl.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefsImpl.kt index 1f48ba24b7..a6db885f14 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefsImpl.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/ImportExportPrefsImpl.kt @@ -11,10 +11,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity -import androidx.work.OneTimeWorkRequest -import androidx.work.Worker -import androidx.work.WorkerParameters -import androidx.work.workDataOf +import androidx.work.* import dagger.android.HasAndroidInjector import info.nightscout.androidaps.BuildConfig import info.nightscout.androidaps.MainApp @@ -32,7 +29,6 @@ import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.maintenance.formats.* -import info.nightscout.androidaps.receivers.DataWorker import info.nightscout.androidaps.utils.AndroidPermission import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.MidnightTime @@ -71,7 +67,6 @@ class ImportExportPrefsImpl @Inject constructor( private val encryptedPrefsFormat: EncryptedPrefsFormat, private val prefFileList: PrefFileListProvider, private val uel: UserEntryLogger, - private val dataWorker: DataWorker, private val dateUtil: DateUtil ) : ImportExportPrefs { @@ -369,10 +364,11 @@ class ImportExportPrefsImpl @Inject constructor( } } - override fun exportUserEntriesCsv() { - dataWorker.enqueue( - OneTimeWorkRequest.Builder(CsvExportWorker::class.java) - .build() + override fun exportUserEntriesCsv(activity: FragmentActivity) { + WorkManager.getInstance(activity).enqueueUniqueWork( + "export", + ExistingWorkPolicy.APPEND_OR_REPLACE, + OneTimeWorkRequest.Builder(CsvExportWorker::class.java).build() ) } 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 a6a1c2bf85..064c3c0998 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 @@ -118,7 +118,7 @@ class MaintenanceFragment : DaggerFragment() { activity?.let { activity -> OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.ue_export_to_csv) + "?") { uel.log(Action.EXPORT_CSV, Sources.Maintenance) - importExportPrefs.exportUserEntriesCsv() + importExportPrefs.exportUserEntriesCsv(activity) } } } diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/ImportExportPrefs.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/ImportExportPrefs.kt index ef5589c322..95dbb3467f 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/ImportExportPrefs.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/ImportExportPrefs.kt @@ -14,5 +14,5 @@ interface ImportExportPrefs { fun prefsFileExists(): Boolean fun verifyStoragePermissions(fragment: Fragment, onGranted: Runnable) fun exportSharedPreferences(f: Fragment) - fun exportUserEntriesCsv() + fun exportUserEntriesCsv(activity: FragmentActivity) } \ No newline at end of file