Skip to content

Commit

Permalink
NF: simplify catchingLifecycleScope
Browse files Browse the repository at this point in the history
My understanding is that this method will often be called either on fragments,
and using the fragment's activity. So it seems to make sense to simplify it,
ensuring that we avoid a parameter that will probably never change.

Actually, since activities are also LifecycleOwner, it may be possible to ensure
this method is even simpler, by defining it on activities instead of defining it
on LifecycleOwner. Unless we have a reason to use an activity distinct from the
LifecycleOwner.

Also cleaning from function I viewed at the same time
  • Loading branch information
Arthur-Milchior committed Aug 13, 2022
1 parent e701342 commit 87ae4d3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 58 deletions.
33 changes: 33 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/CoroutineHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package com.ichi2.anki

import android.app.Activity
import android.content.Context
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.coroutineScope
import anki.collection.Progress
import com.ichi2.anki.UIUtils.showSimpleSnackbar
Expand Down Expand Up @@ -232,3 +235,33 @@ suspend fun AnkiActivity.userAcceptsSchemaChange(col: Collection): Boolean {
.show()
}
}

/**
* Launch a job that catches any uncaught errors, informs the user and prints it to Log.
* 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 LifecycleOwner.catchingLifecycleScope(
activity: Activity,
errorMessage: String? = null,
block: suspend CoroutineScope.() -> Unit
): Job = lifecycle.coroutineScope.launch {
try {
block()
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
// TODO: localize
Timber.w(e, errorMessage)
UIUtils.showDismissibleSnackbar(activity, "An error occurred: $e", R.string.close)
CrashReportService.sendExceptionReport(e, activity::class.java.simpleName)
}
}

/**
* @see [LifecycleOwner.catchingLifecycleScope]
*/
fun Fragment.catchingLifecycleScope(
errorMessage: String? = null,
block: suspend CoroutineScope.() -> Unit
) = (this as LifecycleOwner).catchingLifecycleScope(requireActivity(), errorMessage, block)
3 changes: 1 addition & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/Statistics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import com.ichi2.anki.stats.AnkiStatsTaskHandler
import com.ichi2.anki.stats.AnkiStatsTaskHandler.Companion.getInstance
import com.ichi2.anki.stats.ChartView
import com.ichi2.anki.widgets.DeckDropDownAdapter.SubtitleListener
import com.ichi2.async.catchingLifecycleScope
import com.ichi2.libanki.Collection
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.Decks
Expand Down Expand Up @@ -451,7 +450,7 @@ class Statistics : NavigationDrawerActivity(), DeckSelectionListener, SubtitleLi

private fun createStatisticOverview() {
val handler = (requireActivity() as Statistics).taskHandler
statisticsJob = catchingLifecycleScope(requireActivity(), "createStatisticOverview failed with error") {
statisticsJob = catchingLifecycleScope("createStatisticOverview failed with error") {
handler.createStatisticsOverview(mWebView, mProgressBar)
}
}
Expand Down
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/UIUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ object UIUtils {
}

@JvmStatic
fun getDismissibleSnackbar(activity: Activity?, mainText: String, length: Int, dismissTextResource: Int, root: View): Snackbar {
fun getDismissibleSnackbar(context: Context, mainText: String, length: Int, dismissTextResource: Int, root: View): Snackbar {
val sb = Snackbar.make(root, mainText, length)
sb.setAction(dismissTextResource) {
sb.dismiss()
Expand All @@ -152,7 +152,7 @@ object UIUtils {
val action = view.findViewById<TextView>(com.google.android.material.R.id.snackbar_action)
if (tv != null && action != null) {
tv.setTextColor(Color.WHITE)
action.setTextColor(ContextCompat.getColor(activity!!, R.color.material_light_blue_500))
action.setTextColor(ContextCompat.getColor(context, R.color.material_light_blue_500))
tv.maxLines = 2 // prevent tablets from truncating to 1 line
}
return sb
Expand Down
54 changes: 0 additions & 54 deletions AnkiDroid/src/main/java/com/ichi2/async/CoroutineHelpers.kt

This file was deleted.

0 comments on commit 87ae4d3

Please sign in to comment.