Skip to content

Commit

Permalink
Add withdraw verification with email and 2fa codes
Browse files Browse the repository at this point in the history
  • Loading branch information
abdrasulov committed Jul 5, 2023
1 parent 6540f8d commit eed5f37
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ interface ICexProvider {
suspend fun getAssets(): List<CexAssetRaw>
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"])
Expand Down Expand Up @@ -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<CexAssetRaw> {
return api.getBalances(authToken, secret)
.map {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class CoinzixCexApiService {
address: String,
amount: BigDecimal
): String {
delay(300)
delay(1000)

return "1"
// val params = buildMap {
Expand All @@ -91,6 +91,9 @@ class CoinzixCexApiService {
// )
}

suspend fun confirmWithdraw(withdrawId: String, emailCode: String, twoFactorCode: String?) {
delay(1000)
}

private suspend inline fun <reified T> post(
path: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T : ViewModel> create(modelClass: Class<T>): 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?
)
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -128,6 +128,7 @@ fun WithdrawCexConfirmScreen(
confirmEnabled = true
}
},
showSpinner = !confirmEnabled,
enabled = confirmEnabled
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,15 +20,30 @@ 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(
withdrawId: String,
onNavigateBack: () -> Unit,
onClose: () -> Unit
) {
val viewModel = viewModel {
CexWithdrawVerificationViewModel(withdrawId)
val viewModel = viewModel<CexWithdrawVerificationViewModel>(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>(CodeGetButtonState.Active) }
Expand Down Expand Up @@ -82,7 +98,7 @@ fun WithdrawCexSecurityVerificationScreen(
singleLine = true,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
onValueChange = {
viewModel.onEnter2FaCode(it)
viewModel.onEnterTwoFactorCode(it)
},
)
InfoText(
Expand All @@ -100,7 +116,7 @@ fun WithdrawCexSecurityVerificationScreen(
onClick = {
viewModel.submit()
},
enabled = viewModel.submitEnabled
enabled = uiState.submitEnabled
)
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,7 @@
<string name="CexWithdraw_EmailVerificationInfo">Enter verification code sent to [email protected]</string>
<string name="CexWithdraw_GoogleAuthenticationInfo">Enter google authentication code from your Google Authenticator App</string>
<string name="CexWithdraw_Withdraw">Withdraw</string>
<string name="CexWithdraw_WithdrawSuccess">Success</string>


</resources>

0 comments on commit eed5f37

Please sign in to comment.