Applying changed Download routines
This commit is contained in:
@@ -18,30 +18,47 @@
|
||||
|
||||
package xyz.quaver.pupil.adapters
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.bumptech.glide.RequestManager
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import kotlinx.android.synthetic.main.item_reader.view.*
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import xyz.quaver.hitomi.Reader
|
||||
import xyz.quaver.pupil.BuildConfig
|
||||
import xyz.quaver.pupil.R
|
||||
import xyz.quaver.pupil.util.GalleryDownloader
|
||||
import xyz.quaver.pupil.util.getCachedGallery
|
||||
import java.io.File
|
||||
import xyz.quaver.pupil.util.download.Cache
|
||||
import xyz.quaver.pupil.util.download.DownloadWorker
|
||||
import java.util.*
|
||||
import kotlin.concurrent.schedule
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
class ReaderAdapter(private val glide: RequestManager,
|
||||
private val galleryID: Int,
|
||||
private val images: List<String>) : RecyclerView.Adapter<ReaderAdapter.ViewHolder>() {
|
||||
class ReaderAdapter(private val context: Context,
|
||||
private val galleryID: Int) : RecyclerView.Adapter<ReaderAdapter.ViewHolder>() {
|
||||
|
||||
var isFullScreen = false
|
||||
|
||||
var reader: Reader? = null
|
||||
private val glide = Glide.with(context)
|
||||
|
||||
var onItemClickListener : ((Int) -> (Unit))? = null
|
||||
|
||||
init {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
reader = Cache(context).getReader(galleryID)
|
||||
launch(Dispatchers.Main) {
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ViewHolder(val view: View) : RecyclerView.ViewHolder(view)
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
@@ -60,38 +77,62 @@ class ReaderAdapter(private val glide: RequestManager,
|
||||
else
|
||||
holder.view.layoutParams.height = RecyclerView.LayoutParams.WRAP_CONTENT
|
||||
|
||||
var reader: Reader? = null
|
||||
with (GalleryDownloader[galleryID]?.reader) {
|
||||
if (reader == null && this?.isCompleted == true)
|
||||
runBlocking {
|
||||
reader = await()
|
||||
}
|
||||
}
|
||||
|
||||
holder.view.image.setOnPhotoTapListener { _, _, _ ->
|
||||
onItemClickListener?.invoke(position)
|
||||
}
|
||||
|
||||
glide
|
||||
.load(File(getCachedGallery(holder.view.context, galleryID), images[position]))
|
||||
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
||||
.skipMemoryCache(true)
|
||||
.error(R.drawable.image_broken_variant)
|
||||
.apply {
|
||||
if (BuildConfig.CENSOR)
|
||||
override(5, 8)
|
||||
else {
|
||||
val galleryInfo = reader?.galleryInfo?.get(position)
|
||||
(holder.view.container.layoutParams as ConstraintLayout.LayoutParams)
|
||||
.dimensionRatio = "${reader!!.galleryInfo[position].width}:${reader!!.galleryInfo[position].height}"
|
||||
|
||||
if (galleryInfo != null) {
|
||||
(holder.view.image.layoutParams as ConstraintLayout.LayoutParams)
|
||||
.dimensionRatio = "${galleryInfo.width}:${galleryInfo.height}"
|
||||
}
|
||||
holder.view.reader_item_progressbar.progress = DownloadWorker.getInstance(context).progress[galleryID]?.get(position)?.roundToInt() ?: 0
|
||||
holder.view.reader_index.text = (position+1).toString()
|
||||
|
||||
val progress = DownloadWorker.getInstance(context).progress[galleryID]?.get(position)
|
||||
if (progress?.isFinite() == false) {
|
||||
when {
|
||||
progress.isInfinite() -> {
|
||||
var image = Cache(context).getImages(galleryID)
|
||||
|
||||
while (image?.get(position) == null)
|
||||
image = Cache(context).getImages(galleryID)
|
||||
|
||||
glide
|
||||
.load(image[position])
|
||||
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
||||
.skipMemoryCache(true)
|
||||
.error(R.drawable.image_broken_variant)
|
||||
.apply {
|
||||
if (BuildConfig.CENSOR)
|
||||
override(5, 8)
|
||||
}
|
||||
.into(holder.view.image)
|
||||
}
|
||||
progress.isNaN() -> {
|
||||
glide
|
||||
.load(R.drawable.image_broken_variant)
|
||||
.into(holder.view.image)
|
||||
Snackbar
|
||||
.make(
|
||||
holder.view,
|
||||
DownloadWorker.getInstance(context).exception[galleryID]!![position]?.message
|
||||
?: context.getText(R.string.default_error_msg),
|
||||
Snackbar.LENGTH_INDEFINITE
|
||||
)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
.into(holder.view.image)
|
||||
|
||||
} else {
|
||||
holder.view.image.setImageDrawable(null)
|
||||
|
||||
Timer().schedule(1000) {
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemCount() = images.size
|
||||
override fun getItemCount() = reader?.galleryInfo?.size ?: 0
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user