Replace RxJava in ReaderChapter and reader transitions

This commit is contained in:
arkon 2022-12-02 23:36:33 -05:00
parent bb1e7816e1
commit beda99bbe0
3 changed files with 48 additions and 75 deletions

View File

@ -1,23 +1,19 @@
package eu.kanade.tachiyomi.ui.reader.model package eu.kanade.tachiyomi.ui.reader.model
import com.jakewharton.rxrelay.BehaviorRelay
import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.ui.reader.loader.PageLoader import eu.kanade.tachiyomi.ui.reader.loader.PageLoader
import eu.kanade.tachiyomi.util.system.logcat import eu.kanade.tachiyomi.util.system.logcat
import kotlinx.coroutines.flow.MutableStateFlow
data class ReaderChapter(val chapter: Chapter) { data class ReaderChapter(val chapter: Chapter) {
var state: State = val stateFlow = MutableStateFlow<State>(State.Wait)
State.Wait var state: State
get() = stateFlow.value
set(value) { set(value) {
field = value stateFlow.value = value
stateRelay.call(value)
} }
private val stateRelay by lazy { BehaviorRelay.create(state) }
val stateObserver by lazy { stateRelay.asObservable() }
val pages: List<ReaderPage>? val pages: List<ReaderPage>?
get() = (state as? State.Loaded)?.pages get() = (state as? State.Loaded)?.pages
@ -25,8 +21,7 @@ data class ReaderChapter(val chapter: Chapter) {
var requestedPage: Int = 0 var requestedPage: Int = 0
var references = 0 private var references = 0
private set
fun ref() { fun ref() {
references++ references++

View File

@ -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.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 rx.Subscription import kotlinx.coroutines.CoroutineScope
import rx.android.schedulers.AndroidSchedulers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collectLatest
/** /**
* View of the ViewPager that contains a chapter transition. * View of the ViewPager that contains a chapter transition.
@ -30,17 +33,15 @@ class PagerTransitionHolder(
val transition: ChapterTransition, val transition: ChapterTransition,
) : LinearLayout(readerThemedContext), ViewPagerAdapter.PositionableView { ) : 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. * Item that identifies this view. Needed by the adapter to not recreate views.
*/ */
override val item: Any override val item: Any
get() = transition 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 * View container of the current status of the transition page. Child views will be added
* dynamically. * dynamically.
@ -71,8 +72,7 @@ class PagerTransitionHolder(
*/ */
override fun onDetachedFromWindow() { override fun onDetachedFromWindow() {
super.onDetachedFromWindow() super.onDetachedFromWindow()
statusSubscription?.unsubscribe() stateJob?.cancel()
statusSubscription = null
} }
/** /**
@ -80,19 +80,20 @@ class PagerTransitionHolder(
* state, the pages container is cleaned up before setting the new state. * state, the pages container is cleaned up before setting the new state.
*/ */
private fun observeStatus(chapter: ReaderChapter) { private fun observeStatus(chapter: ReaderChapter) {
statusSubscription?.unsubscribe() stateJob?.cancel()
statusSubscription = chapter.stateObserver stateJob = scope.launchUI {
.observeOn(AndroidSchedulers.mainThread()) chapter.stateFlow
.subscribe { state -> .collectLatest { state ->
pagesContainer.removeAllViews() pagesContainer.removeAllViews()
when (state) { when (state) {
is ReaderChapter.State.Wait -> { 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) 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. * Sets the error state on the pages container.
*/ */

View File

@ -13,9 +13,12 @@ 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 rx.Subscription import kotlinx.coroutines.CoroutineScope
import rx.android.schedulers.AndroidSchedulers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collectLatest
/** /**
* Holder of the webtoon viewer that contains a chapter transition. * Holder of the webtoon viewer that contains a chapter transition.
@ -25,10 +28,8 @@ class WebtoonTransitionHolder(
viewer: WebtoonViewer, viewer: WebtoonViewer,
) : WebtoonBaseHolder(layout, viewer) { ) : WebtoonBaseHolder(layout, viewer) {
/** private val scope = CoroutineScope(Dispatchers.IO)
* Subscription for status changes of the transition page. private var stateJob: Job? = null
*/
private var statusSubscription: Subscription? = null
private val transitionView = ReaderTransitionView(context) private val transitionView = ReaderTransitionView(context)
@ -72,7 +73,7 @@ class WebtoonTransitionHolder(
* Called when the view is recycled and being added to the view pool. * Called when the view is recycled and being added to the view pool.
*/ */
override fun recycle() { override fun recycle() {
unsubscribeStatus() stateJob?.cancel()
} }
/** /**
@ -80,31 +81,21 @@ class WebtoonTransitionHolder(
* state, the pages container is cleaned up before setting the new state. * state, the pages container is cleaned up before setting the new state.
*/ */
private fun observeStatus(chapter: ReaderChapter, transition: ChapterTransition) { private fun observeStatus(chapter: ReaderChapter, transition: ChapterTransition) {
unsubscribeStatus() stateJob?.cancel()
stateJob = scope.launchUI {
statusSubscription = chapter.stateObserver chapter.stateFlow
.observeOn(AndroidSchedulers.mainThread()) .collectLatest { state ->
.subscribe { state -> pagesContainer.removeAllViews()
pagesContainer.removeAllViews() when (state) {
when (state) { is ReaderChapter.State.Loading -> setLoading()
is ReaderChapter.State.Wait -> { 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() pagesContainer.isVisible = pagesContainer.isNotEmpty()
is ReaderChapter.State.Error -> setError(state.error, transition)
is ReaderChapter.State.Loaded -> setLoaded()
} }
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) 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. * Sets the error state on the pages container.
*/ */