AquaStream/app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeChildItemAdapter.kt

202 lines
6.7 KiB
Kotlin
Raw Normal View History

2021-07-29 00:19:42 +00:00
package com.lagradost.cloudstream3.ui.home
import android.view.LayoutInflater
import android.view.ViewGroup
2022-02-13 00:53:40 +00:00
import androidx.recyclerview.widget.DiffUtil
2021-07-29 00:19:42 +00:00
import androidx.recyclerview.widget.RecyclerView
2023-07-15 18:38:06 +00:00
import androidx.viewbinding.ViewBinding
2021-08-29 18:42:44 +00:00
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.SearchResponse
import com.lagradost.cloudstream3.databinding.HomeResultGridBinding
2023-07-15 18:38:06 +00:00
import com.lagradost.cloudstream3.databinding.HomeResultGridExpandedBinding
2021-07-30 23:41:54 +00:00
import com.lagradost.cloudstream3.ui.search.SearchClickCallback
2021-08-25 15:28:25 +00:00
import com.lagradost.cloudstream3.ui.search.SearchResultBuilder
2023-07-28 02:18:28 +00:00
import com.lagradost.cloudstream3.utils.AppUtils.isRtl
2022-04-25 17:34:14 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.IsBottomLayout
import com.lagradost.cloudstream3.utils.UIHelper.toPx
2021-07-29 00:19:42 +00:00
class HomeChildItemAdapter(
2022-02-13 00:53:40 +00:00
val cardList: MutableList<SearchResponse>,
2021-11-28 12:18:01 +00:00
private val nextFocusUp: Int? = null,
private val nextFocusDown: Int? = null,
private val clickCallback: (SearchClickCallback) -> Unit,
2021-07-29 00:19:42 +00:00
) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var isHorizontal: Boolean = false
2022-08-02 16:26:16 +00:00
var hasNext: Boolean = false
2021-07-29 00:19:42 +00:00
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
2023-07-15 18:38:06 +00:00
val expanded = parent.context.IsBottomLayout()
2023-07-28 02:18:28 +00:00
/* val layout = if (bottom) R.layout.home_result_grid_expanded else R.layout.home_result_grid
2023-07-28 02:18:28 +00:00
val root = LayoutInflater.from(parent.context).inflate(layout, parent, false)
val binding = HomeResultGridBinding.bind(root)*/
2023-07-15 18:38:06 +00:00
val inflater = LayoutInflater.from(parent.context)
2023-07-28 02:18:28 +00:00
val binding = if (expanded) HomeResultGridExpandedBinding.inflate(
inflater,
parent,
false
) else HomeResultGridBinding.inflate(inflater, parent, false)
2023-07-15 18:38:06 +00:00
2022-04-25 17:34:14 +00:00
2021-07-29 00:19:42 +00:00
return CardViewHolder(
binding,
2021-11-28 12:18:01 +00:00
clickCallback,
itemCount,
nextFocusUp,
nextFocusDown,
2023-07-28 02:18:28 +00:00
isHorizontal,
parent.isRtl()
2021-07-29 00:19:42 +00:00
)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is CardViewHolder -> {
2022-08-02 16:26:16 +00:00
holder.itemCount = itemCount // i know ugly af
2021-10-22 13:23:48 +00:00
holder.bind(cardList[position], position)
2021-07-29 00:19:42 +00:00
}
}
}
override fun getItemCount(): Int {
return cardList.size
}
2021-10-22 13:23:48 +00:00
override fun getItemId(position: Int): Long {
return (cardList[position].id ?: position).toLong()
}
2022-02-13 00:53:40 +00:00
fun updateList(newList: List<SearchResponse>) {
val diffResult = DiffUtil.calculateDiff(
2022-08-02 16:26:16 +00:00
HomeChildDiffCallback(this.cardList, newList)
2022-02-13 00:53:40 +00:00
)
cardList.clear()
cardList.addAll(newList)
diffResult.dispatchUpdatesTo(this)
}
2021-07-29 00:19:42 +00:00
class CardViewHolder
2021-11-28 12:18:01 +00:00
constructor(
2023-07-15 18:38:06 +00:00
val binding: ViewBinding,
2022-03-29 21:07:41 +00:00
private val clickCallback: (SearchClickCallback) -> Unit,
2022-08-02 16:26:16 +00:00
var itemCount: Int,
2021-11-28 12:18:01 +00:00
private val nextFocusUp: Int? = null,
private val nextFocusDown: Int? = null,
2023-07-28 02:18:28 +00:00
private val isHorizontal: Boolean = false,
private val isRtl: Boolean
2021-11-28 12:18:01 +00:00
) :
RecyclerView.ViewHolder(binding.root) {
2021-07-29 00:19:42 +00:00
2021-12-11 21:23:36 +00:00
fun bind(card: SearchResponse, position: Int) {
2021-11-25 17:26:14 +00:00
// TV focus fixing
/*val nextFocusBehavior = when (position) {
2021-11-25 17:26:14 +00:00
0 -> true
itemCount - 1 -> false
else -> null
}
2023-07-28 02:18:28 +00:00
if (position == 0) { // to fix tv
if (isRtl) {
itemView.nextFocusRightId = R.id.nav_rail_view
itemView.nextFocusLeftId = -1
}
else {
itemView.nextFocusLeftId = R.id.nav_rail_view
itemView.nextFocusRightId = -1
}
} else {
itemView.nextFocusRightId = -1
itemView.nextFocusLeftId = -1
}*/
2023-07-28 02:18:28 +00:00
when (binding) {
2023-07-15 18:38:06 +00:00
is HomeResultGridBinding -> {
binding.backgroundCard.apply {
val min = 114.toPx
val max = 180.toPx
layoutParams =
layoutParams.apply {
width = if (!isHorizontal) {
min
} else {
max
}
height = if (!isHorizontal) {
max
} else {
min
}
}
}
2021-11-25 17:26:14 +00:00
2023-07-28 02:18:28 +00:00
2023-07-15 18:38:06 +00:00
}
2023-07-28 02:18:28 +00:00
2023-07-15 18:38:06 +00:00
is HomeResultGridExpandedBinding -> {
binding.backgroundCard.apply {
val min = 114.toPx
val max = 180.toPx
layoutParams =
layoutParams.apply {
width = if (!isHorizontal) {
min
} else {
max
}
height = if (!isHorizontal) {
max
} else {
min
}
}
}
2023-07-15 18:38:06 +00:00
if (position == 0) { // to fix tv
binding.backgroundCard.nextFocusLeftId = R.id.nav_rail_view
}
}
}
2022-03-29 21:07:41 +00:00
SearchResultBuilder.bind(
clickCallback,
card,
position,
itemView,
null, // nextFocusBehavior,
2022-03-29 21:07:41 +00:00
nextFocusUp,
nextFocusDown
)
2021-12-11 21:23:36 +00:00
itemView.tag = position
2022-03-29 21:07:41 +00:00
2021-10-22 13:23:48 +00:00
//val ani = ScaleAnimation(0.9f, 1.0f, 0.9f, 1f)
//ani.fillAfter = true
//ani.duration = 200
//itemView.startAnimation(ani)
2021-07-29 00:19:42 +00:00
}
}
}
2022-08-02 16:26:16 +00:00
class HomeChildDiffCallback(
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] && oldItemPosition < oldList.size - 1 // always update the last item
}