From 496e0ac21f985577cf92e5877ee03c1a240b6040 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Fri, 7 Dec 2018 17:23:44 -0600 Subject: [PATCH] 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. --- ...sAdapter.kt => TransfersDetailsAdapter.kt} | 28 ++++++++++++++++--- .../database/joins/TransferDetail.kt | 8 +++--- .../database/joins/TransferDetailDao.kt | 2 +- .../fragments/BalancesFragment.kt | 8 +++--- 4 files changed, 33 insertions(+), 13 deletions(-) rename app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/{TransfersAdapter.kt => TransfersDetailsAdapter.kt} (80%) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersAdapter.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersDetailsAdapter.kt similarity index 80% rename from app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersAdapter.kt rename to app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersDetailsAdapter.kt index 631e1bc..c82b82e 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersAdapter.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersDetailsAdapter.kt @@ -14,15 +14,19 @@ import androidx.recyclerview.widget.SortedList import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail 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) : - RecyclerView.Adapter() { +class TransfersDetailsAdapter(private val context: Context) : + RecyclerView.Adapter() { val userId = PreferenceManager.getDefaultSharedPreferences(context) .getString(Constants.KEY_CURRENT_ACCOUNT_ID, "")!! private val mComparator = - Comparator { a, b -> a.id.compareTo(b.id) } + Comparator { a, b -> b.id.compareTo(a.id) } private val mSortedList = SortedList(TransferDetail::class.java, object : SortedList.Callback() { @@ -69,7 +73,7 @@ class TransfersAdapter(private val context: Context) : 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 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.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( if(transferDetail.direction) R.drawable.ic_arrow_receive else R.drawable.ic_arrow_send )) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetail.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetail.kt index a490111..c7123f7 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetail.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetail.kt @@ -4,11 +4,11 @@ data class TransferDetail( val id: String, val from: String, val to: String, - val direction: Boolean // True -> Received, False -> Sent + val direction: Boolean, // True -> Received, False -> Sent // val date: Long, -// val cryptoAmount: Long, -// val cryptoPrecision: Int, -// val cryptoSymbol: String, + val cryptoAmount: Long, + val cryptoPrecision: Int, + val cryptoSymbol: String // val fiatAmount: Long, // val fiatCurrency: String ) \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetailDao.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetailDao.kt index 7322a19..45384e7 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetailDao.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/joins/TransferDetailDao.kt @@ -7,6 +7,6 @@ import androidx.room.Query @Dao 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> } \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt index ef1687a..3d27ddf 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt @@ -13,7 +13,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import cy.agorise.bitsybitshareswallet.R 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.joins.BalanceDetail import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail @@ -63,12 +63,12 @@ class BalancesFragment : Fragment() { // Configure TransferDetailViewModel to show the transaction history mTransferDetailViewModel = ViewModelProviders.of(this).get(TransferDetailViewModel::class.java) - val transfersAdapter = TransfersAdapter(context!!) - rvTransactions.adapter = transfersAdapter + val transfersDetailsAdapter = TransfersDetailsAdapter(context!!) + rvTransactions.adapter = transfersDetailsAdapter rvTransactions.layoutManager = LinearLayoutManager(context) mTransferDetailViewModel.getAll(userId).observe(this, Observer> { transfersDetails -> - transfersAdapter.replaceAll(transfersDetails) + transfersDetailsAdapter.replaceAll(transfersDetails) }) } }