Skip to content

Commit

Permalink
Refactor colors to simplify their definition and not do things twice
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Aug 13, 2024
1 parent 2b9a89a commit 5b73cf7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.infomaniak.swisstransfer.ui.theme

import androidx.compose.material3.darkColorScheme
import androidx.compose.ui.graphics.Color

// Palette
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.infomaniak.swisstransfer.ui.theme

import androidx.compose.material3.lightColorScheme
import androidx.compose.ui.graphics.Color

// Palette
Expand All @@ -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),
)
44 changes: 9 additions & 35 deletions app/src/main/java/com/infomaniak/swisstransfer/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomColors> = staticCompositionLocalOf { CustomColors() }
val LocalCustomColorScheme: ProvidableCompositionLocal<CustomColorScheme> = 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,
Expand All @@ -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,
Expand Down

0 comments on commit 5b73cf7

Please sign in to comment.