Updated on 2026-08-14

This commit is contained in:
Tangem 2024-08-09 09:29:20 +03:00
parent 17e5bd6a6e
commit bd328c7b99

View file

@ -1,25 +1,18 @@
package com.tangem.tap.features.details.ui.securitymode
import android.os.Bundle
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import arrow.core.getOrElse
import com.tangem.common.CompletionResult
import com.tangem.common.core.TangemSdkError
import com.tangem.common.routing.AppRoute
import com.tangem.common.routing.AppRouter
import com.tangem.common.routing.bundle.unbundle
import com.tangem.core.analytics.Analytics
import com.tangem.domain.common.util.cardTypesResolver
import com.tangem.domain.wallets.models.UserWallet
import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.domain.wallets.usecase.GetUserWalletUseCase
import com.tangem.tap.common.analytics.events.AnalyticsParam
import com.tangem.tap.common.analytics.events.Settings
import com.tangem.tap.common.extensions.dispatchNavigationAction
import com.tangem.tap.domain.sdk.TangemSdkManager
import com.tangem.tap.features.details.redux.SecurityOption
import com.tangem.tap.features.details.ui.cardsettings.domain.CardSettingsInteractor
import com.tangem.tap.features.details.ui.common.utils.getAllowedSecurityOptions
import com.tangem.tap.features.details.ui.common.utils.getCurrentSecurityOption
import com.tangem.tap.store
@ -31,24 +24,18 @@ import javax.inject.Inject
@HiltViewModel
internal class SecurityModeViewModel @Inject constructor(
private val getUserWalletUseCase: GetUserWalletUseCase,
private val tangemSdkManager: TangemSdkManager,
savedStateHandle: SavedStateHandle,
private val cardSettingsInteractor: CardSettingsInteractor,
) : ViewModel() {
private val userWalletId = savedStateHandle.get<Bundle>(AppRoute.DetailsSecurity.USER_WALLET_ID_KEY)
?.unbundle(UserWalletId.serializer())
?: error("UserWalletId is required for SecurityModeViewModel")
private val scannedScanResponse = cardSettingsInteractor.scannedScanResponse.value
?: error("Scan response is null")
val screenState = MutableStateFlow(
value = getInitialState(),
)
val screenState = MutableStateFlow(value = getInitialState())
private fun getInitialState(): SecurityModeScreenState {
val userWallet = getUserWallet()
val scanResponse = userWallet.scanResponse
val card = scanResponse.card
val cardTypesResolver = scanResponse.cardTypesResolver
val card = scannedScanResponse.card
val cardTypesResolver = scannedScanResponse.cardTypesResolver
val currentSecurityOption = getCurrentSecurityOption(card)
val allowedSecurityOptions = getAllowedSecurityOptions(card, cardTypesResolver, currentSecurityOption)
@ -66,14 +53,13 @@ internal class SecurityModeViewModel @Inject constructor(
screenState.update { state ->
state.copy(
selectedSecurityMode = securityOption,
isSaveChangesEnabled = securityOption != getCurrentSecurityOption(getUserWallet().scanResponse.card),
isSaveChangesEnabled = securityOption != getCurrentSecurityOption(scannedScanResponse.card),
)
}
}
private fun saveChanges() {
val userWallet = getUserWallet()
val cardId = userWallet.cardId
val cardId = scannedScanResponse.card.cardId
val selectedOption = screenState.value.selectedSecurityMode
viewModelScope.launch {
@ -83,6 +69,15 @@ internal class SecurityModeViewModel @Inject constructor(
SecurityOption.AccessCode -> tangemSdkManager.setAccessCode(cardId)
}
cardSettingsInteractor.update {
it.copy(
card = it.card.copy(
isAccessCodeSet = selectedOption == SecurityOption.AccessCode,
isPasscodeSet = selectedOption == SecurityOption.PassCode,
),
)
}
val paramValue = AnalyticsParam.SecurityMode.from(selectedOption)
when (result) {
is CompletionResult.Success -> {
@ -100,10 +95,4 @@ internal class SecurityModeViewModel @Inject constructor(
}
}
}
private fun getUserWallet(): UserWallet {
return getUserWalletUseCase(userWalletId).getOrElse {
error("Unable to get user wallet $userWalletId: $it")
}
}
}