0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 03:52:15 +02:00

[GSoC] Coroutines migration ConfSetSubdecks (#12592)

* Extracted out ConfSetSubdecks::task to confSetSubdecks

* Migrated ConfSetSubdecks to Coroutines

Since the launch is taking place from inside a AppCompatPreferenceActivity which is not a lifecycleOwner. And launchCatchingTask can only happen from a lifecycleOwner, so a custom CoroutineScope implementation is required which cancels any job (launched from this activity) when the activity gets destroyed and that is achieved by extending 'AppCompatPreferenceActivity: CoroutineScope by MainScope' and calling cancel in onDestroy.

MainScope delegates the CoroutineScope implementation with having Main dispatcher as default for launch{} calls. This is the reason why coroutine is launched using launch{} instead of launchCatchingTask{}

* Exceptions are now handled by CoroutineExceptionHandler

* Moved confSetSubdecks to DeckOptions
This commit is contained in:
Divyansh Kushwaha 2022-10-09 12:53:12 +05:30 committed by GitHub
parent b8eccc1ab7
commit d3a1ab4766
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 25 deletions

View File

@ -19,7 +19,9 @@
package com.ichi2.anki
import android.app.AlarmManager
import android.content.*
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Configuration
import android.os.Bundle
import android.preference.CheckBoxPreference
@ -34,9 +36,7 @@ import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.exception.ConfirmModSchemaException
import com.ichi2.anki.services.ReminderService
import com.ichi2.annotations.NeedsTest
import com.ichi2.async.CollectionTask
import com.ichi2.async.TaskListenerWithContext
import com.ichi2.async.TaskManager
import com.ichi2.async.changeDeckConfiguration
import com.ichi2.compat.CompatHelper
import com.ichi2.libanki.Consts
@ -234,6 +234,7 @@ class DeckOptions :
}
"newSteps" -> mOptions.getJSONObject("new").put("delays", StepsPreference.convertToJSON((value as String)))
"lapSteps" -> mOptions.getJSONObject("lapse").put("delays", StepsPreference.convertToJSON((value as String)))
// TODO: Extract out deckConf, confReset, remConf and confSetSubdecks to a function. They are overall similar.
"deckConf" -> {
val newConfId: Long = (value as String).toLong()
mOptions = col.decks.getConf(newConfId)!!
@ -305,7 +306,22 @@ class DeckOptions :
}
}
"confSetSubdecks" -> if (value as Boolean) {
TaskManager.launchCollectionTask(CollectionTask.ConfSetSubdecks(deck, mOptions), confChangeHandler())
launch(getCoroutineExceptionHandler(this@DeckOptions)) {
preConfChange()
try {
withCol {
Timber.d("confSetSubdecks")
val children = col.decks.children(deck.getLong("id"))
for (childDid in children.values) {
val child = col.decks.get(childDid)
if (child.isDyn) continue
changeDeckConfiguration(deck, mOptions, col)
}
}
} finally {
postConfChange()
}
}
}
"reminderEnabled" -> {
val reminder = JSONObject()

View File

@ -573,27 +573,6 @@ open class CollectionTask<Progress, Result>(val task: TaskDelegateBase<Progress,
}
}
@KotlinCleanup("fix `val changed = execTask()!!`")
class ConfSetSubdecks(private val deck: Deck, private val conf: DeckConfig) : TaskDelegate<Void, Boolean>() {
override fun task(col: Collection, collectionTask: ProgressSenderAndCancelListener<Void>): Boolean {
Timber.d("doInBackgroundConfSetSubdecks")
return try {
val children = col.decks.children(deck.getLong("id"))
for (childDid in children.values) {
val child = col.decks.get(childDid)
if (child.isDyn) {
continue
}
changeDeckConfiguration(deck, conf, col)
}
true
} catch (e: JSONException) {
Timber.w(e)
false
}
}
}
/**
* Deletes the given field in the given model
*/