Some random refactoring of webtoon mode from upstream

To later take the missing chapters stuff

Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com>
This commit is contained in:
Jays2Kings 2021-04-13 23:38:51 -04:00
parent c9ee715f01
commit 6d6b42b2ca
7 changed files with 42 additions and 22 deletions

View File

@ -3,17 +3,13 @@ package eu.kanade.tachiyomi.ui.reader.viewer
import com.tfcporciuncula.flow.Preference import com.tfcporciuncula.flow.Preference
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
/** /**
* Common configuration for all viewers. * Common configuration for all viewers.
*/ */
abstract class ViewerConfig(preferences: PreferencesHelper) { abstract class ViewerConfig(preferences: PreferencesHelper, protected val scope: CoroutineScope) {
protected val scope = CoroutineScope(Job() + Dispatchers.Main)
var imagePropertyChangedListener: (() -> Unit)? = null var imagePropertyChangedListener: (() -> Unit)? = null
var reloadChapterListener: ((Boolean) -> Unit)? = null var reloadChapterListener: ((Boolean) -> Unit)? = null

View File

@ -8,6 +8,7 @@ import eu.kanade.tachiyomi.ui.reader.viewer.navigation.EdgeNavigation
import eu.kanade.tachiyomi.ui.reader.viewer.navigation.KindlishNavigation import eu.kanade.tachiyomi.ui.reader.viewer.navigation.KindlishNavigation
import eu.kanade.tachiyomi.ui.reader.viewer.navigation.LNavigation import eu.kanade.tachiyomi.ui.reader.viewer.navigation.LNavigation
import eu.kanade.tachiyomi.ui.reader.viewer.navigation.RightAndLeftNavigation import eu.kanade.tachiyomi.ui.reader.viewer.navigation.RightAndLeftNavigation
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
@ -17,8 +18,12 @@ import uy.kohesive.injekt.api.get
/** /**
* Configuration used by pager viewers. * Configuration used by pager viewers.
*/ */
class PagerConfig(private val viewer: PagerViewer, preferences: PreferencesHelper = Injekt.get()) : class PagerConfig(
ViewerConfig(preferences) { scope: CoroutineScope,
private val viewer: PagerViewer,
preferences: PreferencesHelper = Injekt.get()
) :
ViewerConfig(preferences, scope) {
var usePageTransitions = false var usePageTransitions = false
private set private set

View File

@ -16,6 +16,8 @@ import eu.kanade.tachiyomi.ui.reader.viewer.BaseViewer
import eu.kanade.tachiyomi.ui.reader.viewer.ViewerNavigation import eu.kanade.tachiyomi.ui.reader.viewer.ViewerNavigation
import eu.kanade.tachiyomi.util.view.gone import eu.kanade.tachiyomi.util.view.gone
import eu.kanade.tachiyomi.util.view.visible import eu.kanade.tachiyomi.util.view.visible
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import timber.log.Timber import timber.log.Timber
/** /**
@ -24,6 +26,8 @@ import timber.log.Timber
@Suppress("LeakingThis") @Suppress("LeakingThis")
abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer { abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
private val scope = MainScope()
/** /**
* View pager used by this viewer. It's abstract to implement L2R, R2L and vertical pagers on * View pager used by this viewer. It's abstract to implement L2R, R2L and vertical pagers on
* top of this class. * top of this class.
@ -33,7 +37,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
/** /**
* Configuration used by the pager, like allow taps, scale mode on images, page transitions... * Configuration used by the pager, like allow taps, scale mode on images, page transitions...
*/ */
val config = PagerConfig(this) val config = PagerConfig(scope, this)
/** /**
* Adapter of the pager. * Adapter of the pager.
@ -139,6 +143,11 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer {
return pager return pager
} }
override fun destroy() {
super.destroy()
scope.cancel()
}
/** /**
* Called when a new page (either a [ReaderPage] or [ChapterTransition]) is marked as active * Called when a new page (either a [ReaderPage] or [ChapterTransition]) is marked as active
*/ */

View File

@ -83,8 +83,7 @@ class WebtoonAdapter(val viewer: WebtoonViewer) : RecyclerView.Adapter<RecyclerV
* Returns the view type for the item at the given [position]. * Returns the view type for the item at the given [position].
*/ */
override fun getItemViewType(position: Int): Int { override fun getItemViewType(position: Int): Int {
val item = items[position] return when (val item = items[position]) {
return when (item) {
is ReaderPage -> PAGE_VIEW is ReaderPage -> PAGE_VIEW
is ChapterTransition -> TRANSITION_VIEW is ChapterTransition -> TRANSITION_VIEW
else -> error("Unknown view type for ${item.javaClass}") else -> error("Unknown view type for ${item.javaClass}")

View File

@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.ui.reader.viewer.navigation.EdgeNavigation
import eu.kanade.tachiyomi.ui.reader.viewer.navigation.KindlishNavigation import eu.kanade.tachiyomi.ui.reader.viewer.navigation.KindlishNavigation
import eu.kanade.tachiyomi.ui.reader.viewer.navigation.LNavigation import eu.kanade.tachiyomi.ui.reader.viewer.navigation.LNavigation
import eu.kanade.tachiyomi.ui.reader.viewer.navigation.RightAndLeftNavigation import eu.kanade.tachiyomi.ui.reader.viewer.navigation.RightAndLeftNavigation
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
@ -16,12 +17,15 @@ import uy.kohesive.injekt.api.get
/** /**
* Configuration used by webtoon viewers. * Configuration used by webtoon viewers.
*/ */
class WebtoonConfig(preferences: PreferencesHelper = Injekt.get()) : ViewerConfig(preferences) { class WebtoonConfig(
scope: CoroutineScope,
preferences: PreferencesHelper = Injekt.get()
) : ViewerConfig(preferences, scope) {
var webtoonCropBorders = false var webtoonCropBorders = false
private set private set
var verticalCropBorders = false var verticalCropBorders = true
private set private set
var sidePadding = 0 var sidePadding = 0

View File

@ -15,6 +15,7 @@ import android.widget.TextView
import androidx.appcompat.widget.AppCompatButton import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatImageView import androidx.appcompat.widget.AppCompatImageView
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.core.view.isVisible
import coil.api.clear import coil.api.clear
import coil.api.loadAny import coil.api.loadAny
import coil.request.CachePolicy import coil.request.CachePolicy
@ -109,7 +110,7 @@ class WebtoonPageHolder(
private var readImageHeaderSubscription: Subscription? = null private var readImageHeaderSubscription: Subscription? = null
init { init {
frame.layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT) refreshLayoutParams()
frame.setBackgroundColor(Color.BLACK) frame.setBackgroundColor(Color.BLACK)
} }
@ -143,9 +144,9 @@ class WebtoonPageHolder(
removeDecodeErrorLayout() removeDecodeErrorLayout()
subsamplingImageView?.recycle() subsamplingImageView?.recycle()
subsamplingImageView?.gone() subsamplingImageView?.isVisible = false
imageView?.clear() imageView?.clear()
imageView?.gone() imageView?.isVisible = false
progressBar.setProgress(0) progressBar.setProgress(0)
} }

View File

@ -7,6 +7,8 @@ import android.view.MotionEvent
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.WebtoonLayoutManager import androidx.recyclerview.widget.WebtoonLayoutManager
import eu.kanade.tachiyomi.ui.reader.ReaderActivity import eu.kanade.tachiyomi.ui.reader.ReaderActivity
@ -15,7 +17,8 @@ import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters
import eu.kanade.tachiyomi.ui.reader.viewer.BaseViewer import eu.kanade.tachiyomi.ui.reader.viewer.BaseViewer
import eu.kanade.tachiyomi.ui.reader.viewer.ViewerNavigation import eu.kanade.tachiyomi.ui.reader.viewer.ViewerNavigation
import eu.kanade.tachiyomi.util.view.visible import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import rx.subscriptions.CompositeSubscription import rx.subscriptions.CompositeSubscription
import timber.log.Timber import timber.log.Timber
import kotlin.math.max import kotlin.math.max
@ -26,6 +29,8 @@ import kotlin.math.min
*/ */
class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = false) : BaseViewer { class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = false) : BaseViewer {
private val scope = MainScope()
/** /**
* Recycler view used by this viewer. * Recycler view used by this viewer.
*/ */
@ -59,7 +64,7 @@ class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = fals
/** /**
* Configuration used by this viewer, like allow taps, or crop image borders. * Configuration used by this viewer, like allow taps, or crop image borders.
*/ */
val config = WebtoonConfig() val config = WebtoonConfig(scope)
/** /**
* Subscriptions to keep while this viewer is used. * Subscriptions to keep while this viewer is used.
@ -68,7 +73,7 @@ class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = fals
init { init {
recycler.setBackgroundColor(Color.BLACK) recycler.setBackgroundColor(Color.BLACK)
recycler.visibility = View.GONE // Don't let the recycler layout yet recycler.isVisible = false // Don't let the recycler layout yet
recycler.layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT) recycler.layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)
recycler.itemAnimator = null recycler.itemAnimator = null
recycler.layoutManager = layoutManager recycler.layoutManager = layoutManager
@ -154,7 +159,7 @@ class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = fals
// Initial opening - preload allowed // Initial opening - preload allowed
currentPage ?: return true currentPage ?: return true
val nextItem = adapter.items.getOrNull(adapter.items.count() - 1) val nextItem = adapter.items.getOrNull(adapter.items.size - 1)
val nextChapter = (nextItem as? ChapterTransition.Next)?.to ?: (nextItem as? ReaderPage)?.chapter val nextChapter = (nextItem as? ChapterTransition.Next)?.to ?: (nextItem as? ReaderPage)?.chapter
// Allow preload for // Allow preload for
@ -179,6 +184,7 @@ class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = fals
*/ */
override fun destroy() { override fun destroy() {
super.destroy() super.destroy()
scope.cancel()
subscriptions.unsubscribe() subscriptions.unsubscribe()
} }
@ -228,11 +234,11 @@ class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = fals
val forceTransition = config.alwaysShowChapterTransition || currentPage is ChapterTransition val forceTransition = config.alwaysShowChapterTransition || currentPage is ChapterTransition
adapter.setChapters(chapters, forceTransition) adapter.setChapters(chapters, forceTransition)
if (recycler.visibility == View.GONE) { if (recycler.isGone) {
Timber.d("Recycler first layout") Timber.d("Recycler first layout")
val pages = chapters.currChapter.pages ?: return val pages = chapters.currChapter.pages ?: return
moveToPage(pages[chapters.currChapter.requestedPage]) moveToPage(pages[min(chapters.currChapter.requestedPage, pages.lastIndex)])
recycler.visible() recycler.isVisible = true
} }
} }