From 39077910b72a3661f5fa8f95af2192f7f0bd82c9 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Tue, 14 Jul 2020 15:04:55 -0500 Subject: [PATCH] Use non-blocking daos in json exporter --- .../java/org/tasks/jobs/BackupServiceTests.kt | 9 +++-- .../org/tasks/backup/TasksJsonExporter.kt | 34 ++++++++-------- .../org/tasks/dialogs/ExportTasksDialog.java | 37 ------------------ .../org/tasks/dialogs/ExportTasksDialog.kt | 39 +++++++++++++++++++ .../main/java/org/tasks/jobs/BackupWork.kt | 2 +- 5 files changed, 62 insertions(+), 59 deletions(-) delete mode 100644 app/src/main/java/org/tasks/dialogs/ExportTasksDialog.java create mode 100644 app/src/main/java/org/tasks/dialogs/ExportTasksDialog.kt diff --git a/app/src/androidTest/java/org/tasks/jobs/BackupServiceTests.kt b/app/src/androidTest/java/org/tasks/jobs/BackupServiceTests.kt index 2ece920b6..f1debc608 100644 --- a/app/src/androidTest/java/org/tasks/jobs/BackupServiceTests.kt +++ b/app/src/androidTest/java/org/tasks/jobs/BackupServiceTests.kt @@ -7,10 +7,11 @@ package org.tasks.jobs import android.net.Uri import androidx.test.InstrumentationRegistry -import com.todoroo.astrid.dao.TaskDaoBlocking +import com.todoroo.astrid.dao.TaskDao import com.todoroo.astrid.data.Task import dagger.hilt.android.testing.HiltAndroidTest import dagger.hilt.android.testing.UninstallModules +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue @@ -30,12 +31,12 @@ import javax.inject.Inject @HiltAndroidTest class BackupServiceTests : InjectingTestCase() { @Inject lateinit var jsonExporter: TasksJsonExporter - @Inject lateinit var taskDao: TaskDaoBlocking + @Inject lateinit var taskDao: TaskDao @Inject lateinit var preferences: Preferences private lateinit var temporaryDirectory: File @Before - override fun setUp() { + override fun setUp() = runBlocking { super.setUp() temporaryDirectory = try { File.createTempFile("backup", System.nanoTime().toString()) @@ -67,7 +68,7 @@ class BackupServiceTests : InjectingTestCase() { } @Test - fun testBackup() { + fun testBackup() = runBlocking { assertEquals(0, temporaryDirectory.list()!!.size) jsonExporter.exportTasks(InstrumentationRegistry.getTargetContext(), ExportType.EXPORT_TYPE_SERVICE, null) diff --git a/app/src/main/java/org/tasks/backup/TasksJsonExporter.kt b/app/src/main/java/org/tasks/backup/TasksJsonExporter.kt index 71f4741dc..4e38bb5ed 100755 --- a/app/src/main/java/org/tasks/backup/TasksJsonExporter.kt +++ b/app/src/main/java/org/tasks/backup/TasksJsonExporter.kt @@ -12,7 +12,7 @@ import com.google.gson.Gson import com.google.gson.GsonBuilder import com.todoroo.andlib.utility.DialogUtilities import com.todoroo.astrid.backup.BackupConstants -import com.todoroo.astrid.dao.TaskDaoBlocking +import com.todoroo.astrid.dao.TaskDao import com.todoroo.astrid.data.Task import org.tasks.BuildConfig import org.tasks.R @@ -32,20 +32,20 @@ import java.util.* import javax.inject.Inject class TasksJsonExporter @Inject constructor( - private val tagDataDao: TagDataDaoBlocking, - private val taskDao: TaskDaoBlocking, - private val userActivityDao: UserActivityDaoBlocking, + private val tagDataDao: TagDataDao, + private val taskDao: TaskDao, + private val userActivityDao: UserActivityDao, private val preferences: Preferences, - private val alarmDao: AlarmDaoBlocking, - private val locationDao: LocationDaoBlocking, - private val tagDao: TagDaoBlocking, - private val googleTaskDao: GoogleTaskDaoBlocking, - private val filterDao: FilterDaoBlocking, - private val googleTaskListDao: GoogleTaskListDaoBlocking, - private val taskAttachmentDao: TaskAttachmentDaoBlocking, - private val caldavDao: CaldavDaoBlocking, + private val alarmDao: AlarmDao, + private val locationDao: LocationDao, + private val tagDao: TagDao, + private val googleTaskDao: GoogleTaskDao, + private val filterDao: FilterDao, + private val googleTaskListDao: GoogleTaskListDao, + private val taskAttachmentDao: TaskAttachmentDao, + private val caldavDao: CaldavDao, private val workManager: WorkManager, - private val taskListMetadataDao: TaskListMetadataDaoBlocking) { + private val taskListMetadataDao: TaskListMetadataDao) { private var context: Context? = null private var exportCount = 0 @@ -59,19 +59,19 @@ class TasksJsonExporter @Inject constructor( progressDialog?.progress = taskNumber } - fun exportTasks(context: Context?, exportType: ExportType, progressDialog: ProgressDialog?) { + suspend fun exportTasks(context: Context?, exportType: ExportType, progressDialog: ProgressDialog?) { this.context = context exportCount = 0 this.progressDialog = progressDialog if (exportType == ExportType.EXPORT_TYPE_MANUAL) { handler = Handler() - Thread(Runnable { runBackup(exportType) }).start() + runBackup(exportType) } else { runBackup(exportType) } } - private fun runBackup(exportType: ExportType) { + private suspend fun runBackup(exportType: ExportType) { try { val filename = getFileName(exportType) val tasks = taskDao.getAll() @@ -109,7 +109,7 @@ class TasksJsonExporter @Inject constructor( } @Throws(IOException::class) - private fun doTasksExport(os: OutputStream?, tasks: List) { + private suspend fun doTasksExport(os: OutputStream?, tasks: List) { val taskBackups: MutableList = ArrayList() for (task in tasks) { setProgress(taskBackups.size, tasks.size) diff --git a/app/src/main/java/org/tasks/dialogs/ExportTasksDialog.java b/app/src/main/java/org/tasks/dialogs/ExportTasksDialog.java deleted file mode 100644 index 604b99730..000000000 --- a/app/src/main/java/org/tasks/dialogs/ExportTasksDialog.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.tasks.dialogs; - -import android.app.Dialog; -import android.app.ProgressDialog; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.DialogFragment; -import dagger.hilt.android.AndroidEntryPoint; -import javax.inject.Inject; -import org.tasks.backup.TasksJsonExporter; - -@AndroidEntryPoint -public class ExportTasksDialog extends DialogFragment { - - @Inject DialogBuilder dialogBuilder; - @Inject TasksJsonExporter tasksJsonExporter; - - public static ExportTasksDialog newExportTasksDialog() { - return new ExportTasksDialog(); - } - - @NonNull - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - ProgressDialog progressDialog = dialogBuilder.newProgressDialog(); - progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - progressDialog.setProgress(0); - progressDialog.setCancelable(false); - progressDialog.setIndeterminate(false); - progressDialog.show(); - - setCancelable(false); - tasksJsonExporter.exportTasks( - getActivity(), TasksJsonExporter.ExportType.EXPORT_TYPE_MANUAL, progressDialog); - return progressDialog; - } -} diff --git a/app/src/main/java/org/tasks/dialogs/ExportTasksDialog.kt b/app/src/main/java/org/tasks/dialogs/ExportTasksDialog.kt new file mode 100644 index 000000000..ddd7c2f1d --- /dev/null +++ b/app/src/main/java/org/tasks/dialogs/ExportTasksDialog.kt @@ -0,0 +1,39 @@ +package org.tasks.dialogs + +import android.app.Dialog +import android.app.ProgressDialog +import android.os.Bundle +import androidx.fragment.app.DialogFragment +import androidx.lifecycle.lifecycleScope +import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.launch +import org.tasks.backup.TasksJsonExporter +import javax.inject.Inject + +@AndroidEntryPoint +class ExportTasksDialog : DialogFragment() { + @Inject lateinit var dialogBuilder: DialogBuilder + @Inject lateinit var tasksJsonExporter: TasksJsonExporter + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val progressDialog = dialogBuilder.newProgressDialog() + progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) + progressDialog.progress = 0 + progressDialog.setCancelable(false) + progressDialog.isIndeterminate = false + progressDialog.show() + isCancelable = false + lifecycleScope.launch(NonCancellable) { + tasksJsonExporter.exportTasks( + activity, TasksJsonExporter.ExportType.EXPORT_TYPE_MANUAL, progressDialog) + } + return progressDialog + } + + companion object { + fun newExportTasksDialog(): ExportTasksDialog { + return ExportTasksDialog() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/tasks/jobs/BackupWork.kt b/app/src/main/java/org/tasks/jobs/BackupWork.kt index 5b1c530e9..38d177833 100644 --- a/app/src/main/java/org/tasks/jobs/BackupWork.kt +++ b/app/src/main/java/org/tasks/jobs/BackupWork.kt @@ -32,7 +32,7 @@ class BackupWork @WorkerInject constructor( override fun scheduleNext() = workManager.scheduleBackup() - private fun startBackup(context: Context?) { + private suspend fun startBackup(context: Context?) { try { deleteOldLocalBackups() } catch (e: Exception) {