2019-04-28 22:43:54 +02:00
|
|
|
package org.schabi.newpipe.local.subscription.item
|
|
|
|
|
|
|
|
import android.os.Parcelable
|
|
|
|
import android.view.View
|
2022-10-25 15:06:48 +02:00
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
2019-04-28 22:43:54 +02:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
import com.xwray.groupie.GroupAdapter
|
2020-11-28 09:02:39 +01:00
|
|
|
import com.xwray.groupie.viewbinding.BindableItem
|
|
|
|
import com.xwray.groupie.viewbinding.GroupieViewHolder
|
2019-04-28 22:43:54 +02:00
|
|
|
import org.schabi.newpipe.R
|
2020-11-28 09:02:39 +01:00
|
|
|
import org.schabi.newpipe.databinding.FeedItemCarouselBinding
|
2022-10-26 23:20:32 +02:00
|
|
|
import org.schabi.newpipe.util.DeviceUtils
|
2022-11-05 20:05:59 +01:00
|
|
|
import org.schabi.newpipe.util.ThemeHelper.getGridSpanCount
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2020-11-28 09:02:39 +01:00
|
|
|
class FeedGroupCarouselItem(
|
2022-10-24 14:01:02 +02:00
|
|
|
private val carouselAdapter: GroupAdapter<GroupieViewHolder<FeedItemCarouselBinding>>,
|
2022-10-26 23:20:32 +02:00
|
|
|
var listViewMode: Boolean
|
2020-11-28 09:02:39 +01:00
|
|
|
) : BindableItem<FeedItemCarouselBinding>() {
|
2022-10-26 23:20:32 +02:00
|
|
|
companion object {
|
|
|
|
const val PAYLOAD_UPDATE_LIST_VIEW_MODE = 2
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2022-10-26 23:20:32 +02:00
|
|
|
private var carouselLayoutManager: LinearLayoutManager? = null
|
2019-04-28 22:43:54 +02:00
|
|
|
private var listState: Parcelable? = null
|
|
|
|
|
|
|
|
override fun getLayout() = R.layout.feed_item_carousel
|
|
|
|
|
|
|
|
fun onSaveInstanceState(): Parcelable? {
|
2022-10-26 23:20:32 +02:00
|
|
|
listState = carouselLayoutManager?.onSaveInstanceState()
|
2019-04-28 22:43:54 +02:00
|
|
|
return listState
|
|
|
|
}
|
|
|
|
|
|
|
|
fun onRestoreInstanceState(state: Parcelable?) {
|
2022-10-26 23:20:32 +02:00
|
|
|
carouselLayoutManager?.onRestoreInstanceState(state)
|
2019-04-28 22:43:54 +02:00
|
|
|
listState = state
|
|
|
|
}
|
|
|
|
|
2020-11-28 09:02:39 +01:00
|
|
|
override fun initializeViewBinding(view: View): FeedItemCarouselBinding {
|
2022-10-26 23:20:32 +02:00
|
|
|
val viewBinding = FeedItemCarouselBinding.bind(view)
|
|
|
|
updateViewMode(viewBinding)
|
|
|
|
return viewBinding
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2022-10-26 23:20:32 +02:00
|
|
|
override fun bind(
|
|
|
|
viewBinding: FeedItemCarouselBinding,
|
|
|
|
position: Int,
|
|
|
|
payloads: MutableList<Any>
|
|
|
|
) {
|
|
|
|
if (payloads.contains(PAYLOAD_UPDATE_LIST_VIEW_MODE)) {
|
|
|
|
updateViewMode(viewBinding)
|
|
|
|
return
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
2022-10-26 23:20:32 +02:00
|
|
|
|
|
|
|
super.bind(viewBinding, position, payloads)
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 09:02:39 +01:00
|
|
|
override fun bind(viewBinding: FeedItemCarouselBinding, position: Int) {
|
|
|
|
viewBinding.recyclerView.apply { adapter = carouselAdapter }
|
2022-10-26 23:20:32 +02:00
|
|
|
carouselLayoutManager?.onRestoreInstanceState(listState)
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 09:02:39 +01:00
|
|
|
override fun unbind(viewHolder: GroupieViewHolder<FeedItemCarouselBinding>) {
|
2019-04-28 22:43:54 +02:00
|
|
|
super.unbind(viewHolder)
|
2022-10-26 23:20:32 +02:00
|
|
|
listState = carouselLayoutManager?.onSaveInstanceState()
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2022-10-26 23:20:32 +02:00
|
|
|
private fun updateViewMode(viewBinding: FeedItemCarouselBinding) {
|
|
|
|
viewBinding.recyclerView.apply { adapter = carouselAdapter }
|
|
|
|
|
|
|
|
val context = viewBinding.root.context
|
|
|
|
carouselLayoutManager = if (listViewMode) {
|
|
|
|
LinearLayoutManager(context)
|
|
|
|
} else {
|
2022-11-05 20:05:59 +01:00
|
|
|
GridLayoutManager(context, getGridSpanCount(context, DeviceUtils.dpToPx(112, context)))
|
2022-10-26 23:20:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
viewBinding.recyclerView.apply {
|
|
|
|
layoutManager = carouselLayoutManager
|
|
|
|
adapter = carouselAdapter
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
2020-05-01 20:13:01 +02:00
|
|
|
}
|