Last active
January 12, 2025 11:53
-
-
Save syarihu/ff00b1ce88effc764edf284bf38be7bc to your computer and use it in GitHub Desktop.
Double tap prevention for jetpack compose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.foundation.clickable | |
import androidx.compose.runtime.LaunchedEffect | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.runtime.setValue | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.composed | |
import kotlinx.coroutines.delay | |
fun Modifier.clickableOnce( | |
onClick: () -> Unit, | |
): Modifier = this.then( | |
composed( | |
inspectorInfo = { | |
name = "clickableOnce" | |
value = onClick | |
}, | |
) { | |
var enableAgain by remember { mutableStateOf(true) } | |
LaunchedEffect( | |
enableAgain, | |
block = { | |
if (enableAgain) return@LaunchedEffect | |
delay(timeMillis = 500L) | |
enableAgain = true | |
}, | |
) | |
Modifier.clickable { | |
if (enableAgain) { | |
enableAgain = false | |
onClick() | |
} | |
} | |
}, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Monabr
It can be resolved by applying as an explicit receiver.
Add
this
keyword tocomposed
of L14