From 3f47ecff8eebc50a6e14e3f7e2fb224f5bbcf9a8 Mon Sep 17 00:00:00 2001 From: lukstbit <52494258+lukstbit@users.noreply.github.com> Date: Sun, 27 Nov 2022 16:02:16 +0200 Subject: [PATCH] Migrate the RepositionField task to coroutine (#12787) * Extract RepositionField task usages into a single method This will make it easier to migrate to coroutine. * Inline the RepositionField task in ModelFieldEditor * Migrate the RepositionField task to coroutine The handler was removed(no other usages) and it's actions were inlined: - on start it showed a ProgressBar(done with withProgress) - on task completed it dismissed the progressbar and reinitialized the activity(or it closed the activity for exception in the task) Also deletes the in layout ProgressBar related code as it isn't use anymore. --- .../java/com/ichi2/anki/ModelFieldEditor.kt | 75 ++++++------------- .../java/com/ichi2/async/CollectionTask.kt | 20 ----- 2 files changed, 22 insertions(+), 73 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt index f87993cc8350..83a6a06470a2 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt @@ -43,14 +43,9 @@ import com.ichi2.anki.dialogs.ModelEditorContextMenu.ModelEditorContextMenuActio import com.ichi2.anki.exception.ConfirmModSchemaException import com.ichi2.anki.servicelayer.LanguageHintService.setLanguageHintForField import com.ichi2.anki.snackbar.showSnackbar -import com.ichi2.async.CollectionTask.RepositionField -import com.ichi2.async.TaskListenerWithContext -import com.ichi2.async.TaskManager import com.ichi2.libanki.Collection import com.ichi2.libanki.Model -import com.ichi2.themes.StyledProgressDialog.Companion.show import com.ichi2.ui.FixedEditText -import com.ichi2.utils.KotlinCleanup import com.ichi2.utils.displayKeyboard import com.ichi2.utils.toStringList import com.ichi2.widget.WidgetStatus @@ -63,8 +58,6 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler { // Position of the current field selected private var currentPos = 0 private lateinit var mFieldsListView: ListView - @Suppress("Deprecation") - private var progressDialog: android.app.ProgressDialog? = null // TODO: Check alternatives to ProgressDialog private var fieldNameInput: EditText? = null private lateinit var collection: Collection private lateinit var mModel: Model @@ -357,11 +350,10 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler { if (pos < 1 || pos > mFieldsLabels.size) { showThemedToast(this@ModelFieldEditor, resources.getString(R.string.toast_out_of_range), true) } else { - val listener = changeFieldHandler() // Input is valid, now attempt to modify try { collection.modSchema() - TaskManager.launchCollectionTask(RepositionField(mModel, mNoteFields.getJSONObject(currentPos), pos - 1), listener) + repositionField(pos - 1) } catch (e: ConfirmModSchemaException) { e.log() @@ -371,13 +363,7 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler { val confirm = Runnable { try { collection.modSchemaNoCheck() - TaskManager.launchCollectionTask( - RepositionField( - mModel, - mNoteFields.getJSONObject(currentPos), pos - 1 - ), - listener - ) + repositionField(pos - 1) } catch (e1: JSONException) { throw RuntimeException(e1) } @@ -393,14 +379,27 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler { } } - // ---------------------------------------------------------------------------- - // HELPER METHODS - // ---------------------------------------------------------------------------- - private fun dismissProgressBar() { - if (progressDialog != null) { - progressDialog!!.dismiss() + private fun repositionField(index: Int) { + launchCatchingTask { + withProgress(message = getString(R.string.model_field_editor_changing)) { + val result = withCol { + Timber.d("doInBackgroundRepositionField") + try { + models.moveField(mModel, mNoteFields.getJSONObject(currentPos), index) + save() + true + } catch (e: ConfirmModSchemaException) { + e.log() + // Should never be reached + false + } + } + if (!result) { + closeActivity() + } + initialize() + } } - progressDialog = null } /* @@ -458,36 +457,6 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler { field.put("sticky", !field.getBoolean("sticky")) } - // ---------------------------------------------------------------------------- - // HANDLERS - // ---------------------------------------------------------------------------- - /* - * Called during the desk task when any field is modified - */ - private fun changeFieldHandler(): ChangeHandler { - return ChangeHandler(this) - } - - private class ChangeHandler(modelFieldEditor: ModelFieldEditor) : TaskListenerWithContext(modelFieldEditor) { - override fun actualOnPreExecute(context: ModelFieldEditor) { - if (context.progressDialog == null) { - context.progressDialog = show( - context, context.intent.getStringExtra("title"), - context.resources.getString(R.string.model_field_editor_changing), false - ) - } - } - - @KotlinCleanup("Convert result to non-null") - override fun actualOnPostExecute(context: ModelFieldEditor, result: Boolean?) { - if (result == false) { - context.closeActivity() - } - context.dismissProgressBar() - context.initialize() - } - } - override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) { android.R.id.home -> { onBackPressed() diff --git a/AnkiDroid/src/main/java/com/ichi2/async/CollectionTask.kt b/AnkiDroid/src/main/java/com/ichi2/async/CollectionTask.kt index fa23f901cc7d..43099bd48921 100644 --- a/AnkiDroid/src/main/java/com/ichi2/async/CollectionTask.kt +++ b/AnkiDroid/src/main/java/com/ichi2/async/CollectionTask.kt @@ -22,7 +22,6 @@ import android.content.Context import com.fasterxml.jackson.core.JsonToken import com.ichi2.anki.* import com.ichi2.anki.AnkiSerialization.factory -import com.ichi2.anki.exception.ConfirmModSchemaException import com.ichi2.anki.exception.ImportExportException import com.ichi2.libanki.* import com.ichi2.libanki.Collection @@ -31,7 +30,6 @@ import com.ichi2.libanki.importer.AnkiPackageImporter import com.ichi2.utils.Computation import com.ichi2.utils.KotlinCleanup import org.apache.commons.compress.archivers.zip.ZipFile -import org.json.JSONObject import timber.log.Timber import java.io.File import java.io.FileNotFoundException @@ -318,24 +316,6 @@ open class CollectionTask(val task: TaskDelegateBase() { - override fun task(col: Collection, collectionTask: ProgressSenderAndCancelListener): Boolean { - Timber.d("doInBackgroundRepositionField") - try { - col.models.moveField(model, field, index) - col.save() - } catch (e: ConfirmModSchemaException) { - e.log() - // Should never be reached - return false - } - return true - } - } - class FindEmptyCards : TaskDelegate?>() { override fun task(col: Collection, collectionTask: ProgressSenderAndCancelListener): List { return col.emptyCids(collectionTask)