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
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>(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<ReaderPage>?
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++

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.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,17 +80,18 @@ 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 ->
stateJob?.cancel()
stateJob = scope.launchUI {
chapter.stateFlow
.collectLatest { state ->
pagesContainer.removeAllViews()
when (state) {
is ReaderChapter.State.Wait -> {
}
is ReaderChapter.State.Loading -> setLoading()
is ReaderChapter.State.Error -> setError(state.error)
is ReaderChapter.State.Loaded -> setLoaded()
is ReaderChapter.State.Wait, is ReaderChapter.State.Loaded -> {
// No additional view is added
}
}
}
}
}
@ -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.
*/

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.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 ->
stateJob?.cancel()
stateJob = scope.launchUI {
chapter.stateFlow
.collectLatest { state ->
pagesContainer.removeAllViews()
when (state) {
is ReaderChapter.State.Wait -> {
}
is ReaderChapter.State.Loading -> setLoading()
is ReaderChapter.State.Error -> setError(state.error, transition)
is ReaderChapter.State.Loaded -> setLoaded()
is ReaderChapter.State.Wait, is ReaderChapter.State.Loaded -> {
// No additional view is added
}
}
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.
*/