From 72b0a046784b1009f1a56c1f472843674876c0f7 Mon Sep 17 00:00:00 2001 From: AmadeyKuspakov Date: Mon, 21 Aug 2023 15:51:18 +0400 Subject: [PATCH 1/9] Integrated new version of uiCoreLib (BasicInputField version upgraded) --- build.gradle | 2 +- .../components/compose/SwapSlippageScreen.kt | 138 +++++++++++------- 2 files changed, 85 insertions(+), 55 deletions(-) diff --git a/build.gradle b/build.gradle index 969252782..d6219e8de 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ buildscript { composeMaterial : '1.4.3', composeCompiler : '1.4.6', composeConstraintLayout: '1.1.0-alpha05', - uiCore : '0.1.0', + uiCore : '0.1.1', soraCard : '0.1.31', lazySodium : '5.0.2', jna : '5.8.0', diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapSlippageScreen.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapSlippageScreen.kt index db7c75f63..9f9ebad94 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapSlippageScreen.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapSlippageScreen.kt @@ -32,16 +32,17 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package jp.co.soramitsu.feature_polkaswap_impl.presentation.screens.swap +import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.background +import androidx.compose.foundation.border import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.defaultMinSize import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.wrapContentHeight import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.foundation.text.KeyboardActions -import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.runtime.Composable @@ -50,12 +51,15 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.input.ImeAction -import androidx.compose.ui.text.input.KeyboardType -import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import java.math.BigDecimal import java.text.DecimalFormatSymbols import java.util.Locale import jp.co.soramitsu.common.R @@ -63,10 +67,8 @@ import jp.co.soramitsu.ui_core.component.button.FilledButton import jp.co.soramitsu.ui_core.component.button.properties.Order import jp.co.soramitsu.ui_core.component.button.properties.Size import jp.co.soramitsu.ui_core.component.card.ContentCard -import jp.co.soramitsu.ui_core.component.input.InputText -import jp.co.soramitsu.ui_core.component.input.InputTextState -import jp.co.soramitsu.ui_core.component.input.number.CurrencyGroupingVisualTransformationSuffix -import jp.co.soramitsu.ui_core.component.input.number.onDecimalChanged +import jp.co.soramitsu.ui_core.component.input.number.BasicNumberInput +import jp.co.soramitsu.ui_core.component.input.number.CurrencyGroupingVisualTransformation import jp.co.soramitsu.ui_core.resources.Dimens import jp.co.soramitsu.ui_core.theme.borderRadius import jp.co.soramitsu.ui_core.theme.customColors @@ -82,14 +84,43 @@ internal fun SwapSlippageScreen( value: Double, onDone: (Double) -> Unit, ) { - val curValue = remember { mutableStateOf(TextFieldValue(value.toString())) } + var curDouble by remember { mutableStateOf(value) } val desc = remember { mutableStateOf(null) } + val frontrun = stringResource(id = R.string.polkaswap_slippage_frontrun) val fail = stringResource(id = R.string.polkaswap_slippage_mayfail) - val decimalFormatSymbols = remember { DecimalFormatSymbols(Locale.getDefault()) } + + val focused = remember { mutableStateOf(false) } + + val focusRequester = remember { + FocusRequester() + } + val visualTransformation = - remember { CurrencyGroupingVisualTransformationSuffix(decimalFormatSymbols, '%') } - var curDouble by remember { mutableStateOf(value) } + remember(Locale.getDefault()) { + CurrencyGroupingVisualTransformation( + decimalFormatSymbols = DecimalFormatSymbols(Locale.getDefault()), + suffix = "%" + ) + } + + val onValueChangeDecorator: (BigDecimal) -> Unit = remember { + { valueAsBigDecimal -> + val valueAsDouble = valueAsBigDecimal.toDouble() + when { + valueAsDouble < minFail -> { + desc.value = fail + } + valueAsDouble > maxFrontrun -> { + desc.value = frontrun + } + else -> { + desc.value = null + } + } + } + } + Column(modifier = Modifier.fillMaxSize()) { ContentCard( modifier = Modifier @@ -103,52 +134,51 @@ internal fun SwapSlippageScreen( .fillMaxWidth() .wrapContentHeight() ) { - InputText( + Column( modifier = Modifier - .background( + .focusRequester(focusRequester) + .onFocusChanged { + focused.value = it.isFocused + }.border( + border = BorderStroke( + width = 1.dp, + color = if (focused.value) MaterialTheme.customColors.fgPrimary + else MaterialTheme.customColors.fgOutline + ), + shape = RoundedCornerShape(MaterialTheme.borderRadius.ml) + ).background( color = MaterialTheme.customColors.bgSurface, shape = RoundedCornerShape(MaterialTheme.borderRadius.ml) - ) - .fillMaxWidth() - .wrapContentHeight(), - maxLines = 1, - visualTransformation = visualTransformation, - singleLine = true, - state = InputTextState(value = curValue.value, descriptionText = desc.value), - onValueChange = { - val result = onDecimalChanged( - it.text, - 2, - decimalFormatSymbols.decimalSeparator, - decimalFormatSymbols.groupingSeparator, - decimalFormatSymbols.minusSign, - ) - if (result != null) { - curValue.value = it.copy(text = result.first) - curDouble = result.second.toDouble() - when { - curDouble < minFail -> { - desc.value = fail - } - curDouble > maxFrontrun -> { - desc.value = frontrun - } - else -> { - desc.value = null - } + ).clip(RoundedCornerShape(MaterialTheme.borderRadius.ml)) + .padding(vertical = Dimens.x1_2, horizontal = Dimens.x2) + .defaultMinSize(minHeight = Dimens.InputHeight) + .wrapContentHeight() + .fillMaxWidth(), + ) { + BasicNumberInput( + modifier = Modifier, + textStyle = MaterialTheme.customTypography.textM, + initial = value.toBigDecimal(), + precision = 2, + enabled = true, + visualTransformation = visualTransformation, + onValueChanged = onValueChangeDecorator, + onKeyboardDone = remember { + { + onDone(curDouble.coerceIn(min, max)) } } - }, - keyboardActions = KeyboardActions( - onDone = { - onDone(curDouble.coerceIn(min, max)) - } - ), - keyboardOptions = KeyboardOptions.Default.copy( - imeAction = ImeAction.Done, - keyboardType = KeyboardType.Decimal - ), - ) + ) + + val currentDescriptionValue = desc.value + + if (currentDescriptionValue != null) + Text( + text = currentDescriptionValue, + color = MaterialTheme.customColors.fgSecondary, + style = MaterialTheme.customTypography.textXS + ) + } Text( modifier = Modifier .padding(Dimens.x2) From 5f8dd7c8522f9f3a40a8478544f2beb4853298bd Mon Sep 17 00:00:00 2001 From: AmadeyKuspakov Date: Thu, 31 Aug 2023 02:01:56 +0400 Subject: [PATCH 2/9] Integrated new ui_lib changes into screens components --- .../common/domain/AssetAmountInputState.kt | 1 - .../compose/components/AssetAmount.kt | 7 ++- .../receiverequest/QRCodeFlowViewModel.kt | 9 ++-- .../screens/send/TransferAmountViewModel.kt | 18 ++++--- .../compose/LiquidityAddConfirmScreen.kt | 4 +- .../compose/LiquidityRemoveConfirmScreen.kt | 4 +- .../components/compose/SwapConfirmScreen.kt | 4 +- .../liquidityadd/LiquidityAddViewModel.kt | 28 +++++------ .../LiquidityRemoveViewModel.kt | 26 +++++----- .../screens/swap/SwapViewModel.kt | 47 ++++++++----------- 10 files changed, 64 insertions(+), 84 deletions(-) diff --git a/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt b/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt index 5af001a34..a737cdd41 100644 --- a/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt +++ b/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt @@ -37,7 +37,6 @@ import java.math.BigDecimal data class AssetAmountInputState( val token: Token, val balance: String, - val amount: BigDecimal, val initialAmount: BigDecimal? = null, val amountFiat: String, val enabled: Boolean, diff --git a/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt b/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt index dab7539db..8515aca3a 100644 --- a/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt +++ b/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt @@ -68,6 +68,7 @@ import jp.co.soramitsu.common.R import jp.co.soramitsu.common.domain.AssetAmountInputState import jp.co.soramitsu.common.domain.OptionsProvider import jp.co.soramitsu.common.presentation.compose.TokenIcon +import jp.co.soramitsu.common.util.ext.orZero import jp.co.soramitsu.common.util.ext.testTagAsId import jp.co.soramitsu.ui_core.component.button.properties.Size import jp.co.soramitsu.ui_core.component.input.number.BasicNumberInput @@ -215,7 +216,6 @@ val previewAssetAmountInputState = AssetAmountInputState( token = previewToken, balance = "10.234 ($2.234.23)", amountFiat = "$2.342.12", - amount = BigDecimal.ZERO, initialAmount = null, enabled = true, error = false, @@ -236,7 +236,6 @@ private fun PreviewAssetAmountInput() { val state = remember { mutableStateOf(previewAssetAmountInputState) } Button(onClick = { state.value = state.value.copy( - amount = bb.value, initialAmount = bb.value, ) bb.value = bb.value.plus(BigDecimal.ONE) @@ -248,14 +247,14 @@ private fun PreviewAssetAmountInput() { state = state.value, onAmountChange = { state.value = state.value.copy( - amount = it, + initialAmount = it, ) }, onSelectToken = {}, onFocusChange = {}, ) Spacer(modifier = Modifier.size(10.dp)) - Text(text = state.value.amount.toPlainString()) + Text(text = state.value.initialAmount.orZero().toPlainString()) AssetAmountInput( modifier = Modifier, state = previewAssetAmountInputState, diff --git a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt index 8d919e905..04f91d7ae 100644 --- a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt +++ b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt @@ -191,10 +191,9 @@ class QRCodeFlowViewModel @AssistedInject constructor( nf = numbersFormatter, precision = DEFAULT_TOKEN_PRINT_PRECISION ), - amount = BigDecimal.ZERO, initialAmount = null, amountFiat = "", - enabled = false, + enabled = true, ) ) }.catch { @@ -303,7 +302,7 @@ class QRCodeFlowViewModel @AssistedInject constructor( fun onRequestAmountChange(amount: BigDecimal) { requestTokenByQrScreenState = requestTokenByQrScreenState.copy( assetAmountInputState = requestTokenByQrScreenState.assetAmountInputState?.copy( - amount = amount, + initialAmount = amount, amountFiat = requestTokenByQrScreenState.assetAmountInputState?.token?.printFiat( amount, numbersFormatter @@ -427,7 +426,7 @@ class QRCodeFlowViewModel @AssistedInject constructor( tokenId = requestTokenByQrScreenState.assetAmountInputState ?.token?.id, amount = requestTokenByQrScreenState.assetAmountInputState - ?.amount.toString() + ?.initialAmount.toString() ) ) } @@ -444,7 +443,7 @@ class QRCodeFlowViewModel @AssistedInject constructor( assetAmountInputState = requestTokenByQrScreenState .assetAmountInputState?.copy( initialAmount = requestTokenByQrScreenState - .assetAmountInputState?.amount, + .assetAmountInputState?.initialAmount, enabled = false, readOnly = true ) diff --git a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt index 728f76ad4..9a246b32f 100644 --- a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt +++ b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt @@ -143,7 +143,7 @@ class TransferAmountViewModel @AssistedInject constructor( calcTransactionFee(asset) } } - sendState.input?.amount?.let { + sendState.input?.initialAmount?.let { checkEnteredAmount(it) } } @@ -171,7 +171,6 @@ class TransferAmountViewModel @AssistedInject constructor( input = AssetAmountInputState( token = asset.token, balance = getAssetBalanceText(asset), - amount = initialSendAmount?.toBigDecimalOrNull() ?: BigDecimal.ZERO, initialAmount = initialSendAmount?.toBigDecimalOrNull(), amountFiat = "", enabled = false, @@ -185,7 +184,7 @@ class TransferAmountViewModel @AssistedInject constructor( token = asset.token, balance = getAssetBalanceText(asset), amountFiat = asset.token.printFiat( - sendState.input?.amount.orZero(), + sendState.input?.initialAmount.orZero(), numbersFormatter ), ) @@ -201,7 +200,7 @@ class TransferAmountViewModel @AssistedInject constructor( val result = interactor.isNotEnoughXorLeftAfterTransaction( networkFeeInXor = fee.orZero(), - xorChange = if (token.id == SubstrateOptionsProvider.feeAssetId) amount else null, + xorChange = if (token.id == SubstrateOptionsProvider.feeAssetId) initialAmount else null, ) sendState = sendState.copy( @@ -212,7 +211,7 @@ class TransferAmountViewModel @AssistedInject constructor( fun onTokenChange(tokenId: String) { curTokenId = tokenId updateCurAsset() - sendState.input?.amount?.let { + sendState.input?.initialAmount?.let { checkEnteredAmount(it) } hasXorReminderWarningBeenChecked = false @@ -230,7 +229,7 @@ class TransferAmountViewModel @AssistedInject constructor( sendState = sendState.copy( input = sendState.input?.copy( readOnly = false, - initialAmount = sendState.input?.amount?.nullZero() + initialAmount = sendState.input?.initialAmount?.nullZero() ), ) } @@ -292,7 +291,7 @@ class TransferAmountViewModel @AssistedInject constructor( fun onConfirmClick() { val curAsset = curAsset ?: return - val amount = sendState.input?.amount ?: return + val amount = sendState.input?.initialAmount ?: return val fee = fee ?: return viewModelScope.launch { sendState = sendState.copy( @@ -390,7 +389,7 @@ class TransferAmountViewModel @AssistedInject constructor( fun onReviewClick() { sendState = sendState.copy( input = sendState.input?.copy( - initialAmount = sendState.input?.amount.orZero() + initialAmount = sendState.input?.initialAmount.orZero() ) ) } @@ -398,7 +397,7 @@ class TransferAmountViewModel @AssistedInject constructor( fun amountChanged(value: BigDecimal) { sendState = sendState.copy( input = sendState.input?.copy( - amount = value, + initialAmount = value, amountFiat = sendState.input?.token?.printFiat( value, numbersFormatter @@ -421,7 +420,6 @@ class TransferAmountViewModel @AssistedInject constructor( } sendState = sendState.copy( input = sendState.input?.copy( - amount = amount, initialAmount = amount, amountFiat = sendState.input?.token?.printFiat( amount, diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityAddConfirmScreen.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityAddConfirmScreen.kt index 3a6c93bac..86312a87e 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityAddConfirmScreen.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityAddConfirmScreen.kt @@ -117,14 +117,14 @@ internal fun LiquidityAddConfirmScreen( SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.assetState1?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.assetState1?.amount?.toPlainString().orEmpty(), + amount = state.assetState1?.initialAmount?.toPlainString().orEmpty(), amountFiat = state.assetState1?.amountFiat.orEmpty(), ) Divider(color = Color.Transparent, modifier = Modifier.width(Dimens.x1)) SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.assetState2?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.assetState2?.amount?.toPlainString().orEmpty(), + amount = state.assetState2?.initialAmount?.toPlainString().orEmpty(), amountFiat = state.assetState2?.amountFiat.orEmpty(), ) } diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityRemoveConfirmScreen.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityRemoveConfirmScreen.kt index 8044b126f..e852642fe 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityRemoveConfirmScreen.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityRemoveConfirmScreen.kt @@ -116,14 +116,14 @@ internal fun LiquidityRemoveConfirmScreen( SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.assetState1?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.assetState1?.amount?.toPlainString().orEmpty(), + amount = state.assetState1?.initialAmount?.toPlainString().orEmpty(), amountFiat = state.assetState1?.amountFiat.orEmpty(), ) Divider(color = Color.Transparent, modifier = Modifier.width(Dimens.x1)) SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.assetState2?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.assetState2?.amount?.toPlainString().orEmpty(), + amount = state.assetState2?.initialAmount?.toPlainString().orEmpty(), amountFiat = state.assetState2?.amountFiat.orEmpty(), ) } diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapConfirmScreen.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapConfirmScreen.kt index f11e86447..a0216dc33 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapConfirmScreen.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapConfirmScreen.kt @@ -117,14 +117,14 @@ internal fun SwapConfirmScreen( SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.tokenFromState?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.tokenFromState?.amount?.toPlainString().orEmpty(), + amount = state.tokenFromState?.initialAmount?.toPlainString().orEmpty(), amountFiat = state.tokenFromState?.amountFiat.orEmpty(), ) Divider(color = Color.Transparent, modifier = Modifier.width(Dimens.x1)) SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.tokenToState?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.tokenToState?.amount?.toPlainString().orEmpty(), + amount = state.tokenToState?.initialAmount?.toPlainString().orEmpty(), amountFiat = state.tokenToState?.amountFiat.orEmpty(), ) } diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt index 3823f855c..043921fd9 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt @@ -54,6 +54,7 @@ import jp.co.soramitsu.common.util.NumbersFormatter import jp.co.soramitsu.common.util.ext.isZero import jp.co.soramitsu.common.util.ext.lazyAsync import jp.co.soramitsu.common.util.ext.nullZero +import jp.co.soramitsu.common.util.ext.orZero import jp.co.soramitsu.common.view.ViewHelper import jp.co.soramitsu.common_wallet.domain.model.LiquidityData import jp.co.soramitsu.common_wallet.domain.model.WithDesired @@ -393,7 +394,6 @@ class LiquidityAddViewModel @AssistedInject constructor( if (desired == WithDesired.INPUT) { addState = addState.copy( assetState2 = addState.assetState2?.copy( - amount = it, initialAmount = it, amountFiat = addState.assetState2?.token?.printFiat( it, @@ -406,7 +406,6 @@ class LiquidityAddViewModel @AssistedInject constructor( } else { addState = addState.copy( assetState1 = addState.assetState1?.copy( - amount = it, initialAmount = it, amountFiat = addState.assetState1?.token?.printFiat( it, @@ -488,7 +487,7 @@ class LiquidityAddViewModel @AssistedInject constructor( val result = assetsInteractor.isNotEnoughXorLeftAfterTransaction( networkFeeInXor = networkFee, - xorChange = if (assetState1.token.id == SubstrateOptionsProvider.feeAssetId) assetState1.amount else null, + xorChange = if (assetState1.token.id == SubstrateOptionsProvider.feeAssetId) assetState1.initialAmount else null, ) addState = addState.copy( @@ -507,10 +506,10 @@ class LiquidityAddViewModel @AssistedInject constructor( fullList = mapAssetsToCardState(list, numbersFormatter), ), assetState1 = addState.assetState1?.copy( - initialAmount = addState.assetState1?.amount?.nullZero(), + initialAmount = addState.assetState1?.initialAmount?.nullZero(), ), assetState2 = addState.assetState2?.copy( - initialAmount = addState.assetState2?.amount?.nullZero(), + initialAmount = addState.assetState2?.initialAmount?.nullZero(), ), ) } @@ -549,10 +548,10 @@ class LiquidityAddViewModel @AssistedInject constructor( fullList = mapAssetsToCardState(list, numbersFormatter), ), assetState1 = addState.assetState1?.copy( - initialAmount = addState.assetState1?.amount?.nullZero(), + initialAmount = addState.assetState1?.initialAmount?.nullZero(), ), assetState2 = addState.assetState2?.copy( - initialAmount = addState.assetState2?.amount?.nullZero(), + initialAmount = addState.assetState2?.initialAmount?.nullZero(), ), ) } @@ -567,7 +566,7 @@ class LiquidityAddViewModel @AssistedInject constructor( assetState1 = state.copy( token = a.token, balance = getAssetBalanceText(a), - amountFiat = a.token.printFiat(state.amount, numbersFormatter), + amountFiat = a.token.printFiat(state.initialAmount.orZero(), numbersFormatter), ), ) hasXorReminderWarningBeenChecked = false @@ -583,7 +582,7 @@ class LiquidityAddViewModel @AssistedInject constructor( assetState2 = state?.copy( token = a.token, balance = getAssetBalanceText(a), - amountFiat = a.token.printFiat(state.amount, numbersFormatter), + amountFiat = a.token.printFiat(state.initialAmount.orZero(), numbersFormatter), ) ?: buildInitialAssetState(a), ) @@ -712,10 +711,10 @@ class LiquidityAddViewModel @AssistedInject constructor( fun onSlippageClick() { addState = addState.copy( assetState1 = addState.assetState1?.copy( - initialAmount = addState.assetState1?.amount?.nullZero(), + initialAmount = addState.assetState1?.initialAmount?.nullZero(), ), assetState2 = addState.assetState2?.copy( - initialAmount = addState.assetState2?.amount?.nullZero(), + initialAmount = addState.assetState2?.initialAmount?.nullZero(), ), ) } @@ -730,7 +729,7 @@ class LiquidityAddViewModel @AssistedInject constructor( fun onAmount1Change(value: BigDecimal) { addState = addState.copy( assetState1 = addState.assetState1?.copy( - amount = value, + initialAmount = value, amountFiat = addState.assetState1?.token?.printFiat( value, numbersFormatter @@ -743,7 +742,7 @@ class LiquidityAddViewModel @AssistedInject constructor( fun onAmount2Change(value: BigDecimal) { addState = addState.copy( assetState2 = addState.assetState2?.copy( - amount = value, + initialAmount = value, amountFiat = addState.assetState2?.token?.printFiat( value, numbersFormatter @@ -781,7 +780,6 @@ class LiquidityAddViewModel @AssistedInject constructor( addState = addState.copy( assetState1 = addState.assetState1?.copy( amountFiat = tokenFrom.printFiat(amount, numbersFormatter), - amount = amount, initialAmount = amount, ) ) @@ -796,7 +794,6 @@ class LiquidityAddViewModel @AssistedInject constructor( addState = addState.copy( assetState2 = addState.assetState2?.copy( amountFiat = tokenTo.printFiat(amount, numbersFormatter), - amount = amount, initialAmount = amount, ) ) @@ -876,7 +873,6 @@ class LiquidityAddViewModel @AssistedInject constructor( private fun buildInitialAssetState(a: Asset) = AssetAmountInputState( token = a.token, balance = getAssetBalanceText(a), - amount = BigDecimal.ZERO, initialAmount = null, amountFiat = "", enabled = true, diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt index 645f69337..2a3c4249c 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt @@ -198,7 +198,6 @@ class LiquidityRemoveViewModel @AssistedInject constructor( assetState1 = AssetAmountInputState( token = asset1.token, balance = getAssetBalanceText(asset1), - amount = BigDecimal.ZERO, initialAmount = null, amountFiat = "", enabled = true, @@ -210,7 +209,6 @@ class LiquidityRemoveViewModel @AssistedInject constructor( assetState2 = AssetAmountInputState( token = asset2.token, balance = getAssetBalanceText(asset2), - amount = BigDecimal.ZERO, initialAmount = null, amountFiat = "", enabled = true, @@ -428,10 +426,10 @@ class LiquidityRemoveViewModel @AssistedInject constructor( fun onSlippageClick() { removeState = removeState.copy( assetState1 = removeState.assetState1?.copy( - initialAmount = removeState.assetState1?.amount?.nullZero(), + initialAmount = removeState.assetState1?.initialAmount?.nullZero(), ), assetState2 = removeState.assetState2?.copy( - initialAmount = removeState.assetState2?.amount?.nullZero(), + initialAmount = removeState.assetState2?.initialAmount?.nullZero(), ), ) } @@ -445,11 +443,11 @@ class LiquidityRemoveViewModel @AssistedInject constructor( fun onAmount1Change(value: BigDecimal) { removeState = removeState.copy( assetState1 = removeState.assetState1?.copy( - amount = value, amountFiat = removeState.assetState1?.token?.printFiat( value, numbersFormatter - ).orEmpty() + ).orEmpty(), + initialAmount = value ) ) amount1Flow.value = value @@ -458,11 +456,11 @@ class LiquidityRemoveViewModel @AssistedInject constructor( fun onAmount2Change(value: BigDecimal) { removeState = removeState.copy( assetState2 = removeState.assetState2?.copy( - amount = value, amountFiat = removeState.assetState2?.token?.printFiat( value, numbersFormatter - ).orEmpty() + ).orEmpty(), + initialAmount = value ) ) amount2Flow.value = value @@ -493,12 +491,12 @@ class LiquidityRemoveViewModel @AssistedInject constructor( poolDataUsable?.let { poolData -> val firstAmountMin = PolkaswapFormulas.calculateMinAmount( - fromToken.amount, + fromToken.initialAmount.orZero(), removeState.slippage ) val secondAmountMin = PolkaswapFormulas.calculateMinAmount( - toToken.amount, + toToken.initialAmount.orZero(), removeState.slippage ) val desired = @@ -623,10 +621,9 @@ class LiquidityRemoveViewModel @AssistedInject constructor( shareOfPool = newPoolShare, ) ) - if (amount1.compareTo(removeState.assetState1?.amount) != 0) { + if (amount1.compareTo(removeState.assetState1?.initialAmount) != 0) { removeState = removeState.copy( assetState1 = removeState.assetState1?.copy( - amount = amount1, initialAmount = amount1, amountFiat = removeState.assetState1?.token?.printFiat( amount1, @@ -635,10 +632,9 @@ class LiquidityRemoveViewModel @AssistedInject constructor( ) ) } - if (amount2.compareTo(removeState.assetState2?.amount) != 0) { + if (amount2.compareTo(removeState.assetState2?.initialAmount) != 0) { removeState = removeState.copy( assetState2 = removeState.assetState2?.copy( - amount = amount2, initialAmount = amount2, amountFiat = removeState.assetState2?.token?.printFiat( amount2, @@ -691,7 +687,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( val result = assetsInteractor.isNotEnoughXorLeftAfterTransaction( networkFeeInXor = networkFee.orZero(), - xorChange = if (assetState1.token.id == SubstrateOptionsProvider.feeAssetId) -assetState1.amount else null, + xorChange = if (assetState1.token.id == SubstrateOptionsProvider.feeAssetId) -assetState1.initialAmount.orZero() else null, ) removeState = removeState.copy( diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt index 3eae9450a..824d6b2ad 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt @@ -180,9 +180,9 @@ class SwapViewModel @AssistedInject constructor( private var amountFromPrev: BigDecimal = BigDecimal.ZERO private var amountToPrev: BigDecimal = BigDecimal.ZERO private val amountFrom: BigDecimal - get() = _swapMainState.value.tokenFromState?.amount.orZero() + get() = _swapMainState.value.tokenFromState?.initialAmount.orZero() private val amountTo: BigDecimal - get() = _swapMainState.value.tokenToState?.amount.orZero() + get() = _swapMainState.value.tokenToState?.initialAmount.orZero() override fun startScreen(): String = SwapRoutes.start @@ -241,11 +241,11 @@ class SwapViewModel @AssistedInject constructor( .onEach { _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( - amount = BigDecimal.ZERO, + initialAmount = BigDecimal.ZERO, amountFiat = "", ), tokenToState = _swapMainState.value.tokenToState?.copy( - amount = BigDecimal.ZERO, + initialAmount = BigDecimal.ZERO, amountFiat = "", ), details = defaultSwapDetailsState(), @@ -280,7 +280,6 @@ class SwapViewModel @AssistedInject constructor( tokenFromState = AssetAmountInputState( token = assetFrom.token, balance = getAssetBalanceText(assetFrom), - amount = BigDecimal.ZERO, initialAmount = null, amountFiat = "", enabled = false, @@ -295,7 +294,6 @@ class SwapViewModel @AssistedInject constructor( tokenToState = AssetAmountInputState( token = assetTo.token, balance = getAssetBalanceText(assetTo), - amount = BigDecimal.ZERO, initialAmount = null, amountFiat = "", enabled = false, @@ -395,10 +393,10 @@ class SwapViewModel @AssistedInject constructor( fun onSlippageClick() { _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.amount?.nullZero(), + initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.amount?.nullZero(), + initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), ), ) } @@ -407,10 +405,10 @@ class SwapViewModel @AssistedInject constructor( _swapMainState.value = _swapMainState.value.copy( selectMarketState = _swapMainState.value.market to availableMarkets, tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.amount?.nullZero(), + initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.amount?.nullZero(), + initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), ), ) } @@ -426,10 +424,10 @@ class SwapViewModel @AssistedInject constructor( ) ), tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.amount?.nullZero(), + initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.amount?.nullZero(), + initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), ), ) } @@ -446,10 +444,10 @@ class SwapViewModel @AssistedInject constructor( ) ), tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.amount?.nullZero(), + initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.amount?.nullZero(), + initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), ), ) } @@ -460,9 +458,9 @@ class SwapViewModel @AssistedInject constructor( if (tokenFromState == null || tokenToState == null) return@with val change = if (tokenFromState.token.id == SubstrateOptionsProvider.feeAssetId) { - tokenFromState.amount + tokenFromState.initialAmount } else if (tokenToState.token.id == SubstrateOptionsProvider.feeAssetId) { - -tokenToState.amount + -tokenToState.initialAmount.orZero() } else { null } val result = assetsInteractor.isNotEnoughXorLeftAfterTransaction( networkFeeInXor = networkFee.orZero(), @@ -499,10 +497,10 @@ class SwapViewModel @AssistedInject constructor( desired = if (desired == WithDesired.INPUT) WithDesired.OUTPUT else WithDesired.INPUT _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.amount?.nullZero() + initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero() ), tokenToState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.amount?.nullZero() + initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero() ), ) setSwapButtonLoading(true) @@ -516,7 +514,6 @@ class SwapViewModel @AssistedInject constructor( tokenToState = AssetAmountInputState( token = token, balance = getAssetBalanceText(asset), - amount = amountTo, initialAmount = amountTo.nullZero(), amountFiat = token.printFiat(amountTo, numbersFormatter), enabled = true, @@ -530,7 +527,6 @@ class SwapViewModel @AssistedInject constructor( tokenFromState = AssetAmountInputState( token = token, balance = getAssetBalanceText(asset), - amount = amountFrom, initialAmount = amountFrom.nullZero(), amountFiat = token.printFiat(amountFrom, numbersFormatter), enabled = _swapMainState.value.tokenToState != null, @@ -710,7 +706,6 @@ class SwapViewModel @AssistedInject constructor( it, numbersFormatter ), - amount = it, initialAmount = it, ) ) @@ -721,7 +716,6 @@ class SwapViewModel @AssistedInject constructor( it, numbersFormatter ), - amount = it, initialAmount = it, ) ) @@ -827,7 +821,7 @@ class SwapViewModel @AssistedInject constructor( fun onFromAmountChange(value: BigDecimal) { _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( - amount = value, + initialAmount = value, amountFiat = _swapMainState.value.tokenFromState?.token?.printFiat( value, numbersFormatter @@ -841,7 +835,7 @@ class SwapViewModel @AssistedInject constructor( fun onToAmountChange(value: BigDecimal) { _swapMainState.value = _swapMainState.value.copy( tokenToState = _swapMainState.value.tokenToState?.copy( - amount = value, + initialAmount = value, amountFiat = _swapMainState.value.tokenToState?.token?.printFiat( value, numbersFormatter @@ -929,12 +923,12 @@ class SwapViewModel @AssistedInject constructor( fromState.token, toState.token, desired, - if (desired == WithDesired.INPUT) fromState.amount else toState.amount, + if (desired == WithDesired.INPUT) fromState.initialAmount.orZero() else toState.initialAmount.orZero(), details.minmax, details.networkFee, details.liquidityFee, details.dex.dexId, - if (desired == WithDesired.OUTPUT) fromState.amount else toState.amount, + if (desired == WithDesired.OUTPUT) fromState.initialAmount.orZero() else toState.initialAmount.orZero(), ) } catch (t: Throwable) { onError(t) @@ -984,7 +978,6 @@ class SwapViewModel @AssistedInject constructor( _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( amountFiat = fromAsset.token.printFiat(amount, numbersFormatter), - amount = amount, initialAmount = amount, ) ) From aa4b03c5ef28f3c84d29cd6edf7b8d602f2afeb2 Mon Sep 17 00:00:00 2001 From: AmadeyKuspakov Date: Thu, 31 Aug 2023 02:01:56 +0400 Subject: [PATCH 3/9] Integrated new ui_lib changes into screens components --- build.gradle | 2 +- .../common/domain/AssetAmountInputState.kt | 3 +- .../compose/components/AssetAmount.kt | 9 ++-- .../receiverequest/QRCodeFlowViewModel.kt | 7 ++-- .../screens/send/TransferAmountViewModel.kt | 8 ++-- .../qr/QrCodeFlowViewModelTest.kt | 2 +- .../liquidityadd/LiquidityAddViewModel.kt | 24 +++++------ .../LiquidityRemoveViewModel.kt | 26 +++++------- .../screens/swap/SwapViewModel.kt | 42 ++++++++----------- 9 files changed, 51 insertions(+), 72 deletions(-) diff --git a/build.gradle b/build.gradle index 50741096b..65b2c69be 100644 --- a/build.gradle +++ b/build.gradle @@ -45,7 +45,7 @@ buildscript { composeCompiler : '1.4.6', composeConstraintLayout: '1.1.0-alpha05', uiCore : '0.1.1', - soraCard : '0.1.33', + soraCard : '0.1.35', lazySodium : '5.0.2', jna : '5.8.0', accompanist : '0.30.1', diff --git a/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt b/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt index 5af001a34..304ae73ac 100644 --- a/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt +++ b/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt @@ -37,8 +37,7 @@ import java.math.BigDecimal data class AssetAmountInputState( val token: Token, val balance: String, - val amount: BigDecimal, - val initialAmount: BigDecimal? = null, + val amount: BigDecimal? = null, val amountFiat: String, val enabled: Boolean, val readOnly: Boolean = false, diff --git a/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt b/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt index dab7539db..2892c6013 100644 --- a/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt +++ b/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt @@ -68,6 +68,7 @@ import jp.co.soramitsu.common.R import jp.co.soramitsu.common.domain.AssetAmountInputState import jp.co.soramitsu.common.domain.OptionsProvider import jp.co.soramitsu.common.presentation.compose.TokenIcon +import jp.co.soramitsu.common.util.ext.orZero import jp.co.soramitsu.common.util.ext.testTagAsId import jp.co.soramitsu.ui_core.component.button.properties.Size import jp.co.soramitsu.ui_core.component.input.number.BasicNumberInput @@ -157,7 +158,7 @@ fun AssetAmountInput( textStyle = MaterialTheme.customTypography.displayS.copy(textAlign = TextAlign.End), enabled = state?.let { it.enabled && !it.readOnly } ?: false, precision = state?.token?.precision ?: OptionsProvider.defaultScale, - initial = state?.initialAmount, + initial = state?.amount, onValueChanged = onAmountChange, focusRequester = focusRequester, textFieldColors = TextFieldDefaults.textFieldColors( @@ -215,8 +216,7 @@ val previewAssetAmountInputState = AssetAmountInputState( token = previewToken, balance = "10.234 ($2.234.23)", amountFiat = "$2.342.12", - amount = BigDecimal.ZERO, - initialAmount = null, + amount = null, enabled = true, error = false, errorHint = "", @@ -237,7 +237,6 @@ private fun PreviewAssetAmountInput() { Button(onClick = { state.value = state.value.copy( amount = bb.value, - initialAmount = bb.value, ) bb.value = bb.value.plus(BigDecimal.ONE) }) { @@ -255,7 +254,7 @@ private fun PreviewAssetAmountInput() { onFocusChange = {}, ) Spacer(modifier = Modifier.size(10.dp)) - Text(text = state.value.amount.toPlainString()) + Text(text = state.value.amount.orZero().toPlainString()) AssetAmountInput( modifier = Modifier, state = previewAssetAmountInputState, diff --git a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt index 8d919e905..072268f2f 100644 --- a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt +++ b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt @@ -191,10 +191,9 @@ class QRCodeFlowViewModel @AssistedInject constructor( nf = numbersFormatter, precision = DEFAULT_TOKEN_PRINT_PRECISION ), - amount = BigDecimal.ZERO, - initialAmount = null, + amount = null, amountFiat = "", - enabled = false, + enabled = true, ) ) }.catch { @@ -443,7 +442,7 @@ class QRCodeFlowViewModel @AssistedInject constructor( .untransformedUserAddress, assetAmountInputState = requestTokenByQrScreenState .assetAmountInputState?.copy( - initialAmount = requestTokenByQrScreenState + amount = requestTokenByQrScreenState .assetAmountInputState?.amount, enabled = false, readOnly = true diff --git a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt index 728f76ad4..9f0efe8c7 100644 --- a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt +++ b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt @@ -171,8 +171,7 @@ class TransferAmountViewModel @AssistedInject constructor( input = AssetAmountInputState( token = asset.token, balance = getAssetBalanceText(asset), - amount = initialSendAmount?.toBigDecimalOrNull() ?: BigDecimal.ZERO, - initialAmount = initialSendAmount?.toBigDecimalOrNull(), + amount = initialSendAmount?.toBigDecimalOrNull(), amountFiat = "", enabled = false, error = false, @@ -230,7 +229,7 @@ class TransferAmountViewModel @AssistedInject constructor( sendState = sendState.copy( input = sendState.input?.copy( readOnly = false, - initialAmount = sendState.input?.amount?.nullZero() + amount = sendState.input?.amount?.nullZero() ), ) } @@ -390,7 +389,7 @@ class TransferAmountViewModel @AssistedInject constructor( fun onReviewClick() { sendState = sendState.copy( input = sendState.input?.copy( - initialAmount = sendState.input?.amount.orZero() + amount = sendState.input?.amount.orZero() ) ) } @@ -422,7 +421,6 @@ class TransferAmountViewModel @AssistedInject constructor( sendState = sendState.copy( input = sendState.input?.copy( amount = amount, - initialAmount = amount, amountFiat = sendState.input?.token?.printFiat( amount, numbersFormatter diff --git a/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt b/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt index 1282b4e1b..924cce6ce 100644 --- a/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt +++ b/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt @@ -187,7 +187,7 @@ class QrCodeFlowViewModelTest { Assert.assertEquals( viewModel.requestTokenByQrScreenState.assetAmountInputState?.amount, - viewModel.requestTokenConfirmScreenState.assetAmountInputState?.initialAmount + viewModel.requestTokenConfirmScreenState.assetAmountInputState?.amount ) } diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt index 3823f855c..9a4fcb033 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt @@ -54,6 +54,7 @@ import jp.co.soramitsu.common.util.NumbersFormatter import jp.co.soramitsu.common.util.ext.isZero import jp.co.soramitsu.common.util.ext.lazyAsync import jp.co.soramitsu.common.util.ext.nullZero +import jp.co.soramitsu.common.util.ext.orZero import jp.co.soramitsu.common.view.ViewHelper import jp.co.soramitsu.common_wallet.domain.model.LiquidityData import jp.co.soramitsu.common_wallet.domain.model.WithDesired @@ -394,7 +395,6 @@ class LiquidityAddViewModel @AssistedInject constructor( addState = addState.copy( assetState2 = addState.assetState2?.copy( amount = it, - initialAmount = it, amountFiat = addState.assetState2?.token?.printFiat( it, numbersFormatter @@ -407,7 +407,6 @@ class LiquidityAddViewModel @AssistedInject constructor( addState = addState.copy( assetState1 = addState.assetState1?.copy( amount = it, - initialAmount = it, amountFiat = addState.assetState1?.token?.printFiat( it, numbersFormatter @@ -507,10 +506,10 @@ class LiquidityAddViewModel @AssistedInject constructor( fullList = mapAssetsToCardState(list, numbersFormatter), ), assetState1 = addState.assetState1?.copy( - initialAmount = addState.assetState1?.amount?.nullZero(), + amount = addState.assetState1?.amount?.nullZero(), ), assetState2 = addState.assetState2?.copy( - initialAmount = addState.assetState2?.amount?.nullZero(), + amount = addState.assetState2?.amount?.nullZero(), ), ) } @@ -549,10 +548,10 @@ class LiquidityAddViewModel @AssistedInject constructor( fullList = mapAssetsToCardState(list, numbersFormatter), ), assetState1 = addState.assetState1?.copy( - initialAmount = addState.assetState1?.amount?.nullZero(), + amount = addState.assetState1?.amount?.nullZero(), ), assetState2 = addState.assetState2?.copy( - initialAmount = addState.assetState2?.amount?.nullZero(), + amount = addState.assetState2?.amount?.nullZero(), ), ) } @@ -567,7 +566,7 @@ class LiquidityAddViewModel @AssistedInject constructor( assetState1 = state.copy( token = a.token, balance = getAssetBalanceText(a), - amountFiat = a.token.printFiat(state.amount, numbersFormatter), + amountFiat = a.token.printFiat(state.amount.orZero(), numbersFormatter), ), ) hasXorReminderWarningBeenChecked = false @@ -583,7 +582,7 @@ class LiquidityAddViewModel @AssistedInject constructor( assetState2 = state?.copy( token = a.token, balance = getAssetBalanceText(a), - amountFiat = a.token.printFiat(state.amount, numbersFormatter), + amountFiat = a.token.printFiat(state.amount.orZero(), numbersFormatter), ) ?: buildInitialAssetState(a), ) @@ -712,10 +711,10 @@ class LiquidityAddViewModel @AssistedInject constructor( fun onSlippageClick() { addState = addState.copy( assetState1 = addState.assetState1?.copy( - initialAmount = addState.assetState1?.amount?.nullZero(), + amount = addState.assetState1?.amount?.nullZero(), ), assetState2 = addState.assetState2?.copy( - initialAmount = addState.assetState2?.amount?.nullZero(), + amount = addState.assetState2?.amount?.nullZero(), ), ) } @@ -782,7 +781,6 @@ class LiquidityAddViewModel @AssistedInject constructor( assetState1 = addState.assetState1?.copy( amountFiat = tokenFrom.printFiat(amount, numbersFormatter), amount = amount, - initialAmount = amount, ) ) amount1Flow.value = amount @@ -797,7 +795,6 @@ class LiquidityAddViewModel @AssistedInject constructor( assetState2 = addState.assetState2?.copy( amountFiat = tokenTo.printFiat(amount, numbersFormatter), amount = amount, - initialAmount = amount, ) ) amount2Flow.value = amount @@ -876,8 +873,7 @@ class LiquidityAddViewModel @AssistedInject constructor( private fun buildInitialAssetState(a: Asset) = AssetAmountInputState( token = a.token, balance = getAssetBalanceText(a), - amount = BigDecimal.ZERO, - initialAmount = null, + amount = null, amountFiat = "", enabled = true, ) diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt index 645f69337..7b6f4440f 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt @@ -198,8 +198,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( assetState1 = AssetAmountInputState( token = asset1.token, balance = getAssetBalanceText(asset1), - amount = BigDecimal.ZERO, - initialAmount = null, + amount = null, amountFiat = "", enabled = true, ), @@ -210,8 +209,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( assetState2 = AssetAmountInputState( token = asset2.token, balance = getAssetBalanceText(asset2), - amount = BigDecimal.ZERO, - initialAmount = null, + amount = null, amountFiat = "", enabled = true, ), @@ -428,10 +426,10 @@ class LiquidityRemoveViewModel @AssistedInject constructor( fun onSlippageClick() { removeState = removeState.copy( assetState1 = removeState.assetState1?.copy( - initialAmount = removeState.assetState1?.amount?.nullZero(), + amount = removeState.assetState1?.amount?.nullZero(), ), assetState2 = removeState.assetState2?.copy( - initialAmount = removeState.assetState2?.amount?.nullZero(), + amount = removeState.assetState2?.amount?.nullZero(), ), ) } @@ -445,11 +443,11 @@ class LiquidityRemoveViewModel @AssistedInject constructor( fun onAmount1Change(value: BigDecimal) { removeState = removeState.copy( assetState1 = removeState.assetState1?.copy( - amount = value, amountFiat = removeState.assetState1?.token?.printFiat( value, numbersFormatter - ).orEmpty() + ).orEmpty(), + amount = value ) ) amount1Flow.value = value @@ -458,11 +456,11 @@ class LiquidityRemoveViewModel @AssistedInject constructor( fun onAmount2Change(value: BigDecimal) { removeState = removeState.copy( assetState2 = removeState.assetState2?.copy( - amount = value, amountFiat = removeState.assetState2?.token?.printFiat( value, numbersFormatter - ).orEmpty() + ).orEmpty(), + amount = value ) ) amount2Flow.value = value @@ -493,12 +491,12 @@ class LiquidityRemoveViewModel @AssistedInject constructor( poolDataUsable?.let { poolData -> val firstAmountMin = PolkaswapFormulas.calculateMinAmount( - fromToken.amount, + fromToken.amount.orZero(), removeState.slippage ) val secondAmountMin = PolkaswapFormulas.calculateMinAmount( - toToken.amount, + toToken.amount.orZero(), removeState.slippage ) val desired = @@ -627,7 +625,6 @@ class LiquidityRemoveViewModel @AssistedInject constructor( removeState = removeState.copy( assetState1 = removeState.assetState1?.copy( amount = amount1, - initialAmount = amount1, amountFiat = removeState.assetState1?.token?.printFiat( amount1, numbersFormatter @@ -639,7 +636,6 @@ class LiquidityRemoveViewModel @AssistedInject constructor( removeState = removeState.copy( assetState2 = removeState.assetState2?.copy( amount = amount2, - initialAmount = amount2, amountFiat = removeState.assetState2?.token?.printFiat( amount2, numbersFormatter @@ -691,7 +687,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( val result = assetsInteractor.isNotEnoughXorLeftAfterTransaction( networkFeeInXor = networkFee.orZero(), - xorChange = if (assetState1.token.id == SubstrateOptionsProvider.feeAssetId) -assetState1.amount else null, + xorChange = if (assetState1.token.id == SubstrateOptionsProvider.feeAssetId) -assetState1.amount.orZero() else null, ) removeState = removeState.copy( diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt index 3eae9450a..2bc2e5ce8 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt @@ -90,7 +90,6 @@ import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch @@ -280,8 +279,7 @@ class SwapViewModel @AssistedInject constructor( tokenFromState = AssetAmountInputState( token = assetFrom.token, balance = getAssetBalanceText(assetFrom), - amount = BigDecimal.ZERO, - initialAmount = null, + amount = null, amountFiat = "", enabled = false, ), @@ -295,8 +293,7 @@ class SwapViewModel @AssistedInject constructor( tokenToState = AssetAmountInputState( token = assetTo.token, balance = getAssetBalanceText(assetTo), - amount = BigDecimal.ZERO, - initialAmount = null, + amount = null, amountFiat = "", enabled = false, ), @@ -395,10 +392,10 @@ class SwapViewModel @AssistedInject constructor( fun onSlippageClick() { _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.amount?.nullZero(), + amount = _swapMainState.value.tokenFromState?.amount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.amount?.nullZero(), + amount = _swapMainState.value.tokenToState?.amount?.nullZero(), ), ) } @@ -407,10 +404,10 @@ class SwapViewModel @AssistedInject constructor( _swapMainState.value = _swapMainState.value.copy( selectMarketState = _swapMainState.value.market to availableMarkets, tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.amount?.nullZero(), + amount = _swapMainState.value.tokenFromState?.amount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.amount?.nullZero(), + amount = _swapMainState.value.tokenToState?.amount?.nullZero(), ), ) } @@ -426,10 +423,10 @@ class SwapViewModel @AssistedInject constructor( ) ), tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.amount?.nullZero(), + amount = _swapMainState.value.tokenFromState?.amount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.amount?.nullZero(), + amount = _swapMainState.value.tokenToState?.amount?.nullZero(), ), ) } @@ -446,10 +443,10 @@ class SwapViewModel @AssistedInject constructor( ) ), tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.amount?.nullZero(), + amount = _swapMainState.value.tokenFromState?.amount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.amount?.nullZero(), + amount = _swapMainState.value.tokenToState?.amount?.nullZero(), ), ) } @@ -462,7 +459,7 @@ class SwapViewModel @AssistedInject constructor( val change = if (tokenFromState.token.id == SubstrateOptionsProvider.feeAssetId) { tokenFromState.amount } else if (tokenToState.token.id == SubstrateOptionsProvider.feeAssetId) { - -tokenToState.amount + -tokenToState.amount.orZero() } else { null } val result = assetsInteractor.isNotEnoughXorLeftAfterTransaction( networkFeeInXor = networkFee.orZero(), @@ -499,10 +496,10 @@ class SwapViewModel @AssistedInject constructor( desired = if (desired == WithDesired.INPUT) WithDesired.OUTPUT else WithDesired.INPUT _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.amount?.nullZero() + amount = _swapMainState.value.tokenToState?.amount?.nullZero() ), tokenToState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.amount?.nullZero() + amount = _swapMainState.value.tokenFromState?.amount?.nullZero() ), ) setSwapButtonLoading(true) @@ -516,8 +513,7 @@ class SwapViewModel @AssistedInject constructor( tokenToState = AssetAmountInputState( token = token, balance = getAssetBalanceText(asset), - amount = amountTo, - initialAmount = amountTo.nullZero(), + amount = amountTo.nullZero(), amountFiat = token.printFiat(amountTo, numbersFormatter), enabled = true, ) @@ -530,8 +526,7 @@ class SwapViewModel @AssistedInject constructor( tokenFromState = AssetAmountInputState( token = token, balance = getAssetBalanceText(asset), - amount = amountFrom, - initialAmount = amountFrom.nullZero(), + amount = amountFrom.nullZero(), amountFiat = token.printFiat(amountFrom, numbersFormatter), enabled = _swapMainState.value.tokenToState != null, ) @@ -711,7 +706,6 @@ class SwapViewModel @AssistedInject constructor( numbersFormatter ), amount = it, - initialAmount = it, ) ) } else { @@ -722,7 +716,6 @@ class SwapViewModel @AssistedInject constructor( numbersFormatter ), amount = it, - initialAmount = it, ) ) } @@ -929,12 +922,12 @@ class SwapViewModel @AssistedInject constructor( fromState.token, toState.token, desired, - if (desired == WithDesired.INPUT) fromState.amount else toState.amount, + if (desired == WithDesired.INPUT) fromState.amount.orZero() else toState.amount.orZero(), details.minmax, details.networkFee, details.liquidityFee, details.dex.dexId, - if (desired == WithDesired.OUTPUT) fromState.amount else toState.amount, + if (desired == WithDesired.OUTPUT) fromState.amount.orZero() else toState.amount.orZero(), ) } catch (t: Throwable) { onError(t) @@ -985,7 +978,6 @@ class SwapViewModel @AssistedInject constructor( tokenFromState = _swapMainState.value.tokenFromState?.copy( amountFiat = fromAsset.token.printFiat(amount, numbersFormatter), amount = amount, - initialAmount = amount, ) ) desired = WithDesired.INPUT From 1d537c5cb69b4ba3aba600e1ed8db57de4faf042 Mon Sep 17 00:00:00 2001 From: AmadeyKuspakov Date: Thu, 31 Aug 2023 17:27:51 +0400 Subject: [PATCH 4/9] Integration of ui_lib in unit tests --- .../presentation/qr/QrCodeFlowViewModelTest.kt | 6 +++--- .../presentation/send/TransferAmountViewModelTest.kt | 4 ++-- .../liquidityremove/LiquidityRemoveViewModel.kt | 4 ++-- .../presentation/polkaswap/SwapViewModelTest.kt | 12 ++++++------ .../liquidity/add/AddLiquidityViewModelTest.kt | 8 ++++---- .../liquidity/remove/RemoveLiquidityViewModelTest.kt | 8 ++++---- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt b/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt index 1282b4e1b..d0527a622 100644 --- a/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt +++ b/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt @@ -176,8 +176,8 @@ class QrCodeFlowViewModelTest { ) Assert.assertEquals( - viewModel.requestTokenByQrScreenState.assetAmountInputState?.amount, - viewModel.requestTokenConfirmScreenState.assetAmountInputState?.amount + viewModel.requestTokenByQrScreenState.assetAmountInputState?.initialAmount, + viewModel.requestTokenConfirmScreenState.assetAmountInputState?.initialAmount ) Assert.assertEquals( @@ -186,7 +186,7 @@ class QrCodeFlowViewModelTest { ) Assert.assertEquals( - viewModel.requestTokenByQrScreenState.assetAmountInputState?.amount, + viewModel.requestTokenByQrScreenState.assetAmountInputState?.initialAmount, viewModel.requestTokenConfirmScreenState.assetAmountInputState?.initialAmount ) } diff --git a/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/send/TransferAmountViewModelTest.kt b/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/send/TransferAmountViewModelTest.kt index 6338abb78..ea8627d97 100644 --- a/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/send/TransferAmountViewModelTest.kt +++ b/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/send/TransferAmountViewModelTest.kt @@ -154,7 +154,7 @@ class TransferAmountViewModelTest { transferAmountViewModel.optionSelected(75) advanceUntilIdle() val state = transferAmountViewModel.sendState - assertTrue(state.input?.amount?.equalTo(BigDecimal.valueOf(3)) == true) + assertTrue(state.input?.initialAmount?.equalTo(BigDecimal.valueOf(3)) == true) } @Test @@ -164,7 +164,7 @@ class TransferAmountViewModelTest { transferAmountViewModel.optionSelected(75) advanceUntilIdle() val state = transferAmountViewModel.sendState - assertTrue(state.input?.amount?.equalTo(BigDecimal.valueOf(0.000075)) == true) + assertTrue(state.input?.initialAmount?.equalTo(BigDecimal.valueOf(0.000075)) == true) } private fun initViewModel( diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt index 2a3c4249c..559edaccf 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt @@ -621,7 +621,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( shareOfPool = newPoolShare, ) ) - if (amount1.compareTo(removeState.assetState1?.initialAmount) != 0) { + if (amount1.compareTo(removeState.assetState1?.initialAmount.orZero()) != 0) { removeState = removeState.copy( assetState1 = removeState.assetState1?.copy( initialAmount = amount1, @@ -632,7 +632,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( ) ) } - if (amount2.compareTo(removeState.assetState2?.initialAmount) != 0) { + if (amount2.compareTo(removeState.assetState2?.initialAmount.orZero()) != 0) { removeState = removeState.copy( assetState2 = removeState.assetState2?.copy( initialAmount = amount2, diff --git a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt index f9f587b56..83b927b6b 100644 --- a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt +++ b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt @@ -311,7 +311,7 @@ class SwapViewModelTest { advanceUntilIdle() viewModel.fromInputPercentClicked(100) advanceUntilIdle() - assertEquals(BigDecimal.ONE, viewModel.swapMainState.value.tokenFromState?.amount) + assertEquals(BigDecimal.ONE, viewModel.swapMainState.value.tokenFromState?.initialAmount) val btn = viewModel.swapMainState.value.swapButtonState assertEquals("Insufficient balance", btn.text) assertEquals(false, btn.enabled) @@ -326,7 +326,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(100) - assertEquals(BigDecimal(90.0), viewModel.swapMainState.value.tokenFromState?.amount) + assertEquals(BigDecimal(90.0), viewModel.swapMainState.value.tokenFromState?.initialAmount) } @Test @@ -338,7 +338,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(50) - assertEquals(BigDecimal(50.0).setScale(19), viewModel.swapMainState.value.tokenFromState?.amount) + assertEquals(BigDecimal(50.0).setScale(19), viewModel.swapMainState.value.tokenFromState?.initialAmount) } @Test @@ -350,7 +350,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(50) - assertEquals(BigDecimal(8.0).setScale(19), viewModel.swapMainState.value.tokenFromState?.amount) + assertEquals(BigDecimal(8.0).setScale(19), viewModel.swapMainState.value.tokenFromState?.initialAmount) } @Test @@ -362,7 +362,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(50) - assertEquals(BigDecimal(25).setScale(19), viewModel.swapMainState.value.tokenFromState?.amount) + assertEquals(BigDecimal(25).setScale(19), viewModel.swapMainState.value.tokenFromState?.initialAmount) } @Test @@ -374,7 +374,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(100) - assertEquals(BigDecimal(100.0), viewModel.swapMainState.value.tokenFromState?.amount) + assertEquals(BigDecimal(100.0), viewModel.swapMainState.value.tokenFromState?.initialAmount) } @Test diff --git a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/add/AddLiquidityViewModelTest.kt b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/add/AddLiquidityViewModelTest.kt index 458dde500..618aa6df9 100644 --- a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/add/AddLiquidityViewModelTest.kt +++ b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/add/AddLiquidityViewModelTest.kt @@ -269,7 +269,7 @@ class AddLiquidityViewModelTest { advanceUntilIdle() viewModel.onAmount1Change(BigDecimal("110.34")) advanceUntilIdle() - assertEquals(BigDecimal("110.34"), viewModel.addState.assetState1?.amount) + assertEquals(BigDecimal("110.34"), viewModel.addState.assetState1?.initialAmount) } @Test @@ -282,7 +282,7 @@ class AddLiquidityViewModelTest { advanceUntilIdle() viewModel.onAmount2Change(BigDecimal("110.34")) advanceUntilIdle() - assertEquals(BigDecimal("110.34"), viewModel.addState.assetState2?.amount) + assertEquals(BigDecimal("110.34"), viewModel.addState.assetState2?.initialAmount) } @Test @@ -332,7 +332,7 @@ class AddLiquidityViewModelTest { viewModel.optionSelected(50) advanceUntilIdle() - assertTrue(viewModel.addState.assetState1?.amount?.equalTo(BigDecimal(0.5)) == true) + assertTrue(viewModel.addState.assetState1?.initialAmount?.equalTo(BigDecimal(0.5)) == true) } @Test @@ -348,7 +348,7 @@ class AddLiquidityViewModelTest { viewModel.optionSelected(50) advanceUntilIdle() - assertTrue(viewModel.addState.assetState2?.amount?.equalTo(BigDecimal(0.5)) == true) + assertTrue(viewModel.addState.assetState2?.initialAmount?.equalTo(BigDecimal(0.5)) == true) } @Test diff --git a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/remove/RemoveLiquidityViewModelTest.kt b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/remove/RemoveLiquidityViewModelTest.kt index bd0bab202..a5785ab0e 100644 --- a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/remove/RemoveLiquidityViewModelTest.kt +++ b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/remove/RemoveLiquidityViewModelTest.kt @@ -199,8 +199,8 @@ class RemoveLiquidityViewModelTest { viewModel.onAmount1Change(BigDecimal(0.5)) advanceUntilIdle() - assertEquals(BigDecimal(0.5), viewModel.removeState.assetState1?.amount) - assertEquals(BigDecimal(0.5), viewModel.removeState.assetState2?.amount) + assertEquals(BigDecimal(0.5), viewModel.removeState.assetState1?.initialAmount) + assertEquals(BigDecimal(0.5), viewModel.removeState.assetState2?.initialAmount) assertEquals("Remove", viewModel.removeState.btnState.text) assertTrue(viewModel.removeState.btnState.enabled) } @@ -214,8 +214,8 @@ class RemoveLiquidityViewModelTest { viewModel.onAmount2Change(BigDecimal("0.35")) advanceUntilIdle() - assertEquals(BigDecimal("0.35"), viewModel.removeState.assetState1?.amount) - assertEquals(BigDecimal("0.35"), viewModel.removeState.assetState2?.amount) + assertEquals(BigDecimal("0.35"), viewModel.removeState.assetState1?.initialAmount) + assertEquals(BigDecimal("0.35"), viewModel.removeState.assetState2?.initialAmount) assertTrue(viewModel.removeState.btnState.enabled) } From 865cfb7129def969ab47ed83c8b0b8ab03001435 Mon Sep 17 00:00:00 2001 From: AmadeyKuspakov Date: Tue, 5 Sep 2023 17:33:54 +0400 Subject: [PATCH 5/9] Fixed minor bug --- .../components/compose/SwapSlippageScreen.kt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapSlippageScreen.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapSlippageScreen.kt index 9f9ebad94..65e8814f5 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapSlippageScreen.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapSlippageScreen.kt @@ -84,7 +84,7 @@ internal fun SwapSlippageScreen( value: Double, onDone: (Double) -> Unit, ) { - var curDouble by remember { mutableStateOf(value) } + var currentValueLocalStorage by remember { mutableStateOf(value) } val desc = remember { mutableStateOf(null) } val frontrun = stringResource(id = R.string.polkaswap_slippage_frontrun) @@ -118,6 +118,9 @@ internal fun SwapSlippageScreen( desc.value = null } } + + // Store inputted value as Double in local storage + currentValueLocalStorage = valueAsDouble } } @@ -158,14 +161,15 @@ internal fun SwapSlippageScreen( BasicNumberInput( modifier = Modifier, textStyle = MaterialTheme.customTypography.textM, - initial = value.toBigDecimal(), + initial = value.toBigDecimal(), // input value is used; no locally stored data!!! precision = 2, enabled = true, visualTransformation = visualTransformation, onValueChanged = onValueChangeDecorator, onKeyboardDone = remember { { - onDone(curDouble.coerceIn(min, max)) + // Get locally stored value and make coerceIn + onDone(currentValueLocalStorage.coerceIn(min, max)) } } ) @@ -195,7 +199,8 @@ internal fun SwapSlippageScreen( order = Order.PRIMARY, text = stringResource(id = R.string.common_done), onClick = { - onDone(curDouble.coerceIn(min, max)) + // Get locally stored value and make coerceIn + onDone(currentValueLocalStorage.coerceIn(min, max)) }, ) } From 8c0bc945b974f0fc09a5f4d546be8a20e29a911c Mon Sep 17 00:00:00 2001 From: AmadeyKuspakov Date: Wed, 6 Sep 2023 20:28:36 +0400 Subject: [PATCH 6/9] Fixed precision calculation on SwapScreen --- .../presentation/compose/util/PolkaswapFormulas.kt | 7 +++---- .../presentation/polkaswap/SwapViewModelTest.kt | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/common_wallet/src/main/java/jp/co/soramitsu/common_wallet/presentation/compose/util/PolkaswapFormulas.kt b/common_wallet/src/main/java/jp/co/soramitsu/common_wallet/presentation/compose/util/PolkaswapFormulas.kt index 48cdfa1ab..ca20d0997 100644 --- a/common_wallet/src/main/java/jp/co/soramitsu/common_wallet/presentation/compose/util/PolkaswapFormulas.kt +++ b/common_wallet/src/main/java/jp/co/soramitsu/common_wallet/presentation/compose/util/PolkaswapFormulas.kt @@ -125,10 +125,9 @@ object PolkaswapFormulas { amount: BigDecimal, percentage: Double, precision: Int, - ): BigDecimal { - return if (percentage == 100.0) amount else amount.safeDivide(Big100, precision) - .multiply(percentage.toBigDecimal()) - } + ): BigDecimal = if (percentage == 100.0) amount else + amount.multiply(percentage.toBigDecimal()) + .safeDivide(Big100, precision) fun calculateOneAmountFromAnother( amount: BigDecimal, diff --git a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt index da1321e4c..14c85eea1 100644 --- a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt +++ b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt @@ -338,7 +338,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(50) - assertEquals(BigDecimal(50.0).setScale(19), viewModel.swapMainState.value.tokenFromState?.initialAmount) + assertEquals(BigDecimal(50.0).setScale(18), viewModel.swapMainState.value.tokenFromState?.initialAmount) } @Test @@ -350,7 +350,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(50) - assertEquals(BigDecimal(8.0).setScale(19), viewModel.swapMainState.value.tokenFromState?.initialAmount) + assertEquals(BigDecimal(8.0).setScale(18), viewModel.swapMainState.value.tokenFromState?.initialAmount) } @Test @@ -362,7 +362,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(50) - assertEquals(BigDecimal(25).setScale(19), viewModel.swapMainState.value.tokenFromState?.initialAmount) + assertEquals(BigDecimal(25).setScale(18), viewModel.swapMainState.value.tokenFromState?.initialAmount) } @Test From d8be5389377e0bba4461a3922a73676106c21107 Mon Sep 17 00:00:00 2001 From: AmadeyKuspakov Date: Wed, 6 Sep 2023 23:53:09 +0400 Subject: [PATCH 7/9] Minor changes --- .../screens/receiverequest/QRCodeFlowViewModel.kt | 2 +- .../screens/liquidityadd/LiquidityAddViewModel.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt index 013f320da..a46354c18 100644 --- a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt +++ b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt @@ -380,7 +380,7 @@ class QRCodeFlowViewModel @Inject constructor( _requestTokenScreenState.value.assetAmountInputState?.let { assetAmountInputState -> _requestTokenScreenState.value = _requestTokenScreenState.value.copy( assetAmountInputState = assetAmountInputState.copy( - initialAmount = assetAmountInputState.amount, + initialAmount = assetAmountInputState.initialAmount, ) ) } diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt index 0f897b784..2f056ee95 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt @@ -508,7 +508,7 @@ class LiquidityAddViewModel @AssistedInject constructor( val list = assets.filter { it.token.id in bases && it.token.id != addToken2 } _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( - initialAmount = _addState.value.assetState1?.amount?.nullZero(), + initialAmount = _addState.value.assetState1?.initialAmount?.nullZero(), ), assetState2 = _addState.value.assetState2?.copy( initialAmount = _addState.value.assetState2?.initialAmount?.nullZero(), @@ -549,7 +549,7 @@ class LiquidityAddViewModel @AssistedInject constructor( } _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( - initialAmount = _addState.value.assetState1?.amount?.nullZero(), + initialAmount = _addState.value.assetState1?.initialAmount?.nullZero(), ), assetState2 = _addState.value.assetState2?.copy( initialAmount = _addState.value.assetState2?.initialAmount?.nullZero(), From 9fb3779053e80283c594463039e85169616062c1 Mon Sep 17 00:00:00 2001 From: AmadeyKuspakov Date: Thu, 7 Sep 2023 23:14:39 +0400 Subject: [PATCH 8/9] Addition of default cursor position --- build.gradle | 2 +- .../common/domain/AssetAmountInputState.kt | 2 + .../compose/components/AssetAmount.kt | 2 + .../screens/send/TransferAmountViewModel.kt | 15 +++++-- .../liquidityadd/LiquidityAddViewModel.kt | 43 ++++++++++++++++++- .../screens/swap/SwapViewModel.kt | 25 +++++++++-- 6 files changed, 79 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index d33b8ed03..155522502 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ buildscript { composeMaterial : '1.4.3', composeCompiler : '1.4.6', composeConstraintLayout: '1.1.0-alpha05', - uiCore : '0.1.2', + uiCore : '0.1.3', soraCard : '0.1.38', lazySodium : '5.0.2', jna : '5.8.0', diff --git a/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt b/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt index a737cdd41..a08270bcf 100644 --- a/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt +++ b/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt @@ -33,6 +33,7 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package jp.co.soramitsu.common.domain import java.math.BigDecimal +import jp.co.soramitsu.ui_core.component.input.number.DefaultCursorPosition data class AssetAmountInputState( val token: Token, @@ -43,4 +44,5 @@ data class AssetAmountInputState( val readOnly: Boolean = false, val error: Boolean = false, val errorHint: String = "", + val defaultCursorPosition: DefaultCursorPosition = DefaultCursorPosition.END ) diff --git a/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt b/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt index 8515aca3a..038bf05a2 100644 --- a/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt +++ b/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt @@ -72,6 +72,7 @@ import jp.co.soramitsu.common.util.ext.orZero import jp.co.soramitsu.common.util.ext.testTagAsId import jp.co.soramitsu.ui_core.component.button.properties.Size import jp.co.soramitsu.ui_core.component.input.number.BasicNumberInput +import jp.co.soramitsu.ui_core.component.input.number.DefaultCursorPosition import jp.co.soramitsu.ui_core.resources.Dimens import jp.co.soramitsu.ui_core.theme.borderRadius import jp.co.soramitsu.ui_core.theme.customColors @@ -158,6 +159,7 @@ fun AssetAmountInput( textStyle = MaterialTheme.customTypography.displayS.copy(textAlign = TextAlign.End), enabled = state?.let { it.enabled && !it.readOnly } ?: false, precision = state?.token?.precision ?: OptionsProvider.defaultScale, + defaultCursorPosition = state?.defaultCursorPosition ?: DefaultCursorPosition.END, initial = state?.initialAmount, onValueChanged = onAmountChange, focusRequester = focusRequester, diff --git a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt index ab2c712e6..fa1a67a70 100644 --- a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt +++ b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt @@ -62,6 +62,7 @@ import jp.co.soramitsu.feature_assets_api.presentation.AssetsRouter import jp.co.soramitsu.feature_assets_impl.presentation.states.SendState import jp.co.soramitsu.feature_wallet_api.launcher.WalletRouter import jp.co.soramitsu.sora.substrate.runtime.SubstrateOptionsProvider +import jp.co.soramitsu.ui_core.component.input.number.DefaultCursorPosition import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.catch @@ -167,6 +168,7 @@ class TransferAmountViewModel @AssistedInject constructor( enabled = false, error = false, errorHint = "", + defaultCursorPosition = DefaultCursorPosition.END ) ) } else { @@ -178,6 +180,7 @@ class TransferAmountViewModel @AssistedInject constructor( _sendState.value.input?.initialAmount.orZero(), numbersFormatter ), + defaultCursorPosition = DefaultCursorPosition.END ) ) } @@ -229,7 +232,8 @@ class TransferAmountViewModel @AssistedInject constructor( _sendState.value = _sendState.value.copy( input = _sendState.value.input?.copy( readOnly = false, - initialAmount = _sendState.value.input?.initialAmount?.nullZero() + initialAmount = _sendState.value.input?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), ) } @@ -392,7 +396,8 @@ class TransferAmountViewModel @AssistedInject constructor( fun onReviewClick() { _sendState.value = _sendState.value.copy( input = _sendState.value.input?.copy( - initialAmount = _sendState.value.input?.initialAmount.orZero() + initialAmount = _sendState.value.input?.initialAmount.orZero(), + defaultCursorPosition = DefaultCursorPosition.START ) ) } @@ -404,7 +409,8 @@ class TransferAmountViewModel @AssistedInject constructor( amountFiat = _sendState.value.input?.token?.printFiat( value, numbersFormatter - ).orEmpty() + ).orEmpty(), + defaultCursorPosition = DefaultCursorPosition.END ) ) enteredFlow.value = value @@ -427,7 +433,8 @@ class TransferAmountViewModel @AssistedInject constructor( amountFiat = _sendState.value.input?.token?.printFiat( amount, numbersFormatter - ).orEmpty() + ).orEmpty(), + defaultCursorPosition = DefaultCursorPosition.START ) ) enteredFlow.value = amount diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt index 2f056ee95..08fd49932 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt @@ -69,6 +69,7 @@ import jp.co.soramitsu.feature_wallet_api.domain.interfaces.WalletInteractor import jp.co.soramitsu.feature_wallet_api.launcher.WalletRouter import jp.co.soramitsu.sora.substrate.runtime.SubstrateOptionsProvider import jp.co.soramitsu.sora.substrate.runtime.isSynthetic +import jp.co.soramitsu.ui_core.component.input.number.DefaultCursorPosition import jp.co.soramitsu.ui_core.component.toolbar.Action import jp.co.soramitsu.ui_core.component.toolbar.BasicToolbarState import jp.co.soramitsu.ui_core.component.toolbar.SoramitsuToolbarState @@ -509,9 +510,11 @@ class LiquidityAddViewModel @AssistedInject constructor( _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( initialAmount = _addState.value.assetState1?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), assetState2 = _addState.value.assetState2?.copy( initialAmount = _addState.value.assetState2?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), ) _searchTokenFilter.value = _searchTokenFilter.value.copy( @@ -550,9 +553,11 @@ class LiquidityAddViewModel @AssistedInject constructor( _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( initialAmount = _addState.value.assetState1?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), assetState2 = _addState.value.assetState2?.copy( initialAmount = _addState.value.assetState2?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), ) _searchTokenFilter.value = _searchTokenFilter.value.copy( @@ -571,6 +576,10 @@ class LiquidityAddViewModel @AssistedInject constructor( token = a.token, balance = getAssetBalanceText(a), amountFiat = a.token.printFiat(state.initialAmount.orZero(), numbersFormatter), + defaultCursorPosition = DefaultCursorPosition.END + ), + assetState2 = _addState.value.assetState2?.copy( + defaultCursorPosition = DefaultCursorPosition.END ), ) hasXorReminderWarningBeenChecked = false @@ -583,10 +592,14 @@ class LiquidityAddViewModel @AssistedInject constructor( val a = assets.first { t -> t.token.id == id } val state = _addState.value.assetState2 _addState.value = _addState.value.copy( + assetState1 = _addState.value.assetState1?.copy( + defaultCursorPosition = DefaultCursorPosition.END + ), assetState2 = state?.copy( token = a.token, balance = getAssetBalanceText(a), amountFiat = a.token.printFiat(state.initialAmount.orZero(), numbersFormatter), + defaultCursorPosition = DefaultCursorPosition.END ) ?: buildInitialAssetState(a), ) @@ -612,15 +625,23 @@ class LiquidityAddViewModel @AssistedInject constructor( assetState1 = _addState.value.assetState1?.copy( balance = getAssetBalanceText(asset), token = asset.token, + defaultCursorPosition = DefaultCursorPosition.END + ), + assetState2 = _addState.value.assetState2?.copy( + defaultCursorPosition = DefaultCursorPosition.END ) ) } assets.find { it.token.id == addToken2 }?.let { asset -> balance2 = asset.balance.transferable _addState.value = _addState.value.copy( + assetState1 = _addState.value.assetState1?.copy( + defaultCursorPosition = DefaultCursorPosition.END + ), assetState2 = _addState.value.assetState2?.copy( balance = getAssetBalanceText(asset), token = asset.token, + defaultCursorPosition = DefaultCursorPosition.END ) ) } @@ -716,9 +737,11 @@ class LiquidityAddViewModel @AssistedInject constructor( _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( initialAmount = _addState.value.assetState1?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), assetState2 = _addState.value.assetState2?.copy( initialAmount = _addState.value.assetState2?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), ) } @@ -735,7 +758,11 @@ class LiquidityAddViewModel @AssistedInject constructor( amountFiat = _addState.value.assetState1?.token?.printFiat( value, numbersFormatter - ).orEmpty() + ).orEmpty(), + defaultCursorPosition = DefaultCursorPosition.END + ), + assetState2 = _addState.value.assetState2?.copy( + defaultCursorPosition = DefaultCursorPosition.END ) ) amount1Flow.value = value @@ -743,12 +770,16 @@ class LiquidityAddViewModel @AssistedInject constructor( fun onAmount2Change(value: BigDecimal) { _addState.value = _addState.value.copy( + assetState1 = _addState.value.assetState1?.copy( + defaultCursorPosition = DefaultCursorPosition.END + ), assetState2 = _addState.value.assetState2?.copy( initialAmount = value, amountFiat = _addState.value.assetState2?.token?.printFiat( value, numbersFormatter - ).orEmpty() + ).orEmpty(), + defaultCursorPosition = DefaultCursorPosition.END ) ) amount2Flow.value = value @@ -783,6 +814,10 @@ class LiquidityAddViewModel @AssistedInject constructor( assetState1 = _addState.value.assetState1?.copy( amountFiat = tokenFrom.printFiat(amount, numbersFormatter), initialAmount = amount, + defaultCursorPosition = DefaultCursorPosition.START + ), + assetState2 = _addState.value.assetState2?.copy( + defaultCursorPosition = DefaultCursorPosition.START ) ) amount1Flow.value = amount @@ -794,9 +829,13 @@ class LiquidityAddViewModel @AssistedInject constructor( tokenTo.precision ) _addState.value = _addState.value.copy( + assetState1 = _addState.value.assetState1?.copy( + defaultCursorPosition = DefaultCursorPosition.START + ), assetState2 = _addState.value.assetState2?.copy( amountFiat = tokenTo.printFiat(amount, numbersFormatter), initialAmount = amount, + defaultCursorPosition = DefaultCursorPosition.START ) ) amount2Flow.value = amount diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt index ac66f267c..93ed56652 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt @@ -75,6 +75,7 @@ import jp.co.soramitsu.feature_polkaswap_impl.presentation.states.SwapMainState import jp.co.soramitsu.feature_polkaswap_impl.presentation.states.defaultSwapDetailsState import jp.co.soramitsu.feature_wallet_api.domain.interfaces.WalletInteractor import jp.co.soramitsu.sora.substrate.runtime.SubstrateOptionsProvider +import jp.co.soramitsu.ui_core.component.input.number.DefaultCursorPosition import jp.co.soramitsu.ui_core.component.toolbar.BasicToolbarState import jp.co.soramitsu.ui_core.component.toolbar.SoramitsuToolbarState import jp.co.soramitsu.ui_core.component.toolbar.SoramitsuToolbarType @@ -399,9 +400,11 @@ class SwapViewModel @AssistedInject constructor( _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), tokenToState = _swapMainState.value.tokenToState?.copy( initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), ) } @@ -411,9 +414,11 @@ class SwapViewModel @AssistedInject constructor( selectMarketState = _swapMainState.value.market to availableMarkets, tokenFromState = _swapMainState.value.tokenFromState?.copy( initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), tokenToState = _swapMainState.value.tokenToState?.copy( initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), ) } @@ -423,9 +428,11 @@ class SwapViewModel @AssistedInject constructor( _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), tokenToState = _swapMainState.value.tokenToState?.copy( initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), ) } @@ -436,9 +443,11 @@ class SwapViewModel @AssistedInject constructor( _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), tokenToState = _swapMainState.value.tokenToState?.copy( initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), ) } @@ -488,10 +497,12 @@ class SwapViewModel @AssistedInject constructor( desired = if (desired == WithDesired.INPUT) WithDesired.OUTPUT else WithDesired.INPUT _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero() + initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), tokenToState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero() + initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), + defaultCursorPosition = DefaultCursorPosition.END ), ) setSwapButtonLoading(true) @@ -804,12 +815,16 @@ class SwapViewModel @AssistedInject constructor( fun onFromAmountChange(value: BigDecimal) { _swapMainState.value = _swapMainState.value.copy( + tokenToState = _swapMainState.value.tokenToState?.copy( + defaultCursorPosition = DefaultCursorPosition.START + ), tokenFromState = _swapMainState.value.tokenFromState?.copy( initialAmount = value, amountFiat = _swapMainState.value.tokenFromState?.token?.printFiat( value, numbersFormatter - ).orEmpty() + ).orEmpty(), + defaultCursorPosition = DefaultCursorPosition.START ) ) fromAmountOnEach() @@ -960,9 +975,13 @@ class SwapViewModel @AssistedInject constructor( amount = subtractFee(amount, transferable, networkFee) } _swapMainState.value = _swapMainState.value.copy( + tokenToState = _swapMainState.value.tokenToState?.copy( + defaultCursorPosition = DefaultCursorPosition.START + ), tokenFromState = _swapMainState.value.tokenFromState?.copy( amountFiat = fromAsset.token.printFiat(amount, numbersFormatter), initialAmount = amount, + defaultCursorPosition = DefaultCursorPosition.START ) ) desired = WithDesired.INPUT From 308e756abce3e418b5ef019fc67be8229c5479ec Mon Sep 17 00:00:00 2001 From: AmadeyKuspakov Date: Tue, 12 Sep 2023 12:51:02 +0400 Subject: [PATCH 9/9] sn-2813 Removed Default cursor position settings --- .../common/domain/AssetAmountInputState.kt | 6 +- .../compose/components/AssetAmount.kt | 12 +-- .../receiverequest/QRCodeFlowViewModel.kt | 12 +-- .../screens/send/TransferAmountViewModel.kt | 35 ++++----- .../qr/QrCodeFlowViewModelTest.kt | 8 +- .../send/TransferAmountViewModelTest.kt | 4 +- .../data/BlockExplorerManager.kt | 11 ++- .../compose/LiquidityAddConfirmScreen.kt | 4 +- .../compose/LiquidityRemoveConfirmScreen.kt | 4 +- .../components/compose/SwapConfirmScreen.kt | 4 +- .../liquidityadd/LiquidityAddViewModel.kt | 73 +++++-------------- .../LiquidityRemoveViewModel.kt | 26 +++---- .../screens/swap/SwapViewModel.kt | 73 +++++++------------ .../polkaswap/SwapViewModelTest.kt | 12 +-- .../add/AddLiquidityViewModelTest.kt | 10 +-- .../remove/RemoveLiquidityViewModelTest.kt | 8 +- 16 files changed, 120 insertions(+), 182 deletions(-) diff --git a/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt b/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt index a08270bcf..1c713ea01 100644 --- a/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt +++ b/common/src/main/java/jp/co/soramitsu/common/domain/AssetAmountInputState.kt @@ -33,16 +33,14 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package jp.co.soramitsu.common.domain import java.math.BigDecimal -import jp.co.soramitsu.ui_core.component.input.number.DefaultCursorPosition data class AssetAmountInputState( val token: Token, val balance: String, - val initialAmount: BigDecimal? = null, + val amount: BigDecimal? = null, val amountFiat: String, val enabled: Boolean, val readOnly: Boolean = false, val error: Boolean = false, - val errorHint: String = "", - val defaultCursorPosition: DefaultCursorPosition = DefaultCursorPosition.END + val errorHint: String = "" ) diff --git a/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt b/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt index 038bf05a2..4b5e59a01 100644 --- a/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt +++ b/common/src/main/java/jp/co/soramitsu/common/presentation/compose/components/AssetAmount.kt @@ -159,8 +159,8 @@ fun AssetAmountInput( textStyle = MaterialTheme.customTypography.displayS.copy(textAlign = TextAlign.End), enabled = state?.let { it.enabled && !it.readOnly } ?: false, precision = state?.token?.precision ?: OptionsProvider.defaultScale, - defaultCursorPosition = state?.defaultCursorPosition ?: DefaultCursorPosition.END, - initial = state?.initialAmount, + defaultCursorPosition = DefaultCursorPosition.START, + initial = state?.amount, onValueChanged = onAmountChange, focusRequester = focusRequester, textFieldColors = TextFieldDefaults.textFieldColors( @@ -218,7 +218,7 @@ val previewAssetAmountInputState = AssetAmountInputState( token = previewToken, balance = "10.234 ($2.234.23)", amountFiat = "$2.342.12", - initialAmount = null, + amount = null, enabled = true, error = false, errorHint = "", @@ -238,7 +238,7 @@ private fun PreviewAssetAmountInput() { val state = remember { mutableStateOf(previewAssetAmountInputState) } Button(onClick = { state.value = state.value.copy( - initialAmount = bb.value, + amount = bb.value, ) bb.value = bb.value.plus(BigDecimal.ONE) }) { @@ -249,14 +249,14 @@ private fun PreviewAssetAmountInput() { state = state.value, onAmountChange = { state.value = state.value.copy( - initialAmount = it, + amount = it, ) }, onSelectToken = {}, onFocusChange = {}, ) Spacer(modifier = Modifier.size(10.dp)) - Text(text = state.value.initialAmount.orZero().toPlainString()) + Text(text = state.value.amount.orZero().toPlainString()) AssetAmountInput( modifier = Modifier, state = previewAssetAmountInputState, diff --git a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt index a46354c18..d7cf36c71 100644 --- a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt +++ b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/receiverequest/QRCodeFlowViewModel.kt @@ -163,7 +163,7 @@ class QRCodeFlowViewModel @Inject constructor( nf = numbersFormatter, precision = DEFAULT_TOKEN_PRINT_PRECISION, ), - initialAmount = null, + amount = null, amountFiat = "", enabled = true, ) else _requestTokenScreenState.value.assetAmountInputState!!.copy( @@ -283,7 +283,7 @@ class QRCodeFlowViewModel @Inject constructor( fun onRequestAmountChange(amount: BigDecimal) { _requestTokenScreenState.value = _requestTokenScreenState.value.copy( assetAmountInputState = _requestTokenScreenState.value.assetAmountInputState?.copy( - initialAmount = amount, + amount = amount, amountFiat = _requestTokenScreenState.value.assetAmountInputState?.token?.printFiat( amount, numbersFormatter @@ -380,7 +380,7 @@ class QRCodeFlowViewModel @Inject constructor( _requestTokenScreenState.value.assetAmountInputState?.let { assetAmountInputState -> _requestTokenScreenState.value = _requestTokenScreenState.value.copy( assetAmountInputState = assetAmountInputState.copy( - initialAmount = assetAmountInputState.initialAmount, + amount = assetAmountInputState.amount, ) ) } @@ -400,7 +400,7 @@ class QRCodeFlowViewModel @Inject constructor( tokenId = _requestTokenScreenState.value.assetAmountInputState ?.token?.id, amount = _requestTokenScreenState.value.assetAmountInputState - ?.initialAmount.toString() + ?.amount.toString() ) ) } @@ -416,8 +416,8 @@ class QRCodeFlowViewModel @Inject constructor( .untransformedUserAddress, assetAmountInputState = _requestTokenScreenState.value .assetAmountInputState?.copy( - initialAmount = _requestTokenScreenState.value - .assetAmountInputState?.initialAmount, + amount = _requestTokenScreenState.value + .assetAmountInputState?.amount, enabled = false, readOnly = true, ) diff --git a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt index fa1a67a70..cad49af65 100644 --- a/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt +++ b/feature_assets_impl/src/main/java/jp/co/soramitsu/feature_assets_impl/presentation/screens/send/TransferAmountViewModel.kt @@ -62,7 +62,6 @@ import jp.co.soramitsu.feature_assets_api.presentation.AssetsRouter import jp.co.soramitsu.feature_assets_impl.presentation.states.SendState import jp.co.soramitsu.feature_wallet_api.launcher.WalletRouter import jp.co.soramitsu.sora.substrate.runtime.SubstrateOptionsProvider -import jp.co.soramitsu.ui_core.component.input.number.DefaultCursorPosition import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.catch @@ -135,7 +134,7 @@ class TransferAmountViewModel @AssistedInject constructor( calcTransactionFee(asset) } } - _sendState.value.input?.initialAmount?.let { + _sendState.value.input?.amount?.let { checkEnteredAmount(it) } } @@ -163,12 +162,11 @@ class TransferAmountViewModel @AssistedInject constructor( input = AssetAmountInputState( token = asset.token, balance = getAssetBalanceText(asset), - initialAmount = initialSendAmount?.toBigDecimalOrNull(), + amount = initialSendAmount?.toBigDecimalOrNull(), amountFiat = "", enabled = false, error = false, - errorHint = "", - defaultCursorPosition = DefaultCursorPosition.END + errorHint = "" ) ) } else { @@ -177,10 +175,9 @@ class TransferAmountViewModel @AssistedInject constructor( token = asset.token, balance = getAssetBalanceText(asset), amountFiat = asset.token.printFiat( - _sendState.value.input?.initialAmount.orZero(), + _sendState.value.input?.amount.orZero(), numbersFormatter - ), - defaultCursorPosition = DefaultCursorPosition.END + ) ) ) } @@ -194,7 +191,7 @@ class TransferAmountViewModel @AssistedInject constructor( val result = interactor.isNotEnoughXorLeftAfterTransaction( networkFeeInXor = fee.orZero(), - xorChange = if (token.id == SubstrateOptionsProvider.feeAssetId) initialAmount else null, + xorChange = if (token.id == SubstrateOptionsProvider.feeAssetId) amount else null, ) _sendState.value = _sendState.value.copy( @@ -205,7 +202,7 @@ class TransferAmountViewModel @AssistedInject constructor( fun onTokenChange(tokenId: String) { curTokenId = tokenId updateCurAsset() - _sendState.value.input?.initialAmount?.let { + _sendState.value.input?.amount?.let { checkEnteredAmount(it) } hasXorReminderWarningBeenChecked = false @@ -232,8 +229,7 @@ class TransferAmountViewModel @AssistedInject constructor( _sendState.value = _sendState.value.copy( input = _sendState.value.input?.copy( readOnly = false, - initialAmount = _sendState.value.input?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _sendState.value.input?.amount?.nullZero() ), ) } @@ -298,7 +294,7 @@ class TransferAmountViewModel @AssistedInject constructor( fun onConfirmClick() { val curAsset = curAsset ?: return - val amount = _sendState.value.input?.initialAmount ?: return + val amount = _sendState.value.input?.amount ?: return val fee = fee ?: return viewModelScope.launch { _sendState.value = _sendState.value.copy( @@ -396,8 +392,7 @@ class TransferAmountViewModel @AssistedInject constructor( fun onReviewClick() { _sendState.value = _sendState.value.copy( input = _sendState.value.input?.copy( - initialAmount = _sendState.value.input?.initialAmount.orZero(), - defaultCursorPosition = DefaultCursorPosition.START + amount = _sendState.value.input?.amount.orZero() ) ) } @@ -405,12 +400,11 @@ class TransferAmountViewModel @AssistedInject constructor( fun amountChanged(value: BigDecimal) { _sendState.value = _sendState.value.copy( input = _sendState.value.input?.copy( - initialAmount = value, + amount = value, amountFiat = _sendState.value.input?.token?.printFiat( value, numbersFormatter - ).orEmpty(), - defaultCursorPosition = DefaultCursorPosition.END + ).orEmpty() ) ) enteredFlow.value = value @@ -429,12 +423,11 @@ class TransferAmountViewModel @AssistedInject constructor( } _sendState.value = _sendState.value.copy( input = _sendState.value.input?.copy( - initialAmount = amount, + amount = amount, amountFiat = _sendState.value.input?.token?.printFiat( amount, numbersFormatter - ).orEmpty(), - defaultCursorPosition = DefaultCursorPosition.START + ).orEmpty() ) ) enteredFlow.value = amount diff --git a/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt b/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt index b3b0db0c8..dead88b69 100644 --- a/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt +++ b/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/qr/QrCodeFlowViewModelTest.kt @@ -175,8 +175,8 @@ class QrCodeFlowViewModelTest { ) Assert.assertEquals( - viewModel.requestTokenScreenState.value.assetAmountInputState?.initialAmount, - viewModel.requestTokenConfirmScreenState.value.assetAmountInputState?.initialAmount + viewModel.requestTokenScreenState.value.assetAmountInputState?.amount, + viewModel.requestTokenConfirmScreenState.value.assetAmountInputState?.amount ) Assert.assertEquals( @@ -185,8 +185,8 @@ class QrCodeFlowViewModelTest { ) Assert.assertEquals( - viewModel.requestTokenScreenState.value.assetAmountInputState?.initialAmount, - viewModel.requestTokenConfirmScreenState.value.assetAmountInputState?.initialAmount + viewModel.requestTokenScreenState.value.assetAmountInputState?.amount, + viewModel.requestTokenConfirmScreenState.value.assetAmountInputState?.amount ) } diff --git a/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/send/TransferAmountViewModelTest.kt b/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/send/TransferAmountViewModelTest.kt index 096af400b..e577df282 100644 --- a/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/send/TransferAmountViewModelTest.kt +++ b/feature_assets_impl/src/test/java/jp/co/soramitsu/feature_assets_impl/presentation/send/TransferAmountViewModelTest.kt @@ -154,7 +154,7 @@ class TransferAmountViewModelTest { transferAmountViewModel.optionSelected(75) advanceUntilIdle() val state = transferAmountViewModel.sendState - assertTrue(state.value.input?.initialAmount?.equalTo(BigDecimal.valueOf(3)) == true) + assertTrue(state.value.input?.amount?.equalTo(BigDecimal.valueOf(3)) == true) } @Test @@ -164,7 +164,7 @@ class TransferAmountViewModelTest { transferAmountViewModel.optionSelected(75) advanceUntilIdle() val state = transferAmountViewModel.sendState - assertTrue(state.value.input?.initialAmount?.equalTo(BigDecimal.valueOf(0.000075)) == true) + assertTrue(state.value.input?.amount?.equalTo(BigDecimal.valueOf(0.000075)) == true) } private fun initViewModel( diff --git a/feature_blockexplorer_api/src/main/java/jp/co/soramitsu/feature_blockexplorer_api/data/BlockExplorerManager.kt b/feature_blockexplorer_api/src/main/java/jp/co/soramitsu/feature_blockexplorer_api/data/BlockExplorerManager.kt index 103f1ab8a..5d1723c43 100644 --- a/feature_blockexplorer_api/src/main/java/jp/co/soramitsu/feature_blockexplorer_api/data/BlockExplorerManager.kt +++ b/feature_blockexplorer_api/src/main/java/jp/co/soramitsu/feature_blockexplorer_api/data/BlockExplorerManager.kt @@ -40,6 +40,7 @@ import javax.inject.Singleton import jp.co.soramitsu.common.config.BuildConfigWrapper import jp.co.soramitsu.common.domain.AppStateProvider import jp.co.soramitsu.common.domain.fiatChange +import jp.co.soramitsu.common.logger.FirebaseWrapper import jp.co.soramitsu.core_db.AppDatabase import jp.co.soramitsu.core_db.model.FiatTokenPriceLocal import jp.co.soramitsu.core_db.model.ReferralLocal @@ -72,7 +73,7 @@ class BlockExplorerManager @Inject constructor( assetsInfo = it } - private suspend fun getAssetsInfoInternal(tokenIds: List): List> { + private suspend fun getAssetsInfoInternal(tokenIds: List): List> = runCatching { val selected = soraConfigManager.getSelectedCurrency() val tokens = db.assetDao().getFiatTokenPriceLocal(selected.code) val yesterdayHour = yesterday() @@ -87,7 +88,10 @@ class BlockExplorerManager @Inject constructor( resultList.add(assetInfo.tokenId to BigInteger(assetInfo.liquidity)) } db.assetDao().insertFiatPrice(fiats) - return resultList + resultList + }.getOrElse { + FirebaseWrapper.recordException(it) + emptyList() } suspend fun updatePoolsSbApy() { @@ -113,6 +117,9 @@ class BlockExplorerManager @Inject constructor( db.referralsDao().insertReferrals(it) } } + .onFailure { + FirebaseWrapper.recordException(it) + } } suspend fun getXorPerEurRatio(): Double? = runCatching { diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityAddConfirmScreen.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityAddConfirmScreen.kt index 86e3664ea..6549fe1ef 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityAddConfirmScreen.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityAddConfirmScreen.kt @@ -118,14 +118,14 @@ internal fun LiquidityAddConfirmScreen( SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.assetState1?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.assetState1?.initialAmount?.toPlainString().orEmpty(), + amount = state.assetState1?.amount?.toPlainString().orEmpty(), amountFiat = state.assetState1?.amountFiat.orEmpty(), ) Divider(color = Color.Transparent, modifier = Modifier.width(Dimens.x1)) SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.assetState2?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.assetState2?.initialAmount?.toPlainString().orEmpty(), + amount = state.assetState2?.amount?.toPlainString().orEmpty(), amountFiat = state.assetState2?.amountFiat.orEmpty(), ) } diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityRemoveConfirmScreen.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityRemoveConfirmScreen.kt index e852642fe..8044b126f 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityRemoveConfirmScreen.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/LiquidityRemoveConfirmScreen.kt @@ -116,14 +116,14 @@ internal fun LiquidityRemoveConfirmScreen( SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.assetState1?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.assetState1?.initialAmount?.toPlainString().orEmpty(), + amount = state.assetState1?.amount?.toPlainString().orEmpty(), amountFiat = state.assetState1?.amountFiat.orEmpty(), ) Divider(color = Color.Transparent, modifier = Modifier.width(Dimens.x1)) SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.assetState2?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.assetState2?.initialAmount?.toPlainString().orEmpty(), + amount = state.assetState2?.amount?.toPlainString().orEmpty(), amountFiat = state.assetState2?.amountFiat.orEmpty(), ) } diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapConfirmScreen.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapConfirmScreen.kt index d52c1c659..0a03b4490 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapConfirmScreen.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/components/compose/SwapConfirmScreen.kt @@ -117,14 +117,14 @@ internal fun SwapConfirmScreen( SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.tokenFromState?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.tokenFromState?.initialAmount?.toPlainString().orEmpty(), + amount = state.tokenFromState?.amount?.toPlainString().orEmpty(), amountFiat = state.tokenFromState?.amountFiat.orEmpty(), ) Divider(color = Color.Transparent, modifier = Modifier.width(Dimens.x1)) SwapAmountSquare( modifier = Modifier.weight(1f), icon = state.tokenToState?.token?.iconUri() ?: DEFAULT_ICON_URI, - amount = state.tokenToState?.initialAmount?.toPlainString().orEmpty(), + amount = state.tokenToState?.amount?.toPlainString().orEmpty(), amountFiat = state.tokenToState?.amountFiat.orEmpty(), ) } diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt index 08fd49932..ab27b7d36 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityadd/LiquidityAddViewModel.kt @@ -69,7 +69,6 @@ import jp.co.soramitsu.feature_wallet_api.domain.interfaces.WalletInteractor import jp.co.soramitsu.feature_wallet_api.launcher.WalletRouter import jp.co.soramitsu.sora.substrate.runtime.SubstrateOptionsProvider import jp.co.soramitsu.sora.substrate.runtime.isSynthetic -import jp.co.soramitsu.ui_core.component.input.number.DefaultCursorPosition import jp.co.soramitsu.ui_core.component.toolbar.Action import jp.co.soramitsu.ui_core.component.toolbar.BasicToolbarState import jp.co.soramitsu.ui_core.component.toolbar.SoramitsuToolbarState @@ -401,7 +400,7 @@ class LiquidityAddViewModel @AssistedInject constructor( if (desired == WithDesired.INPUT) { _addState.value = _addState.value.copy( assetState2 = _addState.value.assetState2?.copy( - initialAmount = it, + amount = it, amountFiat = _addState.value.assetState2?.token?.printFiat( it, numbersFormatter @@ -413,7 +412,7 @@ class LiquidityAddViewModel @AssistedInject constructor( } else { _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( - initialAmount = it, + amount = it, amountFiat = _addState.value.assetState1?.token?.printFiat( it, numbersFormatter @@ -494,7 +493,7 @@ class LiquidityAddViewModel @AssistedInject constructor( val result = assetsInteractor.isNotEnoughXorLeftAfterTransaction( networkFeeInXor = networkFee, - xorChange = if (assetState1.token.id == SubstrateOptionsProvider.feeAssetId) assetState1.initialAmount else null, + xorChange = if (assetState1.token.id == SubstrateOptionsProvider.feeAssetId) assetState1.amount else null, ) _addState.value = _addState.value.copy( @@ -509,12 +508,10 @@ class LiquidityAddViewModel @AssistedInject constructor( val list = assets.filter { it.token.id in bases && it.token.id != addToken2 } _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( - initialAmount = _addState.value.assetState1?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _addState.value.assetState1?.amount?.nullZero() ), assetState2 = _addState.value.assetState2?.copy( - initialAmount = _addState.value.assetState2?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _addState.value.assetState2?.amount?.nullZero() ), ) _searchTokenFilter.value = _searchTokenFilter.value.copy( @@ -552,12 +549,10 @@ class LiquidityAddViewModel @AssistedInject constructor( } _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( - initialAmount = _addState.value.assetState1?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _addState.value.assetState1?.amount?.nullZero() ), assetState2 = _addState.value.assetState2?.copy( - initialAmount = _addState.value.assetState2?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _addState.value.assetState2?.amount?.nullZero() ), ) _searchTokenFilter.value = _searchTokenFilter.value.copy( @@ -575,11 +570,7 @@ class LiquidityAddViewModel @AssistedInject constructor( assetState1 = state.copy( token = a.token, balance = getAssetBalanceText(a), - amountFiat = a.token.printFiat(state.initialAmount.orZero(), numbersFormatter), - defaultCursorPosition = DefaultCursorPosition.END - ), - assetState2 = _addState.value.assetState2?.copy( - defaultCursorPosition = DefaultCursorPosition.END + amountFiat = a.token.printFiat(state.amount.orZero(), numbersFormatter) ), ) hasXorReminderWarningBeenChecked = false @@ -592,14 +583,10 @@ class LiquidityAddViewModel @AssistedInject constructor( val a = assets.first { t -> t.token.id == id } val state = _addState.value.assetState2 _addState.value = _addState.value.copy( - assetState1 = _addState.value.assetState1?.copy( - defaultCursorPosition = DefaultCursorPosition.END - ), assetState2 = state?.copy( token = a.token, balance = getAssetBalanceText(a), - amountFiat = a.token.printFiat(state.initialAmount.orZero(), numbersFormatter), - defaultCursorPosition = DefaultCursorPosition.END + amountFiat = a.token.printFiat(state.amount.orZero(), numbersFormatter) ) ?: buildInitialAssetState(a), ) @@ -624,24 +611,16 @@ class LiquidityAddViewModel @AssistedInject constructor( _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( balance = getAssetBalanceText(asset), - token = asset.token, - defaultCursorPosition = DefaultCursorPosition.END - ), - assetState2 = _addState.value.assetState2?.copy( - defaultCursorPosition = DefaultCursorPosition.END + token = asset.token ) ) } assets.find { it.token.id == addToken2 }?.let { asset -> balance2 = asset.balance.transferable _addState.value = _addState.value.copy( - assetState1 = _addState.value.assetState1?.copy( - defaultCursorPosition = DefaultCursorPosition.END - ), assetState2 = _addState.value.assetState2?.copy( balance = getAssetBalanceText(asset), token = asset.token, - defaultCursorPosition = DefaultCursorPosition.END ) ) } @@ -736,12 +715,10 @@ class LiquidityAddViewModel @AssistedInject constructor( fun onSlippageClick() { _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( - initialAmount = _addState.value.assetState1?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _addState.value.assetState1?.amount?.nullZero(), ), assetState2 = _addState.value.assetState2?.copy( - initialAmount = _addState.value.assetState2?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _addState.value.assetState2?.amount?.nullZero(), ), ) } @@ -754,32 +731,24 @@ class LiquidityAddViewModel @AssistedInject constructor( fun onAmount1Change(value: BigDecimal) { _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( - initialAmount = value, + amount = value, amountFiat = _addState.value.assetState1?.token?.printFiat( value, numbersFormatter ).orEmpty(), - defaultCursorPosition = DefaultCursorPosition.END ), - assetState2 = _addState.value.assetState2?.copy( - defaultCursorPosition = DefaultCursorPosition.END - ) ) amount1Flow.value = value } fun onAmount2Change(value: BigDecimal) { _addState.value = _addState.value.copy( - assetState1 = _addState.value.assetState1?.copy( - defaultCursorPosition = DefaultCursorPosition.END - ), assetState2 = _addState.value.assetState2?.copy( - initialAmount = value, + amount = value, amountFiat = _addState.value.assetState2?.token?.printFiat( value, numbersFormatter ).orEmpty(), - defaultCursorPosition = DefaultCursorPosition.END ) ) amount2Flow.value = value @@ -813,11 +782,7 @@ class LiquidityAddViewModel @AssistedInject constructor( _addState.value = _addState.value.copy( assetState1 = _addState.value.assetState1?.copy( amountFiat = tokenFrom.printFiat(amount, numbersFormatter), - initialAmount = amount, - defaultCursorPosition = DefaultCursorPosition.START - ), - assetState2 = _addState.value.assetState2?.copy( - defaultCursorPosition = DefaultCursorPosition.START + amount = amount, ) ) amount1Flow.value = amount @@ -829,13 +794,9 @@ class LiquidityAddViewModel @AssistedInject constructor( tokenTo.precision ) _addState.value = _addState.value.copy( - assetState1 = _addState.value.assetState1?.copy( - defaultCursorPosition = DefaultCursorPosition.START - ), assetState2 = _addState.value.assetState2?.copy( amountFiat = tokenTo.printFiat(amount, numbersFormatter), - initialAmount = amount, - defaultCursorPosition = DefaultCursorPosition.START + amount = amount, ) ) amount2Flow.value = amount @@ -914,7 +875,7 @@ class LiquidityAddViewModel @AssistedInject constructor( private fun buildInitialAssetState(a: Asset) = AssetAmountInputState( token = a.token, balance = getAssetBalanceText(a), - initialAmount = null, + amount = null, amountFiat = "", enabled = true, ) diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt index 7de9ef3aa..53b6ae5c4 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/liquidityremove/LiquidityRemoveViewModel.kt @@ -198,7 +198,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( assetState1 = AssetAmountInputState( token = asset1.token, balance = getAssetBalanceText(asset1), - initialAmount = null, + amount = null, amountFiat = "", enabled = true, ), @@ -209,7 +209,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( assetState2 = AssetAmountInputState( token = asset2.token, balance = getAssetBalanceText(asset2), - initialAmount = null, + amount = null, amountFiat = "", enabled = true, ), @@ -426,10 +426,10 @@ class LiquidityRemoveViewModel @AssistedInject constructor( fun onSlippageClick() { removeState = removeState.copy( assetState1 = removeState.assetState1?.copy( - initialAmount = removeState.assetState1?.initialAmount?.nullZero(), + amount = removeState.assetState1?.amount?.nullZero(), ), assetState2 = removeState.assetState2?.copy( - initialAmount = removeState.assetState2?.initialAmount?.nullZero(), + amount = removeState.assetState2?.amount?.nullZero(), ), ) } @@ -447,7 +447,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( value, numbersFormatter ).orEmpty(), - initialAmount = value + amount = value ) ) amount1Flow.value = value @@ -460,7 +460,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( value, numbersFormatter ).orEmpty(), - initialAmount = value + amount = value ) ) amount2Flow.value = value @@ -491,12 +491,12 @@ class LiquidityRemoveViewModel @AssistedInject constructor( poolDataUsable?.let { poolData -> val firstAmountMin = PolkaswapFormulas.calculateMinAmount( - fromToken.initialAmount.orZero(), + fromToken.amount.orZero(), removeState.slippage ) val secondAmountMin = PolkaswapFormulas.calculateMinAmount( - toToken.initialAmount.orZero(), + toToken.amount.orZero(), removeState.slippage ) val desired = @@ -621,10 +621,10 @@ class LiquidityRemoveViewModel @AssistedInject constructor( shareOfPool = newPoolShare, ) ) - if (amount1.compareTo(removeState.assetState1?.initialAmount.orZero()) != 0) { + if (amount1.compareTo(removeState.assetState1?.amount.orZero()) != 0) { removeState = removeState.copy( assetState1 = removeState.assetState1?.copy( - initialAmount = amount1, + amount = amount1, amountFiat = removeState.assetState1?.token?.printFiat( amount1, numbersFormatter @@ -632,10 +632,10 @@ class LiquidityRemoveViewModel @AssistedInject constructor( ) ) } - if (amount2.compareTo(removeState.assetState2?.initialAmount.orZero()) != 0) { + if (amount2.compareTo(removeState.assetState2?.amount.orZero()) != 0) { removeState = removeState.copy( assetState2 = removeState.assetState2?.copy( - initialAmount = amount2, + amount = amount2, amountFiat = removeState.assetState2?.token?.printFiat( amount2, numbersFormatter @@ -687,7 +687,7 @@ class LiquidityRemoveViewModel @AssistedInject constructor( val result = assetsInteractor.isNotEnoughXorLeftAfterTransaction( networkFeeInXor = networkFee.orZero(), - xorChange = if (assetState1.token.id == SubstrateOptionsProvider.feeAssetId) -assetState1.initialAmount.orZero() else null, + xorChange = if (assetState1.token.id == SubstrateOptionsProvider.feeAssetId) -assetState1.amount.orZero() else null, ) removeState = removeState.copy( diff --git a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt index ac6578ea5..c17decfc6 100644 --- a/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt +++ b/feature_polkaswap_impl/src/main/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/screens/swap/SwapViewModel.kt @@ -75,7 +75,6 @@ import jp.co.soramitsu.feature_polkaswap_impl.presentation.states.SwapMainState import jp.co.soramitsu.feature_polkaswap_impl.presentation.states.defaultSwapDetailsState import jp.co.soramitsu.feature_wallet_api.domain.interfaces.WalletInteractor import jp.co.soramitsu.sora.substrate.runtime.SubstrateOptionsProvider -import jp.co.soramitsu.ui_core.component.input.number.DefaultCursorPosition import jp.co.soramitsu.ui_core.component.toolbar.BasicToolbarState import jp.co.soramitsu.ui_core.component.toolbar.SoramitsuToolbarState import jp.co.soramitsu.ui_core.component.toolbar.SoramitsuToolbarType @@ -178,9 +177,9 @@ class SwapViewModel @AssistedInject constructor( private var amountFromPrev: BigDecimal = BigDecimal.ZERO private var amountToPrev: BigDecimal = BigDecimal.ZERO private val amountFrom: BigDecimal - get() = _swapMainState.value.tokenFromState?.initialAmount.orZero() + get() = _swapMainState.value.tokenFromState?.amount.orZero() private val amountTo: BigDecimal - get() = _swapMainState.value.tokenToState?.initialAmount.orZero() + get() = _swapMainState.value.tokenToState?.amount.orZero() override fun startScreen(): String = SwapRoutes.start @@ -247,11 +246,11 @@ class SwapViewModel @AssistedInject constructor( .onEach { _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = BigDecimal.ZERO, + amount = BigDecimal.ZERO, amountFiat = "", ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = BigDecimal.ZERO, + amount = BigDecimal.ZERO, amountFiat = "", ), details = defaultSwapDetailsState(), @@ -286,7 +285,7 @@ class SwapViewModel @AssistedInject constructor( tokenFromState = AssetAmountInputState( token = assetFrom.token, balance = getAssetBalanceText(assetFrom), - initialAmount = null, + amount = null, amountFiat = "", enabled = false, ), @@ -300,7 +299,7 @@ class SwapViewModel @AssistedInject constructor( tokenToState = AssetAmountInputState( token = assetTo.token, balance = getAssetBalanceText(assetTo), - initialAmount = null, + amount = null, amountFiat = "", enabled = false, ), @@ -399,12 +398,10 @@ class SwapViewModel @AssistedInject constructor( fun onSlippageClick() { _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _swapMainState.value.tokenFromState?.amount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _swapMainState.value.tokenToState?.amount?.nullZero(), ), ) } @@ -413,12 +410,10 @@ class SwapViewModel @AssistedInject constructor( _swapMainState.value = _swapMainState.value.copy( selectMarketState = _swapMainState.value.market to availableMarkets, tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _swapMainState.value.tokenFromState?.amount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _swapMainState.value.tokenToState?.amount?.nullZero(), ), ) } @@ -433,12 +428,10 @@ class SwapViewModel @AssistedInject constructor( ) _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _swapMainState.value.tokenFromState?.amount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _swapMainState.value.tokenToState?.amount?.nullZero(), ), ) } @@ -454,12 +447,10 @@ class SwapViewModel @AssistedInject constructor( ) _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _swapMainState.value.tokenFromState?.amount?.nullZero(), ), tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _swapMainState.value.tokenToState?.amount?.nullZero(), ), ) } @@ -470,9 +461,9 @@ class SwapViewModel @AssistedInject constructor( if (tokenFromState == null || tokenToState == null) return@with val change = if (tokenFromState.token.id == SubstrateOptionsProvider.feeAssetId) { - tokenFromState.initialAmount + tokenFromState.amount } else if (tokenToState.token.id == SubstrateOptionsProvider.feeAssetId) { - -tokenToState.initialAmount.orZero() + -tokenToState.amount.orZero() } else { null } val result = assetsInteractor.isNotEnoughXorLeftAfterTransaction( networkFeeInXor = networkFee.orZero(), @@ -509,12 +500,10 @@ class SwapViewModel @AssistedInject constructor( desired = if (desired == WithDesired.INPUT) WithDesired.OUTPUT else WithDesired.INPUT _swapMainState.value = _swapMainState.value.copy( tokenFromState = _swapMainState.value.tokenToState?.copy( - initialAmount = _swapMainState.value.tokenToState?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _swapMainState.value.tokenToState?.amount?.nullZero(), ), tokenToState = _swapMainState.value.tokenFromState?.copy( - initialAmount = _swapMainState.value.tokenFromState?.initialAmount?.nullZero(), - defaultCursorPosition = DefaultCursorPosition.END + amount = _swapMainState.value.tokenFromState?.amount?.nullZero(), ), ) setSwapButtonLoading(true) @@ -528,7 +517,7 @@ class SwapViewModel @AssistedInject constructor( tokenToState = AssetAmountInputState( token = token, balance = getAssetBalanceText(asset), - initialAmount = amountTo.nullZero(), + amount = amountTo.nullZero(), amountFiat = token.printFiat(amountTo, numbersFormatter), enabled = true, ) @@ -541,7 +530,7 @@ class SwapViewModel @AssistedInject constructor( tokenFromState = AssetAmountInputState( token = token, balance = getAssetBalanceText(asset), - initialAmount = amountFrom.nullZero(), + amount = amountFrom.nullZero(), amountFiat = token.printFiat(amountFrom, numbersFormatter), enabled = _swapMainState.value.tokenToState != null, ) @@ -720,7 +709,7 @@ class SwapViewModel @AssistedInject constructor( it, numbersFormatter ), - initialAmount = it, + amount = it, ) ) } else { @@ -730,7 +719,7 @@ class SwapViewModel @AssistedInject constructor( it, numbersFormatter ), - initialAmount = it, + amount = it, ) ) } @@ -827,16 +816,12 @@ class SwapViewModel @AssistedInject constructor( fun onFromAmountChange(value: BigDecimal) { _swapMainState.value = _swapMainState.value.copy( - tokenToState = _swapMainState.value.tokenToState?.copy( - defaultCursorPosition = DefaultCursorPosition.START - ), tokenFromState = _swapMainState.value.tokenFromState?.copy( - initialAmount = value, + amount = value, amountFiat = _swapMainState.value.tokenFromState?.token?.printFiat( value, numbersFormatter ).orEmpty(), - defaultCursorPosition = DefaultCursorPosition.START ) ) fromAmountOnEach() @@ -846,7 +831,7 @@ class SwapViewModel @AssistedInject constructor( fun onToAmountChange(value: BigDecimal) { _swapMainState.value = _swapMainState.value.copy( tokenToState = _swapMainState.value.tokenToState?.copy( - initialAmount = value, + amount = value, amountFiat = _swapMainState.value.tokenToState?.token?.printFiat( value, numbersFormatter @@ -934,12 +919,12 @@ class SwapViewModel @AssistedInject constructor( fromState.token, toState.token, desired, - if (desired == WithDesired.INPUT) fromState.initialAmount.orZero() else toState.initialAmount.orZero(), + if (desired == WithDesired.INPUT) fromState.amount.orZero() else toState.amount.orZero(), details.minmax, details.networkFee, details.liquidityFee, details.dex.dexId, - if (desired == WithDesired.OUTPUT) fromState.initialAmount.orZero() else toState.initialAmount.orZero(), + if (desired == WithDesired.OUTPUT) fromState.amount.orZero() else toState.amount.orZero(), ) } catch (t: Throwable) { onError(t) @@ -987,13 +972,9 @@ class SwapViewModel @AssistedInject constructor( amount = subtractFee(amount, transferable, networkFee) } _swapMainState.value = _swapMainState.value.copy( - tokenToState = _swapMainState.value.tokenToState?.copy( - defaultCursorPosition = DefaultCursorPosition.START - ), tokenFromState = _swapMainState.value.tokenFromState?.copy( amountFiat = fromAsset.token.printFiat(amount, numbersFormatter), - initialAmount = amount, - defaultCursorPosition = DefaultCursorPosition.START + amount = amount, ) ) desired = WithDesired.INPUT diff --git a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt index 59260b896..de953617c 100644 --- a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt +++ b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/SwapViewModelTest.kt @@ -285,7 +285,7 @@ class SwapViewModelTest { advanceUntilIdle() viewModel.fromInputPercentClicked(100) advanceUntilIdle() - assertEquals(BigDecimal.ONE, viewModel.swapMainState.value.tokenFromState?.initialAmount) + assertEquals(BigDecimal.ONE, viewModel.swapMainState.value.tokenFromState?.amount) val btn = viewModel.swapMainState.value.swapButtonState assertEquals("Insufficient balance", btn.text) assertEquals(false, btn.enabled) @@ -300,7 +300,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(100) - assertEquals(BigDecimal(90.0), viewModel.swapMainState.value.tokenFromState?.initialAmount) + assertEquals(BigDecimal(90.0), viewModel.swapMainState.value.tokenFromState?.amount) } @Test @@ -312,7 +312,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(50) - assertEquals(BigDecimal(50.0).setScale(18), viewModel.swapMainState.value.tokenFromState?.initialAmount) + assertEquals(BigDecimal(50.0).setScale(18), viewModel.swapMainState.value.tokenFromState?.amount) } @Test @@ -324,7 +324,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(50) - assertEquals(BigDecimal(8.0).setScale(18), viewModel.swapMainState.value.tokenFromState?.initialAmount) + assertEquals(BigDecimal(8.0).setScale(18), viewModel.swapMainState.value.tokenFromState?.amount) } @Test @@ -336,7 +336,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(50) - assertEquals(BigDecimal(25).setScale(18), viewModel.swapMainState.value.tokenFromState?.initialAmount) + assertEquals(BigDecimal(25).setScale(18), viewModel.swapMainState.value.tokenFromState?.amount) } @Test @@ -348,7 +348,7 @@ class SwapViewModelTest { viewModel.toAssetSelected(assetsListItems.last().tokenId) advanceUntilIdle() viewModel.fromInputPercentClicked(100) - assertEquals(BigDecimal(100.0), viewModel.swapMainState.value.tokenFromState?.initialAmount) + assertEquals(BigDecimal(100.0), viewModel.swapMainState.value.tokenFromState?.amount) } @Test diff --git a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/add/AddLiquidityViewModelTest.kt b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/add/AddLiquidityViewModelTest.kt index 8edb14616..8e6311278 100644 --- a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/add/AddLiquidityViewModelTest.kt +++ b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/add/AddLiquidityViewModelTest.kt @@ -39,7 +39,6 @@ import io.mockk.mockkObject import io.mockk.mockkStatic import jp.co.soramitsu.common.R import jp.co.soramitsu.common.domain.CoroutineManager -import jp.co.soramitsu.common.domain.PoolDex import jp.co.soramitsu.common.domain.Token import jp.co.soramitsu.common.domain.iconUri import jp.co.soramitsu.common.logger.FirebaseWrapper @@ -49,7 +48,6 @@ import jp.co.soramitsu.common.util.ext.equalTo import jp.co.soramitsu.common_wallet.domain.model.LiquidityData import jp.co.soramitsu.feature_assets_api.domain.AssetsInteractor import jp.co.soramitsu.feature_assets_api.presentation.AssetsRouter -import jp.co.soramitsu.feature_main_api.launcher.MainRouter import jp.co.soramitsu.feature_polkaswap_api.domain.interfaces.PoolsInteractor import jp.co.soramitsu.feature_polkaswap_impl.presentation.screens.liquidityadd.LiquidityAddViewModel import jp.co.soramitsu.feature_wallet_api.domain.interfaces.WalletInteractor @@ -236,7 +234,7 @@ class AddLiquidityViewModelTest { advanceUntilIdle() viewModel.onAmount1Change(BigDecimal("110.34")) advanceUntilIdle() - assertEquals(BigDecimal("110.34"), viewModel.addState.value.assetState1?.initialAmount) + assertEquals(BigDecimal("110.34"), viewModel.addState.value.assetState1?.amount) } @Test @@ -249,7 +247,7 @@ class AddLiquidityViewModelTest { advanceUntilIdle() viewModel.onAmount2Change(BigDecimal("110.34")) advanceUntilIdle() - assertEquals(BigDecimal("110.34"), viewModel.addState.value.assetState2?.initialAmount) + assertEquals(BigDecimal("110.34"), viewModel.addState.value.assetState2?.amount) } @Test @@ -299,7 +297,7 @@ class AddLiquidityViewModelTest { viewModel.optionSelected(50) advanceUntilIdle() - assertTrue(viewModel.addState.value.assetState1?.initialAmount?.equalTo(BigDecimal(0.5)) == true) + assertTrue(viewModel.addState.value.assetState1?.amount?.equalTo(BigDecimal(0.5)) == true) } @Test @@ -315,7 +313,7 @@ class AddLiquidityViewModelTest { viewModel.optionSelected(50) advanceUntilIdle() - assertTrue(viewModel.addState.value.assetState2?.initialAmount?.equalTo(BigDecimal(0.5)) == true) + assertTrue(viewModel.addState.value.assetState2?.amount?.equalTo(BigDecimal(0.5)) == true) } @Test diff --git a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/remove/RemoveLiquidityViewModelTest.kt b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/remove/RemoveLiquidityViewModelTest.kt index 25c7de876..ae226fc29 100644 --- a/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/remove/RemoveLiquidityViewModelTest.kt +++ b/feature_polkaswap_impl/src/test/java/jp/co/soramitsu/feature_polkaswap_impl/presentation/polkaswap/liquidity/remove/RemoveLiquidityViewModelTest.kt @@ -197,8 +197,8 @@ class RemoveLiquidityViewModelTest { viewModel.onAmount1Change(BigDecimal(0.5)) advanceUntilIdle() - assertEquals(BigDecimal(0.5), viewModel.removeState.assetState1?.initialAmount) - assertEquals(BigDecimal(0.5), viewModel.removeState.assetState2?.initialAmount) + assertEquals(BigDecimal(0.5), viewModel.removeState.assetState1?.amount) + assertEquals(BigDecimal(0.5), viewModel.removeState.assetState2?.amount) assertEquals("Remove", viewModel.removeState.btnState.text) assertTrue(viewModel.removeState.btnState.enabled) } @@ -212,8 +212,8 @@ class RemoveLiquidityViewModelTest { viewModel.onAmount2Change(BigDecimal("0.35")) advanceUntilIdle() - assertEquals(BigDecimal("0.35"), viewModel.removeState.assetState1?.initialAmount) - assertEquals(BigDecimal("0.35"), viewModel.removeState.assetState2?.initialAmount) + assertEquals(BigDecimal("0.35"), viewModel.removeState.assetState1?.amount) + assertEquals(BigDecimal("0.35"), viewModel.removeState.assetState2?.amount) assertTrue(viewModel.removeState.btnState.enabled) }