enabled

abstract fun enabled(): Flow<Boolean>

Returns a cold Flow of Boolean values representing whether this toggle is enabled.

Behavior

  • When a collector starts, the current toggle value is emitted immediately.

  • Subsequent emissions occur whenever the underlying store writes a new State.

  • The flow is cold: a listener is only registered while it is being collected.

  • When collection is cancelled or completed, the registered listener is automatically unregistered.

Thread-safety

Emissions are delivered on the coroutine context where the flow is collected. Multiple collectors will each register their own listener instance.

Example

viewModelScope.launch {
toggle.enabled()
.distinctUntilChanged()
.collect { enabled ->
if (enabled) {
showOnboarding()
} else {
showLoading()
}
}
}

Note: When not context is specified, Dispatchers.IO will be used

Return

a cold Flow that emits the current enabled state and any subsequent changes until the collector is cancelled.