101 lines
3.5 KiB
Kotlin
101 lines
3.5 KiB
Kotlin
package com.lagradost.cloudstream3.ui.search
|
|
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.widget.FrameLayout
|
|
import android.widget.ImageView
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import com.lagradost.cloudstream3.R
|
|
import com.lagradost.cloudstream3.SearchResponse
|
|
import com.lagradost.cloudstream3.ui.AutofitRecyclerView
|
|
import com.lagradost.cloudstream3.utils.UIHelper.IsBottomLayout
|
|
import com.lagradost.cloudstream3.utils.UIHelper.toPx
|
|
import kotlinx.android.synthetic.main.search_result_compact.view.*
|
|
import kotlin.math.roundToInt
|
|
|
|
const val SEARCH_ACTION_LOAD = 0
|
|
const val SEARCH_ACTION_SHOW_METADATA = 1
|
|
const val SEARCH_ACTION_PLAY_FILE = 2
|
|
const val SEARCH_ACTION_FOCUSED = 4
|
|
|
|
class SearchClickCallback(val action: Int, val view: View, val position : Int, val card: SearchResponse)
|
|
|
|
class SearchAdapter(
|
|
private val cardList: MutableList<SearchResponse>,
|
|
private val resView: AutofitRecyclerView,
|
|
private val clickCallback: (SearchClickCallback) -> Unit,
|
|
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
|
val layout = if(parent.context.IsBottomLayout()) R.layout.search_result_grid_expanded else R.layout.search_result_grid
|
|
return CardViewHolder(
|
|
LayoutInflater.from(parent.context).inflate(layout, parent, false),
|
|
clickCallback,
|
|
resView
|
|
)
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
|
when (holder) {
|
|
is CardViewHolder -> {
|
|
holder.bind(cardList[position], position)
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun getItemCount(): Int {
|
|
return cardList.size
|
|
}
|
|
|
|
fun updateList(newList: List<SearchResponse>) {
|
|
val diffResult = DiffUtil.calculateDiff(
|
|
SearchResponseDiffCallback(this.cardList, newList)
|
|
)
|
|
|
|
cardList.clear()
|
|
cardList.addAll(newList)
|
|
|
|
diffResult.dispatchUpdatesTo(this)
|
|
}
|
|
|
|
class CardViewHolder
|
|
constructor(
|
|
itemView: View,
|
|
private val clickCallback: (SearchClickCallback) -> Unit,
|
|
resView: AutofitRecyclerView
|
|
) :
|
|
RecyclerView.ViewHolder(itemView) {
|
|
val cardView: ImageView = itemView.imageView
|
|
|
|
private val compactView = false//itemView.context.getGridIsCompact()
|
|
private val coverHeight: Int = if (compactView) 80.toPx else (resView.itemWidth / 0.68).roundToInt()
|
|
|
|
fun bind(card: SearchResponse, position: Int) {
|
|
if (!compactView) {
|
|
cardView.apply {
|
|
layoutParams = FrameLayout.LayoutParams(
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
coverHeight
|
|
)
|
|
}
|
|
}
|
|
|
|
SearchResultBuilder.bind(clickCallback, card, position, itemView)
|
|
}
|
|
}
|
|
}
|
|
|
|
class SearchResponseDiffCallback(private val oldList: List<SearchResponse>, private val newList: List<SearchResponse>) :
|
|
DiffUtil.Callback() {
|
|
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
|
|
oldList[oldItemPosition].name == newList[newItemPosition].name
|
|
|
|
override fun getOldListSize() = oldList.size
|
|
|
|
override fun getNewListSize() = newList.size
|
|
|
|
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
|
|
oldList[oldItemPosition] == newList[newItemPosition]
|
|
} |