Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: Add configChangesChannel #127

Open
LouisCAD opened this issue Sep 19, 2018 · 0 comments
Open

Idea: Add configChangesChannel #127

LouisCAD opened this issue Sep 19, 2018 · 0 comments

Comments

@LouisCAD
Copy link
Owner

LouisCAD commented Sep 19, 2018

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)
        }
    })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant