Last active
December 27, 2019 11:58
-
-
Save addeeandra/ef5f9e73df729a8c9648e3aa12c250b5 to your computer and use it in GitHub Desktop.
ViewModel Utility Classes
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
import androidx.databinding.ViewDataBinding | |
import androidx.recyclerview.widget.RecyclerView | |
open class BindingViewHolder<T, V : ViewDataBinding, L : BindingListener<T>>( | |
private val binding: V, | |
private val listener: L? | |
) : RecyclerView.ViewHolder(binding.root) { | |
fun bind(data: T) { | |
binding.setVariable(BR.viewmodel, data) | |
listener?.let { binding.setVariable(BR.listener, it) } | |
binding.executePendingBindings() | |
} | |
} |
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
interface BindingListener<T> { | |
fun onEvent(data: T) | |
} |
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
open class BindingRecyclerAdapter<MODEL, BINDING : ViewDataBinding>( | |
private val layoutId: Int, | |
private val items: MutableList<MODEL>, | |
private val _listener: BindingListener<MODEL>? = null, | |
private val _searchImpl: ((items: MutableList<MODEL>, keyword: String) -> List<MODEL>)? = null, | |
private val _sortImpl: ((items: MutableList<MODEL>, type: Int) -> List<MODEL>)? = null | |
) : RecyclerView.Adapter<BindingViewHolder<MODEL, BINDING, BindingListener<MODEL>>>() { | |
private var filteredItems: List<MODEL> = items | |
override fun getItemCount() = filteredItems.size | |
override fun getItemId(position: Int) = position.toLong() | |
override fun onCreateViewHolder( | |
parent: ViewGroup, | |
viewType: Int | |
): BindingViewHolder<MODEL, BINDING, BindingListener<MODEL>> { | |
return BindingViewHolder( | |
DataBindingUtil.inflate( | |
LayoutInflater.from(parent.context), | |
layoutId, | |
parent, | |
false | |
), _listener | |
) | |
} | |
override fun onBindViewHolder( | |
holder: BindingViewHolder<MODEL, BINDING, BindingListener<MODEL>>, | |
position: Int | |
) { | |
holder.bind(filteredItems[position]) | |
} | |
fun searchData( | |
keyword: String, | |
defaultList: MutableList<MODEL> = items, | |
notify: Boolean = true | |
) { | |
filteredItems = _searchImpl?.invoke(items, keyword) ?: defaultList | |
if (notify) notifyDataSetChanged() | |
} | |
fun sortData( | |
type: Int, | |
defaultList: MutableList<MODEL> = items, | |
notify: Boolean = true | |
) { | |
filteredItems = _sortImpl?.invoke(items, type) ?: defaultList | |
if (notify) notifyDataSetChanged() | |
} | |
fun replaceData(newItems: List<MODEL>, notify: Boolean = true) { | |
with(items) { | |
clear() | |
addAll(newItems) | |
} | |
filteredItems = items | |
if (notify) notifyDataSetChanged() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment