Created
February 11, 2025 18:09
-
-
Save vitoksmile/364d563facdf91689862796779ff7f92 to your computer and use it in GitHub Desktop.
ComposeHints
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
| @Stable | |
| class HintController internal constructor() { | |
| private var queue = mutableStateListOf<HintAnchorState>() | |
| internal val hint: HintAnchorState? get() = queue.firstOrNull() | |
| private val pendingRequests = mutableMapOf<HintAnchorState, Continuation<Unit>>() | |
| suspend fun show(hint: HintAnchorState) { | |
| suspendCoroutine { continuation -> | |
| pendingRequests[hint] = continuation | |
| queue.add(hint) | |
| } | |
| } | |
| suspend fun show(vararg hint: HintAnchorState) { | |
| show(hint.toList()) | |
| } | |
| suspend fun show(hints: List<HintAnchorState>) { | |
| suspendCoroutine { continuation -> | |
| pendingRequests[hints.last()] = continuation | |
| queue.addAll(hints) | |
| } | |
| } | |
| internal fun onDismissed(hint: HintAnchorState) { | |
| pendingRequests[hint]?.let { continuation -> | |
| continuation.resume(Unit) | |
| pendingRequests.remove(hint) | |
| } | |
| queue.remove(hint) | |
| } | |
| fun dismiss() { | |
| pendingRequests.values | |
| .forEach { continuation -> | |
| continuation.resumeWithException(CancellationException("Hint was dismissed")) | |
| } | |
| pendingRequests.clear() | |
| queue.clear() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment