diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/core/providers/ICexProvider.kt b/app/src/main/java/io/horizontalsystems/bankwallet/core/providers/ICexProvider.kt index a84bae20c2f..4841ed27e41 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/core/providers/ICexProvider.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/core/providers/ICexProvider.kt @@ -55,6 +55,7 @@ interface ICexProvider { suspend fun getAssets(): List suspend fun getAddress(assetId: String, networkId: String?): CexAddress suspend fun withdraw(assetId: String, networkId: String?, address: String, amount: BigDecimal): String + suspend fun confirmWithdraw(withdrawId: String, emailCode: String, twoFactorCode: String?) } @Entity(primaryKeys = ["id", "accountId"]) @@ -160,6 +161,14 @@ class CoinzixCexProvider( return api.withdraw(authToken, secret, assetId, networkId, address, amount) } + override suspend fun confirmWithdraw( + withdrawId: String, + emailCode: String, + twoFactorCode: String? + ) { + return api.confirmWithdraw(withdrawId, emailCode, twoFactorCode) + } + override suspend fun getAssets(): List { return api.getBalances(authToken, secret) .map { @@ -274,6 +283,14 @@ class BinanceCexProvider(apiKey: String, secretKey: String, override val account TODO("Not yet implemented") } + override suspend fun confirmWithdraw( + withdrawId: String, + emailCode: String, + twoFactorCode: String? + ) { + TODO("Not yet implemented") + } + data class CoinInfo( val coin: String, val name: String, diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/balance/cex/CoinzixCexApiService.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/balance/cex/CoinzixCexApiService.kt index f76fca354f1..27ba521fe9e 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/balance/cex/CoinzixCexApiService.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/balance/cex/CoinzixCexApiService.kt @@ -72,7 +72,7 @@ class CoinzixCexApiService { address: String, amount: BigDecimal ): String { - delay(300) + delay(1000) return "1" // val params = buildMap { @@ -91,6 +91,9 @@ class CoinzixCexApiService { // ) } + suspend fun confirmWithdraw(withdrawId: String, emailCode: String, twoFactorCode: String?) { + delay(1000) + } private suspend inline fun post( path: String, diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/CexWithdrawVerificationViewModel.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/CexWithdrawVerificationViewModel.kt index 55cc414cbe3..7c3c7d328ea 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/CexWithdrawVerificationViewModel.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/CexWithdrawVerificationViewModel.kt @@ -4,21 +4,85 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.viewModelScope +import io.horizontalsystems.bankwallet.core.App +import io.horizontalsystems.bankwallet.core.providers.ICexProvider +import kotlinx.coroutines.launch -class CexWithdrawVerificationViewModel(private val withdrawId: String) : ViewModel() { - var submitEnabled by mutableStateOf(false) +class CexWithdrawVerificationViewModel( + private val withdrawId: String, + private val cexProvider: ICexProvider? +) : ViewModel() { + private var emailCode: String? = null + private var twoFactorCode: String? = null + + private var success = false + private var error: Throwable? = null + + var uiState by mutableStateOf( + CexWithdrawVerificationUiState( + submitEnabled = getSubmitEnabled(), + success = success, + error = error + ) + ) private set fun onEnterEmailCode(v: String) { - TODO("Not yet implemented") + emailCode = v + + emitState() } - fun onEnter2FaCode(v: String) { - TODO("Not yet implemented") + fun onEnterTwoFactorCode(v: String) { + twoFactorCode = v + + emitState() + } + + private fun emitState() { + viewModelScope.launch { + uiState = CexWithdrawVerificationUiState( + submitEnabled = getSubmitEnabled(), + success = success, + error = error + ) + } + } + + private fun getSubmitEnabled(): Boolean { + return !emailCode.isNullOrBlank() && !twoFactorCode.isNullOrBlank() } fun submit() { - TODO("Not yet implemented") + viewModelScope.launch { + val tmpEmailCode = emailCode + val tmpCexProvider = cexProvider + + if (tmpEmailCode == null || tmpCexProvider == null) { + error = IllegalStateException() + } else { + tmpCexProvider.confirmWithdraw(withdrawId, tmpEmailCode, twoFactorCode) + success = true + } + + emitState() + } } + class Factory(private val withdrawId: String) : ViewModelProvider.Factory { + @Suppress("UNCHECKED_CAST") + override fun create(modelClass: Class): T { + val cexProvider = App.cexProviderManager.cexProviderFlow.value + + return CexWithdrawVerificationViewModel(withdrawId, cexProvider) as T + } + } } + +data class CexWithdrawVerificationUiState( + val submitEnabled: Boolean, + val success: Boolean, + val error: Throwable? +) diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/WithdrawCexConfirmScreen.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/WithdrawCexConfirmScreen.kt index 49e2b05aa8e..9a7cfca8dd1 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/WithdrawCexConfirmScreen.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/WithdrawCexConfirmScreen.kt @@ -113,7 +113,7 @@ fun WithdrawCexConfirmScreen( var confirmEnabled by remember { mutableStateOf(true) } ButtonsGroupWithShade { - ButtonPrimaryYellow( + ButtonPrimaryYellowWithSpinner( modifier = Modifier .fillMaxWidth() .padding(start = 16.dp, end = 16.dp, bottom = 32.dp), @@ -128,6 +128,7 @@ fun WithdrawCexConfirmScreen( confirmEnabled = true } }, + showSpinner = !confirmEnabled, enabled = confirmEnabled ) } diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/WithdrawCexSecurityVerificationScreen.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/WithdrawCexSecurityVerificationScreen.kt index 63fbb075ce2..4da29dbc6a2 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/WithdrawCexSecurityVerificationScreen.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/withdrawcex/ui/WithdrawCexSecurityVerificationScreen.kt @@ -9,6 +9,7 @@ import androidx.compose.foundation.verticalScroll import androidx.compose.material.Scaffold import androidx.compose.runtime.* import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalView import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp @@ -19,6 +20,7 @@ import io.horizontalsystems.bankwallet.modules.withdrawcex.WithdrawCexModule.Cod import io.horizontalsystems.bankwallet.ui.compose.ComposeAppTheme import io.horizontalsystems.bankwallet.ui.compose.TranslatableString import io.horizontalsystems.bankwallet.ui.compose.components.* +import io.horizontalsystems.core.helpers.HudHelper @Composable fun WithdrawCexSecurityVerificationScreen( @@ -26,8 +28,22 @@ fun WithdrawCexSecurityVerificationScreen( onNavigateBack: () -> Unit, onClose: () -> Unit ) { - val viewModel = viewModel { - CexWithdrawVerificationViewModel(withdrawId) + val viewModel = viewModel(factory = CexWithdrawVerificationViewModel.Factory(withdrawId)) + val view = LocalView.current + val uiState = viewModel.uiState + val error = uiState.error + + LaunchedEffect(error) { + error?.let { + HudHelper.showErrorMessage(view, it.message ?: it.javaClass.simpleName) + } + } + + if (uiState.success) { + LaunchedEffect(uiState.success) { + HudHelper.showSuccessMessage(view, R.string.CexWithdraw_WithdrawSuccess) + onClose.invoke() + } } var actionButtonState by remember { mutableStateOf(CodeGetButtonState.Active) } @@ -82,7 +98,7 @@ fun WithdrawCexSecurityVerificationScreen( singleLine = true, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), onValueChange = { - viewModel.onEnter2FaCode(it) + viewModel.onEnterTwoFactorCode(it) }, ) InfoText( @@ -100,7 +116,7 @@ fun WithdrawCexSecurityVerificationScreen( onClick = { viewModel.submit() }, - enabled = viewModel.submitEnabled + enabled = uiState.submitEnabled ) } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5a587cd788f..3300d788962 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1696,6 +1696,7 @@ Enter verification code sent to test@gmail.com Enter google authentication code from your Google Authenticator App Withdraw + Success