Created
April 17, 2021 02:45
-
-
Save enginebai/2e436f149c46213dcb506b9e0ba752db to your computer and use it in GitHub Desktop.
This file contains hidden or 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 EpoxyModelClickListener { | |
fun onPriceChanged(price: Double) | |
} | |
class HeaderEpoxyModel : Epoxy { | |
@EpoxyAttribute | |
var double price = 0.0 | |
@EpoxyAttribute | |
var EpoxyModelClickListener clickListener? = null | |
override fun bind(holder: ViewHolder) { | |
// consider to use abstract epoxy model for price updating | |
holder.text.setText(price.toString) | |
// trigger price change from user | |
holder.text.setOnClickListener { | |
// emit new price to listener | |
clickListener?.onPriceChanged(price) | |
} | |
} | |
} | |
class ItemEpoxyModel : Epoxy { | |
@EpoxyAttribute | |
var double price = 0.0 | |
@EpoxyAttribute | |
... | |
override fun bind(holder: ViewHolder) { | |
// the price part is as same as header | |
holder.text.setText(price.toString) | |
// trigger price change from user | |
holder.text.setOnClickListener { | |
// emit new price to listener | |
clickListener?.onPriceChanged(price) | |
} | |
} | |
} | |
class MyEpoxyController(private val clickListener: EpoxyModelClickListener) : EpoxyController() { | |
var price by Delegates.observable<Double>(0.0) { _, _, _ -> | |
requestModelBuild() | |
} | |
override fun buildModels() { | |
headerEpoxyModel() | |
.price(price) | |
.clickListener(clickListener) | |
itemEpoxyModel() | |
.price(price) | |
.clickListener(clickListener) | |
} | |
} | |
class ViewModel { | |
private val _price = MutableLiveData<Double> | |
val price: LiveData<Double> = _price | |
fun updatePrice(price: Double) { | |
_price.value = price | |
} | |
} | |
class MyFragment : EpoxyModelClickListener { | |
private val viewModel by inject() | |
private MyEpoxyController controller by lazy { | |
MyEpoxyController(this).apply { | |
viewModel.price.observe { | |
this.requestModelBuild() | |
} | |
} | |
} | |
@override | |
fun onPriceChanged(price: Double) { | |
viewModel.updatePrice(price) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment