Skip to content

Commit

Permalink
add usd equivalent balance to overview page
Browse files Browse the repository at this point in the history
  • Loading branch information
dreacot committed Apr 8, 2021
1 parent 0e1db85 commit e16308d
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 51 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/com/dcrandroid/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@ class HomeActivity : BaseActivity(), SyncProgressListener, TxAndBlockNotificatio
}
}

fun setToolbarSubTitle(title: CharSequence, showShadow: Boolean) {
if (title == "") {
toolbar_subtitle.visibility = View.GONE
} else {
toolbar_subtitle.visibility = View.VISIBLE
toolbar_subtitle.text = title
app_bar.elevation = if (showShadow) {
resources.getDimension(R.dimen.app_bar_elevation)
} else {
0f
}
}
}

fun checkWifiSync() {
if (!multiWallet!!.readBoolConfigValueForKey(Dcrlibwallet.SyncOnCellularConfigKey, Constants.DEF_SYNC_ON_CELLULAR)) {
// Check if wifi is connected
Expand Down
59 changes: 29 additions & 30 deletions app/src/main/java/com/dcrandroid/dialog/send/AmountInputHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.dcrandroid.extensions.hide
import com.dcrandroid.extensions.show
import com.dcrandroid.extensions.toggleVisibility
import com.dcrandroid.util.CoinFormat
import com.dcrandroid.util.CurrencyUtil
import com.dcrandroid.util.GetExchangeRate
import com.dcrandroid.util.WalletData
import dcrlibwallet.Dcrlibwallet
Expand All @@ -29,12 +30,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.math.BigDecimal
import java.math.RoundingMode
import java.text.DecimalFormat


const val AmountRelativeSize = 0.625f
val usdAmountFormat: DecimalFormat = DecimalFormat("0.0000")
val usdAmountFormat2: DecimalFormat = DecimalFormat("0.00")
val dcrFormat = DecimalFormat("#.########")

Expand Down Expand Up @@ -194,7 +193,7 @@ class AmountInputHelper(private val layout: LinearLayout, private val scrollToBo
layout.send_amount.removeTextChangedListener(this)

dcrAmount = BigDecimal(coin)
usdAmount = dcrToUSD(exchangeDecimal, dcrAmount!!.toDouble())
usdAmount = CurrencyUtil.dcrToUSD(exchangeDecimal, dcrAmount!!.toDouble())

if (currencyIsDCR) {
val dcr = Dcrlibwallet.amountAtom(coin)
Expand Down Expand Up @@ -252,10 +251,10 @@ class AmountInputHelper(private val layout: LinearLayout, private val scrollToBo
if (enteredAmount != null) {
if (currencyIsDCR) {
dcrAmount = enteredAmount!!
usdAmount = dcrToUSD(exchangeDecimal, dcrAmount!!.toDouble())
usdAmount = CurrencyUtil.dcrToUSD(exchangeDecimal, dcrAmount!!.toDouble())
} else {
usdAmount = enteredAmount!!
dcrAmount = usdToDCR(exchangeDecimal, usdAmount!!.toDouble())
dcrAmount = CurrencyUtil.usdToDCR(exchangeDecimal, usdAmount!!.toDouble())
}
} else {
dcrAmount = null
Expand Down Expand Up @@ -312,7 +311,7 @@ class AmountInputHelper(private val layout: LinearLayout, private val scrollToBo
}

if (dcrAmount != null) {
usdAmount = dcrToUSD(exchangeDecimal, dcrAmount!!.toDouble())
usdAmount = CurrencyUtil.dcrToUSD(exchangeDecimal, dcrAmount!!.toDouble())
}

displayEquivalentValue()
Expand All @@ -331,27 +330,27 @@ class AmountInputHelper(private val layout: LinearLayout, private val scrollToBo
}
}

fun dcrToFormattedUSD(exchangeDecimal: BigDecimal?, dcr: Double, scale: Int = 4): String {
if (scale == 4) {
return usdAmountFormat.format(
dcrToUSD(exchangeDecimal, dcr)!!.setScale(scale, BigDecimal.ROUND_HALF_EVEN).toDouble())
}

return usdAmountFormat2.format(
dcrToUSD(exchangeDecimal, dcr)!!.setScale(scale, BigDecimal.ROUND_HALF_EVEN).toDouble())
}

fun dcrToUSD(exchangeDecimal: BigDecimal?, dcr: Double): BigDecimal? {
val dcrDecimal = BigDecimal(dcr)
return exchangeDecimal?.multiply(dcrDecimal)
}

fun usdToDCR(exchangeDecimal: BigDecimal?, usd: Double): BigDecimal? {
if (exchangeDecimal == null) {
return null
}

val usdDecimal = BigDecimal(usd)
// using 8 to be safe
return usdDecimal.divide(exchangeDecimal, 8, RoundingMode.HALF_EVEN)
}
//fun dcrToFormattedUSD(exchangeDecimal: BigDecimal?, dcr: Double, scale: Int = 4): String {
// if (scale == 4) {
// return usdAmountFormat.format(
// dcrToUSD(exchangeDecimal, dcr)!!.setScale(scale, BigDecimal.ROUND_HALF_EVEN).toDouble())
// }
//
// return usdAmountFormat2.format(
// dcrToUSD(exchangeDecimal, dcr)!!.setScale(scale, BigDecimal.ROUND_HALF_EVEN).toDouble())
//}
//
//fun dcrToUSD(exchangeDecimal: BigDecimal?, dcr: Double): BigDecimal? {
// val dcrDecimal = BigDecimal(dcr)
// return exchangeDecimal?.multiply(dcrDecimal)
//}
//
//fun usdToDCR(exchangeDecimal: BigDecimal?, usd: Double): BigDecimal? {
// if (exchangeDecimal == null) {
// return null
// }
//
// val usdDecimal = BigDecimal(usd)
// // using 8 to be safe
// return usdDecimal.divide(exchangeDecimal, 8, RoundingMode.HALF_EVEN)
//}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import com.dcrandroid.dialog.PasswordPromptDialog
import com.dcrandroid.dialog.PinPromptDialog
import com.dcrandroid.extensions.hide
import com.dcrandroid.extensions.show
import com.dcrandroid.util.CoinFormat
import com.dcrandroid.util.PassPromptTitle
import com.dcrandroid.util.PassPromptUtil
import com.dcrandroid.util.Utils
import com.dcrandroid.util.*
import dcrlibwallet.Dcrlibwallet
import dcrlibwallet.Wallet
import kotlinx.android.synthetic.main.confirm_send_sheet.*
Expand Down Expand Up @@ -61,7 +58,7 @@ class ConfirmTransaction(private val fragmentActivity: FragmentActivity, val sen

val dcrAmount = CoinFormat.formatDecred(Dcrlibwallet.amountAtom(transactionData.dcrAmount.toDouble()))
val amountStr = if (transactionData.exchangeDecimal != null) {
val usdAmount = dcrToFormattedUSD(transactionData.exchangeDecimal, transactionData.dcrAmount.toDouble(), 2)
val usdAmount = CurrencyUtil.dcrToFormattedUSD(transactionData.exchangeDecimal, transactionData.dcrAmount.toDouble(), 2)
HtmlCompat.fromHtml(getString(R.string.x_dcr_usd, dcrAmount, usdAmount), 0)
} else {
getString(R.string.x_dcr, dcrAmount)
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/dcrandroid/dialog/send/SendDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.dcrandroid.dialog.InfoDialog
import com.dcrandroid.extensions.hide
import com.dcrandroid.extensions.show
import com.dcrandroid.util.CoinFormat
import com.dcrandroid.util.CurrencyUtil
import com.dcrandroid.util.SnackBar
import com.dcrandroid.util.Utils
import com.dcrandroid.view.util.AccountCustomSpinner
Expand Down Expand Up @@ -405,8 +406,8 @@ class SendDialog(val fragmentActivity: FragmentActivity, dismissListener: Dialog
val feeCoin = Dcrlibwallet.amountCoin(feeAtom)
val totalCostCoin = Dcrlibwallet.amountCoin(totalCostAtom)

val feeUSD = dcrToFormattedUSD(amountHelper.exchangeDecimal, feeCoin)
val totalCostUSD = dcrToFormattedUSD(amountHelper.exchangeDecimal, totalCostCoin, 2)
val feeUSD = CurrencyUtil.dcrToFormattedUSD(amountHelper.exchangeDecimal, feeCoin)
val totalCostUSD = CurrencyUtil.dcrToFormattedUSD(amountHelper.exchangeDecimal, totalCostCoin, 2)

feeSpanned = HtmlCompat.fromHtml(getString(R.string.x_dcr_usd, feeString, feeUSD), 0)
totalCostSpanned = HtmlCompat.fromHtml(getString(R.string.x_dcr_usd, totalCostString, totalCostUSD), 0)
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/dcrandroid/fragments/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ open class BaseFragment : Fragment(), SyncProgressListener, TxAndBlockNotificati
}
}

fun setToolbarSubTitle(title: CharSequence, showShadow: Boolean) {
if (activity is HomeActivity) {
val homeActivity = activity as HomeActivity
homeActivity.setToolbarSubTitle(title, showShadow)
}
}

fun refreshNavigationTabs() {
if (activity is HomeActivity) {
val homeActivity = activity as HomeActivity
Expand Down
71 changes: 66 additions & 5 deletions app/src/main/java/com/dcrandroid/fragments/OverviewFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.text.HtmlCompat
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -24,10 +25,7 @@ import com.dcrandroid.data.Constants
import com.dcrandroid.data.Transaction
import com.dcrandroid.dialog.InfoDialog
import com.dcrandroid.extensions.*
import com.dcrandroid.util.CoinFormat
import com.dcrandroid.util.Deserializer
import com.dcrandroid.util.SnackBar
import com.dcrandroid.util.SyncLayoutUtil
import com.dcrandroid.util.*
import com.google.gson.GsonBuilder
import dcrlibwallet.AccountMixerNotificationListener
import dcrlibwallet.Dcrlibwallet
Expand All @@ -40,10 +38,11 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.math.BigDecimal

const val MAX_TRANSACTIONS = 3

class OverviewFragment : BaseFragment(), ViewTreeObserver.OnScrollChangedListener, AccountMixerNotificationListener {
class OverviewFragment : BaseFragment(), ViewTreeObserver.OnScrollChangedListener, AccountMixerNotificationListener, GetExchangeRate.ExchangeRateCallback {

companion object {
private var closedBackupWarning = false
Expand All @@ -65,9 +64,13 @@ class OverviewFragment : BaseFragment(), ViewTreeObserver.OnScrollChangedListene
private lateinit var recyclerView: RecyclerView

private lateinit var balanceTextView: TextView
private lateinit var usdBalanceTextView: TextView
internal lateinit var noTransactionsTextView: TextView
internal lateinit var transactionsLayout: LinearLayout

var exchangeEnabled = true
var exchangeDecimal: BigDecimal? = null

private lateinit var syncLayout: LinearLayout
private var syncLayoutUtil: SyncLayoutUtil? = null

Expand All @@ -80,6 +83,7 @@ class OverviewFragment : BaseFragment(), ViewTreeObserver.OnScrollChangedListene
recyclerView = view.findViewById(R.id.rv_transactions)

balanceTextView = view.findViewById(R.id.tv_visible_wallet_balance)
usdBalanceTextView = view.findViewById(R.id.tv_visible_usd_wallet_balance)
noTransactionsTextView = view.findViewById(R.id.tv_no_transactions)
transactionsLayout = view.findViewById(R.id.transactions_view)

Expand All @@ -89,6 +93,8 @@ class OverviewFragment : BaseFragment(), ViewTreeObserver.OnScrollChangedListene
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

fetchExchangeRate()

adapter = TransactionListAdapter(context!!, transactions)

recyclerView.layoutManager = LinearLayoutManager(context)
Expand Down Expand Up @@ -206,17 +212,32 @@ class OverviewFragment : BaseFragment(), ViewTreeObserver.OnScrollChangedListene
}

override fun onScrollChanged() {
val totalCostAtom = multiWallet!!.totalWalletBalance()
val totalCostCoin = Dcrlibwallet.amountCoin(totalCostAtom)

if (mainBalanceIsVisible()) {
setToolbarTitle(CoinFormat.format(multiWallet!!.totalWalletBalance(), 0.7f), true)
if (isExchangeEnabled()) {
val formattedUSD = HtmlCompat.fromHtml(getString(R.string.dcr_usd, CurrencyUtil.dcrToFormattedUSD(exchangeDecimal, totalCostCoin, 2)), 0)
setToolbarSubTitle(formattedUSD, true)
}
} else {
setToolbarTitle(R.string.overview, false)
setToolbarSubTitle("", false)
}
}

private fun loadBalance() = GlobalScope.launch(Dispatchers.Main) {
balanceTextView.text = CoinFormat.format(multiWallet!!.totalWalletBalance(), 0.5f)
val totalCostAtom = multiWallet!!.totalWalletBalance()
val totalCostCoin = Dcrlibwallet.amountCoin(totalCostAtom)

if (mainBalanceIsVisible()) {
setToolbarTitle(CoinFormat.format(multiWallet!!.totalWalletBalance(), 0.7f), true)
if (isExchangeEnabled()) {
val formattedUSD = HtmlCompat.fromHtml(getString(R.string.dcr_usd, CurrencyUtil.dcrToFormattedUSD(exchangeDecimal, totalCostCoin, 2)), 0)
setToolbarSubTitle(formattedUSD, true)
}
}
}

Expand Down Expand Up @@ -323,6 +344,46 @@ class OverviewFragment : BaseFragment(), ViewTreeObserver.OnScrollChangedListene
override fun onAccountMixerStarted(walletID: Long) {
setMixerStatus()
}

private fun isExchangeEnabled(): Boolean {
val multiWallet = WalletData.multiWallet!!
val currencyConversion = multiWallet.readInt32ConfigValueForKey(Dcrlibwallet.CurrencyConversionConfigKey, Constants.DEF_CURRENCY_CONVERSION)

exchangeEnabled = currencyConversion > 0

return exchangeEnabled
}

private fun fetchExchangeRate() {
if (!isExchangeEnabled()) {
return
}

println("Getting exchange rate")
val userAgent = multiWallet!!.readStringConfigValueForKey(Dcrlibwallet.UserAgentConfigKey)
GetExchangeRate(userAgent, this).execute()
}

override fun onExchangeRateSuccess(rate: GetExchangeRate.BittrexRateParser) {
exchangeDecimal = rate.usdRate

val totalCostAtom = multiWallet!!.totalWalletBalance()
val totalCostCoin = Dcrlibwallet.amountCoin(totalCostAtom)
val formattedUSD = HtmlCompat.fromHtml(getString(R.string.dcr_usd, CurrencyUtil.dcrToFormattedUSD(exchangeDecimal, totalCostCoin, 2)), 0)
usdBalanceTextView.text = formattedUSD

GlobalScope.launch(Dispatchers.Main) {
usdBalanceTextView.show()
}
}

override fun onExchangeRateError(e: Exception) {
e.printStackTrace()

GlobalScope.launch(Dispatchers.Main) {
usdBalanceTextView.hide()
}
}
}

fun OverviewFragment.showTransactionList() = GlobalScope.launch(Dispatchers.Main) {
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/com/dcrandroid/util/CurrencyUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2018-2019 The Decred developers
* Use of this source code is governed by an ISC
* license that can be found in the LICENSE file.
*/

package com.dcrandroid.util

import java.math.BigDecimal
import java.math.RoundingMode
import java.text.DecimalFormat

val usdAmountFormat: DecimalFormat = DecimalFormat("###,###,###,###,###.####")
val usdAmountFormat2: DecimalFormat = DecimalFormat("###,###,###,###,###.##")

object CurrencyUtil {
fun dcrToFormattedUSD(exchangeDecimal: BigDecimal?, dcr: Double, scale: Int = 4): String {
if (scale == 4) {
return usdAmountFormat.format(
dcrToUSD(exchangeDecimal, dcr)!!.setScale(scale, BigDecimal.ROUND_HALF_EVEN).toDouble())
}
return usdAmountFormat2.format(
dcrToUSD(exchangeDecimal, dcr)!!.setScale(scale, BigDecimal.ROUND_HALF_EVEN).toDouble())
}

fun dcrToUSD(exchangeDecimal: BigDecimal?, dcr: Double): BigDecimal? {
val dcrDecimal = BigDecimal(dcr)
return exchangeDecimal?.multiply(dcrDecimal)
}

fun usdToDCR(exchangeDecimal: BigDecimal?, usd: Double): BigDecimal? {
if (exchangeDecimal == null) {
return null
}

val usdDecimal = BigDecimal(usd)
// using 8 to be safe
return usdDecimal.divide(exchangeDecimal, 8, RoundingMode.HALF_EVEN)
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/light_grey_border_8dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners android:radius="8dp"/>
<!-- <solid android:color="@android:color/white" />-->
<stroke android:width="1dip" android:color="#8997A5"/>
</shape>
Loading

0 comments on commit e16308d

Please sign in to comment.