MVICore
MVICore copied to clipboard
Configuration of custom views
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.
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.