From 25e7c7ec6111a100d09bd55279454b9ae8525edf Mon Sep 17 00:00:00 2001 From: bemusementpark Date: Mon, 29 Jul 2024 16:32:26 +0930 Subject: [PATCH] Simplify ThemeFromPreferences by removing lambdas --- .../appearance/AppearanceSettingsViewModel.kt | 14 +++++++------ .../ui/theme/MaybeFollowSystemColors.kt | 18 +++++++++++++++++ .../ui/theme/ThemeFromPreferences.kt | 20 +++++++------------ .../thoughtcrime/securesms/ui/theme/Themes.kt | 12 +++++------ 4 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/ui/theme/MaybeFollowSystemColors.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/appearance/AppearanceSettingsViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/preferences/appearance/AppearanceSettingsViewModel.kt index 8a125896cf..c662a7a772 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/preferences/appearance/AppearanceSettingsViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/appearance/AppearanceSettingsViewModel.kt @@ -17,13 +17,17 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec private val _uiState = MutableStateFlow(prefs.themeState()) val uiState: StateFlow = _uiState + fun invalidateComposeThemeColors() { + // invalidate compose theme colors + cachedColors = null + } + fun setNewAccent(@StyleRes newAccentColorStyle: Int) { prefs.setAccentColorStyle(newAccentColorStyle) // update UI state _uiState.value = prefs.themeState() - // invalidate compose theme colors - cachedColors = null + invalidateComposeThemeColors() } fun setNewStyle(newThemeStyle: String) { @@ -31,15 +35,13 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec // update UI state _uiState.value = prefs.themeState() - // invalidate compose theme colors - cachedColors = null + invalidateComposeThemeColors() } fun setNewFollowSystemSettings(followSystemSettings: Boolean) { prefs.setFollowSystemSettings(followSystemSettings) _uiState.value = prefs.themeState() - // invalidate compose theme colors - cachedColors = null + invalidateComposeThemeColors() } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/MaybeFollowSystemColors.kt b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/MaybeFollowSystemColors.kt new file mode 100644 index 0000000000..fa0353a1e6 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/MaybeFollowSystemColors.kt @@ -0,0 +1,18 @@ +package org.thoughtcrime.securesms.ui.theme + +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.runtime.Composable + +fun interface MaybeFollowSystemColors { + @Composable + fun get(): ThemeColors +} + +fun FollowSystemColors(light: ThemeColors, dark: ThemeColors) = MaybeFollowSystemColors { + when { + isSystemInDarkTheme() -> dark + else -> light + } +} + +fun IgnoreSystemColors(colors: ThemeColors) = MaybeFollowSystemColors { colors } diff --git a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeFromPreferences.kt b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeFromPreferences.kt index c930dd4b2b..3b111526c7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeFromPreferences.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeFromPreferences.kt @@ -17,7 +17,7 @@ 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 */ -val TextSecurePreferences.colors: @Composable () -> ThemeColors get() { +val TextSecurePreferences.colors: MaybeFollowSystemColors get() { val selectedTheme = getThemeStyle() // get the chosen primary color from the preferences @@ -28,16 +28,13 @@ val TextSecurePreferences.colors: @Composable () -> ThemeColors get() { val createLight = if (isOcean) ::OceanLight else ::ClassicLight val createDark = if (isOcean) ::OceanDark else ::ClassicDark - // 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) - return when { - getFollowSystemSettings() -> { { if (isSystemInDarkTheme()) dark else light } } - "light" in selectedTheme -> { { light } } - else -> { { dark } } + getFollowSystemSettings() -> FollowSystemColors( + light = createLight(selectedPrimary), + dark = createDark(selectedPrimary) + ) + "light" in selectedTheme -> IgnoreSystemColors(createLight(selectedPrimary)) + else -> IgnoreSystemColors(createDark(selectedPrimary)) } } @@ -50,6 +47,3 @@ fun TextSecurePreferences.primaryColor(): Color = when(getSelectedAccentColor()) YELLOW_ACCENT -> primaryYellow else -> primaryGreen } - - - diff --git a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/Themes.kt b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/Themes.kt index ffce1a8e2b..d1105ccbc1 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/Themes.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/Themes.kt @@ -1,7 +1,6 @@ package org.thoughtcrime.securesms.ui.theme import androidx.compose.foundation.background -import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.layout.Box import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.selection.LocalTextSelectionColors @@ -21,7 +20,7 @@ import org.session.libsession.utilities.AppTextSecurePreferences val LocalColors = compositionLocalOf { ClassicDark() } val LocalType = compositionLocalOf { sessionTypography } -var cachedColors: (@Composable () -> ThemeColors)? = null +var cachedColors: MaybeFollowSystemColors? = null /** * Apply a Material2 compose theme based on user selections in SharedPreferences. @@ -33,10 +32,10 @@ fun SessionMaterialTheme( val context = LocalContext.current val preferences = AppTextSecurePreferences(context) - val cachedColors = cachedColors ?: preferences.colors.also { cachedColors = it } + val jjcachedColors = cachedColors ?: preferences.colors.also { cachedColors = it } SessionMaterialTheme( - colors = cachedColors(), + colors = cachedColors.get(), content = content ) } @@ -59,9 +58,8 @@ fun SessionMaterialTheme( LocalType provides sessionTypography, LocalContentColor provides colors.text, LocalTextSelectionColors provides colors.textSelectionColors, - ) { - content() - } + content = content + ) } }