0% found this document useful (0 votes)
24 views3 pages

Card For Credit Adapter

This document contains code for an Android adapter class used to display a list of user credit cards. The CardForCreditAdapter extends the RecyclerView adapter and handles displaying card data like the last 4 digits of the card number, selected/unselected status, card type icon, available balances in UZS and USD currencies. It also handles click listeners to update the selected card.

Uploaded by

Yank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Card For Credit Adapter

This document contains code for an Android adapter class used to display a list of user credit cards. The CardForCreditAdapter extends the RecyclerView adapter and handles displaying card data like the last 4 digits of the card number, selected/unselected status, card type icon, available balances in UZS and USD currencies. It also handles click listeners to update the selected card.

Uploaded by

Yank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package

uz.maroqand.asakamobile.ui.credit.main.new_credit.microloan.card_selection.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import uz.maroqand.asakamobile.R
import uz.maroqand.asakamobile.data.constant.Constants
import uz.maroqand.asakamobile.data.db.entity.UserCard
import uz.maroqand.asakamobile.databinding.ItemCardMicroloanBinding
import uz.maroqand.asakamobile.util.AppUtils
import uz.maroqand.asakamobile.util.ViewUtils.px

class CardForCreditAdapter(var cardList: List<UserCard>, var onCardItemClick:


(UserCard) -> Unit = {}) :
RecyclerView.Adapter<CardForCreditAdapter.CardViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):


CardViewHolder {
return
CardViewHolder(ItemCardMicroloanBinding.inflate(LayoutInflater.from(parent.context)
, parent, false))
}

override fun onBindViewHolder(holder: CardViewHolder, position: Int) {


holder.initCardView(position, cardList[position])
}

override fun getItemCount(): Int {


return cardList.size
}

fun unselectAllCards() {
for (i in cardList.indices) {
cardList[i].isSelected = false
}
notifyDataSetChanged()
}

inner class CardViewHolder(private val cardBinding: ItemCardMicroloanBinding) :


RecyclerView.ViewHolder(cardBinding.root) {

private val mContext = cardBinding.root.context

fun initCardView(position: Int, cardItem: UserCard) {


cardBinding.root.setOnClickListener {
onCardItemClick(cardItem)
for (i in cardList.indices) {
cardList[i].isSelected = i == position
}
notifyDataSetChanged()
}

Glide.with(mContext)
.load(cardItem.backgroundImage)
.transform(MultiTransformation(CenterCrop(), RoundedCorners(8.px)))
.into(cardBinding.cardImg)

with(cardBinding) {
txtCardNumber.text = cardItem.cardNumber!!.takeLast(4)

cardBinding.imgCardChecked.visibility = if (cardItem.isSelected)
View.VISIBLE else View.GONE

imgCardSubType.visibility = View.GONE
val imgId = when (cardItem.cardType) {
Constants.HUMO -> R.drawable.ic_microloan_card_type_humo
Constants.MASTERCARD ->
R.drawable.ic_microloan_card_type_mastercard
Constants.MASTERCARD_UZCARD_COLBEYJ -> {

imgCardSubType.setImageResource(R.drawable.ic_microloan_card_type_uzcard)
imgCardSubType.visibility = View.VISIBLE
R.drawable.ic_microloan_card_type_mastercard
}
Constants.MASTERCARD_HUMO_COLBEYJ -> {

imgCardSubType.setImageResource(R.drawable.ic_microloan_card_type_humo)
imgCardSubType.visibility = View.VISIBLE
R.drawable.ic_microloan_card_type_mastercard
}
else -> R.drawable.ic_microloan_card_type_uzcard
}
imgCardType.setImageResource(imgId)

if (cardItem.status == Constants.CARD_ACTIVE) {
tvCardError.visibility = View.GONE

var uzsBalance = cardItem.balance


var usdBalance = cardItem.balance
if (cardItem.currency?.code == Constants.UZS) {
txtBalanceUzs.visibility = View.VISIBLE
txtBalanceUsd.visibility = View.INVISIBLE
} else if (cardItem.currency?.code == Constants.USD) {
if (cardItem.isMultiCurrency) {
txtBalanceUzs.visibility = View.VISIBLE
txtBalanceUsd.visibility = View.VISIBLE
cardItem.balances?.balance?.let {
it.forEach { bItem ->
if (bItem.currencyCode == Constants.USD) {
usdBalance = bItem.amount
} else {
uzsBalance = bItem.amount
}
}
}
} else {
txtBalanceUzs.visibility = View.INVISIBLE
txtBalanceUsd.visibility = View.VISIBLE
}
}

txtBalanceUzs.text = AppUtils.getFormattedBalance(mContext,
uzsBalance, Constants.UZS, hideBalance = cardItem.hideBalance ?: false)
txtBalanceUsd.text = AppUtils.getFormattedBalance(mContext,
usdBalance, Constants.USD, hideBalance = cardItem.hideBalance ?: false)

} else {
txtBalanceUzs.visibility = View.GONE
txtBalanceUsd.visibility = View.GONE
tvCardError.visibility = View.VISIBLE
tvCardError.text = cardItem.statusText
}
}
}
}
}

You might also like