From d1a3a9d977bf77b9f68ebe449582005b9f3f8fb8 Mon Sep 17 00:00:00 2001 From: aminsato Date: Wed, 6 Nov 2024 14:01:19 +0330 Subject: [PATCH] Fix multiple click --- .../wallet/ui/components/MultiColorButton.kt | 11 ++++++----- .../vultisig/wallet/ui/components/Onclick.kt | 14 ++++++-------- .../wallet/ui/models/send/SendFormViewModel.kt | 18 ++++++++++++++++++ .../wallet/ui/screens/send/SendFormScreen.kt | 1 + 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/vultisig/wallet/ui/components/MultiColorButton.kt b/app/src/main/java/com/vultisig/wallet/ui/components/MultiColorButton.kt index bca34204b..f17a34cdc 100644 --- a/app/src/main/java/com/vultisig/wallet/ui/components/MultiColorButton.kt +++ b/app/src/main/java/com/vultisig/wallet/ui/components/MultiColorButton.kt @@ -28,7 +28,8 @@ internal fun MultiColorButton( trailingIcon: Int? = null, iconColor: Color? = null, backgroundColor: Color? = null, - disabled: Boolean? = false, + isBusy: Boolean = false, + disabled: Boolean = false, iconSize: Dp? = null, borderSize: Dp? = null, minWidth: Dp? = null, @@ -39,7 +40,6 @@ internal fun MultiColorButton( onClick: () -> Unit, content : (@Composable ()->Unit)? = null, ) { - val emptyClickAction: () -> Unit = {} var innerModifier = modifier val appColor = Theme.colors if (borderSize != null) @@ -62,15 +62,16 @@ internal fun MultiColorButton( modifier = innerModifier .clip(shape = RoundedCornerShape(60.dp)) .background( - color = if (disabled == true) appColor.neutral600 else backgroundColor - ?: appColor.turquoise600Main + color = if (disabled) appColor.neutral600 + else backgroundColor ?: appColor.turquoise600Main ) .defaultMinSize( minWidth = minWidth ?: 170.dp, minHeight = minHeight ?: 44.dp ) .clickOnce( - onClick = if (disabled == false) onClick else emptyClickAction + onClick = onClick, + enabled = !disabled && !isBusy, ) ) { if (startIcon != null) diff --git a/app/src/main/java/com/vultisig/wallet/ui/components/Onclick.kt b/app/src/main/java/com/vultisig/wallet/ui/components/Onclick.kt index 9e287840b..037a99f85 100644 --- a/app/src/main/java/com/vultisig/wallet/ui/components/Onclick.kt +++ b/app/src/main/java/com/vultisig/wallet/ui/components/Onclick.kt @@ -1,7 +1,6 @@ package com.vultisig.wallet.ui.components import androidx.compose.foundation.clickable -import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -11,29 +10,28 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.composed import kotlinx.coroutines.delay -private const val COOL_DOWN_PERIOD = 800L +private const val COOL_DOWN_PERIOD = 375L private var lastClickTime = 0L fun Modifier.clickOnce(enabled: Boolean = true, onClick: () -> Unit): Modifier = this.composed { var enableAgain by remember { mutableStateOf(true) } LaunchedEffect(enableAgain) { - if (enableAgain) return@LaunchedEffect + if (enableAgain) + return@LaunchedEffect delay(timeMillis = COOL_DOWN_PERIOD) enableAgain = true } - Modifier.clickable(enabled = enabled) { - val currentTime = System.currentTimeMillis() - if (enableAgain && currentTime - lastClickTime >= COOL_DOWN_PERIOD) { - lastClickTime = currentTime + clickable(enabled = enabled) { + if (enableAgain) { enableAgain = false onClick() } } } -@Composable + fun clickOnce(onClick: () -> Unit): () -> Unit { return { val currentTime = System.currentTimeMillis() diff --git a/app/src/main/java/com/vultisig/wallet/ui/models/send/SendFormViewModel.kt b/app/src/main/java/com/vultisig/wallet/ui/models/send/SendFormViewModel.kt index 751191ce4..ce0af0c9d 100644 --- a/app/src/main/java/com/vultisig/wallet/ui/models/send/SendFormViewModel.kt +++ b/app/src/main/java/com/vultisig/wallet/ui/models/send/SendFormViewModel.kt @@ -92,6 +92,7 @@ internal data class SendFormUiModel( val hasMemo: Boolean = false, val showGasSettings: Boolean = false, val specific: BlockChainSpecificAndUtxo? = null, + val isSendingForm: Boolean = false, ) internal data class SendSrc( @@ -332,6 +333,7 @@ internal class SendFormViewModel @Inject constructor( fun send() { viewModelScope.launch { + enableSendingForm() try { val vaultId = vaultId ?: throw InvalidTransactionDataException( @@ -514,10 +516,26 @@ internal class SendFormViewModel @Inject constructor( ) } catch (e: InvalidTransactionDataException) { showError(e.text) + } finally { + disableSendingForm() } } } + private fun disableSendingForm() { + uiState.update { + it.copy( + isSendingForm = false + ) + } + } + + private fun enableSendingForm() { + uiState.update { + it.copy(isSendingForm = true) + } + } + private fun showError(text: UiText) { uiState.update { it.copy(errorText = text) } } diff --git a/app/src/main/java/com/vultisig/wallet/ui/screens/send/SendFormScreen.kt b/app/src/main/java/com/vultisig/wallet/ui/screens/send/SendFormScreen.kt index 84705aefc..ca228afba 100644 --- a/app/src/main/java/com/vultisig/wallet/ui/screens/send/SendFormScreen.kt +++ b/app/src/main/java/com/vultisig/wallet/ui/screens/send/SendFormScreen.kt @@ -286,6 +286,7 @@ internal fun SendFormScreen( .fillMaxWidth() .align(Alignment.BottomCenter) .padding(all = 16.dp), + isBusy = state.isSendingForm, onClick = { focusManager.clearFocus() onSend()