-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
680fc2a
commit b76d197
Showing
3 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
45 changes: 45 additions & 0 deletions
45
core/ui/src/main/kotlin/com/bff/wespot/util/MultiClickCutter.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.bff.wespot.util | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.remember | ||
import kotlinx.coroutines.FlowPreview | ||
import kotlinx.coroutines.channels.BufferOverflow | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.debounce | ||
|
||
interface MultipleEventsCutterManager { | ||
fun processEvent(event: () -> Unit) | ||
} | ||
|
||
@OptIn(FlowPreview::class) | ||
@Composable | ||
fun <T>multipleEventsCutter( | ||
content: @Composable (MultipleEventsCutterManager) -> T | ||
) : T { | ||
val debounceState = remember { | ||
MutableSharedFlow<() -> Unit>( | ||
replay = 0, | ||
extraBufferCapacity = 1, | ||
onBufferOverflow = BufferOverflow.DROP_OLDEST | ||
) | ||
} | ||
|
||
val result = content( | ||
object : MultipleEventsCutterManager { | ||
override fun processEvent(event: () -> Unit) { | ||
debounceState.tryEmit(event) | ||
} | ||
} | ||
) | ||
|
||
LaunchedEffect(true) { | ||
debounceState | ||
.debounce(300L) | ||
.collect { onClick -> | ||
onClick.invoke() | ||
} | ||
} | ||
|
||
return result | ||
} |