Simplify ThemeFromPreferences by removing lambdas

This commit is contained in:
bemusementpark 2024-07-29 16:32:26 +09:30
parent 492d5217d0
commit 25e7c7ec61
4 changed files with 38 additions and 26 deletions

View File

@ -17,13 +17,17 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec
private val _uiState = MutableStateFlow(prefs.themeState())
val uiState: StateFlow<ThemeState> = _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()
}
}

View File

@ -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 }

View File

@ -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
}

View File

@ -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 <ThemeColors> { 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
)
}
}