material-components / material-components-android-compose-theme-adapter

A library that enables reuse of Material themes defined in XML for theming in Jetpack Compose.
https://material-components.github.io/material-components-android-compose-theme-adapter/
Apache License 2.0
415 stars 40 forks source link

In app dark mode setting is ignored & missing manual dark mode override #81

Closed Boehrsi closed 2 years ago

Boehrsi commented 3 years ago

Situation / problem: I'm currently using Compose in conjunction with views and I'm utilizing my already defined Material Design theme via the MDC-Android Compose Theme Adapter. This works actually great, except for a dark mode applied in app via AppCompatDelegate.setDefaultNightMode(darkMode) and not system wide. The app always stays in the system wide defined theme. That makes actually sense as the ComponentActivity isn't aware of the AppCompat part.

Fix idea: It would be great if this could "just work" somehow, but I'm unaware if this is somehow possible. As an easy fix and addition to this library it would be great to just have a switch for the MdcTheme() constructor or the createMdcTheme() method, to manually set the mode to dark or light.

Used library version: 1.0.1

Frank1234 commented 2 years ago

Did you find any workaround?

Boehrsi commented 2 years ago

Yes, but it's some manual work. I created some wrappers, but as a summary you could do something like this:

// colors, typography and shapes are obtained via `createMdcTheme()`

MaterialTheme(
        colors = adjustColors(colors, isDarkMode),
        typography = typography,
        shapes = shapes
    ) {
        content()
    }

// background, onBackground etc. are obtained via e.g. `ContextCompat.getColor(context, R.color.your_desired_background)`

fun adjustColors(colors: Colors, isDarkMode: Boolean): Colors {
        return if (isDarkMode) {
            colors.copy(
                background = Color(background),
                onBackground = Color(onBackground),
                surface = Color(surface),
                onSurface = Color(onSurface),
                error = Color(error),
                isLight = false
            )
        } else {
            colors
        }
    }
Frank1234 commented 2 years ago

Thank you. I don't think you even need to override the darkmode colors manually? I got it working like this, I believe:


                val (colorScheme, typography) = createMdcTheme(
                    context = context, LayoutDirection.Ltr
                )

                MaterialTheme(
                    colors = colorScheme!!,
                    typography = typography!!,
                ) {
                    ... composable content here
                }

setDefaultNightMode is taken into account, it takes the correct colors from my -night theme.

Boehrsi commented 2 years ago

That sounds pretty nice! I implemented the workaround some versions ago, potentially it's now easier to get a working dark / light flow here. Thanks for the hint. I will check this in the next week. Potentially this issue then can be closed.

Boehrsi commented 2 years ago

@Frank1234 I just double checked this in the project where I initially had the problem. I can confirm everything is working now. Closing this as it's working as intended now (tested with version 1.1.5).