Skip to content

Commit

Permalink
Fix multiple click
Browse files Browse the repository at this point in the history
  • Loading branch information
aminsato committed Nov 6, 2024
1 parent 42ac8e7 commit d1a3a9d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 6 additions & 8 deletions app/src/main/java/com/vultisig/wallet/ui/components/Onclick.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -332,6 +333,7 @@ internal class SendFormViewModel @Inject constructor(

fun send() {
viewModelScope.launch {
enableSendingForm()
try {
val vaultId = vaultId
?: throw InvalidTransactionDataException(
Expand Down Expand Up @@ -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) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ internal fun SendFormScreen(
.fillMaxWidth()
.align(Alignment.BottomCenter)
.padding(all = 16.dp),
isBusy = state.isSendingForm,
onClick = {
focusManager.clearFocus()
onSend()
Expand Down

0 comments on commit d1a3a9d

Please sign in to comment.