Skip to content

Instantly share code, notes, and snippets.

@vitoksmile
Created February 11, 2025 18:09
Show Gist options
  • Select an option

  • Save vitoksmile/364d563facdf91689862796779ff7f92 to your computer and use it in GitHub Desktop.

Select an option

Save vitoksmile/364d563facdf91689862796779ff7f92 to your computer and use it in GitHub Desktop.
ComposeHints
@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