Untitled

mail@pastecode.io avatar
unknown
kotlin
a month ago
4.7 kB
2
Indexable
Never
package widget.liquidpay.com.widget.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
import widget.liquidpay.com.widget.R
import widget.liquidpay.com.widget.databinding.LpVoucherItemCellBinding
import widget.liquidpay.com.widget.databinding.LpVoucherSelectionItemFooterBinding
import widget.liquidpay.com.widget.model.voucherselection.LPVoucherItemState

internal class LPVoucherSelectionAdapter(
    val events: (Event) -> Unit,
) : DiffUtilAdapter<LPVoucherItemState, RecyclerView.ViewHolder>(
        emptyList(),
    ) {
    private val viewTypeVoucher = 0
    private val viewTypeFooter = 1

    private var footerState: FooterState? = null

    fun updateFooter(state: FooterState?) {
        if (footerState != state) {
            footerState = state
            notifyDataSetChanged()
        }
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            viewTypeFooter ->
                FooterViewHolder(
                    binding = LpVoucherSelectionItemFooterBinding.inflate(inflater, parent, false),
                ) {
                    events(Event.LoadMore)
                }
            else ->
                LPVoucherViewHolder(
                    binding = LpVoucherItemCellBinding.inflate(inflater, parent, false),
                ) { item, index ->
                    events(Event.ToggleChange(item, index))
                }
        }
    }


    override fun areItemsTheSame(
        oldItem: LPVoucherItemState,
        newItem: LPVoucherItemState,
    ): Boolean = oldItem.rawData.id == newItem.rawData.id

    override fun getItemCount(): Int = itemsList.count() + (footerState?.let { 1 } ?: 0)

    override fun onBindViewHolder(
        holder: RecyclerView.ViewHolder,
        position: Int,
    ) {
        when (holder) {
            is LPVoucherViewHolder -> holder.bind(itemsList[position])
            is FooterViewHolder -> holder.bind(footerState)
        }
    }

    override fun getItemViewType(position: Int): Int = if (position == itemsList.count()) viewTypeFooter else viewTypeVoucher

    // region ViewHolders

    private inner class LPVoucherViewHolder(
        val binding: LpVoucherItemCellBinding,
        onClick: (LPVoucherItemState, Int) -> Unit,
    ) : RecyclerView.ViewHolder(binding.root) {
        init {
            binding.container.setOnClickListener {
                onClick(itemsList[adapterPosition], adapterPosition)
            }
        }

        fun bind(state: LPVoucherItemState) =
            with(binding) {
                Picasso.get().load(state.image).into(imgVoucher)
                tvVoucherName.text = state.name
                tvVoucherContent.text = state.description
                tvExpDate.text = binding.root.context.getString(R.string.lp_valid_till, state.expiryDateString)
                ivStatus.isActivated = state.status == LPVoucherItemState.Status.Selected
                ivStatus.isEnabled = !state.status.isDisabledOrExceeded()
                tvDisableNotes.isVisible = state.status.isDisabledOrExceeded()
                tvDisableNotes.text = root.context.getString(if (state.status == LPVoucherItemState.Status.Exceeded) R.string.lp_voucher_exceed_note else R.string.lp_disable_note )
            }
    }

    private inner class FooterViewHolder(
        val binding: LpVoucherSelectionItemFooterBinding,
        onClick: () -> Unit,
    ) : RecyclerView.ViewHolder(binding.root) {
        init {
            binding.root.setOnClickListener { onClick() }
        }

        fun bind(mState: FooterState?) {
            val state =
                mState ?: also {
                    binding.root.isVisible = false
                    return
                }
            binding.progress.isVisible = state == FooterState.Loading
            binding.textView.isVisible = state is FooterState.Show
            if (state is FooterState.Show) {
                binding.textView.text = state.text
            }
        }
    }

    // endregion

    // region Models
    sealed class FooterState {
        object Loading : FooterState()

        data class Show(
            val text: String,
        ) : FooterState()
    }

    sealed class Event {
        data class ToggleChange(
            val item: LPVoucherItemState,
            val index: Int,
        ) : Event()

        object LoadMore : Event()
    }

    // endregion
}
Leave a Comment