Splitties
Splitties copied to clipboard
Idea: Add configChangesChannel
This could be helpful to invalidate anything that relies on latest config change.
Examples are most UI related stuff (like parts of a Ui in View DSL), locale bound objects (like DateFormats).
The fact that you can diff two Configuration objects to know what changed is nice to perform fine grained invalidations.
On the bad side, broadcasting with a non conflated channel can introduce problems if a subscriber consumes elements slower than they are offered. There are multiple ways to tackle this problem, including outlawing "failed" offer calls (those which return false).
Here's the simplest approach, which silently fails in case of consumption slower than offering:
import android.content.ComponentCallbacks
import android.content.res.Configuration
import kotlinx.coroutines.experimental.channels.BroadcastChannel
val application = appCtx.applicationContext as Application // TODO: Add this to appCtx?
val configChangesChannel get() = configChangesBroadcastChannel.openSubscription()
private val configChangesBroadcastChannel = BroadcastChannel<Configuration>(capacity = 1).also {
application.registerComponentCallbacks(object : ComponentCallbacks {
override fun onLowMemory() = Unit
override fun onConfigurationChanged(newConfig: Configuration) {
it.offer(newConfig)
}
})
}