Skip to content

Commit

Permalink
Fix Voucher dialog copy/paste issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sabercodic committed Oct 13, 2023
1 parent 127bd50 commit 02e52bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import net.mullvad.mullvadvpn.compose.button.ActionButton
import net.mullvad.mullvadvpn.compose.state.VoucherDialogState
import net.mullvad.mullvadvpn.compose.state.VoucherDialogUiState
import net.mullvad.mullvadvpn.compose.textfield.GroupedTextField
import net.mullvad.mullvadvpn.compose.util.VOUCHER_SEPARATOR
import net.mullvad.mullvadvpn.compose.util.vouchersVisualTransformation
import net.mullvad.mullvadvpn.constant.VOUCHER_LENGTH
import net.mullvad.mullvadvpn.lib.theme.AlphaDescription
Expand Down Expand Up @@ -240,7 +241,12 @@ private fun EnterVoucherBody(
onRedeem(input)
}
},
onValueChanged = { input -> onVoucherInputChange(input.uppercase()) },
onValueChanged = { input ->
val str = input.replace(VOUCHER_SEPARATOR, "")
onVoucherInputChange(
str.substring(0, Integer.min(VOUCHER_LENGTH, str.length)).uppercase()
)
},
isValidValue = uiState.voucherInput.isNotEmpty(),
keyboardType = KeyboardType.Password,
placeholderText = stringResource(id = R.string.voucher_hint),
Expand All @@ -249,12 +255,11 @@ private fun EnterVoucherBody(
.copy(alpha = AlphaDisabled)
.compositeOver(MaterialTheme.colorScheme.primary),
visualTransformation = vouchersVisualTransformation(),
maxCharLength = VOUCHER_LENGTH,
onFocusChange = {},
isDigitsOnlyAllowed = false,
isEnabled = true,
modifier = Modifier.focusRequester(textFieldFocusRequester),
validateRegex = "^[A-Za-z0-9]*$".toRegex()
validateRegex = "^[A-Za-z0-9-]*$".toRegex()
)
}
Spacer(modifier = Modifier.height(Dimens.smallPadding))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ const val VOUCHER_CHUNK_SIZE = 4
const val MAX_VOUCHER_LENGTH = 16

fun vouchersVisualTransformation() = VisualTransformation { text ->
var out = text.chunked(VOUCHER_CHUNK_SIZE).joinToString(VOUCHER_SEPARATOR)
var out =
text
.substring(0, min(MAX_VOUCHER_LENGTH, text.length))
.chunked(VOUCHER_CHUNK_SIZE)
.joinToString(VOUCHER_SEPARATOR)
if (
text.length % VOUCHER_CHUNK_SIZE == 0 &&
text.isNotEmpty() &&
Expand Down

0 comments on commit 02e52bf

Please sign in to comment.