diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/Statistics.kt b/AnkiDroid/src/main/java/com/ichi2/anki/Statistics.kt index df297719d3ea..773068ac2e3b 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/Statistics.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/Statistics.kt @@ -450,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) } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/UIUtils.kt b/AnkiDroid/src/main/java/com/ichi2/anki/UIUtils.kt index 4773de51d8c1..8bd4d016261f 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/UIUtils.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/UIUtils.kt @@ -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() @@ -152,7 +152,7 @@ object UIUtils { val action = view.findViewById(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 diff --git a/AnkiDroid/src/main/java/com/ichi2/async/CoroutineHelpers.kt b/AnkiDroid/src/main/java/com/ichi2/async/CoroutineHelpers.kt index c1ade807f1df..6f48f6469fa3 100644 --- a/AnkiDroid/src/main/java/com/ichi2/async/CoroutineHelpers.kt +++ b/AnkiDroid/src/main/java/com/ichi2/async/CoroutineHelpers.kt @@ -20,6 +20,7 @@ package com.ichi2.async import android.app.Activity +import androidx.fragment.app.Fragment import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.coroutineScope import com.ichi2.anki.CrashReportService @@ -31,7 +32,7 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.launch import timber.log.Timber -/* +/** * 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. @@ -52,3 +53,11 @@ fun LifecycleOwner.catchingLifecycleScope( 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)