https://kotlinlang.org logo
#compose-android
Title
# compose-android
g

gts13

03/11/2024, 8:00 PM
Let's assume we have an app that has some settings and one of the settings is a switch button which changes the color (or font size, whatever) of the
Text
. The setting is stored in
DataStore
. Only in certain screens and at specific places the
Text
must change based on the settings. So imagine we wrap this
Text
in a composable named
DynamicText
. The
DynamicText
can be found in ScreenA, ScreenB and ScreenD. One way I assume to achieve this is to observe the setting in the viewmodels of those screens and update
DynamicText
. However is there any other way to achieve this? I imagine something like a singleton class "Observer" that holds the value of the setting and we can somehow pass it to
DynamicText
, is that possible? Or any other way?
s

Stylianos Gakis

03/11/2024, 9:05 PM
If you want this part of the screens to not come from the ViewModels you could take some inspiration from NiA for example where some part of the App state is just held in a normal state object, which is constructed in your main activity for example, and you can pass this down to any composable which is interested in this state. The source of truth can still be a DataStore or whatever you want it to be.
g

gts13

03/11/2024, 10:10 PM
hmm that seems quite what I wanted. eyxaristw!
🌟 1
s

Stylianos Gakis

03/11/2024, 10:15 PM
Glad to hear it! Parakalo 😊