Skip to content

Commit

Permalink
fix: fix alignment and visitibility issues and remove trailing zeros …
Browse files Browse the repository at this point in the history
…unless editing
  • Loading branch information
HashEngineering committed Mar 7, 2024
1 parent 236be26 commit 91aa743
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class MayaConvertCryptoFragment : Fragment(R.layout.fragment_maya_convert_crypto

if (savedInstanceState == null) {
fragment = ConvertViewFragment.newInstance()
fragment.setViewDetails(getString(R.string.get_quote), null)
fragment.setViewDetails(getString(R.string.button_continue), null)

parentFragmentManager.commit {
setReorderingAllowed(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,8 @@ class ConvertViewFragment : Fragment(R.layout.fragment_convert_currency_view) {
}

binding.currencyOptions.setOnOptionPickedListener { value, _ ->
// setAmountValue(value, viewModel.enteredConvertAmount)
setAmountValue(value)
viewModel.selectedPickerCurrencyCode = value
// viewModel.setAmount()
}
}

Expand Down Expand Up @@ -215,10 +213,10 @@ class ConvertViewFragment : Fragment(R.layout.fragment_convert_currency_view) {
try {
appendIfValidAfter(number.toString())

applyNewValue(value.toString(), binding.currencyOptions.pickedOption)
applyNewValue(value.toString(), binding.currencyOptions.pickedOption, isEditing = true)
} catch (x: Exception) {
value.deleteCharAt(value.length - 1)
applyNewValue(value.toString(), binding.currencyOptions.pickedOption)
applyNewValue(value.toString(), binding.currencyOptions.pickedOption, isEditing = true)
}
}
}
Expand All @@ -231,7 +229,7 @@ class ConvertViewFragment : Fragment(R.layout.fragment_convert_currency_view) {
value.deleteCharAt(value.length - 1)
viewModel.resetSwapValueError()
}
applyNewValue(value.toString(), binding.currencyOptions.pickedOption)
applyNewValue(value.toString(), binding.currencyOptions.pickedOption, isEditing = true)
maxAmountSelected = false
}

Expand All @@ -247,7 +245,7 @@ class ConvertViewFragment : Fragment(R.layout.fragment_convert_currency_view) {
value.append(DECIMAL_SEPARATOR)
}

applyNewValue(value.toString(), binding.currencyOptions.pickedOption)
applyNewValue(value.toString(), binding.currencyOptions.pickedOption, isEditing = true)
}

private fun appendIfValidAfter(number: String) {
Expand All @@ -262,40 +260,45 @@ class ConvertViewFragment : Fragment(R.layout.fragment_convert_currency_view) {
}
}

fun applyNewValue(value: String, currencyCode: String) {
fun applyNewValue(value: String, currencyCode: String, isEditing: Boolean = false) {
// Create a new spannable with the two strings
val newValue = value.ifEmpty { "0" }
viewModel.setEnteredAmount(newValue)

setAmountViewInfo(currencyCode, newValue)
setAmountViewInfo(currencyCode, newValue, isEditing)

val isNonZero = newValue.isNotEmpty() &&
(newValue.toBigDecimalOrNull() ?: BigDecimal.ZERO) > BigDecimal.ZERO
viewModel.updateAmounts()
checkTheUserEnteredValue(isNonZero)
}

private fun setAmountViewInfo(currencyCode: String, value: String) {
private fun setAmountViewInfo(currencyCode: String, value: String, isEditing: Boolean = false) {
binding.inputAmount.dashToFiat = currencyCode == "DASH"
val one = BigDecimal.ONE.setScale(8, RoundingMode.HALF_UP)
var currencyCodeForView = viewModel.selectedLocalCurrencyCode
var amount = value
val rate = when (currencyCode) {
"DASH" -> viewModel.amount.dashFiatExchangeRate
"DASH" -> {
if (!isEditing && value[value.length - 1] != '.') {
amount = amount.toBigDecimal().stripTrailingZeros().toString()
}
viewModel.amount.dashFiatExchangeRate
}
// Fiat
viewModel.selectedLocalCurrencyCode -> {
amount = amount.toBigDecimal().setScale(2, RoundingMode.HALF_UP).toString()
// if (balance[balance.length-1] != '.')
// amount = amount.stripTrailingZeros()

one / viewModel.account.currencyToDashExchangeRate
viewModel.amount.dashFiatExchangeRate
}
// Crypto
else -> {
currencyCodeForView = currencyCode
amount = amount.toBigDecimal().setScale(8, RoundingMode.HALF_UP).toString()
// if (balance[balance.length-1] != '.')
// amount = amount.stripTrailingZeros()
if (isEditing && value[value.length - 1] != '.') {
amount = amount.toBigDecimal().stripTrailingZeros().toPlainString()
}
viewModel.amount.cryptoDashExchangeRate
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import org.dash.wallet.common.services.analytics.AnalyticsConstants
import org.dash.wallet.common.services.analytics.AnalyticsService
import org.dash.wallet.common.util.Constants
import org.dash.wallet.common.util.GenericUtils
import org.dash.wallet.common.util.toBigDecimal
import org.dash.wallet.integrations.maya.model.AccountDataUIModel
import org.dash.wallet.integrations.maya.model.Amount
import org.dash.wallet.integrations.maya.ui.convert_currency.model.SwapRequest
Expand All @@ -47,7 +46,6 @@ import org.dash.wallet.integrations.maya.utils.MayaConstants
import org.slf4j.LoggerFactory
import java.lang.IllegalArgumentException
import java.math.BigDecimal
import java.math.BigInteger
import java.math.RoundingMode
import javax.inject.Inject

Expand Down Expand Up @@ -180,7 +178,10 @@ class ConvertViewViewModel @Inject constructor(

maxForDashCoinBaseAccount = maxCoinValue

setExchangeRates(BigDecimal.ONE.setScale(16, RoundingMode.HALF_UP) / account.currencyToDashExchangeRate, BigDecimal.ONE.setScale(16, RoundingMode.HALF_UP) / account.currencyToCryptoCurrencyExchangeRate)
setExchangeRates(
BigDecimal.ONE.setScale(16, RoundingMode.HALF_UP) / account.currencyToDashExchangeRate,
BigDecimal.ONE.setScale(16, RoundingMode.HALF_UP) / account.currencyToCryptoCurrencyExchangeRate
)
}

fun updateAmounts() {
Expand All @@ -190,15 +191,18 @@ class ConvertViewViewModel @Inject constructor(
Coin.ZERO
}
_enteredConvertDashAmount.value = dashValue
if (!dashValue.isZero) {
_selectedCryptoCurrencyAccount.value?.let {
val cryptoCurrency = amount.crypto.setScale(8, RoundingMode.HALF_UP).toString()
_enteredConvertCryptoAmount.value = Pair(cryptoCurrency, it.coinbaseAccount.currency)
}
val fiatValue = Fiat.parseFiat(selectedLocalCurrencyCode, amount.fiat.setScale(2, RoundingMode.HALF_UP).toString())
_enteredConvertFiatAmount.value = fiatValue

} else {
_selectedCryptoCurrencyAccount.value?.let {
val cryptoCurrency = amount.crypto.setScale(8, RoundingMode.HALF_UP).toString()
_enteredConvertCryptoAmount.value = Pair(cryptoCurrency, it.coinbaseAccount.currency)
}
val fiatValue = Fiat.parseFiat(
selectedLocalCurrencyCode,
amount.fiat.setScale(2, RoundingMode.HALF_UP).toString()
)
_enteredConvertFiatAmount.value = fiatValue

if (dashValue.isZero) {
resetSwapValueError()
}
}
Expand Down Expand Up @@ -393,7 +397,9 @@ class ConvertViewViewModel @Inject constructor(
"DASH" -> amount.dash
selectedLocalCurrencyCode -> amount.fiat
selectedCryptoCurrencyAccount.value!!.coinbaseAccount.currency -> amount.crypto
else -> throw IllegalArgumentException("Currency code $currencyCode is not found (DASH, $selectedLocalCurrencyCode, $selectedCryptoCurrencyAccount.value!!.coinbaseAccount.currency)")
else -> throw IllegalArgumentException(
"Currency code $currencyCode is not found (DASH, $selectedLocalCurrencyCode, $selectedCryptoCurrencyAccount.value!!.coinbaseAccount.currency)"
)
}.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class ConverterView(context: Context, attrs: AttributeSet) : ConstraintLayout(co

private fun setConvertToBtnData() {
binding.convertToBtn.setCryptoItemArrowVisibility(dashToCrypto)
binding.convertToBtn.setCryptoItemAmountVisibility(false)
if (!dashToCrypto) {
binding.convertToBtn.setCryptoItemGroupVisibility(true)
binding.convertToBtn.setConvertItemServiceName(R.string.dash_wallet_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.content.withStyledAttributes
import androidx.core.view.isVisible
import androidx.core.view.marginEnd
import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding
import coil.load
import coil.size.Scale
import coil.transform.CircleCropTransformation
Expand Down Expand Up @@ -89,14 +91,16 @@ class CryptoCurrencyItem @JvmOverloads constructor(
binding.amountFiat.isVisible = isGroupVisible
if (!isGroupVisible) {
binding.dashIconBack.isVisible = false
binding.dashIconBack.isVisible = false
binding.dashIconFront.isVisible = false
} else {
if (GenericUtils.isCurrencySymbolFirst()) {
binding.dashIconBack.isVisible = false
binding.dashIconFront.isVisible = true
binding.dashIconFront.isVisible = binding.amountFiat.text.isNotEmpty()
binding.amountCrypto.updatePadding(right = resources.getDimensionPixelOffset(R.dimen.default_horizontal_padding))
} else {
binding.dashIconBack.isVisible = true
binding.dashIconBack.isVisible = binding.amountFiat.text.isNotEmpty()
binding.dashIconFront.isVisible = false
binding.amountCrypto.updatePadding(right = resources.getDimensionPixelOffset(R.dimen.default_vertical_padding))
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions integrations/maya/src/main/res/layout/crypto_convert_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@
app:layout_constraintTop_toTopOf="@id/amount_crypto"
app:layout_constraintBottom_toBottomOf="@id/amount_crypto"
app:layout_constraintEnd_toStartOf="@id/amount_crypto"
tools:src="@drawable/ic_dash_d_black" />
android:src="@drawable/ic_dash_d_black" />

<TextView
android:id="@+id/amount_crypto"
style="@style/Body2.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@+id/dash_icon_back"
tools:text="1.00" />
tools:text="1.00"
tools:layout_marginEnd="5dp"/>

<ImageView
android:id="@+id/dash_icon_back"
Expand All @@ -141,7 +141,9 @@
app:layout_constraintTop_toTopOf="@id/amount_crypto"
app:layout_constraintBottom_toBottomOf="@id/amount_crypto"
app:layout_constraintEnd_toEndOf="parent"
tools:src="@drawable/ic_dash_d_black" />
android:visibility="gone"
tools:visibility="visible"
android:src="@drawable/ic_dash_d_black" />

<TextView
android:id="@+id/amount_fiat"
Expand All @@ -150,8 +152,8 @@
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/amount_crypto"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@id/convertFromDash_subtitle"
app:layout_constraintBaseline_toBottomOf="@id/convertFromDash_subtitle"
tools:text="$31.00" />

<androidx.constraintlayout.widget.Group
Expand Down

0 comments on commit 91aa743

Please sign in to comment.