Fix reader

This commit is contained in:
Koitharu
2020-03-02 19:18:08 +02:00
parent 9c150512c1
commit b77fa95c7d
3 changed files with 20 additions and 9 deletions

View File

@@ -69,7 +69,6 @@ abstract class BaseReaderFragment(@LayoutRes contentLayoutId: Int) : BaseFragmen
} }
fun findCurrentPageIndex(chapterId: Long): Int { fun findCurrentPageIndex(chapterId: Long): Int {
val pages = this.pages
var offset = 0 var offset = 0
for ((id, count) in chaptersMap) { for ((id, count) in chaptersMap) {
if (id == chapterId) { if (id == chapterId) {
@@ -80,6 +79,17 @@ abstract class BaseReaderFragment(@LayoutRes contentLayoutId: Int) : BaseFragmen
return -1 return -1
} }
fun findChapterOffset(chapterId: Long): Int {
var offset = 0
for ((id, count) in chaptersMap) {
if (id == chapterId) {
return offset
}
offset += count
}
return -1
}
fun getPages(chapterId: Long): List<MangaPage>? { fun getPages(chapterId: Long): List<MangaPage>? {
var offset = 0 var offset = 0
for ((id, count) in chaptersMap) { for ((id, count) in chaptersMap) {
@@ -113,13 +123,12 @@ abstract class BaseReaderFragment(@LayoutRes contentLayoutId: Int) : BaseFragmen
} }
} }
protected fun notifyPageChanged(page: Int) { protected fun notifyPageChanged(page: Int) {
var i = page var i = page
val chapters = lastState?.manga?.chapters ?: return val chapters = lastState?.manga?.chapters ?: return
val chapter = chaptersMap.firstOrNull { x -> val chapter = chaptersMap.firstOrNull { x ->
i -= x.second i -= x.second
i <= 0 i < 0
} ?: return } ?: return
(activity as? ReaderListener)?.onPageChanged( (activity as? ReaderListener)?.onPageChanged(
chapter = chapters.find { x -> x.id == chapter.first } ?: return, chapter = chapters.find { x -> x.id == chapter.first } ?: return,

View File

@@ -1,11 +1,9 @@
package org.koitharu.kotatsu.ui.reader package org.koitharu.kotatsu.ui.reader
import android.content.ContentResolver import android.content.ContentResolver
import android.util.Log
import android.webkit.URLUtil import android.webkit.URLUtil
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import moxy.InjectViewState import moxy.InjectViewState
import moxy.presenterScope import moxy.presenterScope
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
@@ -27,10 +25,12 @@ import org.koitharu.kotatsu.utils.ext.mimeType
@InjectViewState @InjectViewState
class ReaderPresenter : BasePresenter<ReaderView>() { class ReaderPresenter : BasePresenter<ReaderView>() {
private var loaderJob: Job? = null
private var isInitialized = false private var isInitialized = false
fun loadChapter(manga: Manga, chapterId: Long, action: ReaderAction) { fun loadChapter(manga: Manga, chapterId: Long, action: ReaderAction) {
presenterScope.launch { loaderJob?.cancel()
loaderJob = presenterScope.launch {
viewState.onLoadingStateChanged(isLoading = true) viewState.onLoadingStateChanged(isLoading = true)
try { try {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
@@ -63,6 +63,8 @@ class ReaderPresenter : BasePresenter<ReaderView>() {
viewState.onPagesLoaded(chapterId, pages, action) viewState.onPagesLoaded(chapterId, pages, action)
} }
} }
} catch (e: CancellationException){
Log.w(null, "Loader job cancelled", e)
} catch (e: Exception) { } catch (e: Exception) {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
e.printStackTrace() e.printStackTrace()

View File

@@ -44,7 +44,7 @@ class StandardReaderFragment : BaseReaderFragment(R.layout.fragment_reader_stand
it.replaceData(pages) it.replaceData(pages)
lastState?.let { state -> lastState?.let { state ->
if (chapterId == state.chapterId) { if (chapterId == state.chapterId) {
pager.setCurrentItem(state.page, false) pager.setCurrentItem(findChapterOffset(chapterId) + state.page, false)
} }
} }
} }