Skip to content

Instantly share code, notes, and snippets.

@seoft
seoft / SchemeUtil.kt
Created December 17, 2021 14:22
SchemeRouter and SchemeCommand
data class SchemeRouter(@IdRes val destinationId: Int, val `moreOptions`: Any)
data class SchemeCommand(val routers: List<SchemeRouter>, val `moreOptions`: Any)
private val recyclerViewState = SubmittableRecyclerViewState(
object : DiffUtil.ItemCallback<AntonioUiModel>() {
override fun areItemsTheSame(oldItem: AntonioUiModel, newItem: AntonioUiModel) =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: AntonioUiModel, newItem: AntonioUiModel) =
oldItem == newItem
})
override fun onCreate(savedInstanceState: Bundle?) {
sealed class AntonioUiModel(
val type: Type,
open val id: Long,
open val price: Int
) : AntonioBindingModel {
enum class Type { MONITOR, MOUSE, PHONE }
override fun bindingVariableId(): Int = BR.item
data class Monitor(override val id: Long, override val price: Int, val inch: Float) :
dependencies {
// https://github.com/naverz/Antonio
implementation "io.github.naverz:antonio-databinding:1.0.3-alpha"
implementation "io.github.naverz:antonio-annotation:0.0.1-alpha"
kapt "io.github.naverz:antonio-annotation:0.0.1-alpha"
}
private val adapter by lazy {
DataBindingAdapter<NormalUiModel>(
bindingVariableId = BR.item,
variables = mapOf(BR.onNormalListener to onNormalListener),
object : DiffUtil.ItemCallback<BindingItem<NormalUiModel>>() {
override fun areItemsTheSame(
oldItem: BindingItem<NormalUiModel>, newItem: BindingItem<NormalUiModel>
) = oldItem.item.id == newItem.item.id
override fun areContentsTheSame(
data class BindingItem<ITEM>(@LayoutRes val type: Int, val item: ITEM)
fun <T> T.toBindingItem(@LayoutRes type: Int): BindingItem<T> {
return BindingItem(type, this)
}
private val adapter by lazy { NormalAdapter(onNormalListener) }
override fun onCreate(savedInstanceState: Bundle?) {
binding.recyclerView.adapter = adapter
viewModel.uiModels.observe(this) {
adapter.submitList(it)
}
}
class NormalAdapter(
private val onNormalListener: OnNormalListener
) : ListAdapter<NormalUiModel, BindViewHolder<NormalUiModel>>(
object : DiffUtil.ItemCallback<NormalUiModel>() {
override fun areItemsTheSame(oldItem: NormalUiModel, newItem: NormalUiModel) =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: NormalUiModel, newItem: NormalUiModel) =
oldItem == newItem
}
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
>
<data>
<variable
name="item"
interface OnNormalListener {
fun onClicked(item: NormalUiModel)
}