Minor fix
This commit is contained in:
@@ -21,18 +21,19 @@ package xyz.quaver.pupil.adapters
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.graphics.drawable.Animatable
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.daimajia.swipe.SwipeLayout
|
||||
import com.daimajia.swipe.adapters.RecyclerSwipeAdapter
|
||||
import com.daimajia.swipe.interfaces.SwipeAdapterInterface
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import com.facebook.drawee.backends.pipeline.Fresco
|
||||
import com.facebook.drawee.controller.BaseControllerListener
|
||||
import com.facebook.imagepipeline.image.ImageInfo
|
||||
import kotlinx.coroutines.*
|
||||
import xyz.quaver.pupil.R
|
||||
import xyz.quaver.pupil.databinding.SearchResultItemBinding
|
||||
import xyz.quaver.pupil.sources.SearchResult
|
||||
@@ -40,21 +41,25 @@ import xyz.quaver.pupil.types.Tag
|
||||
import xyz.quaver.pupil.ui.view.ProgressCardView
|
||||
import xyz.quaver.pupil.util.downloader.Cache
|
||||
import xyz.quaver.pupil.util.downloader.DownloadManager
|
||||
import xyz.quaver.pupil.util.wordCapitalize
|
||||
import kotlin.time.ExperimentalTime
|
||||
|
||||
class SearchResultsAdapter(private val results: List<SearchResult>) : RecyclerSwipeAdapter<SearchResultsAdapter.ViewHolder>(), SwipeAdapterInterface {
|
||||
|
||||
val onChipClickedHandler = ArrayList<((Tag) -> Unit)>()
|
||||
var onChipClickedHandler: ((Tag) -> Unit)? = null
|
||||
var onDownloadClickedHandler: ((String) -> Unit)? = null
|
||||
var onDeleteClickedHandler: ((String) -> Unit)? = null
|
||||
|
||||
// TODO: migrate to viewBinding
|
||||
val progressUpdateScope = CoroutineScope(Dispatchers.Main + Job())
|
||||
|
||||
inner class ViewHolder(private val binding: SearchResultItemBinding) : RecyclerView.ViewHolder(binding.root) {
|
||||
var itemID: String = ""
|
||||
var update = true
|
||||
|
||||
private var bindJob: Job? = null
|
||||
|
||||
init {
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
while (update) {
|
||||
progressUpdateScope.launch {
|
||||
while (true) {
|
||||
updateProgress()
|
||||
delay(1000)
|
||||
}
|
||||
@@ -92,9 +97,11 @@ class SearchResultsAdapter(private val results: List<SearchResult>) : RecyclerSw
|
||||
override fun onHandRelease(layout: SwipeLayout?, xvel: Float, yvel: Float) {}
|
||||
override fun onUpdate(layout: SwipeLayout?, leftOffset: Int, topOffset: Int) {}
|
||||
})
|
||||
|
||||
binding.tagGroup.onClickListener = onChipClickedHandler
|
||||
}
|
||||
|
||||
fun updateProgress() = CoroutineScope(Dispatchers.Main).launch {
|
||||
private fun updateProgress() = CoroutineScope(Dispatchers.Main).launch {
|
||||
with (itemView as ProgressCardView) {
|
||||
val imageList = Cache.getInstance(context, itemID).metadata.imageList
|
||||
|
||||
@@ -118,32 +125,111 @@ class SearchResultsAdapter(private val results: List<SearchResult>) : RecyclerSw
|
||||
}
|
||||
}
|
||||
|
||||
val controllerListener = object: BaseControllerListener<ImageInfo>() {
|
||||
override fun onIntermediateImageSet(id: String?, imageInfo: ImageInfo?) {
|
||||
imageInfo?.let {
|
||||
binding.thumbnail.aspectRatio = it.width / it.height.toFloat()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFinalImageSet(id: String?, imageInfo: ImageInfo?, animatable: Animatable?) {
|
||||
imageInfo?.let {
|
||||
binding.thumbnail.aspectRatio = it.width / it.height.toFloat()
|
||||
}
|
||||
}
|
||||
}
|
||||
fun bind(result: SearchResult) {
|
||||
bindJob?.cancel()
|
||||
itemID = result.id
|
||||
|
||||
binding.thumbnail.ssiv?.recycle()
|
||||
binding.thumbnail.showImage(Uri.parse(result.thumbnail))
|
||||
binding.thumbnail.controller = Fresco.newDraweeControllerBuilder()
|
||||
.setUri(result.thumbnail)
|
||||
.setControllerListener(controllerListener)
|
||||
.build()
|
||||
|
||||
updateProgress()
|
||||
|
||||
binding.title.text = result.title
|
||||
binding.idView.text = result.id
|
||||
binding.artist.text = result.artists.joinToString { it.wordCapitalize() }
|
||||
|
||||
binding.artist.visibility = if (result.artists.isEmpty()) View.GONE else View.VISIBLE
|
||||
binding.artist.text = result.artists
|
||||
|
||||
with (binding.tagGroup) {
|
||||
tags.clear()
|
||||
tags.addAll(result.tags.map {
|
||||
Tag.parse(it)
|
||||
})
|
||||
refresh()
|
||||
}
|
||||
|
||||
binding.pagecount.text = "-"
|
||||
|
||||
bindJob = MainScope().launch {
|
||||
val extra = result.extra.mapValues {
|
||||
async(Dispatchers.IO) {
|
||||
kotlin.runCatching { withTimeout(1000) {
|
||||
it.value.invoke()
|
||||
} }.getOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
val extraType = listOf(
|
||||
SearchResult.ExtraType.SERIES,
|
||||
SearchResult.ExtraType.TYPE,
|
||||
SearchResult.ExtraType.LANGUAGE
|
||||
)
|
||||
|
||||
binding.extra.text = extra.entries.filter { it.key in extraType }.fold(StringBuilder()) { res, entry ->
|
||||
entry.value.await().let {
|
||||
if (!it.isNullOrEmpty()) {
|
||||
res.append(
|
||||
itemView.context.getString(
|
||||
SearchResult.extraTypeMap[entry.key] ?: error(""),
|
||||
it
|
||||
)
|
||||
)
|
||||
res.append('\n')
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
extra[SearchResult.ExtraType.PAGECOUNT]?.await()?.let {
|
||||
binding.pagecount.text =
|
||||
itemView.context.getString(
|
||||
SearchResult.extraTypeMap[SearchResult.ExtraType.PAGECOUNT] ?: error(""),
|
||||
it
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
extra[SearchResult.ExtraType.GROUP]?.await().let {
|
||||
if (!it.isNullOrEmpty())
|
||||
binding.artist.text = itemView.context.getString(
|
||||
R.string.galleryblock_artist_with_group,
|
||||
result.artists,
|
||||
it
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
|
||||
ViewHolder(SearchResultItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
|
||||
|
||||
@ExperimentalTime
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
mItemManger.bindView(holder.itemView, position)
|
||||
holder.bind(results[position])
|
||||
}
|
||||
|
||||
override fun onViewDetachedFromWindow(holder: ViewHolder) {
|
||||
holder.update = false
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = results.size
|
||||
|
||||
override fun getSwipeLayoutResourceId(position: Int): Int = R.id.swipe_layout
|
||||
|
||||
Reference in New Issue
Block a user