Several reader fixes

This commit is contained in:
inorichi 2018-09-08 12:44:35 +02:00
parent 18f89cc341
commit 116f7d1c4a
9 changed files with 52 additions and 74 deletions

View File

@ -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<ReaderPresenter>() {
}
/**
* 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)
}
/**

View File

@ -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<ReaderActivity>() {
/**
@ -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)
}
/**

View File

@ -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()

View File

@ -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

View File

@ -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)
}
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}
}

View File

@ -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)
}
}

View File

@ -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? {