Skip to content

Commit

Permalink
Merge pull request #1106 from radixdlt/feature/persona-label-name-limit
Browse files Browse the repository at this point in the history
Add persona label length limit
  • Loading branch information
jakub-rdx authored Aug 5, 2024
2 parents 5bf9735 + 16908ef commit f6c3f82
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.babylon.wallet.android.presentation.common.UiMessage
import com.babylon.wallet.android.presentation.common.UiState
import com.babylon.wallet.android.utils.AppEvent
import com.babylon.wallet.android.utils.AppEventBus
import com.babylon.wallet.android.utils.Constants.ACCOUNT_NAME_MAX_LENGTH
import com.babylon.wallet.android.utils.Constants.ENTITY_NAME_MAX_LENGTH
import com.radixdlt.sargon.Account
import com.radixdlt.sargon.AccountAddress
import com.radixdlt.sargon.DisplayName
Expand Down Expand Up @@ -75,8 +75,8 @@ class CreateAccountViewModel @Inject constructor(

fun onAccountNameChange(accountName: String) {
savedStateHandle[ACCOUNT_NAME] = accountName
savedStateHandle[IS_ACCOUNT_NAME_LENGTH_MORE_THAN_THE_MAX] = accountName.count() > ACCOUNT_NAME_MAX_LENGTH
savedStateHandle[CREATE_ACCOUNT_BUTTON_ENABLED] = accountName.trim().isNotEmpty() && accountName.count() <= ACCOUNT_NAME_MAX_LENGTH
savedStateHandle[IS_ACCOUNT_NAME_LENGTH_MORE_THAN_THE_MAX] = accountName.count() > ENTITY_NAME_MAX_LENGTH
savedStateHandle[CREATE_ACCOUNT_BUTTON_ENABLED] = accountName.trim().isNotEmpty() && accountName.count() <= ENTITY_NAME_MAX_LENGTH
}

fun onAccountCreateClick(isWithLedger: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.babylon.wallet.android.presentation.common.UiMessage
import com.babylon.wallet.android.presentation.common.UiState
import com.babylon.wallet.android.utils.AppEvent
import com.babylon.wallet.android.utils.AppEventBus
import com.babylon.wallet.android.utils.Constants.ACCOUNT_NAME_MAX_LENGTH
import com.babylon.wallet.android.utils.Constants.ENTITY_NAME_MAX_LENGTH
import com.radixdlt.sargon.Account
import com.radixdlt.sargon.DepositRule
import com.radixdlt.sargon.DisplayName
Expand Down Expand Up @@ -112,8 +112,8 @@ class AccountSettingsViewModel @Inject constructor(
_state.update { accountPreferenceUiState ->
accountPreferenceUiState.copy(
accountNameChanged = accountNameChanged,
isNewNameValid = accountNameChanged.isNotBlank() && accountNameChanged.count() <= ACCOUNT_NAME_MAX_LENGTH,
isNewNameLengthMoreThanTheMaximum = accountNameChanged.count() > ACCOUNT_NAME_MAX_LENGTH
isNewNameValid = accountNameChanged.isNotBlank() && accountNameChanged.count() <= ENTITY_NAME_MAX_LENGTH,
isNewNameLengthMoreThanTheMaximum = accountNameChanged.count() > ENTITY_NAME_MAX_LENGTH
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.babylon.wallet.android.presentation.model.PersonaFieldWrapper
import com.babylon.wallet.android.presentation.model.empty
import com.babylon.wallet.android.presentation.model.isValid
import com.babylon.wallet.android.presentation.model.sortOrderInt
import com.babylon.wallet.android.utils.Constants
import com.radixdlt.sargon.Persona
import com.radixdlt.sargon.PersonaDataEntryId
import kotlinx.collections.immutable.ImmutableList
Expand All @@ -30,7 +31,6 @@ interface PersonaEditable {
fun onAddFields()
fun validateInput()
fun onFieldFocusChanged(id: PersonaDataEntryId, focused: Boolean)
fun onPersonaDisplayNameFieldFocusChanged(focused: Boolean)
}

class PersonaEditableImpl : PersonaEditable {
Expand All @@ -46,7 +46,7 @@ class PersonaEditableImpl : PersonaEditable {
PersonaFieldWrapper(
id = it.uuid,
entry = IdentifiedEntry.init(it.value, it.id),
valid = true,
isValid = true,
required = requiredFieldKinds.contains(it.value.kind)
)
}.orEmpty().sortedBy { it.entry.value.sortOrderInt() }.toPersistentList()
Expand All @@ -56,7 +56,9 @@ class PersonaEditableImpl : PersonaEditable {
requiredFieldKinds = requiredFieldKinds.toPersistentList(),
currentFields = currentFields,
fieldsToAdd = fieldsToAdd,
personaDisplayName = PersonaDisplayNameFieldWrapper(persona?.displayName?.value.orEmpty())
personaDisplayName = PersonaDisplayNameFieldWrapper(
persona?.displayName?.value.orEmpty().take(Constants.ENTITY_NAME_MAX_LENGTH)
)
)
}
if (shouldValidateInput) {
Expand Down Expand Up @@ -103,18 +105,6 @@ class PersonaEditableImpl : PersonaEditable {
validateInput()
}

override fun onPersonaDisplayNameFieldFocusChanged(focused: Boolean) {
if (!focused) {
_state.update { s ->
val displayName = s.personaDisplayName
s.copy(
personaDisplayName = displayName.copy(shouldDisplayValidationError = displayName.wasEdited)
)
}
validateInput()
}
}

override fun onDisplayNameChanged(value: String) {
_state.update { state ->
state.copy(personaDisplayName = state.personaDisplayName.copy(value = value, wasEdited = true))
Expand Down Expand Up @@ -152,15 +142,11 @@ class PersonaEditableImpl : PersonaEditable {

override fun validateInput() {
val validatedFields = _state.value.currentFields.map { field ->
field.copy(valid = field.entry.value.isValid())
field.copy(isValid = field.entry.value.isValid())
}
_state.update { state ->
state.copy(
currentFields = validatedFields.toPersistentList(),
inputValid = validatedFields.all { it.valid == true } && state.personaDisplayName.value.trim().isNotEmpty(),
personaDisplayName = state.personaDisplayName.copy(
valid = state.personaDisplayName.value.trim().isNotEmpty()
)
currentFields = validatedFields.toPersistentList()
)
}
}
Expand All @@ -186,6 +172,8 @@ data class PersonaEditLogicState(
val currentFields: ImmutableList<PersonaFieldWrapper> = persistentListOf(),
val fieldsToAdd: ImmutableList<PersonaFieldWrapper> = persistentListOf(),
val personaDisplayName: PersonaDisplayNameFieldWrapper = PersonaDisplayNameFieldWrapper(),
val areThereFieldsSelected: Boolean = false,
val inputValid: Boolean = true
)
val areThereFieldsSelected: Boolean = false
) {
val isInputValid: Boolean
get() = currentFields.all { it.isValid == true } && personaDisplayName.isValid
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
package com.babylon.wallet.android.presentation.model

import com.babylon.wallet.android.utils.Constants

data class PersonaDisplayNameFieldWrapper(
val value: String = "",
val valid: Boolean? = null,
val required: Boolean = false,
val wasEdited: Boolean = false,
val shouldDisplayValidationError: Boolean = false
)
val wasEdited: Boolean = false
) {
val validationState: ValidationState
get() {
val valueTrimmed = value.trim()
return when {
valueTrimmed.isEmpty() -> ValidationState.Empty
valueTrimmed.length > Constants.ENTITY_NAME_MAX_LENGTH -> ValidationState.TooLong
else -> ValidationState.Valid
}
}

val isValid: Boolean
get() = validationState == ValidationState.Valid

enum class ValidationState {
Valid,
Empty,
TooLong
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import rdx.works.core.sargon.PersonaDataField
data class PersonaFieldWrapper(
val entry: IdentifiedEntry<PersonaDataField>,
val selected: Boolean = false,
val valid: Boolean? = null,
val isValid: Boolean? = null,
val required: Boolean = false,
val wasEdited: Boolean = false,
val shouldDisplayValidationError: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ fun CreatePersonaScreen(
onDeleteField = viewModel::onDeleteField,
onValueChanged = viewModel::onFieldValueChanged,
onFieldFocusChanged = viewModel::onFieldFocusChanged,
onPersonaDisplayNameFocusChanged = viewModel::onPersonaDisplayNameFieldFocusChanged,
onAddFieldSheetVisible = viewModel::setAddFieldSheetVisible,
onMessageShown = viewModel::onMessageShown
)
Expand Down Expand Up @@ -110,7 +109,6 @@ fun CreatePersonaContent(
onDeleteField: (PersonaDataEntryId) -> Unit,
onValueChanged: (PersonaDataEntryId, PersonaDataField) -> Unit,
onFieldFocusChanged: (PersonaDataEntryId, Boolean) -> Unit,
onPersonaDisplayNameFocusChanged: (Boolean) -> Unit,
onAddFieldSheetVisible: (Boolean) -> Unit,
onMessageShown: () -> Unit
) {
Expand Down Expand Up @@ -144,7 +142,7 @@ fun CreatePersonaContent(
RadixBottomBar(
onClick = onPersonaCreateClick,
text = stringResource(id = R.string.createPersona_saveAndContinueButtonTitle),
enabled = state.continueButtonEnabled,
enabled = state.isContinueButtonEnabled,
insets = WindowInsets.navigationBars.union(WindowInsets.ime)
)
},
Expand All @@ -170,7 +168,6 @@ fun CreatePersonaContent(
bottomSheetState.show()
}
},
onPersonaDisplayNameFocusChanged = onPersonaDisplayNameFocusChanged,
onFieldFocusChanged = onFieldFocusChanged
)
}
Expand Down Expand Up @@ -221,8 +218,7 @@ private fun CreatePersonaContentList(
addButtonEnabled: Boolean,
modifier: Modifier = Modifier,
onAddFieldClick: () -> Unit,
onFieldFocusChanged: (PersonaDataEntryId, Boolean) -> Unit,
onPersonaDisplayNameFocusChanged: (Boolean) -> Unit
onFieldFocusChanged: (PersonaDataEntryId, Boolean) -> Unit
) {
LazyColumn(
modifier = modifier,
Expand Down Expand Up @@ -264,14 +260,15 @@ private fun CreatePersonaContentList(
)
),
hint = stringResource(id = R.string.createPersona_nameNewPersona_placeholder),
onFocusChanged = {
onPersonaDisplayNameFocusChanged(it.hasFocus)
},
error = if (personaName.shouldDisplayValidationError && personaName.valid == false) {
stringResource(id = R.string.createPersona_emptyDisplayName)
error = if (personaName.wasEdited) {
when (personaName.validationState) {
PersonaDisplayNameFieldWrapper.ValidationState.Empty -> stringResource(id = R.string.createPersona_emptyDisplayName)
PersonaDisplayNameFieldWrapper.ValidationState.TooLong -> stringResource(id = R.string.error_personaLabel_tooLong)
else -> null
}
} else {
null
},
}
)
Spacer(modifier = Modifier.height(dimensions.paddingMedium))
Text(
Expand Down Expand Up @@ -300,7 +297,7 @@ private fun CreatePersonaContentList(
},
required = field.required,
phoneInput = field.isPhoneNumber(),
error = if (field.shouldDisplayValidationError && field.valid == false) {
error = if (field.shouldDisplayValidationError && field.isValid == false) {
stringResource(id = R.string.createPersona_requiredField)
} else {
null
Expand Down Expand Up @@ -335,7 +332,6 @@ fun CreateAccountContentPreview() {
currentFields = persistentListOf(),
fieldsToAdd = persistentListOf(),
personaDisplayName = PersonaDisplayNameFieldWrapper("Name"),
continueButtonEnabled = false,
anyFieldSelected = false,
isAddFieldBottomSheetVisible = false
),
Expand All @@ -348,7 +344,6 @@ fun CreateAccountContentPreview() {
onDeleteField = {},
onValueChanged = { _, _ -> },
onFieldFocusChanged = { _, _ -> },
onPersonaDisplayNameFocusChanged = {},
onAddFieldSheetVisible = {},
onMessageShown = {}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class CreatePersonaViewModel @Inject constructor(
it.copy(
anyFieldSelected = s.areThereFieldsSelected,
personaDisplayName = s.personaDisplayName,
continueButtonEnabled = s.inputValid,
currentFields = s.currentFields,
fieldsToAdd = s.fieldsToAdd
)
Expand All @@ -63,7 +62,7 @@ class CreatePersonaViewModel @Inject constructor(
fun onPersonaCreateClick() {
accessFactorSourcesJob?.cancel()
accessFactorSourcesJob = viewModelScope.launch {
val displayName = DisplayName(_state.value.personaDisplayName.value)
val displayName = DisplayName(_state.value.personaDisplayName.value.trim())
val personaData = _state.value.currentFields.toPersonaData()
val factorSource = getProfileUseCase().mainBabylonFactorSource ?: return@launch
accessFactorSourcesProxy.getPublicKeyAndDerivationPathForFactorSource(
Expand Down Expand Up @@ -124,11 +123,16 @@ class CreatePersonaViewModel @Inject constructor(
val currentFields: ImmutableList<PersonaFieldWrapper> = persistentListOf(),
val fieldsToAdd: ImmutableList<PersonaFieldWrapper> = persistentListOf(),
val personaDisplayName: PersonaDisplayNameFieldWrapper = PersonaDisplayNameFieldWrapper(),
val continueButtonEnabled: Boolean = false,
val anyFieldSelected: Boolean = false,
val isAddFieldBottomSheetVisible: Boolean = false,
val uiMessage: UiMessage? = null,
val isNoMnemonicErrorVisible: Boolean = false,
val shouldNavigateToCompletion: Boolean = false
) : UiState
) : UiState {
val isContinueButtonEnabled: Boolean
get() {
val valid = currentFields.all { it.isValid == true } && personaDisplayName.isValid
return valid
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ fun PersonaEditScreen(
onValueChanged = viewModel::onFieldValueChanged,
onDisplayNameChanged = viewModel::onDisplayNameChanged,
onFieldFocusChanged = viewModel::onFieldFocusChanged,
onPersonaDisplayNameFocusChanged = viewModel::onPersonaDisplayNameFieldFocusChanged,
setAddFieldSheetVisible = viewModel::setAddFieldSheetVisible
)
}
Expand All @@ -108,7 +107,6 @@ private fun PersonaEditContent(
onValueChanged: (PersonaDataEntryId, PersonaDataField) -> Unit,
onDisplayNameChanged: (String) -> Unit,
onFieldFocusChanged: (PersonaDataEntryId, Boolean) -> Unit,
onPersonaDisplayNameFocusChanged: (Boolean) -> Unit,
setAddFieldSheetVisible: (Boolean) -> Unit
) {
val keyboardController = LocalSoftwareKeyboardController.current
Expand Down Expand Up @@ -207,7 +205,6 @@ private fun PersonaEditContent(
personaDisplayName = state.personaDisplayName,
addButtonEnabled = state.fieldsToAdd.isNotEmpty(),
onFieldFocusChanged = onFieldFocusChanged,
onPersonaDisplayNameFocusChanged = onPersonaDisplayNameFocusChanged,
dappContextEdit = state.dappContextEdit,
missingFields = state.missingFields
)
Expand Down Expand Up @@ -261,7 +258,6 @@ private fun PersonaDetailList(
personaDisplayName: PersonaDisplayNameFieldWrapper,
addButtonEnabled: Boolean,
onFieldFocusChanged: (PersonaDataEntryId, Boolean) -> Unit,
onPersonaDisplayNameFocusChanged: (Boolean) -> Unit,
dappContextEdit: Boolean,
missingFields: ImmutableList<PersonaDataField.Kind>
) {
Expand Down Expand Up @@ -291,13 +287,14 @@ private fun PersonaDetailList(
onValueChanged = onDisplayNameChanged,
value = personaDisplayName.value,
leftLabel = LabelType.Default(stringResource(id = R.string.authorizedDapps_personaDetails_personaLabelHeading)),
error = if (personaDisplayName.shouldDisplayValidationError && personaDisplayName.valid == false) {
stringResource(id = R.string.createPersona_emptyDisplayName)
error = if (personaDisplayName.wasEdited) {
when (personaDisplayName.validationState) {
PersonaDisplayNameFieldWrapper.ValidationState.Empty -> stringResource(id = R.string.createPersona_emptyDisplayName)
PersonaDisplayNameFieldWrapper.ValidationState.TooLong -> stringResource(id = R.string.error_personaLabel_tooLong)
else -> null
}
} else {
null
},
onFocusChanged = {
onPersonaDisplayNameFocusChanged(it.hasFocus)
}
)
HorizontalDivider(color = RadixTheme.colors.gray4)
Expand Down Expand Up @@ -345,7 +342,7 @@ private fun PersonaDetailList(
},
required = field.required,
phoneInput = field.isPhoneNumber(),
error = if (field.shouldDisplayValidationError && field.valid == false) {
error = if (field.shouldDisplayValidationError && field.isValid == false) {
validationError
} else {
null
Expand Down Expand Up @@ -412,7 +409,6 @@ fun DappDetailContentPreview() {
onValueChanged = { _, _ -> },
onDisplayNameChanged = {},
onFieldFocusChanged = { _, _ -> },
onPersonaDisplayNameFocusChanged = {},
setAddFieldSheetVisible = {}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -47,7 +46,7 @@ class PersonaEditViewModel @Inject constructor(
personaEditState.collect { s ->
_state.update { state ->
state.copy(
saveButtonEnabled = s.inputValid,
saveButtonEnabled = s.isInputValid,
personaDisplayName = s.personaDisplayName,
addFieldButtonEnabled = s.areThereFieldsSelected,
currentFields = s.currentFields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import com.babylon.wallet.android.presentation.model.LedgerDeviceUiModel
import com.babylon.wallet.android.presentation.settings.securitycenter.ledgerhardwarewallets.AddLedgerDeviceUiState
import com.babylon.wallet.android.utils.AppEvent
import com.babylon.wallet.android.utils.AppEventBus
import com.babylon.wallet.android.utils.Constants.ACCOUNT_NAME_MAX_LENGTH
import com.babylon.wallet.android.utils.Constants.DELAY_300_MS
import com.babylon.wallet.android.utils.Constants.ENTITY_NAME_MAX_LENGTH
import com.radixdlt.sargon.FactorSource
import com.radixdlt.sargon.FactorSourceId
import com.radixdlt.sargon.HierarchicalDeterministicPublicKey
Expand Down Expand Up @@ -195,7 +195,7 @@ class ImportLegacyWalletViewModel @Inject constructor(
olympiaAccountsToImport = data.accountData
.map {
// truncate the name, max 30 chars
it.copy(accountName = it.accountName.take(ACCOUNT_NAME_MAX_LENGTH))
it.copy(accountName = it.accountName.take(ENTITY_NAME_MAX_LENGTH))
}
.toPersistentList(),
importButtonEnabled = !allImported,
Expand Down
Loading

0 comments on commit f6c3f82

Please sign in to comment.