Skip to content

Instantly share code, notes, and snippets.

@Draketheb4dass
Created July 28, 2021 14:37
Show Gist options
  • Save Draketheb4dass/cf02825db3ecc9b2ae58c2e16c68543b to your computer and use it in GitHub Desktop.
Save Draketheb4dass/cf02825db3ecc9b2ae58c2e16c68543b to your computer and use it in GitHub Desktop.
package com.jephtecolin.kwii.util
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import androidx.databinding.InverseBindingListener
import com.jephtecolin.kwii.ui.custom.CheckBoxGroup
import com.jephtecolin.kwii.ui.custom.OnCheckedChangeListener
import com.jephtecolin.model.Topping
import timber.log.Timber
@BindingAdapter("setItems")
fun setItems(checkboxGroup: CheckBoxGroup, toppings: Array<Topping>) {
checkboxGroup.setItems(toppings)
}
@BindingAdapter(value = ["setOnCheckedChangeListener", "totalPriceAttrChanged"] , requireAll = false)
fun setOnCheckedChangeListener(checkboxGroup: CheckBoxGroup, listener: OnCheckedChangeListener,
totalPriceAttrChanged: InverseBindingListener) {
totalPriceAttrChanged.onChange()
checkboxGroup.setOnCheckedChangeListener(object : OnCheckedChangeListener {
override fun onCheckedChange(totalPrice: Double) {
Timber.d("binding adapter works")
if (totalPriceAttrChanged != null) {
totalPriceAttrChanged.onChange()
}
}
})
}
@BindingAdapter( "totalPrice")
fun setTotalPrice(checkboxGroup: CheckBoxGroup, totalPrice: Double) {
if(checkboxGroup.totalPrice != totalPrice) {
checkboxGroup.totalPrice = totalPrice
}
}
@InverseBindingAdapter(attribute = "totalPrice")
fun getTotalPrice(checkboxGroup: CheckBoxGroup): Double {
return checkboxGroup.totalPrice
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment