From 32d8be34c8c5e2616eb23fbe6d1bc30e3d57502d Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Fri, 15 May 2020 11:08:10 +0200 Subject: [PATCH] Allow toggling theme with Android night theme --- .../configuration/VectorConfiguration.kt | 2 ++ .../VectorSettingsPreferencesFragment.kt | 17 ++++++++++++++++- .../vector/riotx/features/themes/ThemeUtils.kt | 9 +++++++-- vector/src/main/res/values-de/strings_sc.xml | 1 + vector/src/main/res/values/strings_sc.xml | 1 + .../res/xml/vector_settings_preferences.xml | 9 +++++++++ 6 files changed, 36 insertions(+), 3 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/configuration/VectorConfiguration.kt b/vector/src/main/java/im/vector/riotx/features/configuration/VectorConfiguration.kt index ac542c6cad..2fcf970e04 100644 --- a/vector/src/main/java/im/vector/riotx/features/configuration/VectorConfiguration.kt +++ b/vector/src/main/java/im/vector/riotx/features/configuration/VectorConfiguration.kt @@ -65,12 +65,14 @@ class VectorConfiguration @Inject constructor(private val context: Context) { * * @param theme the new theme */ + /* fun updateApplicationTheme(theme: String) { ThemeUtils.setApplicationTheme(context, theme) updateApplicationSettings(VectorLocale.applicationLocale, FontScale.getFontScalePrefValue(context), theme) } + */ /** * Init the configuration from the saved one diff --git a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsPreferencesFragment.kt b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsPreferencesFragment.kt index 26f114cd44..5185794478 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsPreferencesFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsPreferencesFragment.kt @@ -55,7 +55,22 @@ class VectorSettingsPreferencesFragment @Inject constructor( findPreference(ThemeUtils.APPLICATION_THEME_KEY)!! .onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue -> if (newValue is String) { - vectorConfiguration.updateApplicationTheme(newValue) + //vectorConfiguration.updateApplicationTheme(newValue) + // Restart the Activity + activity?.let { + // Note: recreate does not apply the color correctly + it.startActivity(it.intent) + it.finish() + } + true + } else { + false + } + } + findPreference(ThemeUtils.APPLICATION_DARK_THEME_KEY)!! + .onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue -> + if (newValue is String) { + //vectorConfiguration.updateApplicationTheme(newValue) // Restart the Activity activity?.let { // Note: recreate does not apply the color correctly diff --git a/vector/src/main/java/im/vector/riotx/features/themes/ThemeUtils.kt b/vector/src/main/java/im/vector/riotx/features/themes/ThemeUtils.kt index d4ef50cfb6..6041bf5ba6 100644 --- a/vector/src/main/java/im/vector/riotx/features/themes/ThemeUtils.kt +++ b/vector/src/main/java/im/vector/riotx/features/themes/ThemeUtils.kt @@ -18,6 +18,7 @@ package im.vector.riotx.features.themes import android.app.Activity import android.content.Context +import android.content.res.Configuration import android.graphics.drawable.Drawable import android.util.TypedValue import android.view.Menu @@ -35,6 +36,7 @@ import timber.log.Timber object ThemeUtils { // preference key const val APPLICATION_THEME_KEY = "APPLICATION_THEME_KEY" + const val APPLICATION_DARK_THEME_KEY = "APPLICATION_DARK_THEME_KEY" // the theme possible values private const val THEME_DARK_VALUE = "dark" @@ -57,8 +59,11 @@ object ThemeUtils { * @return the selected application theme */ fun getApplicationTheme(context: Context): String { - return PreferenceManager.getDefaultSharedPreferences(context) - .getString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE)!! + val currentNightMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK + return when (currentNightMode) { + Configuration.UI_MODE_NIGHT_YES -> PreferenceManager.getDefaultSharedPreferences(context).getString(APPLICATION_DARK_THEME_KEY, THEME_DARK_VALUE)!! + else -> PreferenceManager.getDefaultSharedPreferences(context).getString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE)!! + } } /** diff --git a/vector/src/main/res/values-de/strings_sc.xml b/vector/src/main/res/values-de/strings_sc.xml index 9e614e064f..715d5466b8 100644 --- a/vector/src/main/res/values-de/strings_sc.xml +++ b/vector/src/main/res/values-de/strings_sc.xml @@ -3,6 +3,7 @@ (Gelöschte Nachricht) + Nacht-Design SC Schwarz SC Dunkel SC Schwarz, bunte Blasen diff --git a/vector/src/main/res/values/strings_sc.xml b/vector/src/main/res/values/strings_sc.xml index 1f857a2dfb..e6822de680 100644 --- a/vector/src/main/res/values/strings_sc.xml +++ b/vector/src/main/res/values/strings_sc.xml @@ -3,6 +3,7 @@ (Deleted message) + Night Theme SC Black SC Dark SC Black, colored bubbles diff --git a/vector/src/main/res/xml/vector_settings_preferences.xml b/vector/src/main/res/xml/vector_settings_preferences.xml index 765f015186..2d057b0efe 100644 --- a/vector/src/main/res/xml/vector_settings_preferences.xml +++ b/vector/src/main/res/xml/vector_settings_preferences.xml @@ -21,6 +21,15 @@ android:title="@string/settings_theme" app:iconSpaceReserved="false" /> + +