Skip to content

Commit

Permalink
Migrate the RepositionField task to coroutine (ankidroid#12787)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
lukstbit authored Nov 27, 2022
1 parent 3e214a4 commit 3f47ecf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 73 deletions.
75 changes: 22 additions & 53 deletions AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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)
}
Expand All @@ -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
}

/*
Expand Down Expand Up @@ -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, Void?, Boolean?>(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()
Expand Down
20 changes: 0 additions & 20 deletions AnkiDroid/src/main/java/com/ichi2/async/CollectionTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -318,24 +316,6 @@ open class CollectionTask<Progress, Result>(val task: TaskDelegateBase<Progress,
}
}

/**
* Repositions the given field in the given model
*/
class RepositionField(private val model: Model, private val field: JSONObject, private val index: Int) : TaskDelegate<Void, Boolean>() {
override fun task(col: Collection, collectionTask: ProgressSenderAndCancelListener<Void>): 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<Int, List<Long>?>() {
override fun task(col: Collection, collectionTask: ProgressSenderAndCancelListener<Int>): List<Long> {
return col.emptyCids(collectionTask)
Expand Down

0 comments on commit 3f47ecf

Please sign in to comment.