File tree Expand file tree Collapse file tree
app/src/main/java/com/auth0/android/sample/ui Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import androidx.compose.ui.graphics.Color
1818import androidx.compose.ui.graphics.painter.Painter
1919import androidx.compose.ui.text.TextStyle
2020import androidx.compose.ui.unit.dp
21+ import com.auth0.android.sample.ui.util.rememberDebouncedOnClick
2122import com.auth0.universalcomponents.theme.Auth0Theme
2223
2324@OptIn(ExperimentalMaterial3Api ::class )
@@ -50,7 +51,8 @@ fun SampleTopBar(
5051 },
5152 navigationIcon = {
5253 if (showBackNavigation) {
53- IconButton (onClick = onBackClick) {
54+ val debouncedOnBackClick = rememberDebouncedOnClick(onClick = onBackClick)
55+ IconButton (onClick = debouncedOnBackClick) {
5456 Icon (
5557 imageVector = Icons .AutoMirrored .Filled .ArrowBack ,
5658 contentDescription = " Navigate back" ,
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ import com.auth0.android.sample.ui.components.OrDivider
4141import com.auth0.android.sample.ui.theme.auth0ScreenBackground
4242import com.auth0.android.sample.ui.theme.isAuth0DarkTheme
4343import com.auth0.android.sample.ui.components.SampleGradientButton
44+ import com.auth0.android.sample.ui.util.rememberDebouncedOnClick
4445import com.auth0.universalcomponents.theme.Auth0Theme
4546
4647/* *
@@ -82,7 +83,8 @@ fun EmbeddedLoginScreen(
8283 horizontalAlignment = Alignment .CenterHorizontally
8384 ) {
8485 Row (modifier = Modifier .fillMaxWidth()) {
85- IconButton (onClick = onBack) {
86+ val debouncedOnBack = rememberDebouncedOnClick(onClick = onBack)
87+ IconButton (onClick = debouncedOnBack) {
8688 Icon (
8789 imageVector = Icons .AutoMirrored .Filled .ArrowBack ,
8890 contentDescription = " Back" ,
Original file line number Diff line number Diff line change 1+ package com.auth0.android.sample.ui.util
2+
3+ import androidx.compose.runtime.Composable
4+ import androidx.compose.runtime.getValue
5+ import androidx.compose.runtime.remember
6+ import androidx.compose.runtime.rememberUpdatedState
7+
8+ /* *
9+ * Wraps [onClick] so it fires at most once per [windowMs]. Compose can dispatch several queued taps
10+ * within the same frame, before recomposition removes the composable; for back navigation that
11+ * over-pops past the start destination and leaves an empty NavHost (blank screen). Letting only the
12+ * first tap of a burst through avoids that.
13+ */
14+ @Composable
15+ internal fun rememberDebouncedOnClick (
16+ windowMs : Long = 500L,
17+ onClick : () -> Unit
18+ ): () -> Unit {
19+ val latestOnClick by rememberUpdatedState(onClick)
20+ return remember(windowMs) {
21+ var lastClickMs = 0L
22+ {
23+ val now = System .currentTimeMillis()
24+ if (now - lastClickMs >= windowMs) {
25+ lastClickMs = now
26+ latestOnClick()
27+ }
28+ }
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments