Skip to content

Commit

Permalink
Integrate VoucherCode and error message to voucher screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Aug 6, 2024
1 parent 4c97782 commit b87e015
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,10 @@ private fun EnterVoucherBody(

private fun RedeemVoucherError.message(): Int =
when (this) {
RedeemVoucherError.TooShortVoucher,
RedeemVoucherError.InvalidVoucher -> R.string.invalid_voucher
RedeemVoucherError.VoucherAlreadyUsed -> R.string.voucher_already_used
RedeemVoucherError.RpcError,
is RedeemVoucherError.Unknown -> R.string.error_occurred
RedeemVoucherError.EnteredAccountNumber -> R.string.voucher_is_account_number
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.mullvad.mullvadvpn.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import arrow.core.raise.either
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
Expand All @@ -11,7 +12,9 @@ import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.compose.state.VoucherDialogState
import net.mullvad.mullvadvpn.compose.state.VoucherDialogUiState
import net.mullvad.mullvadvpn.constant.VOUCHER_LENGTH
import net.mullvad.mullvadvpn.lib.model.ParseVoucherCodeError
import net.mullvad.mullvadvpn.lib.model.RedeemVoucherError
import net.mullvad.mullvadvpn.lib.model.VoucherCode
import net.mullvad.mullvadvpn.lib.shared.VoucherRepository
import net.mullvad.mullvadvpn.util.VoucherRegexHelper

Expand All @@ -26,11 +29,23 @@ class VoucherDialogViewModel(private val voucherRepository: VoucherRepository) :
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), VoucherDialogUiState.INITIAL)

fun onRedeem(voucherCode: String) {
fun onRedeem(voucherInput: String) {
vmState.update { VoucherDialogState.Verifying }
viewModelScope.launch {
voucherRepository
.submitVoucher(voucherCode)
either {
val voucherCode =
VoucherCode.fromString(voucherInput)
.mapLeft {
when (it) {
is ParseVoucherCodeError.AllDigit ->
RedeemVoucherError.EnteredAccountNumber
is ParseVoucherCodeError.TooShort ->
RedeemVoucherError.TooShortVoucher
}
}
.bind()
voucherRepository.submitVoucher(voucherCode).bind()
}
.fold(
{ error -> setError(error) },
{ success -> handleAddedTime(success.timeAdded) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ import net.mullvad.mullvadvpn.lib.model.UnknownApiAccessMethodError
import net.mullvad.mullvadvpn.lib.model.UnknownCustomListError
import net.mullvad.mullvadvpn.lib.model.UpdateApiAccessMethodError
import net.mullvad.mullvadvpn.lib.model.UpdateCustomListError
import net.mullvad.mullvadvpn.lib.model.VoucherCode
import net.mullvad.mullvadvpn.lib.model.WebsiteAuthToken
import net.mullvad.mullvadvpn.lib.model.WireguardConstraints as ModelWireguardConstraints
import net.mullvad.mullvadvpn.lib.model.WireguardEndpointData as ModelWireguardEndpointData
Expand Down Expand Up @@ -569,8 +570,10 @@ class ManagementService(
.mapLeft(SetWireguardConstraintsError::Unknown)
.mapEmpty()

suspend fun submitVoucher(voucher: String): Either<RedeemVoucherError, RedeemVoucherSuccess> =
Either.catch { grpc.submitVoucher(StringValue.of(voucher)).toDomain() }
suspend fun submitVoucher(
voucher: VoucherCode
): Either<RedeemVoucherError, RedeemVoucherSuccess> =
Either.catch { grpc.submitVoucher(StringValue.of(voucher.value)).toDomain() }
.mapLeftStatus {
when (it.status.code) {
Status.Code.INVALID_ARGUMENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ sealed class RedeemVoucherError {

data object VoucherAlreadyUsed : RedeemVoucherError()

data object TooShortVoucher : RedeemVoucherError()

data object EnteredAccountNumber : RedeemVoucherError()

data object RpcError : RedeemVoucherError()

data class Unknown(val error: Throwable) : RedeemVoucherError()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package net.mullvad.mullvadvpn.lib.shared

import net.mullvad.mullvadvpn.lib.daemon.grpc.ManagementService
import net.mullvad.mullvadvpn.lib.model.VoucherCode

class VoucherRepository(
private val managementService: ManagementService,
private val accountRepository: AccountRepository
) {
suspend fun submitVoucher(voucher: String) =
suspend fun submitVoucher(voucher: VoucherCode) =
managementService.submitVoucher(voucher).onRight {
accountRepository.onVoucherRedeemed(it.newExpiryDate)
}
Expand Down

0 comments on commit b87e015

Please sign in to comment.