Justin
09/22/2021, 5:44 PMtheme
, which is a global appearance setting (you can think of it sort of like light/dark mode) that is only known at runtime.
In other words, this library needs to return everything from Color.primary
to FloatingActionButton()
(for example) based on the runtime-determined theme
of the app.
The theme doesn't change across the app lifecycle (i.e. we know right at init what the theme will be and it's a constant for the rest of that app instance's existence), which is helpful.
What I'm struggling with is where this theme
setting should live. All of the components in this design system library will need to be able to read this value to work correctly, so I can imagine having it be a global var that consumers can set on the library itself.
But when I attempt to build something like this:
interface Theme {
companion object {
var value: Theme = DefaultTheme()
}
}
class DefaultTheme(...): DefaultTheme {...}
...I see the following warning in the console:
w: Theme.kt: With old Native GC, variable in singleton without @ThreadLocal can't be changed after initialization
And I'd generally like to avoid stored mutable state with my K/N libraries if at all possible.
Would love to hear peoples' thoughts on the most idiomatic/safest approach to this!kpgalligan
09/22/2021, 10:59 PMkpgalligan
09/22/2021, 11:00 PMkpgalligan
09/22/2021, 11:01 PMkpgalligan
09/22/2021, 11:02 PMkpgalligan
09/22/2021, 11:03 PMkpgalligan
09/22/2021, 11:04 PMkpgalligan
09/22/2021, 11:11 PMkpgalligan
09/22/2021, 11:15 PMkpgalligan
09/22/2021, 11:17 PMkpgalligan
09/22/2021, 11:18 PMJustin
09/23/2021, 2:42 AMJustin
09/23/2021, 2:42 AMkpgalligan
09/26/2021, 5:58 PMkpgalligan
09/26/2021, 6:01 PMPetter Måhlén
10/05/2021, 5:56 AMa global appearance setting … the components in this design system library will need to be able to read this valueI would have preferred a function
(Theme) -> DesignSystem
that gives you a fully-configured, immutable DesignSystem
instance for the specified Theme
. You’d invoke that function based on the user’s configured value at app startup, no need for any mutable state that I can see? Using DI rather than accessing a global singleton is preferable for a lot of reasons, IMO.