Updated on 2026-08-14

This commit is contained in:
Tangem 2023-04-04 10:32:02 +03:00
parent be67efcca1
commit 40a3ada884
12 changed files with 100 additions and 32 deletions

View file

@ -0,0 +1,6 @@
package com.tangem.feature.referral.models
/**
* Exception that throws when card in demo mode
*/
class DemoModeException : Exception()

View file

@ -59,6 +59,7 @@ import com.tangem.core.ui.components.VerticalSpacer
import com.tangem.core.ui.components.appbar.AppBarWithBackButton
import com.tangem.core.ui.res.TangemColorPalette
import com.tangem.core.ui.res.TangemTheme
import com.tangem.feature.referral.models.DemoModeException
import com.tangem.feature.referral.models.ReferralStateHolder
import com.tangem.feature.referral.models.ReferralStateHolder.ErrorSnackbar
import com.tangem.feature.referral.models.ReferralStateHolder.ReferralInfoContentState
@ -385,16 +386,8 @@ private fun BoxScope.ErrorSnackbarHost(errorSnackbar: ErrorSnackbar?) {
},
)
val message = if (errorSnackbar.throwable.cause != null) {
String.format(
format = stringResource(id = R.string.referral_error_failed_to_load_info_with_reason),
errorSnackbar.throwable.cause,
)
} else {
stringResource(id = R.string.referral_error_failed_to_load_info)
}
val actionLabel = stringResource(id = R.string.warning_button_ok)
val message = getMessageForErrorSnackbar(errorSnackbar)
SideEffect {
coroutineScope.launch {
val result = snackbarHostState.showSnackbar(
@ -463,6 +456,25 @@ private fun BoxScope.CopySnackbarHost(isCopyButtonPressed: MutableState<Boolean>
}
}
@Composable
private fun getMessageForErrorSnackbar(errorSnackbar: ErrorSnackbar): String {
return when (errorSnackbar.throwable) {
is DemoModeException -> {
stringResource(id = R.string.alert_demo_feature_disabled)
}
else -> {
if (errorSnackbar.throwable.cause != null) {
String.format(
format = stringResource(id = R.string.referral_error_failed_to_load_info_with_reason),
errorSnackbar.throwable.cause,
)
} else {
stringResource(id = R.string.referral_error_failed_to_load_info)
}
}
}
}
@Preview(widthDp = 360, showBackground = true)
@Composable
private fun Preview_ReferralScreen_Participant_InLightTheme() {

View file

@ -11,6 +11,7 @@ import com.tangem.feature.referral.domain.ReferralInteractor
import com.tangem.feature.referral.domain.models.DiscountType
import com.tangem.feature.referral.domain.models.ReferralData
import com.tangem.feature.referral.domain.models.ReferralInfo
import com.tangem.feature.referral.models.DemoModeException
import com.tangem.feature.referral.models.ReferralStateHolder
import com.tangem.feature.referral.models.ReferralStateHolder.ErrorSnackbar
import com.tangem.feature.referral.models.ReferralStateHolder.ReferralInfoState
@ -75,21 +76,25 @@ internal class ReferralViewModel @Inject constructor(
}
private fun participate() {
analyticsEventHandler.send(ReferralEvents.ClickParticipate)
uiState = uiState.copy(referralInfoState = ReferralInfoState.Loading)
viewModelScope.launch(dispatchers.main) {
runCatching(dispatchers.io) { referralInteractor.startReferral() }
.onSuccess(::showContent)
.onFailure {
if (it is UserCancelledException) {
val lastRefData = lastReferralData.value
if (lastRefData != null) {
showContent(lastRefData)
if (referralInteractor.isDemoMode) {
showErrorSnackbar(DemoModeException())
} else {
analyticsEventHandler.send(ReferralEvents.ClickParticipate)
uiState = uiState.copy(referralInfoState = ReferralInfoState.Loading)
viewModelScope.launch(dispatchers.main) {
runCatching(dispatchers.io) { referralInteractor.startReferral() }
.onSuccess(::showContent)
.onFailure {
if (it is UserCancelledException) {
val lastRefData = lastReferralData.value
if (lastRefData != null) {
showContent(lastRefData)
}
} else {
showErrorSnackbar(it)
}
} else {
showErrorSnackbar(it)
}
}
}
}
}