Update page loading flow

This commit is contained in:
Koitharu
2020-02-21 20:22:01 +02:00
parent 25b828cf83
commit e84a619f1f
5 changed files with 44 additions and 24 deletions

View File

@@ -48,7 +48,16 @@ class ChaptersDialog : AlertDialogFragment(R.layout.dialog_chapters),
}
override fun onItemClick(item: MangaChapter, position: Int, view: View) {
((parentFragment as? OnChapterChangeListener)
?: (activity as? OnChapterChangeListener))?.let {
dismiss()
it.onChapterChanged(item)
}
}
interface OnChapterChangeListener {
fun onChapterChanged(chapter: MangaChapter)
}
companion object {

View File

@@ -6,6 +6,7 @@ import androidx.core.view.isVisible
import com.davemorrissey.labs.subscaleview.ImageSource
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import kotlinx.android.synthetic.main.item_page.*
import kotlinx.coroutines.*
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.MangaPage
import org.koitharu.kotatsu.ui.common.list.BaseViewHolder
@@ -13,27 +14,36 @@ import org.koitharu.kotatsu.utils.ext.getDisplayMessage
class PageHolder(parent: ViewGroup, private val loader: PageLoader) :
BaseViewHolder<MangaPage, Unit>(parent, R.layout.item_page),
SubsamplingScaleImageView.OnImageEventListener {
SubsamplingScaleImageView.OnImageEventListener, CoroutineScope by loader {
private var job: Job? = null
init {
ssiv.setOnImageEventListener(this)
button_retry.setOnClickListener {
onBind(boundData ?: return@setOnClickListener, Unit)
doLoad(boundData ?: return@setOnClickListener, force = true)
}
}
override fun onBind(data: MangaPage, extra: Unit) {
layout_error.isVisible = false
progressBar.isVisible = true
ssiv.recycle()
loader.load(data.url) {
val uri = it.getOrNull()?.toUri()
if (uri != null) {
doLoad(data, force = false)
}
private fun doLoad(data: MangaPage, force: Boolean) {
job?.cancel()
job = launch {
layout_error.isVisible = false
progressBar.isVisible = true
ssiv.recycle()
try {
val uri = withContext(Dispatchers.IO) {
loader.loadFile(data.url, force)
}.toUri()
ssiv.setImage(ImageSource.uri(uri))
}
val error = it.exceptionOrNull()
if (error != null) {
onError(error)
} catch (e: CancellationException) {
//do nothing
} catch (e: Exception) {
onError(e)
}
}
}

View File

@@ -22,16 +22,7 @@ class PageLoader : KoinComponent, CoroutineScope, DisposableHandle {
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
fun load(url: String, force: Boolean = false, callback: (Result<File>) -> Unit) = launch {
val result = runCatching {
withContext(Dispatchers.IO) {
loadFile(url, force)
}
}
callback(result)
}
private suspend fun loadFile(url: String, force: Boolean): File {
suspend fun loadFile(url: String, force: Boolean): File {
if (!force) {
cache[url]?.let {
return it

View File

@@ -14,6 +14,7 @@ import kotlinx.android.synthetic.main.activity_reader.*
import moxy.ktx.moxyPresenter
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.core.model.MangaChapter
import org.koitharu.kotatsu.core.model.MangaHistory
import org.koitharu.kotatsu.core.model.MangaPage
import org.koitharu.kotatsu.ui.common.BaseFullscreenActivity
@@ -21,9 +22,10 @@ import org.koitharu.kotatsu.utils.GridTouchHelper
import org.koitharu.kotatsu.utils.anim.Motion
import org.koitharu.kotatsu.utils.ext.*
class ReaderActivity : BaseFullscreenActivity(), ReaderView, GridTouchHelper.OnGridTouchListener {
class ReaderActivity : BaseFullscreenActivity(), ReaderView, ChaptersDialog.OnChapterChangeListener,
GridTouchHelper.OnGridTouchListener {
private val presenter by moxyPresenter { ReaderPresenter() }
private val presenter by moxyPresenter(factory = ::ReaderPresenter)
private lateinit var state: ReaderState
@@ -146,6 +148,13 @@ class ReaderActivity : BaseFullscreenActivity(), ReaderView, GridTouchHelper.OnG
return super.dispatchTouchEvent(ev)
}
override fun onChapterChanged(chapter: MangaChapter) {
presenter.loadChapter(state.copy(
chapterId = chapter.id,
page = 0
))
}
companion object {
private const val EXTRA_STATE = "state"

View File

@@ -8,6 +8,7 @@
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:keepScreenOn="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />