Fix follow light

This commit is contained in:
bemusementpark 2024-07-26 21:43:10 +09:30
parent 2818552519
commit 80d08a5fb2
4 changed files with 34 additions and 37 deletions

View File

@ -6,7 +6,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import org.session.libsession.utilities.TextSecurePreferences 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 org.thoughtcrime.securesms.util.themeState import org.thoughtcrime.securesms.util.themeState
import javax.inject.Inject import javax.inject.Inject
@ -21,6 +21,9 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec
prefs.setAccentColorStyle(newAccentColorStyle) prefs.setAccentColorStyle(newAccentColorStyle)
// update UI state // update UI state
_uiState.value = prefs.themeState() _uiState.value = prefs.themeState()
// force compose to refresh its style reference
selectedColorSet = null
} }
fun setNewStyle(newThemeStyle: String) { fun setNewStyle(newThemeStyle: String) {
@ -29,7 +32,7 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec
_uiState.value = prefs.themeState() _uiState.value = prefs.themeState()
// force compose to refresh its style reference // force compose to refresh its style reference
selectedTheme = null selectedColorSet = null
} }
fun setNewFollowSystemSettings(followSystemSettings: Boolean) { fun setNewFollowSystemSettings(followSystemSettings: Boolean) {
@ -37,7 +40,7 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec
_uiState.value = prefs.themeState() _uiState.value = prefs.themeState()
// force compose to refresh its style reference // force compose to refresh its style reference
selectedTheme = null selectedColorSet = null
} }
} }

View File

@ -7,4 +7,6 @@ package org.thoughtcrime.securesms.ui.theme
data class ThemeColorSet( data class ThemeColorSet(
val light: ThemeColors, val light: ThemeColors,
val dark: ThemeColors val dark: ThemeColors
) ) {
fun get(isDark: Boolean): ThemeColors = if (isDark) dark else light
}

View File

@ -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 * 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 * But future themes will be picked and set directly from the "Appearance" screen
*/ */
@Composable fun TextSecurePreferences.getColorSet(): ThemeColorSet {
fun TextSecurePreferences.getComposeTheme(): ThemeColors {
val selectedTheme = getThemeStyle() val selectedTheme = getThemeStyle()
// get the chosen primary color from the preferences // get the chosen primary color from the preferences
val selectedPrimary = primaryColor() val selectedPrimary = primaryColor()
// create a theme set with the appropriate primary val createLight = if ("ocean" in selectedTheme) ::OceanLight else ::ClassicLight
val colorSet = when(selectedTheme){ val createDark = if ("ocean" in selectedTheme) ::OceanDark else ::ClassicDark
TextSecurePreferences.OCEAN_DARK,
TextSecurePreferences.OCEAN_LIGHT -> ThemeColorSet(
light = OceanLight(selectedPrimary),
dark = OceanDark(selectedPrimary)
)
else -> ThemeColorSet( val followSystemSettings = getFollowSystemSettings()
light = ClassicLight(selectedPrimary),
dark = ClassicDark(selectedPrimary) 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()) { fun TextSecurePreferences.primaryColor(): Color = when(getSelectedAccentColor()) {

View File

@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.ui.theme package org.thoughtcrime.securesms.ui.theme
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.LocalTextSelectionColors 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.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import org.session.libsession.utilities.AppTextSecurePreferences import org.session.libsession.utilities.AppTextSecurePreferences
import org.session.libsession.utilities.TextSecurePreferences
// Globally accessible composition local objects // Globally accessible composition local objects
val LocalColors = compositionLocalOf <ThemeColors> { ClassicDark() } val LocalColors = compositionLocalOf <ThemeColors> { ClassicDark() }
val LocalType = compositionLocalOf { sessionTypography } val LocalType = compositionLocalOf { sessionTypography }
var selectedTheme: ThemeColors? = null var selectedColorSet: ThemeColorSet? = null
/** /**
* Apply a Material2 compose theme based on user selections in SharedPreferences. * Apply a Material2 compose theme based on user selections in SharedPreferences.
@ -29,15 +31,15 @@ var selectedTheme: ThemeColors? = null
fun SessionMaterialTheme( fun SessionMaterialTheme(
content: @Composable () -> Unit 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 context = LocalContext.current
val preferences = AppTextSecurePreferences(context) val preferences = AppTextSecurePreferences(context)
selectedTheme = preferences.getComposeTheme()
}
SessionMaterialTheme(colors = selectedTheme ?: ClassicDark()) { content() } val selectedColorSet = selectedColorSet ?: preferences.getColorSet().also { selectedColorSet = it }
SessionMaterialTheme(
colors = selectedColorSet.get(isSystemInDarkTheme()),
content = content
)
} }
/** /**