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 Nov 8, 2023
1 parent 497ea3e commit 09a8c7a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.lib.theme.color.AlphaDescription
import org.joda.time.DateTimeConstants

private const val VALID_VOUCHER_CHARACTER_REGEX_PATTERN = "^[A-Za-z0-9- \r\n]*$"
private const val IGNORED_VOUCHER_CHARACTER_REGEX_PATTERN = """[- \n\r]"""

@Preview(device = Devices.TV_720p)
@Composable
private fun PreviewRedeemVoucherDialog() {
Expand Down Expand Up @@ -224,16 +227,20 @@ private fun EnterVoucherBody(
onRedeem(input)
}
},
onValueChanged = { input -> onVoucherInputChange(input.uppercase()) },
onValueChanged = { input ->
val str = input.replace(Regex(IGNORED_VOUCHER_CHARACTER_REGEX_PATTERN), "")
onVoucherInputChange(
str.substring(0, Integer.min(VOUCHER_LENGTH, str.length)).uppercase()
)
},
isValidValue =
uiState.voucherInput.isEmpty() || uiState.voucherInput.length == MAX_VOUCHER_LENGTH,
keyboardType = KeyboardType.Password,
placeholderText = stringResource(id = R.string.voucher_hint),
visualTransformation = vouchersVisualTransformation(),
maxCharLength = VOUCHER_LENGTH,
isDigitsOnlyAllowed = false,
isEnabled = true,
validateRegex = "^[A-Za-z0-9]*$".toRegex()
validateRegex = VALID_VOUCHER_CHARACTER_REGEX_PATTERN.toRegex()
)
Spacer(modifier = Modifier.height(Dimens.smallPadding))
Row(
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ fun ServiceConnectionManager.settingsListener() =

fun ServiceConnectionManager.splitTunneling() =
this.connectionState.value.readyContainer()?.splitTunneling

fun ServiceConnectionManager.voucherRedeemer() =
this.connectionState.value.readyContainer()?.voucherRedeemer
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ import net.mullvad.mullvadvpn.model.VoucherSubmissionResult
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionContainer
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionState
import net.mullvad.mullvadvpn.ui.serviceconnection.VoucherRedeemer
import net.mullvad.mullvadvpn.ui.serviceconnection.voucherRedeemer

class VoucherDialogViewModel(
serviceConnectionManager: ServiceConnectionManager,
val serviceConnectionManager: ServiceConnectionManager,
private val resources: Resources
) : ViewModel() {

private val vmState = MutableStateFlow<VoucherDialogState>(VoucherDialogState.Default)
private val voucherInput = MutableStateFlow(LoginUiState.INITIAL.accountNumberInput)

private lateinit var voucherRedeemer: VoucherRedeemer
private val _shared: SharedFlow<ServiceConnectionContainer> =
serviceConnectionManager.connectionState
.flatMapLatest { state ->
Expand All @@ -47,8 +46,7 @@ class VoucherDialogViewModel(

var uiState =
_shared
.flatMapLatest { serviceConnection ->
voucherRedeemer = serviceConnection.voucherRedeemer
.flatMapLatest { _ ->
combine(vmState, voucherInput) { state, input ->
VoucherDialogUiState(voucherInput = input, voucherViewModelState = state)
}
Expand All @@ -58,7 +56,7 @@ class VoucherDialogViewModel(
fun onRedeem(voucherCode: String) {
vmState.update { VoucherDialogState.Verifying }
viewModelScope.launch {
when (val result = voucherRedeemer.submit(voucherCode)) {
when (val result = serviceConnectionManager.voucherRedeemer()?.submit(voucherCode)) {
is VoucherSubmissionResult.Ok -> handleAddedTime(result.submission.timeAdded)
is VoucherSubmissionResult.Error -> setError(result.error)
else -> {
Expand Down

0 comments on commit 09a8c7a

Please sign in to comment.