Add System theme option and set as default; closes #904, closes #2387

This commit is contained in:
Tigermouthbear 2020-12-28 23:32:06 -05:00
parent 7ae2b34a9e
commit 3e78098c43
5 changed files with 23 additions and 10 deletions

View File

@ -5,7 +5,7 @@ Features ✨:
- Enable url previews for notices (#2562) - Enable url previews for notices (#2562)
Improvements 🙌: Improvements 🙌:
- - Add System theme option and set as default (#904) (#2387)
Bugfix 🐛: Bugfix 🐛:
- Url previews sometimes attached to wrong message (#2561) - Url previews sometimes attached to wrong message (#2561)

View File

@ -18,6 +18,8 @@ package im.vector.app.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.content.res.Resources
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
@ -39,6 +41,7 @@ object ThemeUtils {
const val APPLICATION_THEME_KEY = "APPLICATION_THEME_KEY" const val APPLICATION_THEME_KEY = "APPLICATION_THEME_KEY"
// the theme possible values // the theme possible values
private const val SYSTEM_THEME_VALUE = "system"
private const val THEME_DARK_VALUE = "dark" private const val THEME_DARK_VALUE = "dark"
private const val THEME_LIGHT_VALUE = "light" private const val THEME_LIGHT_VALUE = "light"
private const val THEME_BLACK_VALUE = "black" private const val THEME_BLACK_VALUE = "black"
@ -54,13 +57,11 @@ object ThemeUtils {
} }
/** /**
* @return true if current theme is Light or Status * @return true if current theme is Light/Status or current theme is System and system theme is light
*/ */
fun isLightTheme(context: Context): Boolean { fun isLightTheme(context: Context): Boolean {
return when (getApplicationTheme(context)) { val theme = getApplicationTheme(context)
THEME_LIGHT_VALUE -> true return theme == THEME_LIGHT_VALUE || (theme == SYSTEM_THEME_VALUE && !isSystemDarkTheme(context.resources))
else -> false
}
} }
/** /**
@ -73,11 +74,11 @@ object ThemeUtils {
val currentTheme = this.currentTheme.get() val currentTheme = this.currentTheme.get()
return if (currentTheme == null) { return if (currentTheme == null) {
val prefs = DefaultSharedPreferences.getInstance(context) val prefs = DefaultSharedPreferences.getInstance(context)
var themeFromPref = prefs.getString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE) ?: THEME_LIGHT_VALUE var themeFromPref = prefs.getString(APPLICATION_THEME_KEY, SYSTEM_THEME_VALUE) ?: SYSTEM_THEME_VALUE
if (themeFromPref == "status") { if (themeFromPref == "status") {
// Migrate to light theme, which is the closest theme // Migrate to light theme, which is the closest theme
themeFromPref = THEME_LIGHT_VALUE themeFromPref = SYSTEM_THEME_VALUE
prefs.edit { putString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE) } prefs.edit { putString(APPLICATION_THEME_KEY, SYSTEM_THEME_VALUE) }
} }
this.currentTheme.set(themeFromPref) this.currentTheme.set(themeFromPref)
themeFromPref themeFromPref
@ -86,6 +87,13 @@ object ThemeUtils {
} }
} }
/**
* @return true if system theme is dark
*/
private fun isSystemDarkTheme(resources: Resources): Boolean {
return resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}
/** /**
* Update the application theme * Update the application theme
* *
@ -94,6 +102,7 @@ object ThemeUtils {
fun setApplicationTheme(context: Context, aTheme: String) { fun setApplicationTheme(context: Context, aTheme: String) {
currentTheme.set(aTheme) currentTheme.set(aTheme)
when (aTheme) { when (aTheme) {
SYSTEM_THEME_VALUE -> context.setTheme(if(isSystemDarkTheme(context.resources)) R.style.AppTheme_Dark else R.style.AppTheme_Light)
THEME_DARK_VALUE -> context.setTheme(R.style.AppTheme_Dark) THEME_DARK_VALUE -> context.setTheme(R.style.AppTheme_Dark)
THEME_BLACK_VALUE -> context.setTheme(R.style.AppTheme_Black) THEME_BLACK_VALUE -> context.setTheme(R.style.AppTheme_Black)
else -> context.setTheme(R.style.AppTheme_Light) else -> context.setTheme(R.style.AppTheme_Light)
@ -110,6 +119,7 @@ object ThemeUtils {
*/ */
fun setActivityTheme(activity: Activity, otherThemes: ActivityOtherThemes) { fun setActivityTheme(activity: Activity, otherThemes: ActivityOtherThemes) {
when (getApplicationTheme(activity)) { when (getApplicationTheme(activity)) {
SYSTEM_THEME_VALUE -> if(isSystemDarkTheme(activity.resources)) activity.setTheme(otherThemes.dark)
THEME_DARK_VALUE -> activity.setTheme(otherThemes.dark) THEME_DARK_VALUE -> activity.setTheme(otherThemes.dark)
THEME_BLACK_VALUE -> activity.setTheme(otherThemes.black) THEME_BLACK_VALUE -> activity.setTheme(otherThemes.black)
} }

View File

@ -92,12 +92,14 @@
<!-- Theme --> <!-- Theme -->
<string-array name="theme_entries"> <string-array name="theme_entries">
<item>@string/system_theme</item>
<item>@string/light_theme</item> <item>@string/light_theme</item>
<item>@string/dark_theme</item> <item>@string/dark_theme</item>
<item>@string/black_them</item> <item>@string/black_them</item>
</string-array> </string-array>
<string-array name="theme_values"> <string-array name="theme_values">
<item>system</item>
<item>light</item> <item>light</item>
<item>dark</item> <item>dark</item>
<item>black</item> <item>black</item>

View File

@ -8,6 +8,7 @@
<string name="resources_script">Latn</string> <string name="resources_script">Latn</string>
<!-- theme --> <!-- theme -->
<string name="system_theme">System Default</string>
<string name="light_theme">Light Theme</string> <string name="light_theme">Light Theme</string>
<string name="dark_theme">Dark Theme</string> <string name="dark_theme">Dark Theme</string>
<string name="black_them">Black Theme</string> <string name="black_them">Black Theme</string>

View File

@ -13,7 +13,7 @@
app:fragment="im.vector.app.features.settings.locale.LocalePickerFragment" /> app:fragment="im.vector.app.features.settings.locale.LocalePickerFragment" />
<im.vector.app.core.preference.VectorListPreference <im.vector.app.core.preference.VectorListPreference
android:defaultValue="light" android:defaultValue="system"
android:entries="@array/theme_entries" android:entries="@array/theme_entries"
android:entryValues="@array/theme_values" android:entryValues="@array/theme_values"
android:key="APPLICATION_THEME_KEY" android:key="APPLICATION_THEME_KEY"