Created
October 23, 2025 14:07
-
-
Save Sottti/a8517b5140aeff26cbfcd96e1530ca85 to your computer and use it in GitHub Desktop.
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
| import android.content.ComponentCallbacks | |
| import android.content.Context | |
| import android.content.res.Configuration | |
| import kotlinx.coroutines.channels.awaitClose | |
| import kotlinx.coroutines.flow.Flow | |
| import kotlinx.coroutines.flow.callbackFlow | |
| import kotlinx.coroutines.flow.conflate | |
| import kotlinx.coroutines.flow.distinctUntilChanged | |
| public fun <T> Context.observeConfigurationChanges( | |
| getValue: () -> T, | |
| ): Flow<T> = callbackFlow { | |
| trySend(getValue()) | |
| val callbacks = object : ComponentCallbacks { | |
| override fun onConfigurationChanged(newConfig: Configuration) { | |
| trySend(getValue()) | |
| } | |
| @Suppress("OVERRIDE_DEPRECATION") | |
| override fun onLowMemory() = Unit | |
| } | |
| registerComponentCallbacks(callbacks) | |
| awaitClose { unregisterComponentCallbacks(callbacks) } | |
| } | |
| .distinctUntilChanged() | |
| .conflate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment