Add cryptoAmount, cryptoPrecision and cryptoSymbol to TransferDetails model and fetch that iformation from the database, which is later used to correctly format and display the crypto amount in the Transactions list.

This commit is contained in:
Severiano Jaramillo 2018-12-07 17:23:44 -06:00
parent 8ef4610db3
commit 496e0ac21f
4 changed files with 33 additions and 13 deletions

View file

@ -14,15 +14,19 @@ import androidx.recyclerview.widget.SortedList
import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.utils.Constants
import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.*
class TransfersAdapter(private val context: Context) : class TransfersDetailsAdapter(private val context: Context) :
RecyclerView.Adapter<TransfersAdapter.ViewHolder>() { RecyclerView.Adapter<TransfersDetailsAdapter.ViewHolder>() {
val userId = PreferenceManager.getDefaultSharedPreferences(context) val userId = PreferenceManager.getDefaultSharedPreferences(context)
.getString(Constants.KEY_CURRENT_ACCOUNT_ID, "")!! .getString(Constants.KEY_CURRENT_ACCOUNT_ID, "")!!
private val mComparator = private val mComparator =
Comparator<TransferDetail> { a, b -> a.id.compareTo(b.id) } Comparator<TransferDetail> { a, b -> b.id.compareTo(a.id) }
private val mSortedList = private val mSortedList =
SortedList<TransferDetail>(TransferDetail::class.java, object : SortedList.Callback<TransferDetail>() { SortedList<TransferDetail>(TransferDetail::class.java, object : SortedList.Callback<TransferDetail>() {
@ -69,7 +73,7 @@ class TransfersAdapter(private val context: Context) :
val tvFiatEquivalent: TextView = itemView.findViewById(R.id.tvFiatEquivalent) val tvFiatEquivalent: TextView = itemView.findViewById(R.id.tvFiatEquivalent)
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransfersAdapter.ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransfersDetailsAdapter.ViewHolder {
val inflater = LayoutInflater.from(context) val inflater = LayoutInflater.from(context)
val transactionView = inflater.inflate(R.layout.item_transaction, parent, false) val transactionView = inflater.inflate(R.layout.item_transaction, parent, false)
@ -87,6 +91,22 @@ class TransfersAdapter(private val context: Context) :
viewHolder.tvFrom.text = transferDetail.from viewHolder.tvFrom.text = transferDetail.from
viewHolder.tvTo.text = transferDetail.to viewHolder.tvTo.text = transferDetail.to
viewHolder.tvDate.text = "02 Oct"
viewHolder.tvTime.text = "15:01:18 CET"
// Show the crypto amount correctly formatted
// TODO lift the DecimalFormat declaration to other place to make things more efficient
val df = DecimalFormat("####."+("#".repeat(transferDetail.cryptoPrecision)))
df.roundingMode = RoundingMode.CEILING
df.decimalFormatSymbols = DecimalFormatSymbols(Locale.getDefault())
val amount = transferDetail.cryptoAmount.toDouble() /
Math.pow(10.toDouble(), transferDetail.cryptoPrecision.toDouble())
val cryptoAmount = "${df.format(amount)} ${transferDetail.cryptoSymbol}"
viewHolder.tvCryptoAmount.text = cryptoAmount
viewHolder.tvFiatEquivalent.text = "$4119.75"
viewHolder.ivDirectionArrow.setImageDrawable(context.getDrawable( viewHolder.ivDirectionArrow.setImageDrawable(context.getDrawable(
if(transferDetail.direction) R.drawable.ic_arrow_receive else R.drawable.ic_arrow_send if(transferDetail.direction) R.drawable.ic_arrow_receive else R.drawable.ic_arrow_send
)) ))

View file

@ -4,11 +4,11 @@ data class TransferDetail(
val id: String, val id: String,
val from: String, val from: String,
val to: String, val to: String,
val direction: Boolean // True -> Received, False -> Sent val direction: Boolean, // True -> Received, False -> Sent
// val date: Long, // val date: Long,
// val cryptoAmount: Long, val cryptoAmount: Long,
// val cryptoPrecision: Int, val cryptoPrecision: Int,
// val cryptoSymbol: String, val cryptoSymbol: String
// val fiatAmount: Long, // val fiatAmount: Long,
// val fiatCurrency: String // val fiatCurrency: String
) )

View file

@ -7,6 +7,6 @@ import androidx.room.Query
@Dao @Dao
interface TransferDetailDao { interface TransferDetailDao {
@Query("SELECT id, IFNULL((SELECT name FROM user_accounts WHERE user_accounts.id=transfers.source), '') AS `from`, IFNULL((SELECT name FROM user_accounts WHERE user_accounts.id=transfers.destination), '') AS `to`, (CASE WHEN destination=:userId THEN 1 ELSE 0 END) AS `direction` FROM transfers") @Query("SELECT transfers.id, IFNULL((SELECT name FROM user_accounts WHERE user_accounts.id=transfers.source), '') AS `from`, IFNULL((SELECT name FROM user_accounts WHERE user_accounts.id=transfers.destination), '') AS `to`, (CASE WHEN destination=:userId THEN 1 ELSE 0 END) AS `direction`, transfers.transfer_amount AS `cryptoAmount`, assets.precision AS `cryptoPrecision`, assets.symbol AS cryptoSymbol FROM transfers INNER JOIN assets WHERE transfers.transfer_asset_id = assets.id")
fun getAll(userId: String): LiveData<List<TransferDetail>> fun getAll(userId: String): LiveData<List<TransferDetail>>
} }

View file

@ -13,7 +13,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.adapters.BalancesAdapter import cy.agorise.bitsybitshareswallet.adapters.BalancesAdapter
import cy.agorise.bitsybitshareswallet.adapters.TransfersAdapter import cy.agorise.bitsybitshareswallet.adapters.TransfersDetailsAdapter
import cy.agorise.bitsybitshareswallet.database.entities.UserAccount import cy.agorise.bitsybitshareswallet.database.entities.UserAccount
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail
@ -63,12 +63,12 @@ class BalancesFragment : Fragment() {
// Configure TransferDetailViewModel to show the transaction history // Configure TransferDetailViewModel to show the transaction history
mTransferDetailViewModel = ViewModelProviders.of(this).get(TransferDetailViewModel::class.java) mTransferDetailViewModel = ViewModelProviders.of(this).get(TransferDetailViewModel::class.java)
val transfersAdapter = TransfersAdapter(context!!) val transfersDetailsAdapter = TransfersDetailsAdapter(context!!)
rvTransactions.adapter = transfersAdapter rvTransactions.adapter = transfersDetailsAdapter
rvTransactions.layoutManager = LinearLayoutManager(context) rvTransactions.layoutManager = LinearLayoutManager(context)
mTransferDetailViewModel.getAll(userId).observe(this, Observer<List<TransferDetail>> { transfersDetails -> mTransferDetailViewModel.getAll(userId).observe(this, Observer<List<TransferDetail>> { transfersDetails ->
transfersAdapter.replaceAll(transfersDetails) transfersDetailsAdapter.replaceAll(transfersDetails)
}) })
} }
} }