1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-11-17 23:34:54 +00:00

Use BindableItem in HeaderWithMenuItem.

This commit is contained in:
Isira Seneviratne 2020-11-28 14:01:30 +05:30
parent 9e5f079cf2
commit a188125982

View File

@ -1,13 +1,12 @@
package org.schabi.newpipe.local.subscription.item package org.schabi.newpipe.local.subscription.item
import android.view.View
import android.view.View.OnClickListener import android.view.View.OnClickListener
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.core.view.isVisible import androidx.core.view.isVisible
import com.xwray.groupie.kotlinandroidextensions.GroupieViewHolder import com.xwray.groupie.viewbinding.BindableItem
import com.xwray.groupie.kotlinandroidextensions.Item
import kotlinx.android.synthetic.main.header_with_menu_item.header_menu_item
import kotlinx.android.synthetic.main.header_with_menu_item.header_title
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.databinding.HeaderWithMenuItemBinding
class HeaderWithMenuItem( class HeaderWithMenuItem(
val title: String, val title: String,
@ -15,37 +14,39 @@ class HeaderWithMenuItem(
var showMenuItem: Boolean = true, var showMenuItem: Boolean = true,
private val onClickListener: (() -> Unit)? = null, private val onClickListener: (() -> Unit)? = null,
private val menuItemOnClickListener: (() -> Unit)? = null private val menuItemOnClickListener: (() -> Unit)? = null
) : Item() { ) : BindableItem<HeaderWithMenuItemBinding>() {
companion object { companion object {
const val PAYLOAD_UPDATE_VISIBILITY_MENU_ITEM = 1 const val PAYLOAD_UPDATE_VISIBILITY_MENU_ITEM = 1
} }
override fun getLayout(): Int = R.layout.header_with_menu_item override fun getLayout(): Int = R.layout.header_with_menu_item
override fun bind(viewHolder: GroupieViewHolder, position: Int, payloads: MutableList<Any>) { override fun bind(viewBinding: HeaderWithMenuItemBinding, position: Int, payloads: MutableList<Any>) {
if (payloads.contains(PAYLOAD_UPDATE_VISIBILITY_MENU_ITEM)) { if (payloads.contains(PAYLOAD_UPDATE_VISIBILITY_MENU_ITEM)) {
updateMenuItemVisibility(viewHolder) updateMenuItemVisibility(viewBinding)
return return
} }
super.bind(viewHolder, position, payloads) super.bind(viewBinding, position, payloads)
} }
override fun bind(viewHolder: GroupieViewHolder, position: Int) { override fun bind(viewBinding: HeaderWithMenuItemBinding, position: Int) {
viewHolder.header_title.text = title viewBinding.headerTitle.text = title
viewHolder.header_menu_item.setImageResource(itemIcon) viewBinding.headerMenuItem.setImageResource(itemIcon)
val listener: OnClickListener? = val listener: OnClickListener? =
onClickListener?.let { OnClickListener { onClickListener.invoke() } } onClickListener?.let { OnClickListener { onClickListener.invoke() } }
viewHolder.root.setOnClickListener(listener) viewBinding.root.setOnClickListener(listener)
val menuItemListener: OnClickListener? = val menuItemListener: OnClickListener? =
menuItemOnClickListener?.let { OnClickListener { menuItemOnClickListener.invoke() } } menuItemOnClickListener?.let { OnClickListener { menuItemOnClickListener.invoke() } }
viewHolder.header_menu_item.setOnClickListener(menuItemListener) viewBinding.headerMenuItem.setOnClickListener(menuItemListener)
updateMenuItemVisibility(viewHolder) updateMenuItemVisibility(viewBinding)
} }
private fun updateMenuItemVisibility(viewHolder: GroupieViewHolder) { override fun initializeViewBinding(view: View) = HeaderWithMenuItemBinding.bind(view)
viewHolder.header_menu_item.isVisible = showMenuItem
private fun updateMenuItemVisibility(viewBinding: HeaderWithMenuItemBinding) {
viewBinding.headerMenuItem.isVisible = showMenuItem
} }
} }