Merge pull request #1577 from bemusementpark/dark-light

Fix followSystemInDark
This commit is contained in:
ThomasSession 2024-07-30 10:01:37 +10:00 committed by GitHub
commit a796f05136
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 54 additions and 59 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.invalidateComposeThemeColors
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,8 @@ 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()
invalidateComposeThemeColors()
} }
fun setNewStyle(newThemeStyle: String) { fun setNewStyle(newThemeStyle: String) {
@ -28,16 +30,13 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec
// update UI state // update UI state
_uiState.value = prefs.themeState() _uiState.value = prefs.themeState()
// force compose to refresh its style reference invalidateComposeThemeColors()
selectedTheme = null
} }
fun setNewFollowSystemSettings(followSystemSettings: Boolean) { fun setNewFollowSystemSettings(followSystemSettings: Boolean) {
prefs.setFollowSystemSettings(followSystemSettings) prefs.setFollowSystemSettings(followSystemSettings)
_uiState.value = prefs.themeState() _uiState.value = prefs.themeState()
// force compose to refresh its style reference invalidateComposeThemeColors()
selectedTheme = null
} }
}
}

View File

@ -1,10 +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 light: ThemeColors,
val dark: ThemeColors
)

View File

@ -0,0 +1,20 @@
package org.thoughtcrime.securesms.ui.theme
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
fun interface ThemeColorsProvider {
@Composable
fun get(): ThemeColors
}
@Suppress("FunctionName")
fun FollowSystemThemeColorsProvider(light: ThemeColors, dark: ThemeColors) = ThemeColorsProvider {
when {
isSystemInDarkTheme() -> dark
else -> light
}
}
@Suppress("FunctionName")
fun ThemeColorsProvider(colors: ThemeColors) = ThemeColorsProvider { colors }

View File

@ -1,7 +1,5 @@
package org.thoughtcrime.securesms.ui.theme package org.thoughtcrime.securesms.ui.theme
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.TextSecurePreferences.Companion.BLUE_ACCENT import org.session.libsession.utilities.TextSecurePreferences.Companion.BLUE_ACCENT
@ -17,38 +15,25 @@ 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.getColorsProvider(): ThemeColorsProvider {
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 isOcean = "ocean" in selectedTheme
val colorSet = when(selectedTheme){
TextSecurePreferences.OCEAN_DARK, val createLight = if (isOcean) ::OceanLight else ::ClassicLight
TextSecurePreferences.OCEAN_LIGHT -> ThemeColorSet( val createDark = if (isOcean) ::OceanDark else ::ClassicDark
light = OceanLight(selectedPrimary),
dark = OceanDark(selectedPrimary) return when {
) getFollowSystemSettings() -> FollowSystemThemeColorsProvider(
light = createLight(selectedPrimary),
else -> ThemeColorSet( dark = createDark(selectedPrimary)
light = ClassicLight(selectedPrimary),
dark = ClassicDark(selectedPrimary)
) )
"light" in selectedTheme -> ThemeColorsProvider(createLight(selectedPrimary))
else -> ThemeColorsProvider(createDark(selectedPrimary))
} }
// 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()) {
@ -60,6 +45,3 @@ fun TextSecurePreferences.primaryColor(): Color = when(getSelectedAccentColor())
YELLOW_ACCENT -> primaryYellow YELLOW_ACCENT -> primaryYellow
else -> primaryGreen else -> primaryGreen
} }

View File

@ -20,7 +20,12 @@ import org.session.libsession.utilities.AppTextSecurePreferences
val LocalColors = compositionLocalOf <ThemeColors> { ClassicDark() } val LocalColors = compositionLocalOf <ThemeColors> { ClassicDark() }
val LocalType = compositionLocalOf { sessionTypography } val LocalType = compositionLocalOf { sessionTypography }
var selectedTheme: ThemeColors? = null var cachedColorsProvider: ThemeColorsProvider? = null
fun invalidateComposeThemeColors() {
// invalidate compose theme colors
cachedColorsProvider = 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 +34,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 val context = LocalContext.current
if(selectedTheme == null) { val preferences = AppTextSecurePreferences(context)
// 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()
}
SessionMaterialTheme(colors = selectedTheme ?: ClassicDark()) { content() } val cachedColors = cachedColorsProvider ?: preferences.getColorsProvider().also { cachedColorsProvider = it }
SessionMaterialTheme(
colors = cachedColors.get(),
content = content
)
} }
/** /**
@ -58,9 +63,8 @@ fun SessionMaterialTheme(
LocalType provides sessionTypography, LocalType provides sessionTypography,
LocalContentColor provides colors.text, LocalContentColor provides colors.text,
LocalTextSelectionColors provides colors.textSelectionColors, LocalTextSelectionColors provides colors.textSelectionColors,
) { content = content
content() )
}
} }
} }