Merge pull request #492 from ricknout/main

Fix issues with NiaTheme gradients and backgrounds
pull/496/head
Nick Rout 2 years ago committed by GitHub
commit a1d0dfcaa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,6 +21,7 @@ import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.test.junit4.createComposeRule
@ -28,10 +29,12 @@ import androidx.compose.ui.unit.dp
import com.google.samples.apps.nowinandroid.core.designsystem.theme.BackgroundTheme
import com.google.samples.apps.nowinandroid.core.designsystem.theme.DarkAndroidBackgroundTheme
import com.google.samples.apps.nowinandroid.core.designsystem.theme.DarkAndroidColorScheme
import com.google.samples.apps.nowinandroid.core.designsystem.theme.DarkAndroidGradientColors
import com.google.samples.apps.nowinandroid.core.designsystem.theme.DarkDefaultColorScheme
import com.google.samples.apps.nowinandroid.core.designsystem.theme.GradientColors
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LightAndroidBackgroundTheme
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LightAndroidColorScheme
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LightAndroidGradientColors
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LightDefaultColorScheme
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalBackgroundTheme
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalGradientColors
@ -42,7 +45,7 @@ import org.junit.Test
/**
* Tests [NiaTheme] using different combinations of the theme mode parameters:
* darkTheme, dynamicColor, and androidTheme.
* darkTheme, disableDynamicTheming, and androidTheme.
*
* It verifies that the various composition locals [MaterialTheme], [LocalGradientColors] and
* [LocalBackgroundTheme] have the expected values for a given theme mode, as specified by the
@ -133,7 +136,7 @@ class ThemeTest {
) {
val colorScheme = LightAndroidColorScheme
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = emptyGradientColors
val gradientColors = LightAndroidGradientColors
assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = LightAndroidBackgroundTheme
assertEquals(backgroundTheme, LocalBackgroundTheme.current)
@ -151,7 +154,7 @@ class ThemeTest {
) {
val colorScheme = DarkAndroidColorScheme
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = emptyGradientColors
val gradientColors = DarkAndroidGradientColors
assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = DarkAndroidBackgroundTheme
assertEquals(backgroundTheme, LocalBackgroundTheme.current)
@ -166,11 +169,11 @@ class ThemeTest {
darkTheme = false,
androidTheme = true
) {
val colorScheme = dynamicLightColorSchemeWithFallback()
val colorScheme = LightAndroidColorScheme
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = dynamicGradientColorsWithFallback(colorScheme)
val gradientColors = LightAndroidGradientColors
assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = defaultBackgroundTheme(colorScheme)
val backgroundTheme = LightAndroidBackgroundTheme
assertEquals(backgroundTheme, LocalBackgroundTheme.current)
}
}
@ -183,11 +186,11 @@ class ThemeTest {
darkTheme = true,
androidTheme = true
) {
val colorScheme = dynamicDarkColorSchemeWithFallback()
val colorScheme = DarkAndroidColorScheme
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = dynamicGradientColorsWithFallback(colorScheme)
val gradientColors = DarkAndroidGradientColors
assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = defaultBackgroundTheme(colorScheme)
val backgroundTheme = DarkAndroidBackgroundTheme
assertEquals(backgroundTheme, LocalBackgroundTheme.current)
}
}
@ -211,7 +214,9 @@ class ThemeTest {
}
}
private val emptyGradientColors = GradientColors()
private fun emptyGradientColors(colorScheme: ColorScheme): GradientColors {
return GradientColors(container = colorScheme.surfaceColorAtElevation(2.dp))
}
private fun defaultGradientColors(colorScheme: ColorScheme): GradientColors {
return GradientColors(
@ -223,7 +228,7 @@ class ThemeTest {
private fun dynamicGradientColorsWithFallback(colorScheme: ColorScheme): GradientColors {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
emptyGradientColors
emptyGradientColors(colorScheme)
} else {
defaultGradientColors(colorScheme)
}

@ -25,6 +25,7 @@ import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.graphics.Color
@ -159,6 +160,16 @@ val DarkAndroidColorScheme = darkColorScheme(
outline = GreenGray60
)
/**
* Light Android gradient colors
*/
val LightAndroidGradientColors = GradientColors(container = DarkGreenGray95)
/**
* Dark Android gradient colors
*/
val DarkAndroidGradientColors = GradientColors(container = Color.Black)
/**
* Light Android background theme
*/
@ -207,33 +218,23 @@ internal fun NiaTheme(
) {
// Color scheme
val colorScheme = when {
!disableDynamicTheming -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
} else {
if (darkTheme) DarkDefaultColorScheme else LightDefaultColorScheme
}
}
androidTheme -> if (darkTheme) DarkAndroidColorScheme else LightAndroidColorScheme
!disableDynamicTheming && supportsDynamicTheming() -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}
else -> if (darkTheme) DarkDefaultColorScheme else LightDefaultColorScheme
}
// Gradient colors
val emptyGradientColors = GradientColors()
val emptyGradientColors = GradientColors(container = colorScheme.surfaceColorAtElevation(2.dp))
val defaultGradientColors = GradientColors(
top = colorScheme.inverseOnSurface,
bottom = colorScheme.primaryContainer,
container = colorScheme.surface
)
val gradientColors = when {
!disableDynamicTheming -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
emptyGradientColors
} else {
defaultGradientColors
}
}
androidTheme -> emptyGradientColors
androidTheme -> if (darkTheme) DarkAndroidGradientColors else LightAndroidGradientColors
!disableDynamicTheming && supportsDynamicTheming() -> emptyGradientColors
else -> defaultGradientColors
}
// Background theme
@ -242,7 +243,6 @@ internal fun NiaTheme(
tonalElevation = 2.dp
)
val backgroundTheme = when {
!disableDynamicTheming -> defaultBackgroundTheme
androidTheme -> if (darkTheme) DarkAndroidBackgroundTheme else LightAndroidBackgroundTheme
else -> defaultBackgroundTheme
}

Loading…
Cancel
Save