diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ViewerConfig.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ViewerConfig.kt index 41c4e5cdfd..d049bdb648 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ViewerConfig.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/ViewerConfig.kt @@ -3,17 +3,13 @@ package eu.kanade.tachiyomi.ui.reader.viewer import com.tfcporciuncula.flow.Preference import eu.kanade.tachiyomi.data.preference.PreferencesHelper import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach /** * Common configuration for all viewers. */ -abstract class ViewerConfig(preferences: PreferencesHelper) { - - protected val scope = CoroutineScope(Job() + Dispatchers.Main) +abstract class ViewerConfig(preferences: PreferencesHelper, protected val scope: CoroutineScope) { var imagePropertyChangedListener: (() -> Unit)? = null var reloadChapterListener: ((Boolean) -> Unit)? = null diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt index c03487ab00..bd0973e08a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerConfig.kt @@ -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.LNavigation import eu.kanade.tachiyomi.ui.reader.viewer.navigation.RightAndLeftNavigation +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach @@ -17,8 +18,12 @@ import uy.kohesive.injekt.api.get /** * Configuration used by pager viewers. */ -class PagerConfig(private val viewer: PagerViewer, preferences: PreferencesHelper = Injekt.get()) : - ViewerConfig(preferences) { +class PagerConfig( + scope: CoroutineScope, + private val viewer: PagerViewer, + preferences: PreferencesHelper = Injekt.get() +) : + ViewerConfig(preferences, scope) { var usePageTransitions = false private set diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt index 6e61952fa7..b6d718df65 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt @@ -16,6 +16,8 @@ import eu.kanade.tachiyomi.ui.reader.viewer.BaseViewer import eu.kanade.tachiyomi.ui.reader.viewer.ViewerNavigation import eu.kanade.tachiyomi.util.view.gone import eu.kanade.tachiyomi.util.view.visible +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.cancel import timber.log.Timber /** @@ -24,6 +26,8 @@ import timber.log.Timber @Suppress("LeakingThis") 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 * 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... */ - val config = PagerConfig(this) + val config = PagerConfig(scope, this) /** * Adapter of the pager. @@ -139,6 +143,11 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer { return pager } + override fun destroy() { + super.destroy() + scope.cancel() + } + /** * Called when a new page (either a [ReaderPage] or [ChapterTransition]) is marked as active */ diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt index f6d28ff49f..a312f7b4c5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt @@ -83,8 +83,7 @@ class WebtoonAdapter(val viewer: WebtoonViewer) : RecyclerView.Adapter PAGE_VIEW is ChapterTransition -> TRANSITION_VIEW else -> error("Unknown view type for ${item.javaClass}") diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonConfig.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonConfig.kt index d1d8594f03..f9c8168a5c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonConfig.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonConfig.kt @@ -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.LNavigation import eu.kanade.tachiyomi.ui.reader.viewer.navigation.RightAndLeftNavigation +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach @@ -16,12 +17,15 @@ import uy.kohesive.injekt.api.get /** * 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 private set - var verticalCropBorders = false + var verticalCropBorders = true private set var sidePadding = 0 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt index 14243228bf..fbf6518455 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt @@ -15,6 +15,7 @@ import android.widget.TextView import androidx.appcompat.widget.AppCompatButton import androidx.appcompat.widget.AppCompatImageView import androidx.core.net.toUri +import androidx.core.view.isVisible import coil.api.clear import coil.api.loadAny import coil.request.CachePolicy @@ -109,7 +110,7 @@ class WebtoonPageHolder( private var readImageHeaderSubscription: Subscription? = null init { - frame.layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT) + refreshLayoutParams() frame.setBackgroundColor(Color.BLACK) } @@ -143,9 +144,9 @@ class WebtoonPageHolder( removeDecodeErrorLayout() subsamplingImageView?.recycle() - subsamplingImageView?.gone() + subsamplingImageView?.isVisible = false imageView?.clear() - imageView?.gone() + imageView?.isVisible = false progressBar.setProgress(0) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt index 2bd6b20ae6..df12abbb69 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt @@ -7,6 +7,8 @@ import android.view.MotionEvent import android.view.View import android.view.ViewGroup 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.WebtoonLayoutManager 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.viewer.BaseViewer 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 timber.log.Timber import kotlin.math.max @@ -26,6 +29,8 @@ import kotlin.math.min */ class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = false) : BaseViewer { + private val scope = MainScope() + /** * 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. */ - val config = WebtoonConfig() + val config = WebtoonConfig(scope) /** * Subscriptions to keep while this viewer is used. @@ -68,7 +73,7 @@ class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = fals init { 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.itemAnimator = null recycler.layoutManager = layoutManager @@ -154,7 +159,7 @@ class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = fals // Initial opening - preload allowed 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 // Allow preload for @@ -179,6 +184,7 @@ class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = fals */ override fun destroy() { super.destroy() + scope.cancel() subscriptions.unsubscribe() } @@ -228,11 +234,11 @@ class WebtoonViewer(val activity: ReaderActivity, val hasMargins: Boolean = fals val forceTransition = config.alwaysShowChapterTransition || currentPage is ChapterTransition adapter.setChapters(chapters, forceTransition) - if (recycler.visibility == View.GONE) { + if (recycler.isGone) { Timber.d("Recycler first layout") val pages = chapters.currChapter.pages ?: return - moveToPage(pages[chapters.currChapter.requestedPage]) - recycler.visible() + moveToPage(pages[min(chapters.currChapter.requestedPage, pages.lastIndex)]) + recycler.isVisible = true } }