Merge pull request #745 from Philoul/Fix/AddWorkerCsvExport
Add Worker to launch exportUserEntriesCsv in Background
This commit is contained in:
commit
50e31a4b2b
2 changed files with 46 additions and 13 deletions
|
@ -3,6 +3,7 @@ package info.nightscout.androidaps.dependencyInjection
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.android.ContributesAndroidInjector
|
import dagger.android.ContributesAndroidInjector
|
||||||
import info.nightscout.androidaps.plugins.general.food.FoodPlugin
|
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.NSClientAddAckWorker
|
||||||
import info.nightscout.androidaps.plugins.general.nsclient.NSClientAddUpdateWorker
|
import info.nightscout.androidaps.plugins.general.nsclient.NSClientAddUpdateWorker
|
||||||
import info.nightscout.androidaps.plugins.general.nsclient.NSClientMbgWorker
|
import info.nightscout.androidaps.plugins.general.nsclient.NSClientMbgWorker
|
||||||
|
@ -30,4 +31,5 @@ abstract class WorkersModule {
|
||||||
@ContributesAndroidInjector abstract fun contributesNSClientUpdateRemoveAckWorker(): NSClientUpdateRemoveAckWorker
|
@ContributesAndroidInjector abstract fun contributesNSClientUpdateRemoveAckWorker(): NSClientUpdateRemoveAckWorker
|
||||||
@ContributesAndroidInjector abstract fun contributesNSClientMbgWorker(): NSClientMbgWorker
|
@ContributesAndroidInjector abstract fun contributesNSClientMbgWorker(): NSClientMbgWorker
|
||||||
@ContributesAndroidInjector abstract fun contributesFoodWorker(): FoodPlugin.FoodWorker
|
@ContributesAndroidInjector abstract fun contributesFoodWorker(): FoodPlugin.FoodWorker
|
||||||
|
@ContributesAndroidInjector abstract fun contributesCsvExportWorker(): ImportExportPrefsImpl.CsvExportWorker
|
||||||
}
|
}
|
|
@ -11,7 +11,10 @@ import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.FragmentActivity
|
import androidx.fragment.app.FragmentActivity
|
||||||
|
import androidx.work.*
|
||||||
|
import dagger.android.HasAndroidInjector
|
||||||
import info.nightscout.androidaps.BuildConfig
|
import info.nightscout.androidaps.BuildConfig
|
||||||
|
import info.nightscout.androidaps.MainApp
|
||||||
import info.nightscout.androidaps.R
|
import info.nightscout.androidaps.R
|
||||||
import info.nightscout.androidaps.activities.DaggerAppCompatActivityWithResult
|
import info.nightscout.androidaps.activities.DaggerAppCompatActivityWithResult
|
||||||
import info.nightscout.androidaps.activities.PreferencesActivity
|
import info.nightscout.androidaps.activities.PreferencesActivity
|
||||||
|
@ -64,7 +67,6 @@ class ImportExportPrefsImpl @Inject constructor(
|
||||||
private val encryptedPrefsFormat: EncryptedPrefsFormat,
|
private val encryptedPrefsFormat: EncryptedPrefsFormat,
|
||||||
private val prefFileList: PrefFileListProvider,
|
private val prefFileList: PrefFileListProvider,
|
||||||
private val uel: UserEntryLogger,
|
private val uel: UserEntryLogger,
|
||||||
private val repository: AppRepository,
|
|
||||||
private val dateUtil: DateUtil
|
private val dateUtil: DateUtil
|
||||||
) : ImportExportPrefs {
|
) : ImportExportPrefs {
|
||||||
|
|
||||||
|
@ -363,19 +365,48 @@ class ImportExportPrefsImpl @Inject constructor(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun exportUserEntriesCsv(activity: FragmentActivity) {
|
override fun exportUserEntriesCsv(activity: FragmentActivity) {
|
||||||
val entries = repository.getUserEntryFilteredDataFromTime(MidnightTime.calc() - T.days(90).msecs()).blockingGet()
|
WorkManager.getInstance(activity).enqueueUniqueWork(
|
||||||
prefFileList.ensureExportDirExists()
|
"export",
|
||||||
val newFile = prefFileList.newExportCsvFile()
|
ExistingWorkPolicy.APPEND_OR_REPLACE,
|
||||||
|
OneTimeWorkRequest.Builder(CsvExportWorker::class.java).build()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
class CsvExportWorker(
|
||||||
classicPrefsFormat.saveCsv(newFile, entries)
|
context: Context,
|
||||||
ToastUtils.okToast(activity, resourceHelper.gs(R.string.ue_exported))
|
params: WorkerParameters
|
||||||
} catch (e: FileNotFoundException) {
|
) : Worker(context, params) {
|
||||||
ToastUtils.errorToast(activity, resourceHelper.gs(R.string.filenotfound) + " " + newFile)
|
|
||||||
log.error(LTag.CORE, "Unhandled exception", e)
|
@Inject lateinit var injector: HasAndroidInjector
|
||||||
} catch (e: IOException) {
|
@Inject lateinit var aapsLogger: AAPSLogger
|
||||||
ToastUtils.errorToast(activity, e.message)
|
@Inject lateinit var repository: AppRepository
|
||||||
log.error(LTag.CORE, "Unhandled exception", e)
|
@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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue