Skip to content

Instantly share code, notes, and snippets.

@zamahaka
Created June 4, 2019 14:13
Show Gist options
  • Save zamahaka/92b9e3b94536b66c897950858183d9fd to your computer and use it in GitHub Desktop.
Save zamahaka/92b9e3b94536b66c897950858183d9fd to your computer and use it in GitHub Desktop.
sealed class TransactionViewHolder(
view: View
) : BaseRecyclerViewHolder(view) {
val expansionLayout: ExpansionLayout = transactionSummaryLayout
private val timeFormatter = DefaultLocaleDateFormat("MMMM d yyyy - hh:mm a")
private var onViewDetailsListener: OnActionListener? = null
private var indicator by view.indicatorView.bindToBackground()
private var requestFrom by txtRequestFrom.bindToText()
private var time by txtTime.bindToText()
private var amount by txtCurrencyAmount.bindToText()
init {
expandedContainer.onClick { expansionLayout.toggle(true) }
onClick(txtViewDetails) { onViewDetailsListener?.invoke(it) }
}
protected fun bindTransaction(meta: TransactionMeta) {
requestFrom = meta.title
time = timeFormatter.format(meta.time).capitalize()
amount = meta.amount
indicator = ColorDrawable(getColor(when (meta.color) {
GREY -> R.color.steel
BLACK -> R.color.dark_slate_blue
GREEN -> R.color.apple
RED -> R.color.dark_salmon_two
YELLOW -> R.color.desert
LIGHT_GREY -> R.color.pinkish_grey
}))
}
fun onViewDetails(listener: OnActionListener?) {
onViewDetailsListener = listener
}
}
class InvoiceViewHolder(
parent: ViewGroup, init: InvoiceViewHolder.() -> Unit
) : TransactionViewHolder(inflate(parent)), BindViewHolder<SummaryInvoice> {
private var onApproveListener: OnActionListener? = null
private var onDeclineListener: OnActionListener? = null
private var message by txtMessage.bindToText()
private var address by txtAddress.bindToText()
private var btnDeclineVisible by btnDecline.bindToVisibility()
private var btnApproveVisible by btnApprove.bindToVisibility()
private var btnApproveText by btnApprove.bindToText()
private var btnApproveEnabled by btnApprove.bindToEnabled()
private var approveProgress by progressApprove.bindToVisibility()
private var btnDeclineText by btnDecline.bindToText()
private var btnDeclineEnabled by btnDecline.bindToEnabled()
private var declineProgress by progressDecline.bindToVisibility()
private var expandedMessageVisible by expandedMessageInfo.bindToVisibility()
init {
apply(init)
onClick(btnApprove) { onApproveListener?.invoke(it) }
onClick(btnDecline) { onDeclineListener?.invoke(it) }
}
override fun bind(data: SummaryInvoice) {
val meta = data.meta
bindTransaction(meta)
btnDeclineVisible = meta.isCancelable
btnApproveVisible = meta.transaction.isMyInvoice()
address = meta.address
meta.message?.let { message ->
this.message = message
expandedMessageVisible = true
} ?: run {
this.message = ""
expandedMessageVisible = false
}
btnApproveText = if (data.approveProgress) "" else getString(R.string.approve)
btnApproveEnabled = !data.approveProgress
approveProgress = data.approveProgress
btnDeclineText = if (data.declineProgress) "" else getDeclineText(meta.transaction)
btnDeclineEnabled = !data.declineProgress
declineProgress = data.declineProgress
}
private fun getDeclineText(transaction: Transaction): CharSequence = getString(
if (transaction.isMyRequest()) R.string.cancel else R.string.decline
)
fun onApprove(listener: OnActionListener?) {
onApproveListener = listener
}
fun onDecline(listener: OnActionListener?) {
onDeclineListener = listener
}
companion object : ViewHolderViewCreator {
override val layoutRes: Int get() = R.layout.item_transaction_summary_invoice
}
}
class PaymentViewHolder(
parent: ViewGroup, init: PaymentViewHolder.() -> Unit
) : TransactionViewHolder(inflate(parent)), BindViewHolder<SummaryPayment> {
private var message by txtMessage.bindToText()
private var address by txtAddress.bindToText()
private var expandedMessageVisible by expandedMessageInfo.bindToVisibility()
init {
apply(init)
}
override fun bind(data: SummaryPayment) {
val meta = data.meta
bindTransaction(meta)
address = meta.address
meta.message?.let { message ->
this.message = message
expandedMessageVisible = true
} ?: run {
this.message = ""
expandedMessageVisible = false
}
}
companion object : ViewHolderViewCreator {
override val layoutRes: Int get() = R.layout.item_transaction_summary_payment
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment