Use MainScope for coroutines in ui package classes (#8845)

(cherry picked from commit 920ca405a2116065840c7dd074209d902bfd3419)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt
This commit is contained in:
Two-Ai 2023-01-07 10:07:09 -05:00 committed by Jobobby04
parent c516848843
commit 65c95fbb78
4 changed files with 21 additions and 25 deletions

View File

@ -13,15 +13,14 @@ import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderPageImageView
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderProgressIndicator
import eu.kanade.tachiyomi.ui.webview.WebViewActivity
import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.system.ImageUtil
import eu.kanade.tachiyomi.util.system.logcat
import eu.kanade.tachiyomi.widget.ViewPagerAdapter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import logcat.LogPriority
import rx.Observable
import rx.Subscription
@ -65,7 +64,7 @@ class PagerPageHolder(
*/
private var errorLayout: ReaderErrorBinding? = null
private val scope = CoroutineScope(Dispatchers.IO)
private val scope = MainScope()
/**
* Subscription for status changes of the page.
@ -144,7 +143,7 @@ class PagerPageHolder(
private fun launchProgressJob() {
progressJob?.cancel()
progressJob = scope.launchUI {
progressJob = scope.launch {
page.progressFlow.collectLatest { value -> progressIndicator.setProgress(value) }
}
}
@ -152,7 +151,7 @@ class PagerPageHolder(
private fun launchProgressJob2() {
extraProgressJob?.cancel()
val extraPage = extraPage ?: return
extraProgressJob = scope.launchUI {
extraProgressJob = scope.launch {
extraPage.progressFlow.collectLatest { value -> progressIndicator.setProgress(((page.progressFlow.value + value) / 2 * 0.95f).roundToInt()) }
}
}
@ -379,7 +378,7 @@ class PagerPageHolder(
logcat(LogPriority.ERROR) { "Cannot combine pages" }
return imageBytes.inputStream()
}
viewer.scope.launchUI { progressIndicator.setProgress(96) }
scope.launch { progressIndicator.setProgress(96) }
val height = imageBitmap.height
val width = imageBitmap.width
@ -407,7 +406,7 @@ class PagerPageHolder(
logcat(LogPriority.ERROR) { "Cannot combine pages" }
return imageBytes.inputStream()
}
viewer.scope.launchUI { progressIndicator.setProgress(97) }
scope.launch { progressIndicator.setProgress(97) }
val height2 = imageBitmap2.height
val width2 = imageBitmap2.width
@ -431,7 +430,7 @@ class PagerPageHolder(
}
return ImageUtil.mergeBitmaps(imageBitmap, imageBitmap2, isLTR, centerMargin, viewer.config.pageCanvasColor) {
viewer.scope.launchUI {
scope.launch {
if (it == 100) {
progressIndicator.hide()
} else {
@ -442,7 +441,7 @@ class PagerPageHolder(
}
private fun splitDoublePages() {
viewer.scope.launchUI {
scope.launch {
delay(100)
viewer.splitDoublePages(page)
if (extraPage?.fullPage == true || page.fullPage) {

View File

@ -15,13 +15,12 @@ import eu.kanade.tachiyomi.ui.reader.model.ChapterTransition
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderButton
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderTransitionView
import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.system.dpToPx
import eu.kanade.tachiyomi.widget.ViewPagerAdapter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
/**
* View of the ViewPager that contains a chapter transition.
@ -33,7 +32,7 @@ class PagerTransitionHolder(
val transition: ChapterTransition,
) : LinearLayout(readerThemedContext), ViewPagerAdapter.PositionableView {
private val scope = CoroutineScope(Dispatchers.IO)
private val scope = MainScope()
private var stateJob: Job? = null
/**
@ -81,7 +80,7 @@ class PagerTransitionHolder(
*/
private fun observeStatus(chapter: ReaderChapter) {
stateJob?.cancel()
stateJob = scope.launchUI {
stateJob = scope.launch {
chapter.stateFlow
.collectLatest { state ->
pagesContainer.removeAllViews()

View File

@ -18,13 +18,12 @@ import eu.kanade.tachiyomi.ui.reader.model.StencilPage
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderPageImageView
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderProgressIndicator
import eu.kanade.tachiyomi.ui.webview.WebViewActivity
import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.system.ImageUtil
import eu.kanade.tachiyomi.util.system.dpToPx
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import rx.Observable
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
@ -71,7 +70,7 @@ class WebtoonPageHolder(
*/
private var page: ReaderPage? = null
private val scope = CoroutineScope(Dispatchers.IO)
private val scope = MainScope()
/**
* Subscription for status changes of the page.
@ -156,7 +155,7 @@ class WebtoonPageHolder(
val page = page ?: return
progressJob = scope.launchUI {
progressJob = scope.launch {
page.progressFlow.collectLatest { value -> progressIndicator.setProgress(value) }
}
}

View File

@ -13,12 +13,11 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.reader.model.ChapterTransition
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderTransitionView
import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.system.dpToPx
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
/**
* Holder of the webtoon viewer that contains a chapter transition.
@ -28,7 +27,7 @@ class WebtoonTransitionHolder(
viewer: WebtoonViewer,
) : WebtoonBaseHolder(layout, viewer) {
private val scope = CoroutineScope(Dispatchers.IO)
private val scope = MainScope()
private var stateJob: Job? = null
private val transitionView = ReaderTransitionView(context)
@ -82,7 +81,7 @@ class WebtoonTransitionHolder(
*/
private fun observeStatus(chapter: ReaderChapter, transition: ChapterTransition) {
stateJob?.cancel()
stateJob = scope.launchUI {
stateJob = scope.launch {
chapter.stateFlow
.collectLatest { state ->
pagesContainer.removeAllViews()