Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.9 kB
1
Indexable
Never
package com.app.healthupp.presentation.page.nutrition.more

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.app.healthupp.databinding.NutritionItemBinding
import com.app.healthupp.domain.util.decim
import com.app.healthupp.model.other.DayBalance
import com.app.healthupp.presentation.page.nutrition.BalanceElement
import com.app.healthupp.presentation.page.nutrition.more.NutritionAdapter.NutritionItemVH
import kotlinx.android.synthetic.main.view_toolbar.view.*

class NutritionAdapter : RecyclerView.Adapter<NutritionItemVH>() {

    var elements: List<DayBalance> = emptyList()
    var onUserClicked: (nutritionInfo: DayBalance) -> Unit = {  }
    var userChecked: (isChecked: Boolean) -> Unit = { }
    private var isChecked = true

    class NutritionItemVH(val binding: NutritionItemBinding) : RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("SetTextI18n")
        fun bind(element: DayBalance){
            val elementColor = element.color ?: DayBalance.getRandomColor()
            binding.descriptionToAmountTv.text = element.name
            binding.infoImage.setBackgroundColor(element.color ?: DayBalance.getRandomColor())
            if(element.isChecked) {
                binding.checkboxImage.setBackgroundColor(
                    element.color ?: DayBalance.getRandomColor()
                )
            }
            else binding.checkboxImage.setBackgroundColor(Color.WHITE)

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NutritionItemVH {
        val inflater = LayoutInflater.from(parent.context)
        val binding = NutritionItemBinding.inflate(inflater, parent, false)

        return NutritionItemVH(binding)
    }

    override fun onBindViewHolder(holder: NutritionItemVH, position: Int) {
        val element = elements[position]
        holder.bind(element)
        println("element: $element")
        /*holder.binding.apply {
            checkboxImage.setOnClickListener {
                if(!isChecked) {
                    println("testujemy!!!")
                    isChecked = true
                    checkboxImage.setBackgroundColor(element.color ?: DayBalance.getRandomColor())
                }
                else {
                    checkboxImage.setBackgroundColor(Color.WHITE)
                    isChecked = false
                }

            }*/
            holder.binding.checkboxImage.setOnClickListener {
                //userChecked()
            }
            holder.binding.infoImage.setOnClickListener {
                onUserClicked(element)
            }
        }

    override fun getItemCount(): Int {
        return elements.size
    }
}