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

This commit is contained in:
Two-Ai 2023-01-07 10:07:09 -05:00 committed by GitHub
parent 6d3a3b3f39
commit 920ca405a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 20 deletions

View File

@ -13,13 +13,12 @@ import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderPageImageView import eu.kanade.tachiyomi.ui.reader.viewer.ReaderPageImageView
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderProgressIndicator import eu.kanade.tachiyomi.ui.reader.viewer.ReaderProgressIndicator
import eu.kanade.tachiyomi.ui.webview.WebViewActivity 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.ImageUtil
import eu.kanade.tachiyomi.widget.ViewPagerAdapter import eu.kanade.tachiyomi.widget.ViewPagerAdapter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import rx.Observable import rx.Observable
import rx.Subscription import rx.Subscription
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
@ -58,7 +57,7 @@ class PagerPageHolder(
*/ */
private var errorLayout: ReaderErrorBinding? = null private var errorLayout: ReaderErrorBinding? = null
private val scope = CoroutineScope(Dispatchers.IO) private val scope = MainScope()
/** /**
* Subscription for status changes of the page. * Subscription for status changes of the page.
@ -108,7 +107,7 @@ class PagerPageHolder(
private fun launchProgressJob() { private fun launchProgressJob() {
progressJob?.cancel() progressJob?.cancel()
progressJob = scope.launchUI { progressJob = scope.launch {
page.progressFlow.collectLatest { value -> progressIndicator.setProgress(value) } page.progressFlow.collectLatest { value -> progressIndicator.setProgress(value) }
} }
} }

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.model.ReaderChapter
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderButton import eu.kanade.tachiyomi.ui.reader.viewer.ReaderButton
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderTransitionView 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.util.system.dpToPx
import eu.kanade.tachiyomi.widget.ViewPagerAdapter import eu.kanade.tachiyomi.widget.ViewPagerAdapter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
/** /**
* View of the ViewPager that contains a chapter transition. * View of the ViewPager that contains a chapter transition.
@ -33,7 +32,7 @@ class PagerTransitionHolder(
val transition: ChapterTransition, val transition: ChapterTransition,
) : LinearLayout(readerThemedContext), ViewPagerAdapter.PositionableView { ) : LinearLayout(readerThemedContext), ViewPagerAdapter.PositionableView {
private val scope = CoroutineScope(Dispatchers.IO) private val scope = MainScope()
private var stateJob: Job? = null private var stateJob: Job? = null
/** /**
@ -81,7 +80,7 @@ class PagerTransitionHolder(
*/ */
private fun observeStatus(chapter: ReaderChapter) { private fun observeStatus(chapter: ReaderChapter) {
stateJob?.cancel() stateJob?.cancel()
stateJob = scope.launchUI { stateJob = scope.launch {
chapter.stateFlow chapter.stateFlow
.collectLatest { state -> .collectLatest { state ->
pagesContainer.removeAllViews() 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.ReaderPageImageView
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderProgressIndicator import eu.kanade.tachiyomi.ui.reader.viewer.ReaderProgressIndicator
import eu.kanade.tachiyomi.ui.webview.WebViewActivity 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.ImageUtil
import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.dpToPx
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import rx.Observable import rx.Observable
import rx.Subscription import rx.Subscription
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
@ -71,7 +70,7 @@ class WebtoonPageHolder(
*/ */
private var page: ReaderPage? = null private var page: ReaderPage? = null
private val scope = CoroutineScope(Dispatchers.IO) private val scope = MainScope()
/** /**
* Subscription for status changes of the page. * Subscription for status changes of the page.
@ -156,7 +155,7 @@ class WebtoonPageHolder(
val page = page ?: return val page = page ?: return
progressJob = scope.launchUI { progressJob = scope.launch {
page.progressFlow.collectLatest { value -> progressIndicator.setProgress(value) } 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.ChapterTransition
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import eu.kanade.tachiyomi.ui.reader.viewer.ReaderTransitionView 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.util.system.dpToPx
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
/** /**
* Holder of the webtoon viewer that contains a chapter transition. * Holder of the webtoon viewer that contains a chapter transition.
@ -28,7 +27,7 @@ class WebtoonTransitionHolder(
viewer: WebtoonViewer, viewer: WebtoonViewer,
) : WebtoonBaseHolder(layout, viewer) { ) : WebtoonBaseHolder(layout, viewer) {
private val scope = CoroutineScope(Dispatchers.IO) private val scope = MainScope()
private var stateJob: Job? = null private var stateJob: Job? = null
private val transitionView = ReaderTransitionView(context) private val transitionView = ReaderTransitionView(context)
@ -82,7 +81,7 @@ class WebtoonTransitionHolder(
*/ */
private fun observeStatus(chapter: ReaderChapter, transition: ChapterTransition) { private fun observeStatus(chapter: ReaderChapter, transition: ChapterTransition) {
stateJob?.cancel() stateJob?.cancel()
stateJob = scope.launchUI { stateJob = scope.launch {
chapter.stateFlow chapter.stateFlow
.collectLatest { state -> .collectLatest { state ->
pagesContainer.removeAllViews() pagesContainer.removeAllViews()