Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix followSystemInDark #1577

Merged
merged 7 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -29,15 +32,15 @@ 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) {
prefs.setFollowSystemSettings(followSystemSettings)
_uiState.value = prefs.themeState()

// force compose to refresh its style reference
selectedTheme = null
selectedColorSet = null
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package org.thoughtcrime.securesms.ui.theme
* 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
)
val colorsWhenSystemInLight: ThemeColors,
bemusementpark marked this conversation as resolved.
Show resolved Hide resolved
val colorsWhenSystemInDark: ThemeColors
) {
fun get(isDark: Boolean): ThemeColors = if (isDark) colorsWhenSystemInDark else colorsWhenSystemInLight
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

else -> ThemeColorSet(
light = ClassicLight(selectedPrimary),
dark = ClassicDark(selectedPrimary)
)
}
val createLight = if ("ocean" in selectedTheme) ::OceanLight else ::ClassicLight
val createDark = if ("ocean" in selectedTheme) ::OceanDark else ::ClassicDark

// deliver the right set from the light/dark mode chosen
val theme = when{
getFollowSystemSettings() -> if(isSystemInDarkTheme()) colorSet.dark else colorSet.light
val followSystemSettings = getFollowSystemSettings()

selectedTheme == TextSecurePreferences.CLASSIC_LIGHT ||
selectedTheme == TextSecurePreferences.OCEAN_LIGHT -> colorSet.light
return if (followSystemSettings) ThemeColorSet(
colorsWhenSystemInLight = createLight(selectedPrimary),
colorsWhenSystemInDark = createDark(selectedPrimary)
) else {
val both = if ("light" in selectedTheme) createLight(selectedPrimary) else createDark(selectedPrimary)
bemusementpark marked this conversation as resolved.
Show resolved Hide resolved

else -> colorSet.dark
ThemeColorSet(
colorsWhenSystemInLight = both,
colorsWhenSystemInDark = both
)
}

return theme
}

fun TextSecurePreferences.primaryColor(): Color = when(getSelectedAccentColor()) {
Expand Down
20 changes: 11 additions & 9 deletions app/src/main/java/org/thoughtcrime/securesms/ui/theme/Themes.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <ThemeColors> { ClassicDark() }
val LocalType = compositionLocalOf { sessionTypography }

var selectedTheme: ThemeColors? = null
var selectedColorSet: ThemeColorSet? = null

/**
* Apply a Material2 compose theme based on user selections in SharedPreferences.
Expand All @@ -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)

val selectedColorSet = selectedColorSet ?: preferences.getColorSet().also { selectedColorSet = it }

SessionMaterialTheme(colors = selectedTheme ?: ClassicDark()) { content() }
SessionMaterialTheme(
colors = selectedColorSet.get(isSystemInDarkTheme()),
bemusementpark marked this conversation as resolved.
Show resolved Hide resolved
content = content
)
}

/**
Expand Down