Skip to content

Commit

Permalink
Refactor adapters using the PosterDiffUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Mar 13, 2021
1 parent 392d4aa commit bf5d4df
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,18 @@ package com.skydoves.bindablesdemo.binding

import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.skydoves.bindables.BindingListAdapter
import com.skydoves.bindablesdemo.recycler.Poster
import com.skydoves.bindablesdemo.recycler.PosterAdapter
import com.skydoves.bindablesdemo.recycler.PosterCircleAdapter
import com.skydoves.bindablesdemo.recycler.PosterLineAdapter
import com.skydoves.whatif.whatIfNotNullAs
import com.skydoves.whatif.whatIfNotNullOrEmpty

object RecyclerViewBinding {
@JvmStatic
@BindingAdapter("adapterPosterList")
fun bindAdapterPosterList(view: RecyclerView, posters: List<Poster>?) {
posters.whatIfNotNullOrEmpty { items ->
view.adapter.whatIfNotNullAs<PosterAdapter> { adapter ->
adapter.addPosterList(items)
}
}
}

@JvmStatic
@BindingAdapter("adapterPosterLineList")
fun bindAdapterPosterLineList(view: RecyclerView, posters: List<Poster>?) {
posters.whatIfNotNullOrEmpty { items ->
view.adapter.whatIfNotNullAs<PosterLineAdapter> { adapter ->
adapter.addPosterList(items)
}
}
}

@JvmStatic
@BindingAdapter("adapterPosterCircleList")
fun bindAdapterPosterCircleList(view: RecyclerView, posters: List<Poster>?) {
posters.whatIfNotNullOrEmpty { items ->
view.adapter.whatIfNotNullAs<PosterCircleAdapter> { adapter ->
adapter.addPosterList(items)
@BindingAdapter("submitList")
fun bindAdapterPosterList(view: RecyclerView, itemList: List<Poster>?) {
itemList.whatIfNotNullOrEmpty { items ->
view.adapter.whatIfNotNullAs<BindingListAdapter<Any, *>> { adapter ->
adapter.submitList(items)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,31 @@ package com.skydoves.bindablesdemo.recycler
import android.view.ViewGroup
import androidx.databinding.Bindable
import androidx.recyclerview.widget.RecyclerView
import com.skydoves.bindables.BindingRecyclerViewAdapter
import com.skydoves.bindables.BindingListAdapter
import com.skydoves.bindables.binding
import com.skydoves.bindablesdemo.R
import com.skydoves.bindablesdemo.databinding.ItemPosterBinding

class PosterAdapter : BindingRecyclerViewAdapter<PosterAdapter.PosterViewHolder>() {

private val items = mutableListOf<Poster>()
class PosterAdapter : BindingListAdapter<Poster, PosterAdapter.PosterViewHolder>(PosterDiffUtil()) {

@get:Bindable
val isEmpty: Boolean
get() = items.isEmpty()
get() = itemCount == 0

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PosterViewHolder {
val binding = parent.binding<ItemPosterBinding>(R.layout.item_poster)
return PosterViewHolder(binding)
}

override fun onBindViewHolder(holder: PosterViewHolder, position: Int) {
holder.binding.poster = items[position]
holder.binding.poster = getItem(position)
holder.binding.executePendingBindings()
}

fun addPosterList(list: List<Poster>) {
items.clear()
items.addAll(list)
notifyDataSetChanged()
override fun submitList(list: MutableList<Poster>?) {
super.submitList(list)
notifyPropertyChanged(::isEmpty)
}

override fun getItemCount() = items.size

class PosterViewHolder(val binding: ItemPosterBinding) : RecyclerView.ViewHolder(binding.root)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,32 @@ package com.skydoves.bindablesdemo.recycler
import android.view.ViewGroup
import androidx.databinding.Bindable
import androidx.recyclerview.widget.RecyclerView
import com.skydoves.bindables.BindingRecyclerViewAdapter
import com.skydoves.bindables.BindingListAdapter
import com.skydoves.bindables.binding
import com.skydoves.bindablesdemo.R
import com.skydoves.bindablesdemo.databinding.ItemPosterCircleBinding

class PosterCircleAdapter : BindingRecyclerViewAdapter<PosterCircleAdapter.PosterViewHolder>() {

private val items = mutableListOf<Poster>()
class PosterCircleAdapter : BindingListAdapter<Poster, PosterCircleAdapter.PosterViewHolder>(PosterDiffUtil()) {

@get:Bindable
val isEmpty: Boolean
get() = items.isEmpty()
get() = itemCount == 0

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PosterViewHolder {
val binding = parent.binding<ItemPosterCircleBinding>(R.layout.item_poster_circle)
return PosterViewHolder(binding)
}

override fun onBindViewHolder(holder: PosterViewHolder, position: Int) {
holder.binding.poster = items[position]
holder.binding.poster = getItem(position)
holder.binding.executePendingBindings()
}

fun addPosterList(list: List<Poster>) {
items.clear()
items.addAll(list)
notifyDataSetChanged()
override fun submitList(list: MutableList<Poster>?) {
super.submitList(list)
notifyPropertyChanged(::isEmpty)
}

override fun getItemCount() = items.size

class PosterViewHolder(val binding: ItemPosterCircleBinding) :
RecyclerView.ViewHolder(binding.root)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,32 @@ package com.skydoves.bindablesdemo.recycler
import android.view.ViewGroup
import androidx.databinding.Bindable
import androidx.recyclerview.widget.RecyclerView
import com.skydoves.bindables.BindingRecyclerViewAdapter
import com.skydoves.bindables.BindingListAdapter
import com.skydoves.bindables.binding
import com.skydoves.bindablesdemo.R
import com.skydoves.bindablesdemo.databinding.ItemPosterLineBinding

class PosterLineAdapter : BindingRecyclerViewAdapter<PosterLineAdapter.PosterViewHolder>() {

private val items = mutableListOf<Poster>()
class PosterLineAdapter : BindingListAdapter<Poster, PosterLineAdapter.PosterViewHolder>(PosterDiffUtil()) {

@get:Bindable
val isEmpty: Boolean
get() = items.isEmpty()
get() = itemCount == 0

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PosterViewHolder {
val binding = parent.binding<ItemPosterLineBinding>(R.layout.item_poster_line)
return PosterViewHolder(binding)
}

override fun onBindViewHolder(holder: PosterViewHolder, position: Int) {
holder.binding.poster = items[position]
holder.binding.poster = getItem(position)
holder.binding.executePendingBindings()
}

fun addPosterList(list: List<Poster>) {
items.clear()
items.addAll(list)
notifyDataSetChanged()
override fun submitList(list: MutableList<Poster>?) {
super.submitList(list)
notifyPropertyChanged(::isEmpty)
}

override fun getItemCount() = items.size

class PosterViewHolder(val binding: ItemPosterLineBinding) :
RecyclerView.ViewHolder(binding.root)
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
android:clipToPadding="false"
android:padding="4dp"
app:adapter="@{adapter}"
app:adapterPosterList="@{viewModel.data}"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
app:submitList="@{viewModel.data}"
tools:listitem="@layout/item_poster" />

<androidx.appcompat.widget.AppCompatTextView
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_library.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
android:orientation="vertical"
android:padding="4dp"
app:adapter="@{adapter}"
app:adapterPosterLineList="@{viewModel.data}"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:submitList="@{viewModel.data}"
tools:listitem="@layout/item_poster_line" />

<androidx.appcompat.widget.AppCompatTextView
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_radio.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
android:clipToPadding="false"
android:padding="4dp"
app:adapter="@{adapter}"
app:adapterPosterCircleList="@{viewModel.data}"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:spanCount="2"
app:submitList="@{viewModel.data}"
tools:listitem="@layout/item_poster_circle" />

<androidx.appcompat.widget.AppCompatTextView
Expand Down

0 comments on commit bf5d4df

Please sign in to comment.