-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated from recycler adapter to ListAdapter Async DiffUtil Added Signed-off-by: Yogesh Paliyal <paliyalyogesh@gmail.com>
- Loading branch information
1 parent
73db9b9
commit 7c25bc3
Showing
12 changed files
with
385 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...l_adapter/src/main/java/com/yogeshpaliyal/universal_adapter/adapter/ContentListAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.yogeshpaliyal.universal_adapter.adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.databinding.ViewDataBinding | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.recyclerview.widget.AsyncDifferConfig | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.yogeshpaliyal.universal_adapter.BR | ||
import com.yogeshpaliyal.universal_adapter.model.BaseDiffUtil | ||
|
||
|
||
/* | ||
* @author Yogesh Paliyal | ||
* techpaliyal@gmail.com | ||
* https://techpaliyal.com | ||
* created on 02-05-2021 19:57 | ||
*/ | ||
class ContentListAdapter<T>( | ||
val lifecycleOwner: LifecycleOwner?, | ||
var resource: Int, | ||
val listener: Any?, | ||
val customBinding: ((itemBinding: ViewDataBinding, item: T, bindingAdapterPosition: Int) -> Unit)? | ||
) : | ||
ListAdapter<T, ContentListAdapter<T>.ViewHolder>(AsyncDifferConfig.Builder(object : | ||
DiffUtil.ItemCallback<T>() { | ||
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean { | ||
return if (oldItem is BaseDiffUtil && newItem is BaseDiffUtil) { | ||
oldItem.getDiffId() == newItem.getDiffId() | ||
} else { | ||
oldItem.hashCode() == newItem.hashCode() | ||
} | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean { | ||
return if (oldItem is BaseDiffUtil && newItem is BaseDiffUtil) { | ||
oldItem.getDiffBody() == newItem.getDiffBody() | ||
} else { | ||
oldItem.hashCode() == newItem.hashCode() | ||
} | ||
} | ||
|
||
}).build()) { | ||
|
||
|
||
inner class ViewHolder(val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) { | ||
fun bind(model: T) { | ||
binding.lifecycleOwner = lifecycleOwner | ||
|
||
if (customBinding == null) { | ||
binding.setVariable(BR.model, model) | ||
binding.setVariable(BR.listener, listener) | ||
binding.executePendingBindings() | ||
} else { | ||
|
||
customBinding.invoke( | ||
binding, | ||
model, | ||
bindingAdapterPosition | ||
) | ||
} | ||
|
||
} | ||
} | ||
|
||
override fun submitList(list: List<T>?) { | ||
super.submitList(list?.let { ArrayList(it) }) | ||
} | ||
|
||
override fun getItemViewType(position: Int): Int { | ||
return resource | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val binding = DataBindingUtil.inflate<ViewDataBinding>( | ||
LayoutInflater.from(parent.context), | ||
resource, | ||
parent, | ||
false | ||
) | ||
return ViewHolder(binding) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
holder.bind(getItem(position)) | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
universal_adapter/src/main/java/com/yogeshpaliyal/universal_adapter/adapter/ErrorAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.yogeshpaliyal.universal_adapter.adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.databinding.ViewDataBinding | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.recyclerview.widget.AsyncDifferConfig | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.yogeshpaliyal.universal_adapter.BR | ||
|
||
|
||
/* | ||
* @author Yogesh Paliyal | ||
* techpaliyal@gmail.com | ||
* https://techpaliyal.com | ||
* created on 02-05-2021 19:57 | ||
*/ | ||
class ErrorAdapter<T>( | ||
val lifecycleOwner: LifecycleOwner?, | ||
var resource: Int, | ||
var message: String = "", | ||
val listener: Any?, | ||
val customBinding: ((itemBinding: ViewDataBinding, message: String?) -> Unit)? | ||
) : | ||
ListAdapter<T, ErrorAdapter<T>.ViewHolder>(AsyncDifferConfig.Builder<T>(object : | ||
DiffUtil.ItemCallback<T>() { | ||
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean { | ||
return false | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean { | ||
return false | ||
} | ||
|
||
}).build()) { | ||
|
||
override fun getItemViewType(position: Int): Int { | ||
return resource | ||
} | ||
|
||
inner class ViewHolder(val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) { | ||
fun bind() { | ||
binding.lifecycleOwner = lifecycleOwner | ||
|
||
if (customBinding == null) { | ||
binding.setVariable(BR.message, message) | ||
binding.setVariable(BR.listener, listener) | ||
binding.executePendingBindings() | ||
} else { | ||
|
||
customBinding.invoke( | ||
binding, | ||
message | ||
) | ||
} | ||
|
||
} | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val binding = DataBindingUtil.inflate<ViewDataBinding>( | ||
LayoutInflater.from(parent.context), | ||
resource, | ||
parent, | ||
false | ||
) | ||
return ViewHolder(binding) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
holder.bind() | ||
} | ||
|
||
override fun getItemCount(): Int = 1 | ||
} |
62 changes: 62 additions & 0 deletions
62
...ersal_adapter/src/main/java/com/yogeshpaliyal/universal_adapter/adapter/LoadingAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.yogeshpaliyal.universal_adapter.adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.databinding.ViewDataBinding | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.recyclerview.widget.AsyncDifferConfig | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
|
||
/* | ||
* @author Yogesh Paliyal | ||
* techpaliyal@gmail.com | ||
* https://techpaliyal.com | ||
* created on 02-05-2021 19:57 | ||
*/ | ||
class LoadingAdapter<T>( | ||
val lifecycleOwner: LifecycleOwner?, | ||
var resource: Int, val count: Int | ||
) : | ||
ListAdapter<T, LoadingAdapter<T>.ViewHolder>(AsyncDifferConfig.Builder<T>(object : | ||
DiffUtil.ItemCallback<T>() { | ||
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean { | ||
return false | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean { | ||
return false | ||
} | ||
|
||
}).build()) { | ||
|
||
|
||
inner class ViewHolder(val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) { | ||
fun bind() { | ||
binding.lifecycleOwner = lifecycleOwner | ||
} | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val binding = DataBindingUtil.inflate<ViewDataBinding>( | ||
LayoutInflater.from(parent.context), | ||
resource, | ||
parent, | ||
false | ||
) | ||
return ViewHolder(binding) | ||
} | ||
|
||
override fun getItemViewType(position: Int): Int { | ||
return resource | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
holder.bind() | ||
} | ||
|
||
override fun getItemCount(): Int = count | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.