Skip to content

Commit

Permalink
improve SchemeTheme
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 18, 2024
1 parent 2e404f0 commit f6c6a62
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 84 deletions.
1 change: 1 addition & 0 deletions app/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ kotlin {

implementation(libs.haze)
implementation(libs.haze.materials)
implementation(libs.kache)

api(libs.ktor)
api(libs.ktor.content.negotiation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import dev.datlag.burningseries.model.BSUtil
import dev.datlag.burningseries.shared.common.bottomShadowBrush
import dev.datlag.burningseries.shared.ui.custom.Cover
import dev.datlag.burningseries.shared.ui.theme.SchemeTheme
import dev.datlag.burningseries.shared.ui.theme.onPrimary
import dev.datlag.burningseries.shared.ui.theme.primary
import dev.datlag.burningseries.shared.ui.theme.rememberSchemeThemeDominantColorState

@Composable
Expand All @@ -32,7 +34,7 @@ fun SeriesCard(
) {
SchemeTheme(
key = series.hrefPrimary
) {
) { updater ->
Card(
modifier = modifier,
onClick = {
Expand All @@ -42,15 +44,11 @@ fun SeriesCard(
Box(
modifier = Modifier.fillMaxSize()
) {
val scope = rememberCoroutineScope()
val colorState = rememberSchemeThemeDominantColorState(
key = series.hrefPrimary,
applyMinContrast = true,
minContrastBackgroundColor = MaterialTheme.colorScheme.surfaceVariant
)
val animatedColor by animateColorAsState(
targetValue = colorState.color
)

Cover(
key = series.coverHref,
Expand All @@ -59,19 +57,15 @@ fun SeriesCard(
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop,
onSuccess = { state ->
SchemeTheme.update(
key = series.hrefPrimary,
input = state.painter,
scope = scope
)
updater?.update(state.painter)
}
)

Column(
modifier = Modifier
.align(Alignment.BottomStart)
.fillMaxWidth()
.bottomShadowBrush(animatedColor)
.bottomShadowBrush(colorState.primary)
.padding(16.dp)
.padding(top = 16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp, Alignment.CenterVertically)
Expand All @@ -83,13 +77,13 @@ fun SeriesCard(
fontWeight = FontWeight.Bold,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.fillMaxWidth(),
color = colorState.onColor
color = colorState.onPrimary
)
series.subTitle?.let {
Text(
text = it,
modifier = Modifier.fillMaxWidth(),
color = colorState.onColor,
color = colorState.onPrimary,
maxLines = 2
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private fun DefaultScreen(component: SeriesComponent, loadingEpisode: String?) {
.clip(MaterialTheme.shapes.medium)
.align(Alignment.CenterVertically)
) {
val scope = rememberCoroutineScope()
val updater = SchemeTheme.create(commonHref)

Cover(
modifier = Modifier.fillMaxSize(),
Expand All @@ -190,7 +190,7 @@ private fun DefaultScreen(component: SeriesComponent, loadingEpisode: String?) {
stringResource(SharedRes.strings.loading_intent_series)
},
onSuccess = { success ->
SchemeTheme.update(commonHref, success.painter, scope)
updater?.update(success.painter)
}
)
IconButton(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package dev.datlag.burningseries.shared.ui.theme

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import com.materialkolor.Contrast
import com.materialkolor.PaletteStyle
import com.materialkolor.rememberDynamicColorScheme
import dev.datlag.burningseries.shared.LocalDarkMode

@Composable
fun DynamicMaterialTheme(
seedColor: Color?,
animate: Boolean = false,
animationSpec: AnimationSpec<Color> = spring(stiffness = Spring.StiffnessLow),
content: @Composable () -> Unit
) {
val dynamicColorScheme = if (seedColor != null) {
rememberDynamicColorScheme(
seedColor = seedColor,
isDark = LocalDarkMode.current,
style = PaletteStyle.TonalSpot,
contrastLevel = Contrast.Default.value,
isExtendedFidelity = false
)
} else {
MaterialTheme.colorScheme
}
val animatedColorScheme = 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),
surfaceTint = dynamicColorScheme.surfaceTint.animate(animationSpec),
surfaceBright = dynamicColorScheme.surfaceBright.animate(animationSpec),
surfaceDim = dynamicColorScheme.surfaceDim.animate(animationSpec),
surfaceContainer = dynamicColorScheme.surfaceContainer.animate(animationSpec),
surfaceContainerHigh = dynamicColorScheme.surfaceContainerHigh.animate(animationSpec),
surfaceContainerHighest = dynamicColorScheme.surfaceContainerHighest.animate(animationSpec),
surfaceContainerLow = dynamicColorScheme.surfaceContainerLow.animate(animationSpec),
surfaceContainerLowest = dynamicColorScheme.surfaceContainerLowest.animate(animationSpec),
surfaceVariant = dynamicColorScheme.surfaceVariant.animate(animationSpec),
error = dynamicColorScheme.error.animate(animationSpec),
errorContainer = dynamicColorScheme.errorContainer.animate(animationSpec),
onPrimary = dynamicColorScheme.onPrimary.animate(animationSpec),
onPrimaryContainer = dynamicColorScheme.onPrimaryContainer.animate(animationSpec),
onSecondary = dynamicColorScheme.onSecondary.animate(animationSpec),
onSecondaryContainer = dynamicColorScheme.onSecondaryContainer.animate(animationSpec),
onTertiary = dynamicColorScheme.onTertiary.animate(animationSpec),
onTertiaryContainer = dynamicColorScheme.onTertiaryContainer.animate(animationSpec),
onBackground = dynamicColorScheme.onBackground.animate(animationSpec),
onSurface = dynamicColorScheme.onSurface.animate(animationSpec),
onSurfaceVariant = dynamicColorScheme.onSurfaceVariant.animate(animationSpec),
onError = dynamicColorScheme.onError.animate(animationSpec),
onErrorContainer = dynamicColorScheme.onErrorContainer.animate(animationSpec),
inversePrimary = dynamicColorScheme.inversePrimary.animate(animationSpec),
inverseSurface = dynamicColorScheme.inverseSurface.animate(animationSpec),
inverseOnSurface = dynamicColorScheme.inverseOnSurface.animate(animationSpec),
outline = dynamicColorScheme.outline.animate(animationSpec),
outlineVariant = dynamicColorScheme.outlineVariant.animate(animationSpec),
scrim = dynamicColorScheme.scrim.animate(animationSpec),
)
}

MaterialTheme(
colorScheme = animatedColorScheme
) {
content()
}
}

@Composable
private fun Color.animate(animationSpec: AnimationSpec<Color>): Color {
return animateColorAsState(this, animationSpec).value
}
Loading

0 comments on commit f6c6a62

Please sign in to comment.