Hello! Is there a DSL for creating `ColorStateList...
# splitties
d
Hello! Is there a DSL for creating
ColorStateList
in code? I mean not loading from resources (
colorSL
), but actually specifying states and colors manually.
l
Hello! There's none yet, but I've thought about making one. It's a little more complicated than for drawable state lists because you need to pass all the states in the constructor, which means you need to create a custom builder. I kept on using xml for these until now. Do you have an API design in mind?
d
I didn't think about it thoroughly yet. Just starting out with splitties, so making myself familiar with the api. I cannot use xml, because I have an ongoing experiment of a fully dynamic theme switching, without relying on context-wrappers or activity restarts at all, basically I have my own simplest rx-based theme changing engine. So I think I'll live with it for some time, see what patterns regrading color SL's emerge and then perhaps I can suggest some API design )) I prefer doing it this way lately)
l
You could make it
Flow
based as well with
StateFlow
😉
d
Yeah, I know, but switching a heavily Rx based (and otherwise stable) architecture to coroutines would be to much experimentation at the moment 🙂
l
to -> too?
d
hmm. no)) RxJava -> (to) coroutines
l
to much -> too much I meant 😄
d
ah, right! )
l
Please, let me know about how it goes for dynamic theming 🙂
d
So far I reached a point where I seamlessly able to switch theme at runtime. My ContourLayout based programmatic layout looks like this (
ThemeableUi
delegate does all the hooking magic),
applyStyle
is my custom extension:
Copy code
class MyLayout : ContourLayout, Ui by ThemeableUi {
  val title = textView {
    // Will be called automatically on theme change
    applyStyle { theme ->
      add(theme.textStyles.largeTitle)
      textColor(theme.colors.accent)
    }

    text = "Hello, world!"

    applyLayout(
      x = centerHorizontallyTo { parent.centerX() },
      y = centerVerticallyTo { parent.centerY() }
    )
  }
Theme class looks like this:
Copy code
sealed class Theme {
  abstract val id: String
  abstract val description: ThemeDescription
  abstract val textStyles: TextStyles
  abstract val colors: Colors

  data class Light(
    override val id: String,
    override val description: ThemeDescription,
    override val textStyles: TextStyles,
    override val colors: Colors
  ) : Theme() { /* constructor omitted */ }

  data class Dark(
    override val id: String,
    override val description: ThemeDescription,
    override val textStyles: TextStyles,
    override val colors: Colors
  ) : Theme() { /* constructor omitted */ }
}
I am using this on a new project, I plan to see how this all works out and then I maybe write a blog post on this stuff )
It uses
airbnb/paris
for applying view styles dynamically