Skip to content

Commit

Permalink
added dynamic theme
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Nov 10, 2023
1 parent 3737799 commit 7975615
Show file tree
Hide file tree
Showing 63 changed files with 7,141 additions and 20 deletions.
1 change: 1 addition & 0 deletions app/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ kotlin {

api(project(":model"))
api(project(":network"))
api(project(":color"))
}
}
val androidMain by getting {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.datlag.burningseries.ui.theme

import androidx.compose.material3.ColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.toPixelMap
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import dev.datlag.burningseries.color.createTheme
import dev.datlag.burningseries.color.utils.ThemeUtils
import dev.datlag.burningseries.ui.theme.image.PainterImage

@Composable
actual fun SchemeThemeSystemProvider(
scheme: ColorScheme,
content: @Composable () -> Unit
) {
content()
}

@Composable
actual fun loadImageScheme(key: Any, painter: Painter) {
if (!SchemeTheme.containsScheme(key)) {
val density = LocalDensity.current
val layout = LocalLayoutDirection.current

SchemeTheme.createColorScheme(key) {
val bitmap = PainterImage(
painter,
density,
layout
).asBitmap()

ThemeUtils.intArrayToTheme(
bitmap.toPixelMap().buffer
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dev.datlag.burningseries.ui.screen.initial
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.*
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
Expand Down Expand Up @@ -80,8 +82,7 @@ private fun MediumScreen(
) {
var selectedPage by remember { mutableIntStateOf(0) }

Scaffold(
) {
Scaffold {
Row(modifier = Modifier.padding(it)) {
NavigationRail {
Spacer(modifier = Modifier.weight(1F))
Expand Down Expand Up @@ -120,8 +121,7 @@ private fun ExpandedScreen(
) {
var selectedPage by remember { mutableIntStateOf(0) }

Scaffold(
) {
Scaffold {
PermanentNavigationDrawer(
modifier = Modifier.padding(it),
drawerContent = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import dev.datlag.burningseries.common.bounceClick
import dev.datlag.burningseries.common.ifTrue
Expand All @@ -24,6 +25,7 @@ import dev.datlag.burningseries.model.BSUtil
import dev.datlag.burningseries.model.Home
import dev.datlag.burningseries.ui.custom.CountryImage
import dev.datlag.burningseries.ui.theme.CountryImage
import dev.datlag.burningseries.ui.theme.loadImageScheme
import dev.icerock.moko.resources.compose.painterResource
import io.kamel.core.Resource
import io.kamel.image.asyncPainterResource
Expand All @@ -32,7 +34,7 @@ import io.kamel.image.asyncPainterResource
@Composable
fun LazyGridItemScope.EpisodeItem(episode: Home.Episode, onclick: () -> Unit) {
ElevatedCard(
modifier = Modifier.animateItemPlacement().height(200.dp).bounceClick().onClick {
modifier = Modifier.animateItemPlacement().height(150.dp).bounceClick().onClick {
onclick()
}
) {
Expand All @@ -58,6 +60,7 @@ fun LazyGridItemScope.EpisodeItem(episode: Home.Episode, onclick: () -> Unit) {
contentDescription = episode.title,
modifier = Modifier.aspectRatio(1F, true)
)
loadImageScheme(BSUtil.fixSeriesHref(episode.href), resource.value)
}
}
}
Expand All @@ -69,18 +72,27 @@ fun LazyGridItemScope.EpisodeItem(episode: Home.Episode, onclick: () -> Unit) {
text = episode.series ?: episode.title,
fontWeight = FontWeight.SemiBold,
style = MaterialTheme.typography.titleLarge,
modifier = Modifier.padding(bottom = 8.dp),
maxLines = 2
modifier = Modifier.padding(bottom = 4.dp),
maxLines = 2,
overflow = TextOverflow.Ellipsis,
softWrap = true
)
if (episode.series != null) {
Text(
text = episode.episode ?: episode.title,
modifier = Modifier.padding(bottom = 8.dp),
maxLines = 3
)
Row(
modifier = Modifier.fillMaxWidth().weight(1F),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = episode.episode ?: episode.title,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
softWrap = true
)
}
}

Row(
modifier = Modifier.padding(top = 4.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Expand All @@ -89,7 +101,7 @@ fun LazyGridItemScope.EpisodeItem(episode: Home.Episode, onclick: () -> Unit) {
CountryImage(
code = code,
description = episode.flags.firstOrNull()?.title,
modifier = Modifier.size(36.dp)
modifier = Modifier.size(24.dp)
)
}
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import dev.datlag.burningseries.common.bounceClick
import dev.datlag.burningseries.common.onClick
import dev.datlag.burningseries.model.BSUtil
import dev.datlag.burningseries.model.Home
import dev.datlag.burningseries.ui.theme.loadImageScheme
import io.kamel.core.Resource
import io.kamel.image.asyncPainterResource

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LazyGridItemScope.SeriesItem(series: Home.Series, onClick: () -> Unit) {
ElevatedCard(
modifier = Modifier.animateItemPlacement().height(200.dp).bounceClick().onClick {
modifier = Modifier.animateItemPlacement().height(150.dp).bounceClick().onClick {
onClick()
}
) {
Expand All @@ -50,6 +51,7 @@ fun LazyGridItemScope.SeriesItem(series: Home.Series, onClick: () -> Unit) {
contentDescription = series.title,
modifier = Modifier.aspectRatio(1F, true)
)
loadImageScheme(BSUtil.fixSeriesHref(series.href), resource.value)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.StateFlow
interface SeriesComponent : Component {

val initialTitle: String
val initialHref: String
val initialCoverHref: String?

val seriesState: StateFlow<SeriesState>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import dev.datlag.burningseries.ui.custom.readmore.ToggleArea
import dev.datlag.burningseries.ui.custom.state.ErrorState
import dev.datlag.burningseries.ui.custom.state.LoadingState
import dev.datlag.burningseries.ui.screen.initial.series.component.SeasonAndLanguageButtons
import dev.datlag.burningseries.ui.theme.SchemeTheme
import dev.datlag.burningseries.ui.theme.loadImageScheme
import dev.datlag.burningseries.ui.theme.shape.DiagonalShape
import dev.icerock.moko.resources.compose.stringResource
import io.kamel.core.Resource
Expand All @@ -50,15 +52,18 @@ import kotlin.math.abs
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
@Composable
fun SeriesScreen(component: SeriesComponent) {
when (calculateWindowSizeClass().widthSizeClass) {
WindowWidthSizeClass.Compact -> CompactScreen(component)
else -> DefaultScreen(component)
SchemeTheme(BSUtil.fixSeriesHref(component.initialHref)) {
when (calculateWindowSizeClass().widthSizeClass) {
WindowWidthSizeClass.Compact -> CompactScreen(component)
else -> DefaultScreen(component)
}
}
}

@Composable
private fun CompactScreen(component: SeriesComponent) {
val seriesState by component.seriesState.collectAsStateWithLifecycle()
val title = remember(seriesState) { (seriesState as? SeriesState.Success)?.series?.title ?: component.initialTitle }

DefaultCollapsingToolbar(
expandedBody = { state ->
Expand All @@ -75,11 +80,12 @@ private fun CompactScreen(component: SeriesComponent) {
contentDescription = component.initialTitle,
contentScale = ContentScale.FillWidth,
)
loadImageScheme(BSUtil.fixSeriesHref(component.initialHref), resource.value)
}
}

Text(
text = component.initialTitle,
text = title,
modifier = Modifier.road(Alignment.TopStart, Alignment.BottomStart).padding(16.dp),
color = LocalContentColor.current.copy(alpha = run {
val alpha = state.toolbarState.progress
Expand Down Expand Up @@ -109,7 +115,7 @@ private fun CompactScreen(component: SeriesComponent) {
}

Text(
text = component.initialTitle,
text = title,
color = LocalContentColor.current.copy(alpha = run {
val alpha = reversedProgress
if (alpha < 0.7F) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SeriesScreenComponent(
componentContext: ComponentContext,
override val di: DI,
override val initialTitle: String,
val initialHref: String,
override val initialHref: String,
override val initialCoverHref: String?,
private val onGoBack: () -> Unit
) : SeriesComponent, ComponentContext by componentContext {
Expand Down
Loading

0 comments on commit 7975615

Please sign in to comment.