Skip to content

Instantly share code, notes, and snippets.

@Draketheb4dass
Created July 28, 2021 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Draketheb4dass/841a7feb318709932c9e4c2e0bb50902 to your computer and use it in GitHub Desktop.
Save Draketheb4dass/841a7feb318709932c9e4c2e0bb50902 to your computer and use it in GitHub Desktop.
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<Topping>) {
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment