From 80d08a5fb242c78c96724b2846525b0517961d2a Mon Sep 17 00:00:00 2001 From: bemusementpark Date: Fri, 26 Jul 2024 21:43:10 +0930 Subject: [PATCH 1/7] Fix follow light --- .../appearance/AppearanceSettingsViewModel.kt | 9 +++-- .../securesms/ui/theme/ThemeColorSet.kt | 4 ++- .../ui/theme/ThemeFromPreferences.kt | 36 +++++++------------ .../thoughtcrime/securesms/ui/theme/Themes.kt | 20 ++++++----- 4 files changed, 33 insertions(+), 36 deletions(-) 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 afe33400ff..fcc1ffb4bc 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 @@ -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.selectedTheme +import org.thoughtcrime.securesms.ui.theme.selectedColorSet import org.thoughtcrime.securesms.util.ThemeState import org.thoughtcrime.securesms.util.themeState import javax.inject.Inject @@ -21,6 +21,9 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec prefs.setAccentColorStyle(newAccentColorStyle) // update UI state _uiState.value = prefs.themeState() + + // force compose to refresh its style reference + selectedColorSet = null } fun setNewStyle(newThemeStyle: String) { @@ -29,7 +32,7 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec _uiState.value = prefs.themeState() // force compose to refresh its style reference - selectedTheme = null + selectedColorSet = null } fun setNewFollowSystemSettings(followSystemSettings: Boolean) { @@ -37,7 +40,7 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec _uiState.value = prefs.themeState() // force compose to refresh its style reference - selectedTheme = null + selectedColorSet = null } } \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt index 3ff4d55b61..59ea383063 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt @@ -7,4 +7,6 @@ package org.thoughtcrime.securesms.ui.theme data class ThemeColorSet( val light: ThemeColors, val dark: ThemeColors -) \ No newline at end of file +) { + fun get(isDark: Boolean): ThemeColors = if (isDark) dark else light +} 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 8988e84ca7..130c370e26 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,38 +17,28 @@ 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 */ -@Composable -fun TextSecurePreferences.getComposeTheme(): ThemeColors { +fun TextSecurePreferences.getColorSet(): ThemeColorSet { val selectedTheme = getThemeStyle() // get the chosen primary color from the preferences val selectedPrimary = primaryColor() - // create a theme set with the appropriate primary - val colorSet = when(selectedTheme){ - TextSecurePreferences.OCEAN_DARK, - TextSecurePreferences.OCEAN_LIGHT -> ThemeColorSet( - light = OceanLight(selectedPrimary), - dark = OceanDark(selectedPrimary) - ) - - else -> ThemeColorSet( - light = ClassicLight(selectedPrimary), - dark = ClassicDark(selectedPrimary) - ) - } + val createLight = if ("ocean" in selectedTheme) ::OceanLight else ::ClassicLight + val createDark = if ("ocean" in selectedTheme) ::OceanDark else ::ClassicDark - // deliver the right set from the light/dark mode chosen - val theme = when{ - getFollowSystemSettings() -> if(isSystemInDarkTheme()) colorSet.dark else colorSet.light + val followSystemSettings = getFollowSystemSettings() - selectedTheme == TextSecurePreferences.CLASSIC_LIGHT || - selectedTheme == TextSecurePreferences.OCEAN_LIGHT -> colorSet.light + return if (followSystemSettings) ThemeColorSet( + light = createLight(selectedPrimary), + dark = createDark(selectedPrimary) + ) else { + val both = if ("light" in selectedTheme) createLight(selectedPrimary) else createDark(selectedPrimary) - else -> colorSet.dark + ThemeColorSet( + light = both, + dark = both + ) } - - return theme } fun TextSecurePreferences.primaryColor(): Color = when(getSelectedAccentColor()) { 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 87e91e0ab0..d4b05572f6 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,6 +1,7 @@ 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 @@ -15,12 +16,13 @@ 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 { ClassicDark() } val LocalType = compositionLocalOf { sessionTypography } -var selectedTheme: ThemeColors? = null +var selectedColorSet: ThemeColorSet? = null /** * Apply a Material2 compose theme based on user selections in SharedPreferences. @@ -29,15 +31,15 @@ var selectedTheme: ThemeColors? = null fun SessionMaterialTheme( content: @Composable () -> Unit ) { - // set the theme data if it hasn't been done yet - if(selectedTheme == null) { - // Some values can be set from the preferences, and if not should fallback to a default value - val context = LocalContext.current - val preferences = AppTextSecurePreferences(context) - selectedTheme = preferences.getComposeTheme() - } + val context = LocalContext.current + val preferences = AppTextSecurePreferences(context) + + val selectedColorSet = selectedColorSet ?: preferences.getColorSet().also { selectedColorSet = it } - SessionMaterialTheme(colors = selectedTheme ?: ClassicDark()) { content() } + SessionMaterialTheme( + colors = selectedColorSet.get(isSystemInDarkTheme()), + content = content + ) } /** From 90f6fee579fdd6dd0cfea6dba1ec52fb5eefb945 Mon Sep 17 00:00:00 2001 From: bemusementpark Date: Fri, 26 Jul 2024 21:49:55 +0930 Subject: [PATCH 2/7] Change ThemeColorSet naming --- .../org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt | 6 +++--- .../securesms/ui/theme/ThemeFromPreferences.kt | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt index 59ea383063..a4b15e456b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt @@ -5,8 +5,8 @@ package org.thoughtcrime.securesms.ui.theme * light theme, and [dark] representing the [ThemeColors] to use when the system is in a dark theme. */ data class ThemeColorSet( - val light: ThemeColors, - val dark: ThemeColors + val colorsWhenSystemInLight: ThemeColors, + val colorsWhenSystemInDark: ThemeColors ) { - fun get(isDark: Boolean): ThemeColors = if (isDark) dark else light + fun get(isDark: Boolean): ThemeColors = if (isDark) colorsWhenSystemInDark else colorsWhenSystemInLight } 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 130c370e26..d3e0927635 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 @@ -29,14 +29,14 @@ fun TextSecurePreferences.getColorSet(): ThemeColorSet { val followSystemSettings = getFollowSystemSettings() return if (followSystemSettings) ThemeColorSet( - light = createLight(selectedPrimary), - dark = createDark(selectedPrimary) + colorsWhenSystemInLight = createLight(selectedPrimary), + colorsWhenSystemInDark = createDark(selectedPrimary) ) else { val both = if ("light" in selectedTheme) createLight(selectedPrimary) else createDark(selectedPrimary) ThemeColorSet( - light = both, - dark = both + colorsWhenSystemInLight = both, + colorsWhenSystemInDark = both ) } } From 492d5217d0e137f9d711c163339d5b954b1399fd Mon Sep 17 00:00:00 2001 From: bemusementpark Date: Mon, 29 Jul 2024 14:49:37 +0930 Subject: [PATCH 3/7] Add colors lambda --- .../appearance/AppearanceSettingsViewModel.kt | 17 ++++++------ .../securesms/ui/theme/ThemeColorSet.kt | 12 --------- .../ui/theme/ThemeFromPreferences.kt | 26 +++++++++---------- .../thoughtcrime/securesms/ui/theme/Themes.kt | 7 +++-- 4 files changed, 24 insertions(+), 38 deletions(-) delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.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 fcc1ffb4bc..8a125896cf 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 @@ -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 } - -} \ No newline at end of file +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt deleted file mode 100644 index a4b15e456b..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorSet.kt +++ /dev/null @@ -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 -} 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 d3e0927635..c930dd4b2b 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,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 } } } } 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 d4b05572f6..ffce1a8e2b 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 @@ -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 { 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 ) } From 25e7c7ec6111a100d09bd55279454b9ae8525edf Mon Sep 17 00:00:00 2001 From: bemusementpark Date: Mon, 29 Jul 2024 16:32:26 +0930 Subject: [PATCH 4/7] 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 + ) } } From 7bb1a3a513035b8acb012a9717e611f46674fd08 Mon Sep 17 00:00:00 2001 From: bemusementpark Date: Mon, 29 Jul 2024 16:36:17 +0930 Subject: [PATCH 5/7] Suppress compose name warning --- .../thoughtcrime/securesms/ui/theme/MaybeFollowSystemColors.kt | 2 ++ app/src/main/java/org/thoughtcrime/securesms/ui/theme/Themes.kt | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) 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 index fa0353a1e6..6bb0d3c2ad 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/MaybeFollowSystemColors.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/MaybeFollowSystemColors.kt @@ -8,6 +8,7 @@ fun interface MaybeFollowSystemColors { fun get(): ThemeColors } +@Suppress("FunctionName") fun FollowSystemColors(light: ThemeColors, dark: ThemeColors) = MaybeFollowSystemColors { when { isSystemInDarkTheme() -> dark @@ -15,4 +16,5 @@ fun FollowSystemColors(light: ThemeColors, dark: ThemeColors) = MaybeFollowSyste } } +@Suppress("FunctionName") fun IgnoreSystemColors(colors: ThemeColors) = MaybeFollowSystemColors { colors } 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 d1105ccbc1..ec19b3b100 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 @@ -32,7 +32,7 @@ fun SessionMaterialTheme( val context = LocalContext.current val preferences = AppTextSecurePreferences(context) - val jjcachedColors = cachedColors ?: preferences.colors.also { cachedColors = it } + val cachedColors = cachedColors ?: preferences.colors.also { cachedColors = it } SessionMaterialTheme( colors = cachedColors.get(), From 447ea85333df87632d90165d7230342a6e819ba5 Mon Sep 17 00:00:00 2001 From: bemusementpark Date: Mon, 29 Jul 2024 17:35:55 +0930 Subject: [PATCH 6/7] Improve naming --- .../appearance/AppearanceSettingsViewModel.kt | 4 ++-- ...ybeFollowSystemColors.kt => ThemeColorsProvider.kt} | 6 +++--- .../securesms/ui/theme/ThemeFromPreferences.kt | 10 ++++------ .../java/org/thoughtcrime/securesms/ui/theme/Themes.kt | 4 ++-- 4 files changed, 11 insertions(+), 13 deletions(-) rename app/src/main/java/org/thoughtcrime/securesms/ui/theme/{MaybeFollowSystemColors.kt => ThemeColorsProvider.kt} (60%) 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 c662a7a772..47cc9f69bf 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 @@ -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.cachedColors +import org.thoughtcrime.securesms.ui.theme.cachedColorsProvider import org.thoughtcrime.securesms.util.ThemeState import org.thoughtcrime.securesms.util.themeState import javax.inject.Inject @@ -19,7 +19,7 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec fun invalidateComposeThemeColors() { // invalidate compose theme colors - cachedColors = null + cachedColorsProvider = null } fun setNewAccent(@StyleRes newAccentColorStyle: Int) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/MaybeFollowSystemColors.kt b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorsProvider.kt similarity index 60% rename from app/src/main/java/org/thoughtcrime/securesms/ui/theme/MaybeFollowSystemColors.kt rename to app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorsProvider.kt index 6bb0d3c2ad..0713254afc 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ui/theme/MaybeFollowSystemColors.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/ui/theme/ThemeColorsProvider.kt @@ -3,13 +3,13 @@ package org.thoughtcrime.securesms.ui.theme import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.runtime.Composable -fun interface MaybeFollowSystemColors { +fun interface ThemeColorsProvider { @Composable fun get(): ThemeColors } @Suppress("FunctionName") -fun FollowSystemColors(light: ThemeColors, dark: ThemeColors) = MaybeFollowSystemColors { +fun FollowSystemThemeColorsProvider(light: ThemeColors, dark: ThemeColors) = ThemeColorsProvider { when { isSystemInDarkTheme() -> dark else -> light @@ -17,4 +17,4 @@ fun FollowSystemColors(light: ThemeColors, dark: ThemeColors) = MaybeFollowSyste } @Suppress("FunctionName") -fun IgnoreSystemColors(colors: ThemeColors) = MaybeFollowSystemColors { colors } +fun ThemeColorsProvider(colors: ThemeColors) = ThemeColorsProvider { 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 3b111526c7..403235ef79 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 @@ -1,7 +1,5 @@ package org.thoughtcrime.securesms.ui.theme -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.runtime.Composable import androidx.compose.ui.graphics.Color import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences.Companion.BLUE_ACCENT @@ -17,7 +15,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: MaybeFollowSystemColors get() { +fun TextSecurePreferences.getColorsProvider(): ThemeColorsProvider { val selectedTheme = getThemeStyle() // get the chosen primary color from the preferences @@ -29,12 +27,12 @@ val TextSecurePreferences.colors: MaybeFollowSystemColors get() { val createDark = if (isOcean) ::OceanDark else ::ClassicDark return when { - getFollowSystemSettings() -> FollowSystemColors( + getFollowSystemSettings() -> FollowSystemThemeColorsProvider( light = createLight(selectedPrimary), dark = createDark(selectedPrimary) ) - "light" in selectedTheme -> IgnoreSystemColors(createLight(selectedPrimary)) - else -> IgnoreSystemColors(createDark(selectedPrimary)) + "light" in selectedTheme -> ThemeColorsProvider(createLight(selectedPrimary)) + else -> ThemeColorsProvider(createDark(selectedPrimary)) } } 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 ec19b3b100..9b83028ec8 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 @@ -20,7 +20,7 @@ import org.session.libsession.utilities.AppTextSecurePreferences val LocalColors = compositionLocalOf { ClassicDark() } val LocalType = compositionLocalOf { sessionTypography } -var cachedColors: MaybeFollowSystemColors? = null +var cachedColorsProvider: ThemeColorsProvider? = null /** * Apply a Material2 compose theme based on user selections in SharedPreferences. @@ -32,7 +32,7 @@ fun SessionMaterialTheme( val context = LocalContext.current val preferences = AppTextSecurePreferences(context) - val cachedColors = cachedColors ?: preferences.colors.also { cachedColors = it } + val cachedColors = cachedColorsProvider ?: preferences.getColorsProvider().also { cachedColorsProvider = it } SessionMaterialTheme( colors = cachedColors.get(), From ce501fd36389ded62e9901daac21b9fc29b31b23 Mon Sep 17 00:00:00 2001 From: bemusementpark Date: Tue, 30 Jul 2024 00:19:16 +0930 Subject: [PATCH 7/7] Move invalidateComposeThemeColors() --- .../preferences/appearance/AppearanceSettingsViewModel.kt | 7 +------ .../java/org/thoughtcrime/securesms/ui/theme/Themes.kt | 5 +++++ 2 files changed, 6 insertions(+), 6 deletions(-) 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 47cc9f69bf..2547e23e22 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 @@ -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.cachedColorsProvider +import org.thoughtcrime.securesms.ui.theme.invalidateComposeThemeColors import org.thoughtcrime.securesms.util.ThemeState import org.thoughtcrime.securesms.util.themeState import javax.inject.Inject @@ -17,11 +17,6 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec private val _uiState = MutableStateFlow(prefs.themeState()) val uiState: StateFlow = _uiState - fun invalidateComposeThemeColors() { - // invalidate compose theme colors - cachedColorsProvider = null - } - fun setNewAccent(@StyleRes newAccentColorStyle: Int) { prefs.setAccentColorStyle(newAccentColorStyle) // update UI state 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 9b83028ec8..2f4957565b 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 @@ -22,6 +22,11 @@ val LocalType = compositionLocalOf { sessionTypography } var cachedColorsProvider: ThemeColorsProvider? = null +fun invalidateComposeThemeColors() { + // invalidate compose theme colors + cachedColorsProvider = null +} + /** * Apply a Material2 compose theme based on user selections in SharedPreferences. */