zircles-android/app/src/main/java/cash/z/ecc/android/ui/detail/TransactionViewHolder.kt

62 lines
2.6 KiB
Kotlin
Raw Normal View History

2020-01-02 15:53:06 -08:00
package cash.z.ecc.android.ui.detail
2019-12-23 16:07:10 -08:00
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cash.z.ecc.android.R
2020-01-02 15:53:06 -08:00
import cash.z.ecc.android.ext.toAppColor
2019-12-23 16:07:10 -08:00
import cash.z.wallet.sdk.entity.ConfirmedTransaction
import cash.z.wallet.sdk.ext.toAbbreviatedAddress
2019-12-23 16:07:10 -08:00
import cash.z.wallet.sdk.ext.convertZatoshiToZecString
import java.text.SimpleDateFormat
import java.util.*
class TransactionViewHolder<T : ConfirmedTransaction>(itemView: View) : RecyclerView.ViewHolder(itemView) {
2020-01-02 15:53:06 -08:00
private val indicator = itemView.findViewById<View>(R.id.indicator)
2019-12-23 16:07:10 -08:00
private val amountText = itemView.findViewById<TextView>(R.id.text_transaction_amount)
2020-01-02 15:53:06 -08:00
private val topText = itemView.findViewById<TextView>(R.id.text_transaction_top)
private val bottomText = itemView.findViewById<TextView>(R.id.text_transaction_bottom)
2019-12-23 16:07:10 -08:00
private val formatter = SimpleDateFormat("M/d h:mma", Locale.getDefault())
fun bindTo(transaction: T?) {
2020-01-02 15:53:06 -08:00
var lineOne: String = ""
var lineTwo: String = ""
var amount: String = ""
var amountColor: Int = 0
var indicatorBackground: Int = 0
2020-01-02 15:53:06 -08:00
transaction?.apply {
amount = value.convertZatoshiToZecString()
// TODO: these might be good extension functions
val timestamp = formatter.format(blockTimeInSeconds * 1000L)
val isMined = blockTimeInSeconds != 0L
when {
!toAddress.isNullOrEmpty() -> {
lineOne = "You paid ${toAddress?.toAbbreviatedAddress()}"
2020-01-02 15:53:06 -08:00
lineTwo = if (isMined) "Sent $timestamp" else "Pending confirmation"
amount = "- $amount"
amountColor = R.color.zcashRed
indicatorBackground = R.drawable.background_indicator_outbound
2020-01-02 15:53:06 -08:00
}
raw == null || raw?.isEmpty() == true -> {
lineOne = "Unknown paid you"
lineTwo = "Received $timestamp"
amount = "+ $amount"
amountColor = R.color.zcashGreen
indicatorBackground = R.drawable.background_indicator_inbound
2020-01-02 15:53:06 -08:00
}
else -> {
lineOne = "Unknown"
lineTwo = "Unknown"
}
}
}
topText.text = lineOne
bottomText.text = lineTwo
amountText.text = amount
amountText.setTextColor(amountColor.toAppColor())
val context = itemView.context
indicator.background = context.resources.getDrawable(indicatorBackground)
2019-12-23 16:07:10 -08:00
}
}