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) - ) + val createLight = if ("ocean" in selectedTheme) ::OceanLight else ::ClassicLight + val createDark = if ("ocean" in selectedTheme) ::OceanDark else ::ClassicDark - else -> ThemeColorSet( - light = ClassicLight(selectedPrimary), - dark = ClassicDark(selectedPrimary) + val followSystemSettings = getFollowSystemSettings() + + return if (followSystemSettings) ThemeColorSet( + light = createLight(selectedPrimary), + dark = createDark(selectedPrimary) + ) else { + val both = if ("light" in selectedTheme) createLight(selectedPrimary) else createDark(selectedPrimary) + + ThemeColorSet( + light = both, + dark = both ) } - - // deliver the right set from the light/dark mode chosen - val theme = when{ - getFollowSystemSettings() -> if(isSystemInDarkTheme()) colorSet.dark else colorSet.light - - selectedTheme == TextSecurePreferences.CLASSIC_LIGHT || - selectedTheme == TextSecurePreferences.OCEAN_LIGHT -> colorSet.light - - else -> colorSet.dark - } - - 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) - SessionMaterialTheme(colors = selectedTheme ?: ClassicDark()) { content() } + val selectedColorSet = selectedColorSet ?: preferences.getColorSet().also { selectedColorSet = it } + + SessionMaterialTheme( + colors = selectedColorSet.get(isSystemInDarkTheme()), + content = content + ) } /**