Add colors lambda

This commit is contained in:
bemusementpark 2024-07-29 14:49:37 +09:30
parent 90f6fee579
commit 492d5217d0
4 changed files with 24 additions and 38 deletions

View File

@ -6,7 +6,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import org.session.libsession.utilities.TextSecurePreferences
import org.thoughtcrime.securesms.ui.theme.selectedColorSet
import org.thoughtcrime.securesms.ui.theme.cachedColors
import org.thoughtcrime.securesms.util.ThemeState
import org.thoughtcrime.securesms.util.themeState
import javax.inject.Inject
@ -22,8 +22,8 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec
// update UI state
_uiState.value = prefs.themeState()
// force compose to refresh its style reference
selectedColorSet = null
// invalidate compose theme colors
cachedColors = null
}
fun setNewStyle(newThemeStyle: String) {
@ -31,16 +31,15 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec
// update UI state
_uiState.value = prefs.themeState()
// force compose to refresh its style reference
selectedColorSet = null
// invalidate compose theme colors
cachedColors = null
}
fun setNewFollowSystemSettings(followSystemSettings: Boolean) {
prefs.setFollowSystemSettings(followSystemSettings)
_uiState.value = prefs.themeState()
// force compose to refresh its style reference
selectedColorSet = null
// invalidate compose theme colors
cachedColors = null
}
}

View File

@ -1,12 +0,0 @@
package org.thoughtcrime.securesms.ui.theme
/**
* This class holds two instances of [ThemeColors], [light] representing the [ThemeColors] to use when the system is in a
* light theme, and [dark] representing the [ThemeColors] to use when the system is in a dark theme.
*/
data class ThemeColorSet(
val colorsWhenSystemInLight: ThemeColors,
val colorsWhenSystemInDark: ThemeColors
) {
fun get(isDark: Boolean): ThemeColors = if (isDark) colorsWhenSystemInDark else colorsWhenSystemInLight
}

View File

@ -17,27 +17,27 @@ import org.session.libsession.utilities.TextSecurePreferences.Companion.YELLOW_A
* Some behaviour is hardcoded to cater for legacy usage of people with themes already set
* But future themes will be picked and set directly from the "Appearance" screen
*/
fun TextSecurePreferences.getColorSet(): ThemeColorSet {
val TextSecurePreferences.colors: @Composable () -> ThemeColors get() {
val selectedTheme = getThemeStyle()
// get the chosen primary color from the preferences
val selectedPrimary = primaryColor()
val createLight = if ("ocean" in selectedTheme) ::OceanLight else ::ClassicLight
val createDark = if ("ocean" in selectedTheme) ::OceanDark else ::ClassicDark
val isOcean = "ocean" in selectedTheme
val followSystemSettings = getFollowSystemSettings()
val createLight = if (isOcean) ::OceanLight else ::ClassicLight
val createDark = if (isOcean) ::OceanDark else ::ClassicDark
return if (followSystemSettings) ThemeColorSet(
colorsWhenSystemInLight = createLight(selectedPrimary),
colorsWhenSystemInDark = createDark(selectedPrimary)
) else {
val both = if ("light" in selectedTheme) createLight(selectedPrimary) else createDark(selectedPrimary)
// create the light and dark themes outside the lambda to avoid creating them every time
// [SessionMaterialTheme] is called. Creating both when we don't followSystemSettings is but a
// minor inefficiency that increases readability.
val light = createLight(selectedPrimary)
val dark = createDark(selectedPrimary)
ThemeColorSet(
colorsWhenSystemInLight = both,
colorsWhenSystemInDark = both
)
return when {
getFollowSystemSettings() -> { { if (isSystemInDarkTheme()) dark else light } }
"light" in selectedTheme -> { { light } }
else -> { { dark } }
}
}

View File

@ -16,13 +16,12 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.dp
import org.session.libsession.utilities.AppTextSecurePreferences
import org.session.libsession.utilities.TextSecurePreferences
// Globally accessible composition local objects
val LocalColors = compositionLocalOf <ThemeColors> { ClassicDark() }
val LocalType = compositionLocalOf { sessionTypography }
var selectedColorSet: ThemeColorSet? = null
var cachedColors: (@Composable () -> ThemeColors)? = null
/**
* Apply a Material2 compose theme based on user selections in SharedPreferences.
@ -34,10 +33,10 @@ fun SessionMaterialTheme(
val context = LocalContext.current
val preferences = AppTextSecurePreferences(context)
val selectedColorSet = selectedColorSet ?: preferences.getColorSet().also { selectedColorSet = it }
val cachedColors = cachedColors ?: preferences.colors.also { cachedColors = it }
SessionMaterialTheme(
colors = selectedColorSet.get(isSystemInDarkTheme()),
colors = cachedColors(),
content = content
)
}