From 116f7d1c4aca994306533b2e4d05315cf6598b81 Mon Sep 17 00:00:00 2001 From: inorichi Date: Sat, 8 Sep 2018 12:44:35 +0200 Subject: [PATCH] Several reader fixes --- .../tachiyomi/ui/reader/ReaderActivity.kt | 18 ++++-------- .../tachiyomi/ui/reader/ReaderPresenter.kt | 17 +++-------- .../ui/reader/loader/HttpPageLoader.kt | 8 ++++-- .../ui/reader/model/ReaderChapter.kt | 5 ---- .../viewer/pager/PagerTransitionHolder.kt | 7 ++--- .../ui/reader/viewer/pager/PagerViewer.kt | 22 +++++++-------- .../viewer/webtoon/WebtoonTransitionHolder.kt | 7 ++--- .../ui/reader/viewer/webtoon/WebtoonViewer.kt | 28 +++++++++++-------- .../eu/kanade/tachiyomi/util/ImageUtil.kt | 14 ++++------ 9 files changed, 52 insertions(+), 74 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index e73d27687d..91d62ea22e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -22,6 +22,7 @@ import eu.kanade.tachiyomi.ui.base.activity.BaseRxActivity import eu.kanade.tachiyomi.ui.reader.ReaderPresenter.SetAsCoverResult.AddToLibraryFirst import eu.kanade.tachiyomi.ui.reader.ReaderPresenter.SetAsCoverResult.Error import eu.kanade.tachiyomi.ui.reader.ReaderPresenter.SetAsCoverResult.Success +import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter import eu.kanade.tachiyomi.ui.reader.model.ReaderPage import eu.kanade.tachiyomi.ui.reader.model.ViewerChapters import eu.kanade.tachiyomi.ui.reader.viewer.BaseViewer @@ -429,20 +430,11 @@ class ReaderActivity : BaseRxActivity() { } /** - * Called from the viewer when the next chapter should be preloaded. It should be called when - * the viewer is reaching the end of the chapter or the transition page is active. + * Called from the viewer when the given [chapter] should be preloaded. It should be called when + * the viewer is reaching the beginning or end of a chapter or the transition page is active. */ - fun requestPreloadNextChapter() { - presenter.preloadNextChapter() - } - - /** - * Called from the viewer when the previous chapter should be preloaded. It should be called - * when the viewer is going backwards and reaching the beginning of the chapter or the - * transition page is active. - */ - fun requestPreloadPreviousChapter() { - presenter.preloadPreviousChapter() + fun requestPreloadChapter(chapter: ReaderChapter) { + presenter.preloadChapter(chapter) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index eae1729ac6..50931553e9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -43,7 +43,7 @@ class ReaderPresenter( private val sourceManager: SourceManager = Injekt.get(), private val downloadManager: DownloadManager = Injekt.get(), private val coverCache: CoverCache = Injekt.get(), - val preferences: PreferencesHelper = Injekt.get() + private val preferences: PreferencesHelper = Injekt.get() ) : BasePresenter() { /** @@ -331,19 +331,10 @@ class ReaderPresenter( } /** - * Called from the activity to preload the next chapter. + * Called from the activity to preload the given [chapter]. */ - fun preloadNextChapter() { - val nextChapter = viewerChaptersRelay.value?.nextChapter ?: return - preload(nextChapter) - } - - /** - * Called from the activity to preload the previous chapter. - */ - fun preloadPreviousChapter() { - val prevChapter = viewerChaptersRelay.value?.prevChapter ?: return - preload(prevChapter) + fun preloadChapter(chapter: ReaderChapter) { + preload(chapter) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt index 3a12bed604..223e9811e3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt @@ -62,8 +62,12 @@ class HttpPageLoader( // Cache current page list progress for online chapters to allow a faster reopen val pages = chapter.pages if (pages != null) { - // TODO check compatibility with ReaderPage - Completable.fromAction { chapterCache.putPageListToCache(chapter.chapter, pages) } + Completable + .fromAction { + // Convert to pages without reader information + val pagesToSave = pages.map { Page(it.index, it.url, it.imageUrl) } + chapterCache.putPageListToCache(chapter.chapter, pagesToSave) + } .onErrorComplete() .subscribeOn(Schedulers.io()) .subscribe() 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 366ce25952..06f08b6db6 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 @@ -2,7 +2,6 @@ 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.DownloadPageLoader import eu.kanade.tachiyomi.ui.reader.loader.PageLoader import timber.log.Timber @@ -26,10 +25,6 @@ data class ReaderChapter(val chapter: Chapter) { var requestedPage: Int = 0 - val isDownloaded - get() = pageLoader is DownloadPageLoader - - var references = 0 private set 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 a763f0f659..716d99872c 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 @@ -168,10 +168,9 @@ class PagerTransitionHolder( wrapContent() setText(R.string.action_retry) setOnClickListener { - if (transition is ChapterTransition.Next) { - viewer.activity.requestPreloadNextChapter() - } else { - viewer.activity.requestPreloadPreviousChapter() + val toChapter = transition.to + if (toChapter != null) { + viewer.activity.requestPreloadChapter(toChapter) } } } 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 33ac650f8f..ae2e8a42b9 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 @@ -74,7 +74,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer { if (page != null && currentPage != page) { currentPage = page when (page) { - is ReaderPage -> onPageSelected(page) + is ReaderPage -> onPageSelected(page, position) is ChapterTransition -> onTransitionSelected(page) } } @@ -128,14 +128,17 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer { * Called from the ViewPager listener when a [page] is marked as active. It notifies the * activity of the change and requests the preload of the next chapter if this is the last page. */ - private fun onPageSelected(page: ReaderPage) { + private fun onPageSelected(page: ReaderPage, position: Int) { val pages = page.chapter.pages!! // Won't be null because it's the loaded chapter Timber.d("onPageSelected: ${page.number}/${pages.size}") activity.onPageSelected(page) if (page === pages.last()) { Timber.d("Request preload next chapter because we're at the last page") - activity.requestPreloadNextChapter() + val transition = adapter.items.getOrNull(position + 1) as? ChapterTransition.Next + if (transition?.to != null) { + activity.requestPreloadChapter(transition.to) + } } } @@ -145,15 +148,10 @@ abstract class PagerViewer(val activity: ReaderActivity) : BaseViewer { */ private fun onTransitionSelected(transition: ChapterTransition) { Timber.d("onTransitionSelected: $transition") - when (transition) { - is ChapterTransition.Prev -> { - Timber.d("Request preload previous chapter because we're on the transition") - activity.requestPreloadPreviousChapter() - } - is ChapterTransition.Next -> { - Timber.d("Request preload next chapter because we're on the transition") - activity.requestPreloadNextChapter() - } + val toChapter = transition.to + if (toChapter != null) { + Timber.d("Request preload destination chapter because we're on the transition") + activity.requestPreloadChapter(toChapter) } } 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 3906155032..6bbed60089 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 @@ -180,10 +180,9 @@ class WebtoonTransitionHolder( wrapContent() setText(R.string.action_retry) setOnClickListener { - if (transition is ChapterTransition.Next) { - viewer.activity.requestPreloadNextChapter() - } else { - viewer.activity.requestPreloadPreviousChapter() + val toChapter = transition.to + if (toChapter != null) { + viewer.activity.requestPreloadChapter(toChapter) } } } 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 2bb5f15433..ecb7d2aaf7 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 @@ -68,12 +68,12 @@ class WebtoonViewer(val activity: ReaderActivity) : BaseViewer { recycler.adapter = adapter recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) { - val index = layoutManager.findLastEndVisibleItemPosition() - val item = adapter.items.getOrNull(index) + val position = layoutManager.findLastEndVisibleItemPosition() + val item = adapter.items.getOrNull(position) if (item != null && currentPage != item) { currentPage = item when (item) { - is ReaderPage -> onPageSelected(item) + is ReaderPage -> onPageSelected(item, position) is ChapterTransition -> onTransitionSelected(item) } } @@ -81,8 +81,8 @@ class WebtoonViewer(val activity: ReaderActivity) : BaseViewer { if (dy < 0) { val firstIndex = layoutManager.findFirstVisibleItemPosition() val firstItem = adapter.items.getOrNull(firstIndex) - if (firstItem is ChapterTransition.Prev) { - activity.requestPreloadPreviousChapter() + if (firstItem is ChapterTransition.Prev && firstItem.to != null) { + activity.requestPreloadChapter(firstItem.to) } } } @@ -125,29 +125,33 @@ class WebtoonViewer(val activity: ReaderActivity) : BaseViewer { } /** - * Called from the ViewPager listener when a [page] is marked as active. It notifies the + * Called from the RecyclerView listener when a [page] is marked as active. It notifies the * activity of the change and requests the preload of the next chapter if this is the last page. */ - private fun onPageSelected(page: ReaderPage) { + private fun onPageSelected(page: ReaderPage, position: Int) { val pages = page.chapter.pages!! // Won't be null because it's the loaded chapter Timber.d("onPageSelected: ${page.number}/${pages.size}") activity.onPageSelected(page) if (page === pages.last()) { Timber.d("Request preload next chapter because we're at the last page") - activity.requestPreloadNextChapter() + val transition = adapter.items.getOrNull(position + 1) as? ChapterTransition.Next + if (transition?.to != null) { + activity.requestPreloadChapter(transition.to) + } } } /** - * Called from the ViewPager listener when a [transition] is marked as active. It request the + * Called from the RecyclerView listener when a [transition] is marked as active. It request the * preload of the destination chapter of the transition. */ private fun onTransitionSelected(transition: ChapterTransition) { Timber.d("onTransitionSelected: $transition") - if (transition is ChapterTransition.Prev) { - Timber.d("Request preload previous chapter because we're on the transition") - activity.requestPreloadPreviousChapter() + val toChapter = transition.to + if (toChapter != null) { + Timber.d("Request preload destination chapter because we're on the transition") + activity.requestPreloadChapter(toChapter) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt index dd9721957b..fa879d4b4f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ImageUtil.kt @@ -6,16 +6,12 @@ import java.net.URLConnection object ImageUtil { fun isImage(name: String, openStream: (() -> InputStream)? = null): Boolean { - try { - val guessedMime = URLConnection.guessContentTypeFromName(name) - if (guessedMime.startsWith("image/")) { - return true - } + val contentType = try { + URLConnection.guessContentTypeFromName(name) } catch (e: Exception) { - /* Ignore error */ - } - - return openStream?.let { findImageType(it) } != null + null + } ?: openStream?.let { findImageType(it)?.mime } + return contentType?.startsWith("image/") ?: false } fun findImageType(openStream: () -> InputStream): ImageType? {