Skip to content

Commit

Permalink
Merge pull request #1577 from bemusementpark/dark-light
Browse files Browse the repository at this point in the history
Fix followSystemInDark
  • Loading branch information
ThomasSession authored Jul 30, 2024
2 parents 6fb0f17 + ce501fd commit a796f05
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 58 deletions.
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.invalidateComposeThemeColors
import org.thoughtcrime.securesms.util.ThemeState
import org.thoughtcrime.securesms.util.themeState
import javax.inject.Inject
Expand All @@ -21,23 +21,22 @@ class AppearanceSettingsViewModel @Inject constructor(private val prefs: TextSec
prefs.setAccentColorStyle(newAccentColorStyle)
// update UI state
_uiState.value = prefs.themeState()

invalidateComposeThemeColors()
}

fun setNewStyle(newThemeStyle: String) {
prefs.setThemeStyle(newThemeStyle)
// update UI state
_uiState.value = prefs.themeState()

// force compose to refresh its style reference
selectedTheme = null
invalidateComposeThemeColors()
}

fun setNewFollowSystemSettings(followSystemSettings: Boolean) {
prefs.setFollowSystemSettings(followSystemSettings)
_uiState.value = prefs.themeState()

// force compose to refresh its style reference
selectedTheme = null
invalidateComposeThemeColors()
}

}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 }
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.thoughtcrime.securesms.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.TextSecurePreferences.Companion.BLUE_ACCENT
Expand All @@ -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
* But future themes will be picked and set directly from the "Appearance" screen
*/
@Composable
fun TextSecurePreferences.getComposeTheme(): ThemeColors {
fun TextSecurePreferences.getColorsProvider(): ThemeColorsProvider {
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)
)
}

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

selectedTheme == TextSecurePreferences.CLASSIC_LIGHT ||
selectedTheme == TextSecurePreferences.OCEAN_LIGHT -> colorSet.light
val createLight = if (isOcean) ::OceanLight else ::ClassicLight
val createDark = if (isOcean) ::OceanDark else ::ClassicDark

else -> colorSet.dark
return when {
getFollowSystemSettings() -> FollowSystemThemeColorsProvider(
light = createLight(selectedPrimary),
dark = createDark(selectedPrimary)
)
"light" in selectedTheme -> ThemeColorsProvider(createLight(selectedPrimary))
else -> ThemeColorsProvider(createDark(selectedPrimary))
}

return theme
}

fun TextSecurePreferences.primaryColor(): Color = when(getSelectedAccentColor()) {
Expand All @@ -60,6 +45,3 @@ fun TextSecurePreferences.primaryColor(): Color = when(getSelectedAccentColor())
YELLOW_ACCENT -> primaryYellow
else -> primaryGreen
}



28 changes: 16 additions & 12 deletions app/src/main/java/org/thoughtcrime/securesms/ui/theme/Themes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import org.session.libsession.utilities.AppTextSecurePreferences
val LocalColors = compositionLocalOf <ThemeColors> { ClassicDark() }
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.
Expand All @@ -29,15 +34,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)

SessionMaterialTheme(colors = selectedTheme ?: ClassicDark()) { content() }
val cachedColors = cachedColorsProvider ?: preferences.getColorsProvider().also { cachedColorsProvider = it }

SessionMaterialTheme(
colors = cachedColors.get(),
content = content
)
}

/**
Expand All @@ -58,9 +63,8 @@ fun SessionMaterialTheme(
LocalType provides sessionTypography,
LocalContentColor provides colors.text,
LocalTextSelectionColors provides colors.textSelectionColors,
) {
content()
}
content = content
)
}
}

Expand Down

0 comments on commit a796f05

Please sign in to comment.