Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple click #1312

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.vultisig.wallet.ui.components.library.UiCirclesLoader
import com.vultisig.wallet.ui.theme.Theme

@Composable
Expand All @@ -28,7 +29,8 @@ internal fun MultiColorButton(
trailingIcon: Int? = null,
iconColor: Color? = null,
backgroundColor: Color? = null,
disabled: Boolean? = false,
isLoading: Boolean = false,
disabled: Boolean = false,
iconSize: Dp? = null,
borderSize: Dp? = null,
minWidth: Dp? = null,
Expand All @@ -39,7 +41,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 +63,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 && !isLoading,
)
) {
if (startIcon != null)
Expand All @@ -82,12 +84,19 @@ internal fun MultiColorButton(
modifier = Modifier.size(iconSize ?: 15.dp)
)
else UiSpacer(iconSize ?: 25.dp)
content?.invoke() ?: Text(
text = text,
color = if (disabled == true) appColor.neutral800 else textColor
?: appColor.oxfordBlue800,
style = textStyle ?: Theme.montserrat.subtitle1
)
if (isLoading) {
UiCirclesLoader(
color1 = textColor ?: appColor.oxfordBlue800
)
} else {
content?.invoke() ?: Text(
text = text,
color = if (disabled == true) appColor.neutral800 else textColor
?: appColor.oxfordBlue800,
style = textStyle ?: Theme.montserrat.subtitle1
)
}

if (trailingIcon != null)
Icon(
painter = painterResource(trailingIcon),
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 isLoading: Boolean = false,
)

internal data class SendSrc(
Expand Down Expand Up @@ -332,6 +333,7 @@ internal class SendFormViewModel @Inject constructor(

fun send() {
viewModelScope.launch {
showLoading()
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 {
hideLoading()
}
}
}

private fun hideLoading() {
uiState.update {
it.copy(
isLoading = false
)
}
}

private fun showLoading() {
uiState.update {
it.copy(isLoading = 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 @@ -10,6 +10,7 @@ internal fun NavController.route(route: String, opts: NavigationOptions? = null)
popBackStack()
} else {
navigate(route) {
launchSingleTop=true
if (opts != null) {
if (opts.popUpTo != null) {
popUpTo(opts.popUpTo) {
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),
isLoading = state.isLoading,
onClick = {
focusManager.clearFocus()
onSend()
Expand Down