Skip to content

Commit

Permalink
Feature: Deprecate AnimatedDynamicTheme, add animate to DynamicMateri…
Browse files Browse the repository at this point in the history
…alTheme (#103)
  • Loading branch information
jordond authored Feb 9, 2024
1 parent 33547ac commit b9ba16f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.materialkolor.AnimatedDynamicMaterialTheme
import com.materialkolor.DynamicMaterialTheme
import com.materialkolor.PaletteStyle

private val AppShapes = Shapes(
Expand All @@ -37,16 +37,17 @@ internal fun AppTheme(
seedColor: Color,
paletteStyle: PaletteStyle = PaletteStyle.TonalSpot,
useDarkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable() () -> Unit,
content: @Composable () -> Unit,
) {
MaterialTheme(
typography = AppTypography,
shapes = AppShapes,
content = {
AnimatedDynamicMaterialTheme(
DynamicMaterialTheme(
seedColor = seedColor,
useDarkTheme = useDarkTheme,
style = paletteStyle,
animate = true,
content = {
Surface(content = content)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import com.materialkolor.dynamiccolor.MaterialDynamicColors
import com.materialkolor.ktx.getColor
import com.materialkolor.ktx.toDynamicScheme

/**
Expand Down Expand Up @@ -47,34 +48,34 @@ public fun dynamicColorScheme(
val colors = MaterialDynamicColors(isExtendedFidelity)

return ColorScheme(
background = Color(colors.background().getArgb(scheme)),
error = Color(colors.error().getArgb(scheme)),
errorContainer = Color(colors.errorContainer().getArgb(scheme)),
inverseOnSurface = Color(colors.inverseOnSurface().getArgb(scheme)),
inversePrimary = Color(colors.inversePrimary().getArgb(scheme)),
inverseSurface = Color(colors.inverseSurface().getArgb(scheme)),
onBackground = Color(colors.onBackground().getArgb(scheme)),
onError = Color(colors.onError().getArgb(scheme)),
onErrorContainer = Color(colors.onErrorContainer().getArgb(scheme)),
onPrimary = Color(colors.onPrimary().getArgb(scheme)),
onPrimaryContainer = Color(colors.onPrimaryContainer().getArgb(scheme)),
onSecondary = Color(colors.onSecondary().getArgb(scheme)),
onSecondaryContainer = Color(colors.onSecondaryContainer().getArgb(scheme)),
onSurface = Color(colors.onSurface().getArgb(scheme)),
onSurfaceVariant = Color(colors.onSurfaceVariant().getArgb(scheme)),
onTertiary = Color(colors.onTertiary().getArgb(scheme)),
onTertiaryContainer = Color(colors.onTertiaryContainer().getArgb(scheme)),
outline = Color(colors.outline().getArgb(scheme)),
outlineVariant = Color(colors.outlineVariant().getArgb(scheme)),
primary = Color(colors.primary().getArgb(scheme)),
primaryContainer = Color(colors.primaryContainer().getArgb(scheme)),
scrim = Color(colors.scrim().getArgb(scheme)),
secondary = Color(colors.secondary().getArgb(scheme)),
secondaryContainer = Color(colors.secondaryContainer().getArgb(scheme)),
surface = Color(colors.surface().getArgb(scheme)),
surfaceTint = Color(colors.surfaceTint().getArgb(scheme)),
surfaceVariant = Color(colors.surfaceVariant().getArgb(scheme)),
tertiary = Color(colors.tertiary().getArgb(scheme)),
tertiaryContainer = Color(colors.tertiaryContainer().getArgb(scheme))
background = colors.background().getColor(scheme),
error = colors.error().getColor(scheme),
errorContainer = colors.errorContainer().getColor(scheme),
inverseOnSurface = colors.inverseOnSurface().getColor(scheme),
inversePrimary = colors.inversePrimary().getColor(scheme),
inverseSurface = colors.inverseSurface().getColor(scheme),
onBackground = colors.onBackground().getColor(scheme),
onError = colors.onError().getColor(scheme),
onErrorContainer = colors.onErrorContainer().getColor(scheme),
onPrimary = colors.onPrimary().getColor(scheme),
onPrimaryContainer = colors.onPrimaryContainer().getColor(scheme),
onSecondary = colors.onSecondary().getColor(scheme),
onSecondaryContainer = colors.onSecondaryContainer().getColor(scheme),
onSurface = colors.onSurface().getColor(scheme),
onSurfaceVariant = colors.onSurfaceVariant().getColor(scheme),
onTertiary = colors.onTertiary().getColor(scheme),
onTertiaryContainer = colors.onTertiaryContainer().getColor(scheme),
outline = colors.outline().getColor(scheme),
outlineVariant = colors.outlineVariant().getColor(scheme),
primary = colors.primary().getColor(scheme),
primaryContainer = colors.primaryContainer().getColor(scheme),
scrim = colors.scrim().getColor(scheme),
secondary = colors.secondary().getColor(scheme),
secondaryContainer = colors.secondaryContainer().getColor(scheme),
surface = colors.surface().getColor(scheme),
surfaceTint = colors.surfaceTint().getColor(scheme),
surfaceVariant = colors.surfaceVariant().getColor(scheme),
tertiary = colors.tertiary().getColor(scheme),
tertiaryContainer = colors.tertiaryContainer().getColor(scheme),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import com.materialkolor.dynamiccolor.MaterialDynamicColors
* @param[style] The style of the color scheme.
* @param[contrastLevel] The contrast level of the color scheme.
* @param[isExtendedFidelity] Whether to use the extended fidelity color set. See [MaterialDynamicColors].
* @param[animate] Whether to animate the color scheme or not.
* @param[animationSpec] The animation spec to use for animating the color scheme.
* @param[content] The Composable content of the theme.
*/
@Composable
Expand All @@ -37,16 +39,40 @@ public fun DynamicMaterialTheme(
shapes: Shapes = MaterialTheme.shapes,
typography: Typography = MaterialTheme.typography,
isExtendedFidelity: Boolean = false,
animate: Boolean = false,
animationSpec: AnimationSpec<Color> = spring(stiffness = Spring.StiffnessLow),
content: @Composable () -> Unit,
) {
val colorScheme: ColorScheme = rememberDynamicColorScheme(
val dynamicColorScheme: ColorScheme = rememberDynamicColorScheme(
seedColor = seedColor,
isDark = useDarkTheme,
style = style,
contrastLevel = contrastLevel,
isExtendedFidelity = isExtendedFidelity,
)

val colorScheme =
if (!animate) dynamicColorScheme
else {
dynamicColorScheme.copy(
primary = dynamicColorScheme.primary.animate(animationSpec),
primaryContainer = dynamicColorScheme.primaryContainer.animate(animationSpec),
secondary = dynamicColorScheme.secondary.animate(animationSpec),
secondaryContainer = dynamicColorScheme.secondaryContainer.animate(animationSpec),
tertiary = dynamicColorScheme.tertiary.animate(animationSpec),
tertiaryContainer = dynamicColorScheme.tertiaryContainer.animate(animationSpec),
background = dynamicColorScheme.background.animate(animationSpec),
surface = dynamicColorScheme.surface.animate(animationSpec),
error = dynamicColorScheme.error.animate(animationSpec),
onPrimary = dynamicColorScheme.onPrimary.animate(animationSpec),
onSecondary = dynamicColorScheme.onSecondary.animate(animationSpec),
onTertiary = dynamicColorScheme.onTertiary.animate(animationSpec),
onBackground = dynamicColorScheme.onBackground.animate(animationSpec),
onSurface = dynamicColorScheme.onSurface.animate(animationSpec),
onError = dynamicColorScheme.onError.animate(animationSpec),
)
}

CompositionLocalProvider(LocalDynamicMaterialThemeSeed provides seedColor) {
MaterialTheme(
colorScheme = colorScheme,
Expand All @@ -72,6 +98,10 @@ public fun DynamicMaterialTheme(
* @param[isExtendedFidelity] Whether to use the extended fidelity color set. See [MaterialDynamicColors].
* @param[content] The Composable content of the theme.
*/
@Deprecated(
level = DeprecationLevel.WARNING,
message = "Use DynamicMaterialTheme with animate = true instead."
)
@Composable
public fun AnimatedDynamicMaterialTheme(
seedColor: Color,
Expand All @@ -84,38 +114,21 @@ public fun AnimatedDynamicMaterialTheme(
isExtendedFidelity: Boolean = false,
content: @Composable () -> Unit,
) {
val colors: ColorScheme = rememberDynamicColorScheme(
DynamicMaterialTheme(
seedColor = seedColor,
isDark = useDarkTheme,
useDarkTheme = useDarkTheme,
style = style,
contrastLevel = contrastLevel,
shapes = shapes,
typography = typography,
isExtendedFidelity = isExtendedFidelity,
animate = true,
animationSpec = animationSpec,
content = content,
)
}

val animatedColorScheme = colors.copy(
primary = animateColorAsState(colors.primary, animationSpec).value,
primaryContainer = animateColorAsState(colors.primaryContainer, animationSpec).value,
secondary = animateColorAsState(colors.secondary, animationSpec).value,
secondaryContainer = animateColorAsState(colors.secondaryContainer, animationSpec).value,
tertiary = animateColorAsState(colors.tertiary, animationSpec).value,
tertiaryContainer = animateColorAsState(colors.tertiaryContainer, animationSpec).value,
background = animateColorAsState(colors.background, animationSpec).value,
surface = animateColorAsState(colors.surface, animationSpec).value,
error = animateColorAsState(colors.error, animationSpec).value,
onPrimary = animateColorAsState(colors.onPrimary, animationSpec).value,
onSecondary = animateColorAsState(colors.onSecondary, animationSpec).value,
onTertiary = animateColorAsState(colors.onTertiary, animationSpec).value,
onBackground = animateColorAsState(colors.onBackground, animationSpec).value,
onSurface = animateColorAsState(colors.onSurface, animationSpec).value,
onError = animateColorAsState(colors.onError, animationSpec).value,
)

CompositionLocalProvider(LocalDynamicMaterialThemeSeed provides seedColor) {
MaterialTheme(
colorScheme = animatedColorScheme,
shapes = shapes,
typography = typography,
content = content,
)
}
@Composable
private fun Color.animate(animationSpec: AnimationSpec<Color>): Color {
return animateColorAsState(this, animationSpec).value
}

0 comments on commit b9ba16f

Please sign in to comment.