Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add usd equivalent balance to overview page #539

Merged
merged 6 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -282,6 +282,11 @@ class HomeActivity : BaseActivity(), SyncProgressListener, TxAndBlockNotificatio

showOrHideFab(position)

// Hide title bar usd balance if not OverviewFragment
if (position > 0) {
toolbar_subtitle.visibility = View.GONE
}

adapter.changeActiveTab(position)
}

Expand All @@ -294,6 +299,15 @@ class HomeActivity : BaseActivity(), SyncProgressListener, TxAndBlockNotificatio
}
}

fun setToolbarSubTitle(subtitle: CharSequence) {
if (subtitle == "") {
toolbar_subtitle.visibility = View.GONE
} else {
toolbar_subtitle.visibility = View.VISIBLE
toolbar_subtitle.text = subtitle
}
}

fun checkWifiSync() {
if (!multiWallet!!.readBoolConfigValueForKey(Dcrlibwallet.SyncOnCellularConfigKey, Constants.DEF_SYNC_ON_CELLULAR)) {
// Check if wifi is connected
Expand Down
36 changes: 5 additions & 31 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 @@ -329,29 +328,4 @@ class AmountInputHelper(private val layout: LinearLayout, private val scrollToBo
layout.exchange_layout.show()
}
}
}

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(subtitle: CharSequence) {
if (activity is HomeActivity) {
val homeActivity = activity as HomeActivity
homeActivity.setToolbarSubTitle(subtitle)
}
}

fun refreshNavigationTabs() {
if (activity is HomeActivity) {
val homeActivity = activity as HomeActivity
Expand Down
67 changes: 62 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,12 @@ 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 exchangeDecimal: BigDecimal? = null

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

Expand All @@ -80,6 +82,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 Down Expand Up @@ -147,6 +150,8 @@ class OverviewFragment : BaseFragment(), ViewTreeObserver.OnScrollChangedListene
mixer_status_rv.adapter = MixerStatusAdapter()
setMixerStatus()
multiWallet?.setAccountMixerNotification(this)

fetchExchangeRate()
}

private fun setMixerStatus() = GlobalScope.launch(Dispatchers.Main) {
Expand Down Expand Up @@ -206,17 +211,32 @@ class OverviewFragment : BaseFragment(), ViewTreeObserver.OnScrollChangedListene
}

override fun onScrollChanged() {
val totalBalanceAtom = multiWallet!!.totalWalletBalance()
val totalBalanceCoin = Dcrlibwallet.amountCoin(totalBalanceAtom)

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

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

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

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

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

return currencyConversion > 0
}

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 totalBalanceAtom = multiWallet!!.totalWalletBalance()
val totalBalanceCoin = Dcrlibwallet.amountCoin(totalBalanceAtom)
val formattedUSD = HtmlCompat.fromHtml(getString(R.string.usd_symbol_format, CurrencyUtil.dcrToFormattedUSD(exchangeDecimal, totalBalanceCoin, 2)), 0)

GlobalScope.launch(Dispatchers.Main) {
usdBalanceTextView.text = formattedUSD
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)
}
}
5 changes: 5 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,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners android:radius="8dp"/>
<stroke android:width="0.5dp" android:color="#C4CBD2"/>
</shape>
19 changes: 17 additions & 2 deletions app/src/main/res/layout/activity_tabs.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ 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.
Expand Down Expand Up @@ -55,6 +54,22 @@
app:fontFamily="@font/source_sans_pro"
android:layout_marginStart="16dp" />

<TextView
android:id="@+id/toolbar_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:background="@drawable/light_grey_border_8dp"
android:textColor="#3D5873"
android:paddingTop="@dimen/margin_padding_size_4"
android:paddingBottom="@dimen/margin_padding_size_4"
android:paddingStart="@dimen/margin_padding_size_6"
android:paddingEnd="@dimen/margin_padding_size_6"
android:layout_marginStart="@dimen/margin_padding_size_8"
android:textSize="@dimen/edit_text_size_14"
android:visibility="gone"
app:fontFamily="@font/source_sans_pro" />

</LinearLayout>

</androidx.appcompat.widget.Toolbar>
Expand Down
Loading