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

Configuration of custom views #109

Open
Dixisss opened this issue Aug 27, 2019 · 1 comment
Open

Configuration of custom views #109

Dixisss opened this issue Aug 27, 2019 · 1 comment
Labels
question Further information is requested

Comments

@Dixisss
Copy link

Dixisss commented Aug 27, 2019

Could you please help me to find out better solutions to configurate custom views?
I have 20 different features on one screen. Each feature has its own view. The app has a dynamic theme. Each view has one input/state. I need to set theme settings for each view.
I hope that anybody has some ideas or any examples of how to do it in the best way.

@ShikaSD
Copy link
Contributor

ShikaSD commented Aug 28, 2019

Hi @Dixisss

If you are using Android theme (xml defined), I am not sure there's a solution better than reinflate your views.

In case the theme is some configuration object (e.g. Kotlin data class), you can try representing your theme as a feature state and try to use it to get some reactivity on its updates

Imagining existing setup:

data class Theme(
    val bgColor: Int = Color.BLACK
)

Feature for theme can look like this:

class ThemeFeature : ReducerFeature<Wish, Theme, Nothing>(...) {
    sealed class Wish {
        object SwitchToDark : Wish()
        object SwitchToLight : Wish()
    }
}

When the feature is set up, you can combine it in the following way:

class ThemedWrapper<T>(
    val theme: Theme,
    val viewModel: T
)

class ThemedTransformer<T, R>(val stateToViewModel: (T) -> R): (Pair<Theme, T>) -> ThemedWrapper<R> {
    override fun invoke(pair: Pair<Theme, T>): ThemedWrapper<R> =
        ThemedWrapper(it.first, stateToViewModel(it.second))
}

val featureWithTheme = Observable.combineLatest(
    wrap(themeFeature),
    wrap(feature),
    ::Pair
)

object StateToViewModel: (State) -> ViewModel // your existing transformer

Binder().apply {
    bind(featureWithTheme to view using ThemedTransformer(StateToViewModel))
}

This way you will guarantee atomicity of the theme updates and make sure the view has received the update.
Note that your view has to be able to accept ThemedWrapper<ViewModel> to make this work.

I am pretty sure there is a more elegant way to push these updates, so try to test it and see what is working best for you.

@zsoltk zsoltk added the question Further information is requested label Aug 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants