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
class ClassicAdapter : RecyclerView.Adapter<ClassicHolder>() { | |
private val viewModel = PersonItemViewModel() | |
private val data: List<Person> | |
get() = viewModel.data | |
fun setData(persons: List<Person>) { | |
viewModel.data = persons | |
notifyDataSetChanged() |
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
class ClassicHolder(private val binding: ItemPersonBinding) : RecyclerView.ViewHolder(binding.root) { | |
fun bind(viewModel: PersonItemViewModel, position: Int) { | |
binding.setVariable(BR.viewModel, viewModel) | |
binding.setVariable(BR.position, position) | |
binding.executePendingBindings() | |
} | |
companion object { | |
fun create(parent: ViewGroup): ClassicHolder { | |
val inflater = LayoutInflater.from(parent.context) |
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
<?xml version="1.0" encoding="utf-8"?> | |
<layout> | |
<data> | |
<variable | |
name="position" | |
type="Integer" /> | |
<variable | |
name="viewModel" | |
type="plus.yeti.prostoadapter.ui.main.PersonItemViewModel" /> |
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
class PersonItemViewModel : ProstoViewModel<Person>() { | |
override var data: List<Person> = emptyList() | |
fun getName(position: Int) = data[position].lastName + ", " + data[position].firstName | |
fun getEmail(position: Int) = data[position].email | |
fun hasDog(position: Int): Boolean = data[position].hasDog | |
} |
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
abstract class ProstoViewModel<T>: ViewModel() { | |
abstract var data: List<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 ProstoHolder<TBinding : ViewDataBinding>(val binding: TBinding) : RecyclerView.ViewHolder(binding.root) { | |
open fun <TData, TViewModel : ProstoViewModel<TData>> bind(viewModel: TViewModel, position: Int) { | |
binding.setVariable(BR.viewModel, viewModel) | |
binding.setVariable(BR.position, position) | |
binding.executePendingBindings() | |
} | |
companion object { | |
fun <TBinding : ViewDataBinding> create(parent: ViewGroup, layoutId: Int): ProstoHolder<TBinding> { | |
val inflater = LayoutInflater.from(parent.context) |
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
abstract class ProstoAdapter<TBinding : ViewDataBinding, TData> : RecyclerView.Adapter<ProstoHolder<TBinding>>() { | |
abstract val viewModel: ProstoViewModel<TData> | |
abstract val layoutId: Int | |
private var dataSize: Int = 0 | |
open fun setData(data: List<TData>) { | |
this.dataSize = data.size | |
viewModel.data = data |
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
class MainFragment : Fragment() { | |
private val adapter = | |
object : ProstoAdapter<ItemPersonBinding, Person>() { | |
override val viewModel = PersonItemViewModel() | |
override val layoutId = R.layout.item_person | |
} | |
override fun onActivityCreated(savedInstanceState: Bundle?) { | |
super.onActivityCreated(savedInstanceState) | |
mainRecyclerView.adapter = adapter |
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
sealed interface InstanceType<T> { | |
fun interface Factory<T> : InstanceType<T> { | |
fun build(): T | |
} | |
fun interface ParamFactory<T> : InstanceType<T> { | |
fun build(vararg params: Any): T | |
class Params { | |
var parameters: Array<out Any> = arrayOf() |
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
module { | |
singleton<A> { AImpl() } | |
factory<B> { | |
BImpl( | |
a = get() | |
) | |
} | |
factoryWithParams<C> { (aParam) -> | |
C( | |
a = aParam as A, |
OlderNewer