Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NF: simplify catchingLifecycleScope #12011

Merged
merged 1 commit into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/Statistics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ class Statistics : NavigationDrawerActivity(), DeckSelectionListener, SubtitleLi
private fun createChart() {
val statisticsActivity = requireActivity() as Statistics
val taskHandler = statisticsActivity.taskHandler
statisticsJob = viewLifecycleOwner.catchingLifecycleScope(requireActivity()) {
statisticsJob = catchingLifecycleScope {
taskHandler.createChart(getChartTypeFromPosition(mSectionNumber), mProgressBar, mChart)
}
}
Expand Down Expand Up @@ -451,7 +451,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
16 changes: 15 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/async/CoroutineHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +31,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.
Expand All @@ -51,3 +52,16 @@ 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)

fun Activity.catchingLifecycleScope(
errorMessage: String? = null,
block: suspend CoroutineScope.() -> Unit
) = (this as LifecycleOwner).catchingLifecycleScope(this, errorMessage, block)