Skip to content

Commit 12c3051

Browse files
committed
UIC-761 : Added a debounce logic for sample app
1 parent dbb63df commit 12c3051

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

app/src/main/java/com/auth0/android/sample/ui/components/SampleTopBar.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import androidx.compose.ui.graphics.Color
1818
import androidx.compose.ui.graphics.painter.Painter
1919
import androidx.compose.ui.text.TextStyle
2020
import androidx.compose.ui.unit.dp
21+
import com.auth0.android.sample.ui.util.rememberDebouncedOnClick
2122
import 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",

app/src/main/java/com/auth0/android/sample/ui/screens/EmbeddedLoginScreen.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import com.auth0.android.sample.ui.components.OrDivider
4141
import com.auth0.android.sample.ui.theme.auth0ScreenBackground
4242
import com.auth0.android.sample.ui.theme.isAuth0DarkTheme
4343
import com.auth0.android.sample.ui.components.SampleGradientButton
44+
import com.auth0.android.sample.ui.util.rememberDebouncedOnClick
4445
import 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",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)