mirror of
https://github.com/oxen-io/session-android.git
synced 2025-01-12 20:53:38 +00:00
Add colors lambda
This commit is contained in:
parent
90f6fee579
commit
492d5217d0
@ -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.selectedColorSet
|
import org.thoughtcrime.securesms.ui.theme.cachedColors
|
||||||
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
|
||||||
@ -22,8 +22,8 @@ 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
|
// invalidate compose theme colors
|
||||||
selectedColorSet = null
|
cachedColors = null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setNewStyle(newThemeStyle: String) {
|
fun setNewStyle(newThemeStyle: String) {
|
||||||
@ -31,16 +31,15 @@ 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
|
// invalidate compose theme colors
|
||||||
selectedColorSet = null
|
cachedColors = 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
|
// invalidate compose theme colors
|
||||||
selectedColorSet = null
|
cachedColors = 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
|
|
||||||
}
|
|
@ -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
|
* 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
|
||||||
*/
|
*/
|
||||||
fun TextSecurePreferences.getColorSet(): ThemeColorSet {
|
val TextSecurePreferences.colors: @Composable () -> ThemeColors get() {
|
||||||
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()
|
||||||
|
|
||||||
val createLight = if ("ocean" in selectedTheme) ::OceanLight else ::ClassicLight
|
val isOcean = "ocean" in selectedTheme
|
||||||
val createDark = if ("ocean" in selectedTheme) ::OceanDark else ::ClassicDark
|
|
||||||
|
|
||||||
val followSystemSettings = getFollowSystemSettings()
|
val createLight = if (isOcean) ::OceanLight else ::ClassicLight
|
||||||
|
val createDark = if (isOcean) ::OceanDark else ::ClassicDark
|
||||||
|
|
||||||
return if (followSystemSettings) ThemeColorSet(
|
// create the light and dark themes outside the lambda to avoid creating them every time
|
||||||
colorsWhenSystemInLight = createLight(selectedPrimary),
|
// [SessionMaterialTheme] is called. Creating both when we don't followSystemSettings is but a
|
||||||
colorsWhenSystemInDark = createDark(selectedPrimary)
|
// minor inefficiency that increases readability.
|
||||||
) else {
|
val light = createLight(selectedPrimary)
|
||||||
val both = if ("light" in selectedTheme) createLight(selectedPrimary) else createDark(selectedPrimary)
|
val dark = createDark(selectedPrimary)
|
||||||
|
|
||||||
ThemeColorSet(
|
return when {
|
||||||
colorsWhenSystemInLight = both,
|
getFollowSystemSettings() -> { { if (isSystemInDarkTheme()) dark else light } }
|
||||||
colorsWhenSystemInDark = both
|
"light" in selectedTheme -> { { light } }
|
||||||
)
|
else -> { { dark } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,13 +16,12 @@ 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 selectedColorSet: ThemeColorSet? = null
|
var cachedColors: (@Composable () -> ThemeColors)? = null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply a Material2 compose theme based on user selections in SharedPreferences.
|
* Apply a Material2 compose theme based on user selections in SharedPreferences.
|
||||||
@ -34,10 +33,10 @@ fun SessionMaterialTheme(
|
|||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val preferences = AppTextSecurePreferences(context)
|
val preferences = AppTextSecurePreferences(context)
|
||||||
|
|
||||||
val selectedColorSet = selectedColorSet ?: preferences.getColorSet().also { selectedColorSet = it }
|
val cachedColors = cachedColors ?: preferences.colors.also { cachedColors = it }
|
||||||
|
|
||||||
SessionMaterialTheme(
|
SessionMaterialTheme(
|
||||||
colors = selectedColorSet.get(isSystemInDarkTheme()),
|
colors = cachedColors(),
|
||||||
content = content
|
content = content
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user