package com.jephtecolin.kwii.ui.custom import android.content.Context import android.util.AttributeSet import android.view.ViewGroup import android.widget.LinearLayout import androidx.databinding.Bindable import com.jephtecolin.kwii.R import com.jephtecolin.kwii.util.TAG import com.jephtecolin.model.Topping import timber.log.Timber class CheckBoxGroup @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : LinearLayout(context, attrs) { val parent: LinearLayout private lateinit var mOnCheckedChangeListener: OnCheckedChangeListener var totalPrice: Double = 0.0 init { inflate(context, R.layout.checkbox_group, this) parent = findViewById(R.id.root) } fun setItems(toppings: Array) { toppings.forEach { topping -> val checkBox = CheckboxCustom(context) checkBox.layoutParams = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) checkBox.setTitle(topping.name) checkBox.setPrice(topping.price.toString()) checkBox.setOnClickListener { Timber.tag(TAG).d("checkbox checked: %s", checkBox.isChecked()) if(checkBox.isChecked()) { totalPrice+= topping.price mOnCheckedChangeListener.onCheckedChange(totalPrice) } else { totalPrice-= topping.price mOnCheckedChangeListener.onCheckedChange(totalPrice) } } parent.addView(checkBox) } } fun setOnCheckedChangeListener(listener: OnCheckedChangeListener?) { listener?.let { this.mOnCheckedChangeListener = it} } } interface OnCheckedChangeListener { fun onCheckedChange(totalPrice: Double) }