Address design system PR feedback

pull/485/head
Nick Rout 2 years ago
parent cac02c418d
commit 9fd47261c9

@ -21,6 +21,7 @@ import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.dynamicDarkColorScheme import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -62,16 +63,9 @@ class ThemeTest {
) { ) {
val colorScheme = LightDefaultColorScheme val colorScheme = LightDefaultColorScheme
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme) assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = GradientColors( val gradientColors = defaultGradientColors(colorScheme)
top = colorScheme.inverseOnSurface,
bottom = colorScheme.primaryContainer,
container = colorScheme.surface
)
assertEquals(gradientColors, LocalGradientColors.current) assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = BackgroundTheme( val backgroundTheme = defaultBackgroundTheme(colorScheme)
color = colorScheme.surface,
tonalElevation = 2.dp
)
assertEquals(backgroundTheme, LocalBackgroundTheme.current) assertEquals(backgroundTheme, LocalBackgroundTheme.current)
} }
} }
@ -87,16 +81,9 @@ class ThemeTest {
) { ) {
val colorScheme = DarkDefaultColorScheme val colorScheme = DarkDefaultColorScheme
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme) assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = GradientColors( val gradientColors = defaultGradientColors(colorScheme)
top = colorScheme.inverseOnSurface,
bottom = colorScheme.primaryContainer,
container = colorScheme.surface
)
assertEquals(gradientColors, LocalGradientColors.current) assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = BackgroundTheme( val backgroundTheme = defaultBackgroundTheme(colorScheme)
color = colorScheme.surface,
tonalElevation = 2.dp
)
assertEquals(backgroundTheme, LocalBackgroundTheme.current) assertEquals(backgroundTheme, LocalBackgroundTheme.current)
} }
} }
@ -109,26 +96,11 @@ class ThemeTest {
darkTheme = false, darkTheme = false,
androidTheme = false androidTheme = false
) { ) {
val colorScheme = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val colorScheme = dynamicLightColorSchemeWithFallback()
dynamicLightColorScheme(LocalContext.current)
} else {
LightDefaultColorScheme
}
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme) assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val gradientColors = dynamicGradientColorsWithFallback(colorScheme)
GradientColors()
} else {
GradientColors(
top = colorScheme.inverseOnSurface,
bottom = colorScheme.primaryContainer,
container = colorScheme.surface
)
}
assertEquals(gradientColors, LocalGradientColors.current) assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = BackgroundTheme( val backgroundTheme = defaultBackgroundTheme(colorScheme)
color = colorScheme.surface,
tonalElevation = 2.dp
)
assertEquals(backgroundTheme, LocalBackgroundTheme.current) assertEquals(backgroundTheme, LocalBackgroundTheme.current)
} }
} }
@ -141,26 +113,11 @@ class ThemeTest {
darkTheme = true, darkTheme = true,
androidTheme = false androidTheme = false
) { ) {
val colorScheme = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val colorScheme = dynamicDarkColorSchemeWithFallback()
dynamicDarkColorScheme(LocalContext.current)
} else {
DarkDefaultColorScheme
}
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme) assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val gradientColors = dynamicGradientColorsWithFallback(colorScheme)
GradientColors()
} else {
GradientColors(
top = colorScheme.inverseOnSurface,
bottom = colorScheme.primaryContainer,
container = colorScheme.surface
)
}
assertEquals(gradientColors, LocalGradientColors.current) assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = BackgroundTheme( val backgroundTheme = defaultBackgroundTheme(colorScheme)
color = colorScheme.surface,
tonalElevation = 2.dp
)
assertEquals(backgroundTheme, LocalBackgroundTheme.current) assertEquals(backgroundTheme, LocalBackgroundTheme.current)
} }
} }
@ -176,7 +133,7 @@ class ThemeTest {
) { ) {
val colorScheme = LightAndroidColorScheme val colorScheme = LightAndroidColorScheme
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme) assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = GradientColors() val gradientColors = emptyGradientColors
assertEquals(gradientColors, LocalGradientColors.current) assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = LightAndroidBackgroundTheme val backgroundTheme = LightAndroidBackgroundTheme
assertEquals(backgroundTheme, LocalBackgroundTheme.current) assertEquals(backgroundTheme, LocalBackgroundTheme.current)
@ -194,7 +151,7 @@ class ThemeTest {
) { ) {
val colorScheme = DarkAndroidColorScheme val colorScheme = DarkAndroidColorScheme
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme) assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = GradientColors() val gradientColors = emptyGradientColors
assertEquals(gradientColors, LocalGradientColors.current) assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = DarkAndroidBackgroundTheme val backgroundTheme = DarkAndroidBackgroundTheme
assertEquals(backgroundTheme, LocalBackgroundTheme.current) assertEquals(backgroundTheme, LocalBackgroundTheme.current)
@ -209,23 +166,11 @@ class ThemeTest {
darkTheme = false, darkTheme = false,
androidTheme = true androidTheme = true
) { ) {
val colorScheme = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val colorScheme = dynamicLightColorSchemeWithFallback()
dynamicLightColorScheme(LocalContext.current)
} else {
LightDefaultColorScheme
}
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme) assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val gradientColors = dynamicGradientColorsWithFallback(colorScheme)
GradientColors()
} else {
GradientColors(
top = colorScheme.inverseOnSurface,
bottom = colorScheme.primaryContainer,
container = colorScheme.surface
)
}
assertEquals(gradientColors, LocalGradientColors.current) assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = LightAndroidBackgroundTheme val backgroundTheme = defaultBackgroundTheme(colorScheme)
assertEquals(backgroundTheme, LocalBackgroundTheme.current) assertEquals(backgroundTheme, LocalBackgroundTheme.current)
} }
} }
@ -238,28 +183,59 @@ class ThemeTest {
darkTheme = true, darkTheme = true,
androidTheme = true androidTheme = true
) { ) {
val colorScheme = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val colorScheme = dynamicDarkColorSchemeWithFallback()
dynamicDarkColorScheme(LocalContext.current)
} else {
DarkDefaultColorScheme
}
assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme) assertColorSchemesEqual(colorScheme, MaterialTheme.colorScheme)
val gradientColors = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val gradientColors = dynamicGradientColorsWithFallback(colorScheme)
GradientColors()
} else {
GradientColors(
top = colorScheme.inverseOnSurface,
bottom = colorScheme.primaryContainer,
container = colorScheme.surface
)
}
assertEquals(gradientColors, LocalGradientColors.current) assertEquals(gradientColors, LocalGradientColors.current)
val backgroundTheme = DarkAndroidBackgroundTheme val backgroundTheme = defaultBackgroundTheme(colorScheme)
assertEquals(backgroundTheme, LocalBackgroundTheme.current) assertEquals(backgroundTheme, LocalBackgroundTheme.current)
} }
} }
} }
@Composable
private fun dynamicLightColorSchemeWithFallback(): ColorScheme {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
dynamicLightColorScheme(LocalContext.current)
} else {
LightDefaultColorScheme
}
}
@Composable
private fun dynamicDarkColorSchemeWithFallback(): ColorScheme {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
dynamicDarkColorScheme(LocalContext.current)
} else {
DarkDefaultColorScheme
}
}
private val emptyGradientColors = GradientColors()
private fun defaultGradientColors(colorScheme: ColorScheme): GradientColors {
return GradientColors(
top = colorScheme.inverseOnSurface,
bottom = colorScheme.primaryContainer,
container = colorScheme.surface
)
}
private fun dynamicGradientColorsWithFallback(colorScheme: ColorScheme): GradientColors {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
emptyGradientColors
} else {
defaultGradientColors(colorScheme)
}
}
private fun defaultBackgroundTheme(colorScheme: ColorScheme): BackgroundTheme {
return BackgroundTheme(
color = colorScheme.surface,
tonalElevation = 2.dp
)
}
/** /**
* Workaround for the fact that the NiA design system specify all color scheme values. * Workaround for the fact that the NiA design system specify all color scheme values.
*/ */

@ -234,12 +234,12 @@ fun NiaTextButton(
* Internal Now in Android button content layout for arranging the text label and leading icon. * Internal Now in Android button content layout for arranging the text label and leading icon.
* *
* @param text The button text label content. * @param text The button text label content.
* @param leadingIcon The button leading icon content. Pass `null` here for no leading icon. * @param leadingIcon The button leading icon content. Default is `null` for no leading icon.Ï
*/ */
@Composable @Composable
private fun NiaButtonContent( private fun NiaButtonContent(
text: @Composable () -> Unit, text: @Composable () -> Unit,
leadingIcon: @Composable (() -> Unit)? leadingIcon: @Composable (() -> Unit)? = null
) { ) {
if (leadingIcon != null) { if (leadingIcon != null) {
Box(Modifier.sizeIn(maxHeight = ButtonDefaults.IconSize)) { Box(Modifier.sizeIn(maxHeight = ButtonDefaults.IconSize)) {

@ -115,12 +115,12 @@ fun <T> NiaDropdownMenuButton(
* trailing icon. * trailing icon.
* *
* @param text The button text label content. * @param text The button text label content.
* @param trailingIcon The button trailing icon content. Pass `null` here for no trailing icon. * @param trailingIcon The button trailing icon content. Default is `null` for no trailing icon.
*/ */
@Composable @Composable
private fun NiaDropdownMenuButtonContent( private fun NiaDropdownMenuButtonContent(
text: @Composable () -> Unit, text: @Composable () -> Unit,
trailingIcon: @Composable (() -> Unit)?, trailingIcon: @Composable (() -> Unit)? = null,
) { ) {
Box( Box(
Modifier Modifier

@ -77,12 +77,12 @@ fun NiaViewToggleButton(
* trailing icon. * trailing icon.
* *
* @param text The button text label content. * @param text The button text label content.
* @param trailingIcon The button trailing icon content. Pass `null` here for no trailing icon. * @param trailingIcon The button trailing icon content. Default is `null` for no trailing icon.
*/ */
@Composable @Composable
private fun NiaViewToggleButtonContent( private fun NiaViewToggleButtonContent(
text: @Composable () -> Unit, text: @Composable () -> Unit,
trailingIcon: @Composable (() -> Unit)?, trailingIcon: @Composable (() -> Unit)? = null,
) { ) {
Box( Box(
Modifier Modifier

@ -241,10 +241,10 @@ internal fun NiaTheme(
color = colorScheme.surface, color = colorScheme.surface,
tonalElevation = 2.dp tonalElevation = 2.dp
) )
val backgroundTheme = if (androidTheme) { val backgroundTheme = when {
if (darkTheme) DarkAndroidBackgroundTheme else LightAndroidBackgroundTheme !disableDynamicTheming -> defaultBackgroundTheme
} else { androidTheme -> if (darkTheme) DarkAndroidBackgroundTheme else LightAndroidBackgroundTheme
defaultBackgroundTheme else -> defaultBackgroundTheme
} }
// Composition locals // Composition locals
CompositionLocalProvider( CompositionLocalProvider(

Loading…
Cancel
Save