|
|
|
|
@ -1,27 +1,26 @@
|
|
|
|
|
package org.koitharu.kotatsu.reader.ui.thumbnails.adapter
|
|
|
|
|
|
|
|
|
|
import androidx.core.net.toUri
|
|
|
|
|
import android.graphics.drawable.Drawable
|
|
|
|
|
import coil.ImageLoader
|
|
|
|
|
import coil.request.ImageRequest
|
|
|
|
|
import coil.size.PixelSize
|
|
|
|
|
import com.google.android.material.R as materialR
|
|
|
|
|
import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding
|
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
|
import org.koitharu.kotatsu.base.ui.list.OnListItemClickListener
|
|
|
|
|
import org.koitharu.kotatsu.databinding.ItemPageThumbBinding
|
|
|
|
|
import org.koitharu.kotatsu.local.data.PagesCache
|
|
|
|
|
import org.koitharu.kotatsu.parsers.model.MangaPage
|
|
|
|
|
import org.koitharu.kotatsu.reader.domain.PageLoader
|
|
|
|
|
import org.koitharu.kotatsu.reader.ui.thumbnails.PageThumbnail
|
|
|
|
|
import org.koitharu.kotatsu.utils.ext.IgnoreErrors
|
|
|
|
|
import org.koitharu.kotatsu.utils.ext.referer
|
|
|
|
|
import org.koitharu.kotatsu.utils.ext.setTextColorAttr
|
|
|
|
|
import com.google.android.material.R as materialR
|
|
|
|
|
|
|
|
|
|
fun pageThumbnailAD(
|
|
|
|
|
coil: ImageLoader,
|
|
|
|
|
scope: CoroutineScope,
|
|
|
|
|
cache: PagesCache,
|
|
|
|
|
clickListener: OnListItemClickListener<MangaPage>
|
|
|
|
|
loader: PageLoader,
|
|
|
|
|
clickListener: OnListItemClickListener<MangaPage>,
|
|
|
|
|
) = adapterDelegateViewBinding<PageThumbnail, PageThumbnail, ItemPageThumbBinding>(
|
|
|
|
|
{ inflater, parent -> ItemPageThumbBinding.inflate(inflater, parent, false) }
|
|
|
|
|
) {
|
|
|
|
|
@ -33,6 +32,29 @@ fun pageThumbnailAD(
|
|
|
|
|
height = (gridWidth * 13f / 18f).toInt()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
suspend fun loadPageThumbnail(item: PageThumbnail): Drawable? = withContext(Dispatchers.Default) {
|
|
|
|
|
item.page.preview?.let { url ->
|
|
|
|
|
coil.execute(
|
|
|
|
|
ImageRequest.Builder(context)
|
|
|
|
|
.data(url)
|
|
|
|
|
.referer(item.page.referer)
|
|
|
|
|
.size(thumbSize)
|
|
|
|
|
.allowRgb565(true)
|
|
|
|
|
.build()
|
|
|
|
|
).drawable
|
|
|
|
|
}?.let { drawable ->
|
|
|
|
|
return@withContext drawable
|
|
|
|
|
}
|
|
|
|
|
val file = loader.loadPage(item.page, force = false)
|
|
|
|
|
coil.execute(
|
|
|
|
|
ImageRequest.Builder(context)
|
|
|
|
|
.data(file)
|
|
|
|
|
.size(thumbSize)
|
|
|
|
|
.allowRgb565(true)
|
|
|
|
|
.build()
|
|
|
|
|
).drawable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
binding.root.setOnClickListener {
|
|
|
|
|
clickListener.onItemClick(item.page, itemView)
|
|
|
|
|
}
|
|
|
|
|
@ -45,22 +67,11 @@ fun pageThumbnailAD(
|
|
|
|
|
setTextColorAttr(if (item.isCurrent) materialR.attr.colorOnTertiary else android.R.attr.textColorPrimary)
|
|
|
|
|
text = (item.number).toString()
|
|
|
|
|
}
|
|
|
|
|
job = scope.launch(Dispatchers.Default + IgnoreErrors) {
|
|
|
|
|
val url = item.page.preview ?: item.page.url.let {
|
|
|
|
|
val pageUrl = item.repository.getPageUrl(item.page)
|
|
|
|
|
cache[pageUrl]?.toUri()?.toString() ?: pageUrl
|
|
|
|
|
}
|
|
|
|
|
val drawable = coil.execute(
|
|
|
|
|
ImageRequest.Builder(context)
|
|
|
|
|
.data(url)
|
|
|
|
|
.referer(item.page.referer)
|
|
|
|
|
.size(thumbSize)
|
|
|
|
|
.allowRgb565(true)
|
|
|
|
|
.build()
|
|
|
|
|
).drawable
|
|
|
|
|
withContext(Dispatchers.Main) {
|
|
|
|
|
binding.imageViewThumb.setImageDrawable(drawable)
|
|
|
|
|
}
|
|
|
|
|
job = scope.launch {
|
|
|
|
|
val drawable = runCatching {
|
|
|
|
|
loadPageThumbnail(item)
|
|
|
|
|
}.getOrNull()
|
|
|
|
|
binding.imageViewThumb.setImageDrawable(drawable)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|