From 5b73cf740aba2319215902ef2dd3b9e813d95fca Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Tue, 13 Aug 2024 14:06:10 +0200 Subject: [PATCH] Refactor colors to simplify their definition and not do things twice --- .../swisstransfer/ui/theme/ColorDark.kt | 32 +++++++------- .../swisstransfer/ui/theme/ColorLight.kt | 34 +++++++------- .../swisstransfer/ui/theme/Theme.kt | 44 ++++--------------- 3 files changed, 44 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/ColorDark.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/ColorDark.kt index 572c39126..be8a19634 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/ColorDark.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/ColorDark.kt @@ -18,6 +18,7 @@ package com.infomaniak.swisstransfer.ui.theme +import androidx.compose.material3.darkColorScheme import androidx.compose.ui.graphics.Color // Palette @@ -36,21 +37,22 @@ private const val specific2 = 0xFF334117 private const val specific3 = 0xFF503E0F private const val specific4 = 0xFFEAC35D -// Roles -val dark_primary = Color(green_main) -val dark_onPrimary = Color(dark1) -val dark_secondaryContainer = Color(dark3) -val dark_onSecondaryContainer = Color(green_main) -val dark_tertiary = Color(green_dark) -val dark_onTertiary = Color(green_main) - -val dark_background = Color(dark1) -val dark_surface = dark_background -val dark_onSurface = Color(green_main) -val dark_onSurfaceVariant = Color(rabbit) -val dark_surfaceContainerHighest = Color(dark2) - -val DarkColors = CustomColors( +val DarkColorScheme = darkColorScheme( + primary = Color(green_main), + onPrimary = Color(dark1), + secondaryContainer = Color(dark3), + onSecondaryContainer = Color(green_main), + tertiary = Color(green_dark), + onTertiary = Color(green_main), + + background = Color(dark1), + surface = Color(dark1), // Same value as background + onSurface = Color(green_main), + onSurfaceVariant = Color(rabbit), + surfaceContainerHighest = Color(dark2), +) + +val CustomDarkColorScheme = CustomColorScheme( primaryTextColor = Color(rabbit), secondaryTextColor = Color(shark), navigationItemBackground = Color(dark2), diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/ColorLight.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/ColorLight.kt index 12da1cb7f..87bd51cbd 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/ColorLight.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/ColorLight.kt @@ -18,6 +18,7 @@ package com.infomaniak.swisstransfer.ui.theme +import androidx.compose.material3.lightColorScheme import androidx.compose.ui.graphics.Color // Palette @@ -41,23 +42,24 @@ private const val specific4 = 0xFFCF9E1B private const val on_primary = 0xFFF7FCFA private const val white = 0xFFFFFFFF -// Roles -val light_primary = Color(green_main) -val light_onPrimary = Color(on_primary) -val light_secondaryContainer = Color(green_secondary) -val light_onSecondaryContainer = Color(green_main) -val light_tertiary = Color(green_dark) -val light_onTertiary = Color(green_contrast) - -val light_background = Color(white) -val light_surface = light_background -val light_onSurface = Color(green_main) -val light_onSurfaceVariant = Color(green_dark) -val light_surfaceContainerHighest = Color(polar_bear) - -val LightColors = CustomColors( +val LightColorScheme = lightColorScheme( + primary = Color(green_main), + onPrimary = Color(on_primary), + secondaryContainer = Color(green_secondary), + onSecondaryContainer = Color(green_main), + tertiary = Color(green_dark), + onTertiary = Color(green_contrast), + + background = Color(white), + surface = Color(white), // Same value as background + onSurface = Color(green_main), + onSurfaceVariant = Color(green_dark), + surfaceContainerHighest = Color(polar_bear), +) + +val CustomLightColorScheme = CustomColorScheme( primaryTextColor = Color(orca), secondaryTextColor = Color(elephant), - navigationItemBackground = light_background, + navigationItemBackground = LightColorScheme.background, divider = Color(mouse), ) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/Theme.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/Theme.kt index 30e513ff3..7e09ffaa8 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/Theme.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/theme/Theme.kt @@ -19,51 +19,25 @@ package com.infomaniak.swisstransfer.ui.theme import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.material3.* +import androidx.compose.material3.ColorScheme +import androidx.compose.material3.LocalTextStyle +import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.* import androidx.compose.ui.graphics.Color -private val DarkColorScheme = darkColorScheme( - primary = dark_primary, - onPrimary = dark_onPrimary, - secondaryContainer = dark_secondaryContainer, - onSecondaryContainer = dark_onSecondaryContainer, - tertiary = dark_tertiary, - onTertiary = dark_onTertiary, - background = dark_background, - surface = dark_surface, - onSurface = dark_onSurface, - onSurfaceVariant = dark_onSurfaceVariant, - surfaceContainerHighest = dark_surfaceContainerHighest, -) - -private val LightColorScheme = lightColorScheme( - primary = light_primary, - onPrimary = light_onPrimary, - secondaryContainer = light_secondaryContainer, - onSecondaryContainer = light_onSecondaryContainer, - tertiary = light_tertiary, - onTertiary = light_onTertiary, - background = light_background, - surface = light_surface, - onSurface = light_onSurface, - onSurfaceVariant = light_onSurfaceVariant, - surfaceContainerHighest = light_surfaceContainerHighest, -) - val LocalCustomTypography = staticCompositionLocalOf { Typography } -val LocalCustomColors: ProvidableCompositionLocal = staticCompositionLocalOf { CustomColors() } +val LocalCustomColorScheme: ProvidableCompositionLocal = staticCompositionLocalOf { CustomColorScheme() } @Composable fun SwissTransferTheme( darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit, ) { - val customColors = if (darkTheme) DarkColors else LightColors + val customColors = if (darkTheme) CustomDarkColorScheme else CustomLightColorScheme CompositionLocalProvider( LocalCustomTypography provides Typography, LocalTextStyle provides Typography.bodyRegular, - LocalCustomColors provides customColors, + LocalCustomColorScheme provides customColors, ) { MaterialTheme( colorScheme = if (darkTheme) DarkColorScheme else LightColorScheme, @@ -77,16 +51,16 @@ object SwissTransferTheme { val typography: CustomTypography @Composable get() = LocalCustomTypography.current - val colors: CustomColors + val colors: CustomColorScheme @Composable - get() = LocalCustomColors.current + get() = LocalCustomColorScheme.current val materialColors: ColorScheme @Composable get() = MaterialTheme.colorScheme } @Immutable -data class CustomColors( +data class CustomColorScheme( val primaryTextColor: Color = Color.Unspecified, val secondaryTextColor: Color = Color.Unspecified, val navigationItemBackground: Color = Color.Unspecified,