Skip to content

Commit

Permalink
Prohibit identical currencies (#137)
Browse files Browse the repository at this point in the history
* Prohibit identical currencies
* Portfolio: Replace currencies info dialog with general InfoDialog
* Change alpha for prohibited codes, use CurrencySearchModel
  • Loading branch information
mdrlzy authored Jan 30, 2025
1 parent d370d54 commit d60d770
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand All @@ -25,15 +24,25 @@ import dev.arkbuilders.rate.core.presentation.R
import dev.arkbuilders.rate.core.presentation.theme.ArkColor

@Composable
fun InfoMarketCapitalizationDialog(onDismiss: () -> Unit) {
fun InfoDialog(
title: String,
desc: String,
onDismiss: () -> Unit,
) {
Dialog(onDismissRequest = { onDismiss() }) {
InfoMarketCapitalizationDialogContent(onDismiss)
Content(title, desc, onDismiss)
}
}

@Preview
@Preview(showBackground = true)
@Composable
private fun InfoMarketCapitalizationDialogContent(onDismiss: () -> Unit = {}) {
private fun Content(
title: String = "Currency Already Selected",
desc: String =
"Please choose a different currency to complete the pairing. " +
"Identical currencies cannot be paired together.",
onDismiss: () -> Unit = {},
) {
Column(
modifier =
Modifier
Expand All @@ -59,7 +68,7 @@ private fun InfoMarketCapitalizationDialogContent(onDismiss: () -> Unit = {}) {
onClick = { onDismiss() },
) {
Icon(
modifier = Modifier,
modifier = Modifier.size(12.dp),
painter = painterResource(id = R.drawable.ic_close),
contentDescription = "",
tint = ArkColor.FGQuinary,
Expand All @@ -68,73 +77,15 @@ private fun InfoMarketCapitalizationDialogContent(onDismiss: () -> Unit = {}) {
}
Text(
modifier = Modifier.padding(top = 12.dp, start = 16.dp, end = 16.dp),
text = "Market Capitalization",
text = title,
fontWeight = FontWeight.SemiBold,
fontSize = 18.sp,
color = ArkColor.TextPrimary,
)
Text(
modifier = Modifier.padding(top = 4.dp, start = 16.dp, end = 16.dp, bottom = 36.dp),
text = stringResource(id = R.string.info_dialog_market_capitalization_description),
fontSize = 18.sp,
color = ArkColor.TextTertiary,
)
}
}

@Composable
fun InfoValueOfCirculatingDialog(onDismiss: () -> Unit) {
Dialog(onDismissRequest = { onDismiss() }) {
InfoValueOfCirculatingDialogContent(onDismiss)
}
}

@Preview
@Composable
private fun InfoValueOfCirculatingDialogContent(onDismiss: () -> Unit = {}) {
Column(
modifier =
Modifier
.fillMaxWidth()
.background(Color.White, RoundedCornerShape(12.dp)),
) {
Box(modifier = Modifier.fillMaxWidth()) {
Icon(
modifier =
Modifier
.padding(top = 20.dp, start = 16.dp)
.align(Alignment.TopStart),
painter = painterResource(id = R.drawable.ic_info_bg),
contentDescription = "",
tint = Color.Unspecified,
)
IconButton(
modifier =
Modifier
.size(44.dp)
.padding(top = 12.dp, end = 12.dp)
.align(Alignment.TopEnd),
onClick = { onDismiss() },
) {
Icon(
modifier = Modifier,
painter = painterResource(id = R.drawable.ic_close),
contentDescription = "",
tint = ArkColor.FGQuinary,
)
}
}
Text(
modifier = Modifier.padding(top = 12.dp, start = 16.dp, end = 16.dp),
text = "Value of Circulating Currency",
fontWeight = FontWeight.SemiBold,
fontSize = 18.sp,
color = ArkColor.TextPrimary,
)
Text(
modifier = Modifier.padding(top = 4.dp, start = 16.dp, end = 16.dp, bottom = 36.dp),
text = stringResource(id = R.string.info_dialog_value_of_circulating_description),
fontSize = 18.sp,
text = desc,
fontSize = 14.sp,
color = ArkColor.TextTertiary,
)
}
Expand Down
5 changes: 5 additions & 0 deletions core/presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<string name="app_name">ARK Rate</string>
<string name="app_name_debug">ARK Rate [Debug]</string>

<string name="info_dialog_market_capitalization">Market Capitalization</string>
<string name="info_dialog_market_capitalization_description">The total market value of a cryptocurrency\'s circulating supply.\n\nIt is analogous to the free-float capitalization in the stock market.\n\nCap = Current Price x Circulating Supply.</string>
<string name="info_dialog_value_of_circulating">Value of Circulating Currency</string>
<string name="info_dialog_value_of_circulating_description">Currency in circulation refers to the amount of cash–in the form of paper notes or coins–within a country that is physically used to conduct transactions between consumers and businesses.</string>

<string name="bottom_nav_portfolio">Portfolios</string>
Expand Down Expand Up @@ -64,6 +66,9 @@
<string name="settings_latest_rates_refresh">Latest rates refresh</string>
<string name="settings_latest_alerts_check">Latest alerts check</string>

<string name="search_currency_already_selected">Currency already selected</string>
<string name="search_currency_already_selected_desc">Please choose a different currency. Identical currencies cannot be used together.</string>

<string name="group_create_group">Create Group</string>
<string name="group_please_enter_a_name_for_this_group">Please enter a name for this group.</string>
<string name="group_name">Group name</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ fun AddPairAlertScreen(
)
AppSharedFlow.ShowAddedSnackbarPairAlert.flow.emit(visuals)
}

is AddPairAlertScreenEffect.NavigateSearchBase ->
navigator.navigate(
SearchCurrencyScreenDestination(
appSharedFlowKeyString = AppSharedFlowKey.AddPairAlertBase.name,
prohibitedCodes = effect.prohibitedCodes.toTypedArray(),
),
)

is AddPairAlertScreenEffect.NavigateSearchTarget ->
navigator.navigate(
SearchCurrencyScreenDestination(
appSharedFlowKeyString = AppSharedFlowKey.AddPairAlertTarget.name,
prohibitedCodes = effect.prohibitedCodes.toTypedArray(),
),
)
}
}

Expand All @@ -127,24 +143,40 @@ fun AddPairAlertScreen(
},
) {
Box(modifier = Modifier.padding(it)) {
Content(state, navigator, viewModel)
Content(
state = state,
navigateSearchBase = viewModel::onNavigateSearchBase,
navigateSearchTarget = viewModel::onNavigateSearchTarget,
onGroupSelect = viewModel::onGroupSelect,
onPriceOrPercentChanged = viewModel::onPriceOrPercentChanged,
onOneTimeChanged = viewModel::onOneTimeChanged,
onPriceOrPercentInputChanged = viewModel::onPriceOrPercentInputChanged,
onIncreaseToggle = viewModel::onIncreaseToggle,
onSaveClick = viewModel::onSaveClick,
)
}
}
}

@Composable
private fun Content(
state: AddPairAlertScreenState,
navigator: DestinationsNavigator,
viewModel: AddPairAlertViewModel,
navigateSearchBase: () -> Unit,
navigateSearchTarget: () -> Unit,
onGroupSelect: (String) -> Unit,
onPriceOrPercentChanged: (Boolean) -> Unit,
onOneTimeChanged: (Boolean) -> Unit,
onPriceOrPercentInputChanged: (String) -> Unit,
onIncreaseToggle: () -> Unit,
onSaveClick: () -> Unit,
) {
var showNewGroupDialog by remember { mutableStateOf(false) }
var showGroupsPopup by remember { mutableStateOf(false) }
var addGroupBtnWidth by remember { mutableStateOf(0) }

if (showNewGroupDialog) {
GroupCreateDialog(onDismiss = { showNewGroupDialog = false }) {
viewModel.onGroupSelect(it)
onGroupSelect(it)
}
}

Expand All @@ -155,12 +187,18 @@ private fun Content(
.weight(1f)
.verticalScroll(rememberScrollState()),
) {
PriceOrPercent(state, viewModel::onPriceOrPercentChanged)
EditCondition(state, viewModel, navigator)
PriceOrPercent(state, onPriceOrPercentChanged)
EditCondition(
state = state,
navigateSearchBase = navigateSearchBase,
navigateSearchTarget = navigateSearchTarget,
onPriceOrPercentInputChanged = onPriceOrPercentInputChanged,
onIncreaseToggle = onIncreaseToggle,
)
OneTimeOrRecurrent(
state.priceOrPercent.isLeft(),
state.oneTimeNotRecurrent,
viewModel::onOneTimeChanged,
onOneTimeChanged,
)
DropDownWithIcon(
modifier =
Expand Down Expand Up @@ -192,7 +230,7 @@ private fun Content(
GroupSelectPopup(
groups = state.availableGroups,
widthPx = addGroupBtnWidth,
onGroupSelect = { viewModel.onGroupSelect(it) },
onGroupSelect = onGroupSelect,
onNewGroupClick = { showNewGroupDialog = true },
onDismiss = { showGroupsPopup = false },
)
Expand All @@ -207,7 +245,7 @@ private fun Content(
Modifier
.fillMaxWidth()
.padding(16.dp),
onClick = { viewModel.onSaveClick() },
onClick = onSaveClick,
enabled = state.finishEnabled,
) {
Text(
Expand Down Expand Up @@ -344,8 +382,10 @@ private fun DropDownBtn(
@Composable
private fun EditCondition(
state: AddPairAlertScreenState,
viewModel: AddPairAlertViewModel,
navigator: DestinationsNavigator,
navigateSearchBase: () -> Unit,
navigateSearchTarget: () -> Unit,
onPriceOrPercentInputChanged: (String) -> Unit,
onIncreaseToggle: () -> Unit,
) {
val ctx = LocalContext.current
Column(
Expand All @@ -368,9 +408,7 @@ private fun EditCondition(
modifier = Modifier.padding(start = 8.dp),
title = state.targetCode,
) {
navigator.navigate(
SearchCurrencyScreenDestination(AppSharedFlowKey.AddPairAlertTarget.name),
)
navigateSearchTarget()
}
Text(
modifier = Modifier.padding(start = 8.dp),
Expand All @@ -385,7 +423,7 @@ private fun EditCondition(
if (state.oneTimeNotRecurrent && state.priceOrPercent.isLeft())
this
else
clickable { viewModel.onIncreaseToggle() }
clickable { onIncreaseToggle() }
},
verticalAlignment = Alignment.CenterVertically,
) {
Expand Down Expand Up @@ -448,7 +486,7 @@ private fun EditCondition(
ifLeft = { it },
ifRight = { it },
),
onValueChange = { viewModel.onPriceOrPercentInputChanged(it) },
onValueChange = { onPriceOrPercentInputChanged(it) },
)
if (state.priceOrPercent.isLeft()) {
Text(
Expand Down Expand Up @@ -488,9 +526,7 @@ private fun EditCondition(
modifier = Modifier.padding(start = 16.dp),
title = state.baseCode,
) {
navigator.navigate(
SearchCurrencyScreenDestination(AppSharedFlowKey.AddPairAlertBase.name),
)
navigateSearchBase()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ sealed class AddPairAlertScreenEffect {
data object NavigateBack : AddPairAlertScreenEffect()

class NotifyPairAdded(val pair: PairAlert) : AddPairAlertScreenEffect()

data class NavigateSearchTarget(
val prohibitedCodes: List<CurrencyCode>,
) : AddPairAlertScreenEffect()

data class NavigateSearchBase(
val prohibitedCodes: List<CurrencyCode>,
) : AddPairAlertScreenEffect()
}

class AddPairAlertViewModel(
Expand Down Expand Up @@ -346,6 +354,18 @@ class AddPairAlertViewModel(
reduce { state.copy(finishEnabled = enabled) }
}

fun onNavigateSearchBase() =
intent {
val prohibitedCodes = listOf(state.targetCode)
postSideEffect(AddPairAlertScreenEffect.NavigateSearchBase(prohibitedCodes))
}

fun onNavigateSearchTarget() =
intent {
val prohibitedCodes = listOf(state.baseCode)
postSideEffect(AddPairAlertScreenEffect.NavigateSearchTarget(prohibitedCodes))
}

companion object {
private val INITIAL_ONE_TIME_SCALE = BigDecimal.valueOf(1.1)
private val INITIAL_RECURRENT_SCALE = BigDecimal.valueOf(10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ fun AddAssetScreen(navigator: DestinationsNavigator) {
),
)
}

is AddAssetSideEffect.NavigateSearchAdd ->
navigator.navigate(
SearchCurrencyScreenDestination(
appSharedFlowKeyString = AppSharedFlowKey.AddAsset.toString(),
prohibitedCodes = effect.prohibitedCodes.toTypedArray(),
),
)

is AddAssetSideEffect.NavigateSearchSet ->
navigator.navigate(
SearchCurrencyScreenDestination(
appSharedFlowKeyString = AppSharedFlowKey.SetAssetCode.name,
pos = effect.index,
prohibitedCodes = effect.prohibitedCodes.toTypedArray(),
),
)
}
}

Expand All @@ -115,18 +132,10 @@ fun AddAssetScreen(navigator: DestinationsNavigator) {
Content(
state = state,
onAssetValueChanged = viewModel::onAssetValueChange,
onNewCurrencyClick = {
navigator.navigate(
SearchCurrencyScreenDestination(AppSharedFlowKey.AddAsset.toString()),
)
},
onNewCurrencyClick = viewModel::onAddCode,
onAssetRemove = viewModel::onAssetRemove,
onGroupSelect = viewModel::onGroupSelect,
onCodeChange = {
navigator.navigate(
SearchCurrencyScreenDestination(AppSharedFlowKey.SetAssetCode.name, it),
)
},
onCodeChange = viewModel::onSetCode,
onAddAsset = viewModel::onAddAsset,
)
}
Expand Down
Loading

0 comments on commit d60d770

Please sign in to comment.