Skip to content

Commit

Permalink
improvement: use repeatOnLifecycle
Browse files Browse the repository at this point in the history
review request
  • Loading branch information
david-allison committed Jan 8, 2025
1 parent 1055323 commit b9b20d7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.color.MaterialColors
import com.google.android.material.snackbar.Snackbar
import com.ichi2.anim.ActivityTransitionAnimation
Expand Down Expand Up @@ -725,14 +727,16 @@ open class AnkiActivity :
// TODO: Move this to an extension method once we have context parameters
protected fun <T> Flow<T>.launchCollectionInLifecycleScope(block: suspend (T) -> Unit) {
lifecycleScope.launch {
this@launchCollectionInLifecycleScope.collect {
if (isRobolectric) {
// hack: lifecycleScope/runOnUiThread do not handle our
// test dispatcher overriding both IO and Main
// in tests, waitForAsyncTasksToComplete may be required.
HandlerUtils.postOnNewHandler { runBlocking { block(it) } }
} else {
block(it)
lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED) {
this@launchCollectionInLifecycleScope.collect {
if (isRobolectric) {
// hack: lifecycleScope/runOnUiThread do not handle our
// test dispatcher overriding both IO and Main
// in tests, waitForAsyncTasksToComplete may be required.
HandlerUtils.postOnNewHandler { runBlocking { block(it) } }
} else {
block(it)
}
}
}
}
Expand Down

0 comments on commit b9b20d7

Please sign in to comment.