From 315d830357361a0979e0654eff6fefcac357dbc8 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 29 Apr 2023 14:11:44 +0200 Subject: [PATCH] android: export all theme colors (#2348) --- .../java/chat/simplex/app/ui/theme/Theme.kt | 3 ++ .../chat/simplex/app/ui/theme/ThemeManager.kt | 29 ++++++++++--------- .../chat/simplex/app/views/helpers/Util.kt | 5 ++-- .../app/views/usersettings/Appearance.kt | 22 +++++++------- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/ui/theme/Theme.kt b/apps/android/app/src/main/java/chat/simplex/app/ui/theme/Theme.kt index 85b3a0dbdf..118bca224a 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/ui/theme/Theme.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/ui/theme/Theme.kt @@ -153,6 +153,9 @@ data class ThemeOverrides ( } } +@Serializable +data class ThemeData (val colors: ThemeColors) + fun Modifier.themedBackground(baseTheme: DefaultTheme = CurrentColors.value.base, shape: Shape = RectangleShape): Modifier { return if (baseTheme == DefaultTheme.SIMPLEX) { this.background(brush = Brush.linearGradient( diff --git a/apps/android/app/src/main/java/chat/simplex/app/ui/theme/ThemeManager.kt b/apps/android/app/src/main/java/chat/simplex/app/ui/theme/ThemeManager.kt index e6646c9bc2..ef9c42fe7c 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/ui/theme/ThemeManager.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/ui/theme/ThemeManager.kt @@ -44,15 +44,19 @@ object ThemeManager { return ActiveTheme(themeName, baseTheme.first, theme.colors.toColors(theme.base), theme.colors.toAppColors(theme.base)) } - fun currentThemeOverrides(darkForSystemTheme: Boolean): ThemeOverrides { - val themeName = appPrefs.currentTheme.get()!! - val nonSystemThemeName = if (themeName != DefaultTheme.SYSTEM.name) { - themeName - } else { - if (darkForSystemTheme) appPrefs.systemDarkTheme.get()!! else DefaultTheme.LIGHT.name - } - val overrides = appPrefs.themeOverrides.get().toMutableMap() - return overrides[nonSystemThemeName] ?: ThemeOverrides(base = CurrentColors.value.base, colors = ThemeColors()) + fun currentThemeData(darkForSystemTheme: Boolean): ThemeData { + val t = currentColors(darkForSystemTheme) + return ThemeData(colors = ThemeColors( + primary = t.colors.primary.toReadableHex(), + primaryVariant = t.colors.primaryVariant.toReadableHex(), + secondary = t.colors.secondary.toReadableHex(), + secondaryVariant = t.colors.secondaryVariant.toReadableHex(), + background = t.colors.background.toReadableHex(), + surface = t.colors.surface.toReadableHex(), + title = t.appColors.title.toReadableHex(), + sentMessage = t.appColors.sentMessage.toReadableHex(), + receivedMessage = t.appColors.receivedMessage.toReadableHex() + )) } // colors, default theme enum, localized name of theme @@ -124,12 +128,11 @@ object ThemeManager { CurrentColors.value = currentColors(!CurrentColors.value.colors.isLight) } - fun saveAndApplyThemeOverrides(theme: ThemeOverrides, darkForSystemTheme: Boolean) { + fun saveAndApplyThemeData(name: String, theme: ThemeData, darkForSystemTheme: Boolean) { val overrides = appPrefs.themeOverrides.get().toMutableMap() - val prevValue = overrides[theme.base.name] ?: ThemeOverrides(base = CurrentColors.value.base, colors = ThemeColors()) - overrides[theme.base.name] = prevValue.copy(colors = theme.colors) + val prevValue = overrides[name] ?: ThemeOverrides(base = CurrentColors.value.base, colors = ThemeColors()) + overrides[name] = prevValue.copy(colors = theme.colors) appPrefs.themeOverrides.set(overrides) - appPrefs.currentTheme.set(theme.base.name) CurrentColors.value = currentColors(!CurrentColors.value.colors.isLight) } diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/helpers/Util.kt b/apps/android/app/src/main/java/chat/simplex/app/views/helpers/Util.kt index 39f7d88932..8b20bafc29 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/helpers/Util.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/helpers/Util.kt @@ -37,6 +37,7 @@ import androidx.core.text.HtmlCompat import chat.simplex.app.* import chat.simplex.app.R import chat.simplex.app.model.* +import chat.simplex.app.ui.theme.ThemeData import chat.simplex.app.ui.theme.ThemeOverrides import com.charleskorn.kaml.decodeFromStream import kotlinx.coroutines.* @@ -390,10 +391,10 @@ fun getDrawableFromUri(uri: Uri, withAlertOnException: Boolean = true): Drawable } } -fun getThemeFromUri(uri: Uri, withAlertOnException: Boolean = true): ThemeOverrides? { +fun getThemeFromUri(uri: Uri, withAlertOnException: Boolean = true): ThemeData? { SimplexApp.context.contentResolver.openInputStream(uri).use { runCatching { - return yaml.decodeFromStream(it!!) + return yaml.decodeFromStream(it!!) }.onFailure { if (withAlertOnException) { AlertManager.shared.showAlertMsg( diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/usersettings/Appearance.kt b/apps/android/app/src/main/java/chat/simplex/app/views/usersettings/Appearance.kt index 4b5e65d710..538a470bf1 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/usersettings/Appearance.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/usersettings/Appearance.kt @@ -236,24 +236,22 @@ fun CustomizeThemeView(editColor: (ThemeColor, Color) -> Unit) { } SectionSpacer() SectionView { - if (currentTheme.base.hasChangedAnyColor(currentTheme.colors, currentTheme.appColors)) { - val context = LocalContext.current - val theme = remember { mutableStateOf(null as String?) } - val exportThemeLauncher = rememberSaveThemeLauncher(context, theme) - SectionItemView({ - val overrides = ThemeManager.currentThemeOverrides(isInDarkTheme) - theme.value = yaml.encodeToString(overrides) - exportThemeLauncher.launch("simplex.theme") - }) { - Text(generalGetString(R.string.export_theme), color = colors.primary) - } + val context = LocalContext.current + val theme = remember { mutableStateOf(null as String?) } + val exportThemeLauncher = rememberSaveThemeLauncher(context, theme) + SectionItemView({ + val themeData = ThemeManager.currentThemeData(isInDarkTheme) + theme.value = yaml.encodeToString(themeData) + exportThemeLauncher.launch("simplex.theme") + }) { + Text(generalGetString(R.string.export_theme), color = colors.primary) } val importThemeLauncher = rememberGetContentLauncher { uri: Uri? -> if (uri != null) { val theme = getThemeFromUri(uri) if (theme != null) { - ThemeManager.saveAndApplyThemeOverrides(theme, isInDarkTheme) + ThemeManager.saveAndApplyThemeData(currentTheme.name, theme, isInDarkTheme) } } }