Untitled

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

import android.annotation.SuppressLint
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.app.healthupp.databinding.ItemBalanceOfDayBinding
import com.app.healthupp.databinding.MoreButtonBinding
import com.app.healthupp.domain.util.decim
import com.app.healthupp.model.other.DayBalance
import java.lang.IllegalStateException

sealed class BalanceElement {
    data class DayBalanceElement(val dayBalance: DayBalance) : BalanceElement()
    object More : BalanceElement()
}

class BalanceAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    var onClick: () -> Unit = {}
    var elements: List<DayBalance> = emptyList()
        set(value) {
            field = value
            balanceElements = field.map {
                BalanceElement.DayBalanceElement(it)
            }
        }
    private var balanceElements: List<BalanceElement> = emptyList()
        set(value) {
            field = value + listOf(BalanceElement.More)
            notifyDataSetChanged()
        }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            0 -> BalanceViewHolder(ItemBalanceOfDayBinding.inflate(inflater, parent, false))
            1 -> MoreButtonViewHolder(MoreButtonBinding.inflate(inflater, parent, false))
            else -> throw IllegalStateException("BalanceAdapter, unknown viewType")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val element = balanceElements[position]
        val viewType = getItemViewType(position)
        println("wartośc odzywcza: $element")
        when (viewType) {
            0 -> (holder as BalanceViewHolder).bind((element as BalanceElement.DayBalanceElement).dayBalance)
            1 -> (holder as MoreButtonViewHolder).binding.root.setOnClickListener {
                onClick()
                Log.d(
                    "BalanceAdapter",
                    "MoreButtonClicked"
                )

            }
            else -> throw IllegalStateException("BalanceAdapter, unknownViewType")
        }
    }

    override fun getItemViewType(position: Int): Int {
        val item = balanceElements[position]
        return when (item) {
            is BalanceElement.DayBalanceElement -> 0
            else -> 1
        }
    }

    override fun getItemCount(): Int = balanceElements.size

    class BalanceViewHolder(private val binding: ItemBalanceOfDayBinding) : RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("SetTextI18n")
        fun bind(element: DayBalance) {
            binding.balanceQuantityAndUnitTextView.text = "${element.quantity?.decim("#.#")}${element.unit}"
            binding.balanceElementTypeTextView.text = element.name
            binding.balanceQuantityAndUnitTextView.setTextColor(element.color ?: DayBalance.getRandomColor())
            binding.balanceElementTypeTextView.background.setTint(element.color ?: DayBalance.getRandomColor())
        }
    }

    class MoreButtonViewHolder(val binding: MoreButtonBinding) : RecyclerView.ViewHolder(binding.root)
}