Allow toggling theme with Android night theme

This commit is contained in:
SpiritCroc 2020-05-15 11:08:10 +02:00
parent bce7845f9b
commit 32d8be34c8
6 changed files with 36 additions and 3 deletions

View File

@ -65,12 +65,14 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
* *
* @param theme the new theme * @param theme the new theme
*/ */
/*
fun updateApplicationTheme(theme: String) { fun updateApplicationTheme(theme: String) {
ThemeUtils.setApplicationTheme(context, theme) ThemeUtils.setApplicationTheme(context, theme)
updateApplicationSettings(VectorLocale.applicationLocale, updateApplicationSettings(VectorLocale.applicationLocale,
FontScale.getFontScalePrefValue(context), FontScale.getFontScalePrefValue(context),
theme) theme)
} }
*/
/** /**
* Init the configuration from the saved one * Init the configuration from the saved one

View File

@ -55,7 +55,22 @@ class VectorSettingsPreferencesFragment @Inject constructor(
findPreference<VectorListPreference>(ThemeUtils.APPLICATION_THEME_KEY)!! findPreference<VectorListPreference>(ThemeUtils.APPLICATION_THEME_KEY)!!
.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue -> .onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue is String) { 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<VectorListPreference>(ThemeUtils.APPLICATION_DARK_THEME_KEY)!!
.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue is String) {
//vectorConfiguration.updateApplicationTheme(newValue)
// Restart the Activity // Restart the Activity
activity?.let { activity?.let {
// Note: recreate does not apply the color correctly // Note: recreate does not apply the color correctly

View File

@ -18,6 +18,7 @@ package im.vector.riotx.features.themes
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.content.res.Configuration
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.util.TypedValue import android.util.TypedValue
import android.view.Menu import android.view.Menu
@ -35,6 +36,7 @@ import timber.log.Timber
object ThemeUtils { object ThemeUtils {
// preference key // preference key
const val APPLICATION_THEME_KEY = "APPLICATION_THEME_KEY" const val APPLICATION_THEME_KEY = "APPLICATION_THEME_KEY"
const val APPLICATION_DARK_THEME_KEY = "APPLICATION_DARK_THEME_KEY"
// the theme possible values // the theme possible values
private const val THEME_DARK_VALUE = "dark" private const val THEME_DARK_VALUE = "dark"
@ -57,8 +59,11 @@ object ThemeUtils {
* @return the selected application theme * @return the selected application theme
*/ */
fun getApplicationTheme(context: Context): String { fun getApplicationTheme(context: Context): String {
return PreferenceManager.getDefaultSharedPreferences(context) val currentNightMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
.getString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE)!! 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)!!
}
} }
/** /**

View File

@ -3,6 +3,7 @@
<string name="redacted_stub_text">(Gelöschte Nachricht)</string> <string name="redacted_stub_text">(Gelöschte Nachricht)</string>
<string name="settings_dark_theme">Nacht-Design</string>
<string name="sc_theme">SC Schwarz</string> <string name="sc_theme">SC Schwarz</string>
<string name="sc_dark_theme">SC Dunkel</string> <string name="sc_dark_theme">SC Dunkel</string>
<string name="sc_colored_theme">SC Schwarz, bunte Blasen</string> <string name="sc_colored_theme">SC Schwarz, bunte Blasen</string>

View File

@ -3,6 +3,7 @@
<string name="redacted_stub_text">(Deleted message)</string> <string name="redacted_stub_text">(Deleted message)</string>
<string name="settings_dark_theme">Night Theme</string>
<string name="sc_theme">SC Black</string> <string name="sc_theme">SC Black</string>
<string name="sc_dark_theme">SC Dark</string> <string name="sc_dark_theme">SC Dark</string>
<string name="sc_colored_theme">SC Black, colored bubbles</string> <string name="sc_colored_theme">SC Black, colored bubbles</string>

View File

@ -21,6 +21,15 @@
android:title="@string/settings_theme" android:title="@string/settings_theme"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<im.vector.riotx.core.preference.VectorListPreference
android:defaultValue="dark"
android:entries="@array/theme_entries"
android:entryValues="@array/theme_values"
android:key="APPLICATION_DARK_THEME_KEY"
android:summary="%s"
android:title="@string/settings_dark_theme"
app:iconSpaceReserved="false" />
<im.vector.riotx.core.preference.VectorListPreference <im.vector.riotx.core.preference.VectorListPreference
android:defaultValue="start" android:defaultValue="start"
android:entries="@array/bubble_style_entries" android:entries="@array/bubble_style_entries"