garretyoder / Colorful

Android runtime theme library
Apache License 2.0
2.13k stars 194 forks source link

Not working after app restart #27

Closed MFlisar closed 6 years ago

MFlisar commented 6 years ago

After trying this quite a while now, I can't get it working after restarting my app.

What I do:

1) Init colorful with the values from my own preferences (had some theming already before I tried your library):

    fun init(app: Application) {
        var primaryColor = when (AppPrefs.themeColor) {
            "0" -> ThemeColor.GREEN
            "1" -> ThemeColor.RED
            "2" -> ThemeColor.PINK
            "3" -> ThemeColor.BLUE
            else -> ThemeColor.GREEN
        }
        var accentColor = when (AppPrefs.themeColor) {
            "0" -> ThemeColor.ORANGE
            "1" -> ThemeColor.BLUE
            "2" -> ThemeColor.BLUE
            "3" -> ThemeColor.RED
            else -> ThemeColor.GREEN
        }
        val defaults = Defaults(
                primaryColor = primaryColor,
                accentColor = accentColor,
                useDarkTheme = darkTheme(),
                translucent = false,
                customTheme = baseActivityTheme()
        )
        initColorful(app, defaults)
    }

2) Update colorful whenever my preferences change:

    fun update(context: Context) {
        var primaryColor = when (themeColor()) {
            Color.Green -> ThemeColor.GREEN
            Color.Red -> ThemeColor.RED
            Color.Rosa -> ThemeColor.PINK
            Color.Blue -> ThemeColor.BLUE
        }
        var accentColor = when (themeColor()) {
            Color.Green -> ThemeColor.ORANGE
            Color.Red -> ThemeColor.BLUE
            Color.Rosa -> ThemeColor.BLUE
            Color.Blue -> ThemeColor.RED
        }
        Colorful().edit()
                .setPrimaryColor(primaryColor)
                .setAccentColor(accentColor)
                .setDarkTheme(darkTheme())
                .setTranslucent(false)
                .setCustomThemeOverride(baseActivityTheme())
                .apply(context)
    }

Observations

Any ideas why?

MFlisar commented 6 years ago

I could solve it. I think your current beta feature of using a custom theme breaks things...

My workaround is following (this works with custom themes as well):

open class BaseActivity : AppCompatActivity(), CBaseActivity {

    override var themeString: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(ThemeManager.baseActivityTheme()) // <= this works perfectly fine!!! here I apply a custom theme
        super.onCreate(savedInstanceState)
        handleOnCreate(this, savedInstanceState, true, false)
    }

    override fun onResume() {
        super.onResume()
        handleOnResume(this)
    }
}

This way, I set the custom theme and then apply your theming on this theme. This works fine for me. To understand my code above, please check out my pull request that I'll make now, I made small improvements...

I would suggest applying custom themes like above instead of what you do currently, I can add this change to my pull request if this is ok for you

garretyoder commented 6 years ago

The idea behind the user defined theme is that it doesn't inherit from anything. If it inherits from something, it will override colorful defined style values. If you want to use a different base theme (Say, activity background is orange instead of the default white or grey), then you would use your own activity with your own base theme, and override set to false, at which point Colorful wouldn't replace your theme.

What does the custom style you're trying to merge look like, and is this issue reproducible when not using a custom style?

MFlisar commented 6 years ago

Understood that now. Maybe customStyle would be a better name for this option?

Would make sense to support passing in a custom base theme as well that will replace the default one...

garretyoder commented 6 years ago

That's not a half bad idea, that's why this feature is marked beta even though it works fine, I'm not sure of the direction of it yet. Adding a custom base theme is something I could do.

For now, if you want to use a custom base theme my suggestion would be to have your theme inherit from one of colorful's base themes, make your modifications, and then use a if for colorful().darkTheme to decide which one to manually apply.

If you use Colorful().apply, and set override to false, it won't replace the theme the activity already has but will still do coloring.