Skip to content

Commit

Permalink
Shift launchCatchingTask to FragmentActivity; hook undo up in StudyOp…
Browse files Browse the repository at this point in the history
…tions

Change based on ankidroid#12011 (comment)
  • Loading branch information
dae committed Aug 15, 2022
1 parent e5a052f commit 0d051f5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
6 changes: 4 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/BackendUndo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package com.ichi2.anki

import androidx.fragment.app.FragmentActivity
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.UIUtils.showSimpleSnackbar
import com.ichi2.libanki.undoNew
import com.ichi2.libanki.undoableOp
import com.ichi2.utils.BlocksSchemaUpgrade
import net.ankiweb.rsdroid.BackendException

suspend fun AnkiActivity.backendUndoAndShowPopup(): Boolean {
suspend fun FragmentActivity.backendUndoAndShowPopup(): Boolean {
return try {
val changes = withProgress() {
undoableOp {
Expand All @@ -31,7 +33,7 @@ suspend fun AnkiActivity.backendUndoAndShowPopup(): Boolean {
}
showSimpleSnackbar(
this,
col.tr.undoActionUndone(changes.operation),
TR.undoActionUndone(changes.operation),
false
)
true
Expand Down
24 changes: 17 additions & 7 deletions AnkiDroid/src/main/java/com/ichi2/anki/CoroutineHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package com.ichi2.anki

import android.content.Context
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.coroutineScope
import anki.collection.Progress
import com.ichi2.anki.UIUtils.showSimpleSnackbar
Expand All @@ -36,27 +38,35 @@ import kotlin.coroutines.suspendCoroutine
* Errors from the backend contain localized text that is often suitable to show to the user as-is.
* Other errors should ideally be handled in the block.
*/
fun AnkiActivity.launchCatchingTask(
fun FragmentActivity.launchCatchingTask(
errorMessage: String? = null,
block: suspend CoroutineScope.() -> Unit
): Job {
val extraInfo = errorMessage ?: ""
return lifecycle.coroutineScope.launch {
try {
block()
} catch (exc: CancellationException) {
// do nothing
} catch (exc: BackendInterruptedException) {
Timber.e("caught: %s", exc)
Timber.e("caught: %s %s", exc, extraInfo)
showSimpleSnackbar(this@launchCatchingTask, exc.localizedMessage, false)
} catch (exc: BackendException) {
Timber.e("caught: %s", exc)
Timber.e("caught: %s %s", exc, extraInfo)
showError(this@launchCatchingTask, exc.localizedMessage!!)
} catch (exc: Exception) {
Timber.e("caught: %s", exc)
Timber.e("caught: %s %s", exc, extraInfo)
showError(this@launchCatchingTask, exc.toString())
}
}
}

/** See [FragmentActivity.launchCatchingTask] */
fun Fragment.launchCatchingTask(
errorMessage: String? = null,
block: suspend CoroutineScope.() -> Unit
): Job = requireActivity().launchCatchingTask(errorMessage, block)

private fun showError(context: Context, msg: String) {
AlertDialog.Builder(context)
.setTitle(R.string.vague_error)
Expand Down Expand Up @@ -90,7 +100,7 @@ suspend fun <T> Backend.withProgress(
* Run the provided operation, showing a progress window until it completes.
* Progress info is polled from the backend.
*/
suspend fun <T> AnkiActivity.withProgress(
suspend fun <T> FragmentActivity.withProgress(
extractProgress: ProgressContext.() -> Unit,
onCancel: ((Backend) -> Unit)? = { it.setWantsAbort() },
op: suspend () -> T
Expand All @@ -117,7 +127,7 @@ suspend fun <T> AnkiActivity.withProgress(
* Run the provided operation, showing a progress window with the provided
* message until the operation completes.
*/
suspend fun <T> AnkiActivity.withProgress(
suspend fun <T> FragmentActivity.withProgress(
message: String = resources.getString(R.string.dialog_processing),
op: suspend () -> T
): T = withProgressDialog(
Expand All @@ -130,7 +140,7 @@ suspend fun <T> AnkiActivity.withProgress(
}

private suspend fun <T> withProgressDialog(
context: AnkiActivity,
context: FragmentActivity,
onCancel: (() -> Unit)?,
@Suppress("Deprecation") // ProgressDialog deprecation
op: suspend (android.app.ProgressDialog) -> T
Expand Down
13 changes: 12 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/StudyOptionsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import com.ichi2.themes.StyledProgressDialog.Companion.show
import com.ichi2.utils.FragmentFactoryUtils.instantiate
import com.ichi2.utils.HtmlUtils.convertNewlinesToHtml
import com.ichi2.utils.KotlinCleanup
import net.ankiweb.rsdroid.BackendFactory
import timber.log.Timber

class StudyOptionsFragment : Fragment(), Toolbar.OnMenuItemClickListener {
Expand Down Expand Up @@ -255,7 +256,17 @@ class StudyOptionsFragment : Fragment(), Toolbar.OnMenuItemClickListener {
when (item.itemId) {
R.id.action_undo -> {
Timber.i("StudyOptionsFragment:: Undo button pressed")
Undo().runWithHandler(mUndoListener)
if (BackendFactory.defaultLegacySchema) {
Undo().runWithHandler(mUndoListener)
} else {
launchCatchingTask {
if (requireActivity().backendUndoAndShowPopup()) {
openReviewer()
} else {
Undo().runWithHandler(mUndoListener)
}
}
}
return true
}
R.id.action_deck_or_study_options -> {
Expand Down

0 comments on commit 0d051f5

Please sign in to comment.