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) { 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 { companion object {

View File

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

View File

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

View File

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

View File

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