From beda99bbe02aefde7a24c2109ae0784a78514e34 Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 2 Dec 2022 23:36:33 -0500 Subject: [PATCH] Replace RxJava in ReaderChapter and reader transitions --- .../ui/reader/model/ReaderChapter.kt | 17 ++---- .../viewer/pager/PagerTransitionHolder.kt | 48 +++++++-------- .../viewer/webtoon/WebtoonTransitionHolder.kt | 58 +++++++------------ 3 files changed, 48 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/ReaderChapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/ReaderChapter.kt index a332a96ec..fe216f9ac 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/ReaderChapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/ReaderChapter.kt @@ -1,23 +1,19 @@ package eu.kanade.tachiyomi.ui.reader.model -import com.jakewharton.rxrelay.BehaviorRelay import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.ui.reader.loader.PageLoader import eu.kanade.tachiyomi.util.system.logcat +import kotlinx.coroutines.flow.MutableStateFlow data class ReaderChapter(val chapter: Chapter) { - var state: State = - State.Wait + val stateFlow = MutableStateFlow(State.Wait) + var state: State + get() = stateFlow.value set(value) { - field = value - stateRelay.call(value) + stateFlow.value = value } - private val stateRelay by lazy { BehaviorRelay.create(state) } - - val stateObserver by lazy { stateRelay.asObservable() } - val pages: List? get() = (state as? State.Loaded)?.pages @@ -25,8 +21,7 @@ data class ReaderChapter(val chapter: Chapter) { var requestedPage: Int = 0 - var references = 0 - private set + private var references = 0 fun ref() { references++ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerTransitionHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerTransitionHolder.kt index 4445923d8..464b0f0f2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerTransitionHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerTransitionHolder.kt @@ -15,10 +15,13 @@ 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 rx.Subscription -import rx.android.schedulers.AndroidSchedulers +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.collectLatest /** * View of the ViewPager that contains a chapter transition. @@ -30,17 +33,15 @@ class PagerTransitionHolder( val transition: ChapterTransition, ) : LinearLayout(readerThemedContext), ViewPagerAdapter.PositionableView { + private val scope = CoroutineScope(Dispatchers.IO) + private var stateJob: Job? = null + /** * Item that identifies this view. Needed by the adapter to not recreate views. */ override val item: Any get() = transition - /** - * Subscription for status changes of the transition page. - */ - private var statusSubscription: Subscription? = null - /** * View container of the current status of the transition page. Child views will be added * dynamically. @@ -71,8 +72,7 @@ class PagerTransitionHolder( */ override fun onDetachedFromWindow() { super.onDetachedFromWindow() - statusSubscription?.unsubscribe() - statusSubscription = null + stateJob?.cancel() } /** @@ -80,19 +80,20 @@ class PagerTransitionHolder( * state, the pages container is cleaned up before setting the new state. */ private fun observeStatus(chapter: ReaderChapter) { - statusSubscription?.unsubscribe() - statusSubscription = chapter.stateObserver - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { state -> - pagesContainer.removeAllViews() - when (state) { - is ReaderChapter.State.Wait -> { + stateJob?.cancel() + stateJob = scope.launchUI { + chapter.stateFlow + .collectLatest { state -> + pagesContainer.removeAllViews() + when (state) { + is ReaderChapter.State.Loading -> setLoading() + is ReaderChapter.State.Error -> setError(state.error) + is ReaderChapter.State.Wait, is ReaderChapter.State.Loaded -> { + // No additional view is added + } } - is ReaderChapter.State.Loading -> setLoading() - is ReaderChapter.State.Error -> setError(state.error) - is ReaderChapter.State.Loaded -> setLoaded() } - } + } } /** @@ -111,13 +112,6 @@ class PagerTransitionHolder( pagesContainer.addView(textView) } - /** - * Sets the loaded state on the pages container. - */ - private fun setLoaded() { - // No additional view is added - } - /** * Sets the error state on the pages container. */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonTransitionHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonTransitionHolder.kt index 67e9b5b1d..e8e071573 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonTransitionHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonTransitionHolder.kt @@ -13,9 +13,12 @@ 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 rx.Subscription -import rx.android.schedulers.AndroidSchedulers +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.collectLatest /** * Holder of the webtoon viewer that contains a chapter transition. @@ -25,10 +28,8 @@ class WebtoonTransitionHolder( viewer: WebtoonViewer, ) : WebtoonBaseHolder(layout, viewer) { - /** - * Subscription for status changes of the transition page. - */ - private var statusSubscription: Subscription? = null + private val scope = CoroutineScope(Dispatchers.IO) + private var stateJob: Job? = null private val transitionView = ReaderTransitionView(context) @@ -72,7 +73,7 @@ class WebtoonTransitionHolder( * Called when the view is recycled and being added to the view pool. */ override fun recycle() { - unsubscribeStatus() + stateJob?.cancel() } /** @@ -80,31 +81,21 @@ class WebtoonTransitionHolder( * state, the pages container is cleaned up before setting the new state. */ private fun observeStatus(chapter: ReaderChapter, transition: ChapterTransition) { - unsubscribeStatus() - - statusSubscription = chapter.stateObserver - .observeOn(AndroidSchedulers.mainThread()) - .subscribe { state -> - pagesContainer.removeAllViews() - when (state) { - is ReaderChapter.State.Wait -> { + stateJob?.cancel() + stateJob = scope.launchUI { + chapter.stateFlow + .collectLatest { state -> + pagesContainer.removeAllViews() + when (state) { + is ReaderChapter.State.Loading -> setLoading() + is ReaderChapter.State.Error -> setError(state.error, transition) + is ReaderChapter.State.Wait, is ReaderChapter.State.Loaded -> { + // No additional view is added + } } - is ReaderChapter.State.Loading -> setLoading() - is ReaderChapter.State.Error -> setError(state.error, transition) - is ReaderChapter.State.Loaded -> setLoaded() + pagesContainer.isVisible = pagesContainer.isNotEmpty() } - pagesContainer.isVisible = pagesContainer.isNotEmpty() - } - - addSubscription(statusSubscription) - } - - /** - * Unsubscribes from the status subscription. - */ - private fun unsubscribeStatus() { - removeSubscription(statusSubscription) - statusSubscription = null + } } /** @@ -123,13 +114,6 @@ class WebtoonTransitionHolder( pagesContainer.addView(textView) } - /** - * Sets the loaded state on the pages container. - */ - private fun setLoaded() { - // No additional view is added - } - /** * Sets the error state on the pages container. */