Skip to content

Commit

Permalink
major improvements
Browse files Browse the repository at this point in the history
Migrated from recycler adapter to ListAdapter
Async DiffUtil Added

Signed-off-by: Yogesh Paliyal <paliyalyogesh@gmail.com>
  • Loading branch information
yogeshpaliyal committed May 2, 2021
1 parent 73db9b9 commit 7c25bc3
Show file tree
Hide file tree
Showing 12 changed files with 385 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.techpaliyal.androidkotlinmvvm.model.BasicModel
import com.techpaliyal.androidkotlinmvvm.ui.view_model.BasicListingActivityViewModel
import com.techpaliyal.androidkotlinmvvm.ui.view_model.initViewModel
import com.yogeshpaliyal.universal_adapter.BR
import com.yogeshpaliyal.universal_adapter.adapter.SectionUniversalRecyclerAdapterBuilder
import com.yogeshpaliyal.universal_adapter.adapter.UniversalAdapterViewType
import com.yogeshpaliyal.universal_adapter.utils.Resource
import com.yogeshpaliyal.universal_adapter.utils.UniversalAdapterBuilder
Expand Down Expand Up @@ -55,18 +54,18 @@ class BasicListingActivity : AppCompatActivity() {
})
)

SectionUniversalRecyclerAdapterBuilder<BasicModel, Unit, Unit>(contentOption)
contentOption.build()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityListingBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.recyclerView.adapter = mAdapter.concatedAdapter
binding.recyclerView.adapter = mAdapter.build()

mViewModel.data.observe(this, Observer {
mAdapter.updateContent(Resource.success(it))
mAdapter.updateData(Resource.success(it))
})

mViewModel.fetchData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MultiSelectListingActivity : AppCompatActivity() {
binding = ActivityListingBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.recyclerView.adapter = mAdapter
binding.recyclerView.adapter = mAdapter.build()

mViewModel.data.observe(this, Observer {
mAdapter.updateData(Resource.success(it))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.techpaliyal.androidkotlinmvvm.listeners.BasicListener
import com.techpaliyal.androidkotlinmvvm.model.UserModel
import com.techpaliyal.androidkotlinmvvm.ui.view_model.LoadingListingViewModel
import com.techpaliyal.androidkotlinmvvm.ui.view_model.initViewModel
import com.yogeshpaliyal.universal_adapter.adapter.UniversalRecyclerAdapter
import com.yogeshpaliyal.universal_adapter.utils.UniversalAdapterBuilder

class PaginationListingActivity : AppCompatActivity() {
lateinit var binding: ActivityListingBinding
Expand All @@ -32,7 +32,7 @@ class PaginationListingActivity : AppCompatActivity() {
}

private val mAdapter by lazy {
UniversalRecyclerAdapter<UserModel>(
UniversalAdapterBuilder<UserModel>(
R.layout.item_user,
resourceLoading = R.layout.item_user_shimmer,
defaultLoadingItems = 5,
Expand All @@ -42,15 +42,15 @@ class PaginationListingActivity : AppCompatActivity() {
Toast.makeText(this@PaginationListingActivity, model.name, Toast.LENGTH_SHORT)
.show()
}
})
}).build()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityListingBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.recyclerView.adapter = mAdapter
binding.recyclerView.adapter = mAdapter.build()

binding.recyclerView.setupPagination {
if (mViewModel.fetchJob?.isActive == false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ShimmerListingActivity : AppCompatActivity() {
binding = ActivityListingBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.recyclerView.adapter = mAdapter
binding.recyclerView.adapter = mAdapter.build()

mViewModel.data.observe(this, Observer {
mAdapter.updateData(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import com.techpaliyal.androidkotlinmvvm.listeners.BasicListener
import com.techpaliyal.androidkotlinmvvm.model.UserModel
import com.techpaliyal.androidkotlinmvvm.ui.view_model.UserListingActivityViewModel
import com.techpaliyal.androidkotlinmvvm.ui.view_model.initViewModel
import com.yogeshpaliyal.universal_adapter.adapter.UniversalRecyclerAdapter
import com.yogeshpaliyal.universal_adapter.utils.Resource
import com.yogeshpaliyal.universal_adapter.utils.UniversalAdapterBuilder

/**
* @author Yogesh Paliyal
Expand All @@ -34,13 +34,13 @@ class UserListingActivity : AppCompatActivity() {
}

private val mAdapter by lazy {
UniversalRecyclerAdapter<UserModel>(
UniversalAdapterBuilder<UserModel>(
R.layout.item_user,
mListener = object : BasicListener<UserModel> {
override fun onClick(model: UserModel) {
Toast.makeText(this@UserListingActivity, model.name, Toast.LENGTH_SHORT).show()
}
})
}).build()
}


Expand All @@ -49,7 +49,7 @@ class UserListingActivity : AppCompatActivity() {
binding = ActivityListingBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.recyclerView.adapter = mAdapter
binding.recyclerView.adapter = mAdapter.build()

mViewModel.data.observe(this, Observer {
mAdapter.updateData(Resource.success(it))
Expand Down
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))
}

}
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
}
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.yogeshpaliyal.universal_adapter.adapter

import androidx.annotation.LayoutRes
import androidx.databinding.ViewDataBinding
import com.yogeshpaliyal.universal_adapter.utils.Resource


/*
Expand Down Expand Up @@ -52,6 +51,6 @@ sealed class UniversalAdapterViewType {
)
val errorListener: Any? = null,
val listener: Any? = null,
val customBindingMapping: ((itemBinding: ViewDataBinding, item: Resource<List<T>?>?) -> Unit)? = null
val customBindingMapping: ((itemBinding: ViewDataBinding, message: String?) -> Unit)? = null
)
}
Loading

0 comments on commit 7c25bc3

Please sign in to comment.