From b73d7e9bada5cecac2bce665880df39c314751ca Mon Sep 17 00:00:00 2001 From: mdrlzy Date: Sat, 21 Dec 2024 07:10:34 +0600 Subject: [PATCH] Quick: swap inpunt/output btn --- .../arkbuilders/rate/core/domain/CurrUtils.kt | 1 - .../src/main/res/drawable/ic_refresh.xml | 12 +++++++ .../quick/presentation/add/AddQuickScreen.kt | 32 +++++++++++++++++-- .../presentation/add/AddQuickViewModel.kt | 26 +++++++++++++++ 4 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 core/presentation/src/main/res/drawable/ic_refresh.xml diff --git a/core/domain/src/main/java/dev/arkbuilders/rate/core/domain/CurrUtils.kt b/core/domain/src/main/java/dev/arkbuilders/rate/core/domain/CurrUtils.kt index ec0bd4d11..2adf09b63 100644 --- a/core/domain/src/main/java/dev/arkbuilders/rate/core/domain/CurrUtils.kt +++ b/core/domain/src/main/java/dev/arkbuilders/rate/core/domain/CurrUtils.kt @@ -64,7 +64,6 @@ object CurrUtils { "#." + "#".repeat(fractionSize), DecimalFormatSymbols(Locale.ENGLISH), ) - df.roundingMode = RoundingMode.CEILING return df.format(number) } diff --git a/core/presentation/src/main/res/drawable/ic_refresh.xml b/core/presentation/src/main/res/drawable/ic_refresh.xml new file mode 100644 index 000000000..ad5cec5cb --- /dev/null +++ b/core/presentation/src/main/res/drawable/ic_refresh.xml @@ -0,0 +1,12 @@ + + + + diff --git a/feature/quick/src/main/java/dev/arkbuilders/rate/feature/quick/presentation/add/AddQuickScreen.kt b/feature/quick/src/main/java/dev/arkbuilders/rate/feature/quick/presentation/add/AddQuickScreen.kt index d00822d35..2fa235f13 100644 --- a/feature/quick/src/main/java/dev/arkbuilders/rate/feature/quick/presentation/add/AddQuickScreen.kt +++ b/feature/quick/src/main/java/dev/arkbuilders/rate/feature/quick/presentation/add/AddQuickScreen.kt @@ -17,6 +17,7 @@ import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.verticalScroll @@ -25,6 +26,7 @@ import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Icon +import androidx.compose.material3.OutlinedButton import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -60,7 +62,7 @@ import dev.arkbuilders.rate.core.presentation.AppSharedFlowKey import dev.arkbuilders.rate.core.presentation.R import dev.arkbuilders.rate.core.presentation.theme.ArkColor import dev.arkbuilders.rate.core.presentation.ui.AppButton -import dev.arkbuilders.rate.core.presentation.ui.AppHorDiv16 +import dev.arkbuilders.rate.core.presentation.ui.AppHorDiv import dev.arkbuilders.rate.core.presentation.ui.AppTopBarBack import dev.arkbuilders.rate.core.presentation.ui.ArkBasicTextField import dev.arkbuilders.rate.core.presentation.ui.DropDownWithIcon @@ -150,6 +152,7 @@ fun AddQuickScreen( ), ) }, + onSwapClick = viewModel::onSwapClick, onAddAsset = viewModel::onAddQuickPair, ) } @@ -165,6 +168,7 @@ private fun Content( onCurrencyRemove: (Int) -> Unit = {}, onGroupSelect: (String) -> Unit = {}, onCodeChange: (Int) -> Unit = {}, + onSwapClick: () -> Unit = {}, onAddAsset: () -> Unit = {}, ) { var showNewGroupDialog by remember { mutableStateOf(false) } @@ -184,7 +188,7 @@ private fun Content( .weight(1f) .verticalScroll(rememberScrollState()), ) { - Currencies(state, onAmountChanged, onCurrencyRemove, onCodeChange) + Currencies(state, onAmountChanged, onCurrencyRemove, onCodeChange, onSwapClick) Button( modifier = Modifier.padding(start = 16.dp, top = 16.dp), shape = RoundedCornerShape(8.dp), @@ -277,6 +281,7 @@ private fun Currencies( onAmountChanged: (String) -> Unit, onCurrencyRemove: (Int) -> Unit, onCodeChange: (Int) -> Unit, + onSwapClick: () -> Unit, ) { val from = state.currencies.first() Text( @@ -292,7 +297,7 @@ private fun Currencies( onAmountChanged = onAmountChanged, onCodeChange = onCodeChange, ) - AppHorDiv16(modifier = Modifier.padding(top = 16.dp)) + SwapBtn(modifier = Modifier.padding(top = 16.dp), onClick = onSwapClick) Text( modifier = Modifier.padding(top = 16.dp, start = 16.dp), text = "To", @@ -468,3 +473,24 @@ private fun ToResult( } } } + +@Composable +private fun SwapBtn( + modifier: Modifier = Modifier, + onClick: () -> Unit, +) { + Row(modifier = modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) { + AppHorDiv(modifier = Modifier.weight(1f).padding(start = 16.dp, end = 12.dp)) + OutlinedButton( + modifier = Modifier.size(40.dp), + shape = CircleShape, + border = BorderStroke(1.dp, ArkColor.BorderSecondary), + contentPadding = PaddingValues(0.dp), + onClick = onClick, + colors = ButtonDefaults.outlinedButtonColors(contentColor = Color.Black), + ) { + Icon(painter = painterResource(R.drawable.ic_refresh), contentDescription = null) + } + AppHorDiv(modifier = Modifier.weight(1f).padding(start = 12.dp, end = 16.dp)) + } +} diff --git a/feature/quick/src/main/java/dev/arkbuilders/rate/feature/quick/presentation/add/AddQuickViewModel.kt b/feature/quick/src/main/java/dev/arkbuilders/rate/feature/quick/presentation/add/AddQuickViewModel.kt index 3c28d4f54..032bfb9e6 100644 --- a/feature/quick/src/main/java/dev/arkbuilders/rate/feature/quick/presentation/add/AddQuickViewModel.kt +++ b/feature/quick/src/main/java/dev/arkbuilders/rate/feature/quick/presentation/add/AddQuickViewModel.kt @@ -138,6 +138,32 @@ class AddQuickViewModel( checkFinishEnabled() } + fun onSwapClick() = + intent { + if (state.currencies.size < 2) + return@intent + + val newFrom = + state.currencies.last().let { amount -> + if (amount.value.isEmpty()) + return@let amount + + val withoutCommas = amount.value.replace(",", "") + amount.copy(value = CurrUtils.roundOff(withoutCommas.toBigDecimalArk())) + } + val newList = + state.currencies.toMutableList().apply { + removeLast() + add(0, newFrom) + } + + val calc = calcToResult(newList) + + reduce { + state.copy(currencies = calc) + } + } + fun onAddQuickPair() = intent { val from = state.currencies.first()