fix theme preference defaults (#4061)
closes #4060 Also I noticed that we had the theme defaults twice in the code so I refactored a bit so only one are still in there.
This commit is contained in:
parent
8935b554d7
commit
ff1c4a4b27
7 changed files with 21 additions and 25 deletions
|
@ -47,6 +47,7 @@ import com.keylesspalace.tusky.db.AccountManager;
|
||||||
import com.keylesspalace.tusky.di.Injectable;
|
import com.keylesspalace.tusky.di.Injectable;
|
||||||
import com.keylesspalace.tusky.interfaces.AccountSelectionListener;
|
import com.keylesspalace.tusky.interfaces.AccountSelectionListener;
|
||||||
import com.keylesspalace.tusky.interfaces.PermissionRequester;
|
import com.keylesspalace.tusky.interfaces.PermissionRequester;
|
||||||
|
import com.keylesspalace.tusky.settings.AppTheme;
|
||||||
import com.keylesspalace.tusky.settings.PrefKeys;
|
import com.keylesspalace.tusky.settings.PrefKeys;
|
||||||
import com.keylesspalace.tusky.util.ThemeUtils;
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
||||||
|
|
||||||
|
@ -77,7 +78,7 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab
|
||||||
/* There isn't presently a way to globally change the theme of a whole application at
|
/* There isn't presently a way to globally change the theme of a whole application at
|
||||||
* runtime, just individual activities. So, each activity has to set its theme before any
|
* runtime, just individual activities. So, each activity has to set its theme before any
|
||||||
* views are created. */
|
* views are created. */
|
||||||
String theme = preferences.getString(APP_THEME, ThemeUtils.APP_THEME_DEFAULT);
|
String theme = preferences.getString(APP_THEME, AppTheme.DEFAULT.getValue());
|
||||||
Log.d("activeTheme", theme);
|
Log.d("activeTheme", theme);
|
||||||
if (ThemeUtils.isBlack(getResources().getConfiguration(), theme)) {
|
if (ThemeUtils.isBlack(getResources().getConfiguration(), theme)) {
|
||||||
setTheme(R.style.TuskyBlackTheme);
|
setTheme(R.style.TuskyBlackTheme);
|
||||||
|
|
|
@ -25,13 +25,12 @@ import androidx.work.WorkManager
|
||||||
import autodispose2.AutoDisposePlugins
|
import autodispose2.AutoDisposePlugins
|
||||||
import com.keylesspalace.tusky.components.notifications.NotificationHelper
|
import com.keylesspalace.tusky.components.notifications.NotificationHelper
|
||||||
import com.keylesspalace.tusky.di.AppInjector
|
import com.keylesspalace.tusky.di.AppInjector
|
||||||
|
import com.keylesspalace.tusky.settings.AppTheme
|
||||||
import com.keylesspalace.tusky.settings.NEW_INSTALL_SCHEMA_VERSION
|
import com.keylesspalace.tusky.settings.NEW_INSTALL_SCHEMA_VERSION
|
||||||
import com.keylesspalace.tusky.settings.PrefKeys
|
import com.keylesspalace.tusky.settings.PrefKeys
|
||||||
import com.keylesspalace.tusky.settings.PrefKeys.APP_THEME
|
import com.keylesspalace.tusky.settings.PrefKeys.APP_THEME
|
||||||
import com.keylesspalace.tusky.settings.SCHEMA_VERSION
|
import com.keylesspalace.tusky.settings.SCHEMA_VERSION
|
||||||
import com.keylesspalace.tusky.util.APP_THEME_DEFAULT
|
|
||||||
import com.keylesspalace.tusky.util.LocaleManager
|
import com.keylesspalace.tusky.util.LocaleManager
|
||||||
import com.keylesspalace.tusky.util.THEME_NIGHT
|
|
||||||
import com.keylesspalace.tusky.util.setAppNightMode
|
import com.keylesspalace.tusky.util.setAppNightMode
|
||||||
import com.keylesspalace.tusky.worker.PruneCacheWorker
|
import com.keylesspalace.tusky.worker.PruneCacheWorker
|
||||||
import com.keylesspalace.tusky.worker.WorkerFactory
|
import com.keylesspalace.tusky.worker.WorkerFactory
|
||||||
|
@ -90,7 +89,7 @@ class TuskyApplication : Application(), HasAndroidInjector {
|
||||||
EmojiPackHelper.init(this, DefaultEmojiPackList.get(this), allowPackImports = false)
|
EmojiPackHelper.init(this, DefaultEmojiPackList.get(this), allowPackImports = false)
|
||||||
|
|
||||||
// init night mode
|
// init night mode
|
||||||
val theme = sharedPreferences.getString(APP_THEME, APP_THEME_DEFAULT)
|
val theme = sharedPreferences.getString(APP_THEME, AppTheme.DEFAULT.value)
|
||||||
setAppNightMode(theme)
|
setAppNightMode(theme)
|
||||||
|
|
||||||
localeManager.setLocale()
|
localeManager.setLocale()
|
||||||
|
@ -144,7 +143,7 @@ class TuskyApplication : Application(), HasAndroidInjector {
|
||||||
// didn't have an explicit preference set use the previous default, so the
|
// didn't have an explicit preference set use the previous default, so the
|
||||||
// theme does not unexpectedly change.
|
// theme does not unexpectedly change.
|
||||||
if (!sharedPreferences.contains(APP_THEME)) {
|
if (!sharedPreferences.contains(APP_THEME)) {
|
||||||
editor.putString(APP_THEME, THEME_NIGHT)
|
editor.putString(APP_THEME, AppTheme.NIGHT.value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
editor.putInt(PrefKeys.SCHEMA_VERSION, newVersion)
|
editor.putInt(PrefKeys.SCHEMA_VERSION, newVersion)
|
||||||
|
|
|
@ -93,9 +93,9 @@ import com.keylesspalace.tusky.entity.Attachment
|
||||||
import com.keylesspalace.tusky.entity.Emoji
|
import com.keylesspalace.tusky.entity.Emoji
|
||||||
import com.keylesspalace.tusky.entity.NewPoll
|
import com.keylesspalace.tusky.entity.NewPoll
|
||||||
import com.keylesspalace.tusky.entity.Status
|
import com.keylesspalace.tusky.entity.Status
|
||||||
|
import com.keylesspalace.tusky.settings.AppTheme
|
||||||
import com.keylesspalace.tusky.settings.PrefKeys
|
import com.keylesspalace.tusky.settings.PrefKeys
|
||||||
import com.keylesspalace.tusky.settings.PrefKeys.APP_THEME
|
import com.keylesspalace.tusky.settings.PrefKeys.APP_THEME
|
||||||
import com.keylesspalace.tusky.util.APP_THEME_DEFAULT
|
|
||||||
import com.keylesspalace.tusky.util.MentionSpan
|
import com.keylesspalace.tusky.util.MentionSpan
|
||||||
import com.keylesspalace.tusky.util.PickMediaFiles
|
import com.keylesspalace.tusky.util.PickMediaFiles
|
||||||
import com.keylesspalace.tusky.util.getInitialLanguages
|
import com.keylesspalace.tusky.util.getInitialLanguages
|
||||||
|
@ -209,7 +209,7 @@ class ComposeActivity :
|
||||||
|
|
||||||
activeAccount = accountManager.activeAccount ?: return
|
activeAccount = accountManager.activeAccount ?: return
|
||||||
|
|
||||||
val theme = preferences.getString(APP_THEME, APP_THEME_DEFAULT)
|
val theme = preferences.getString(APP_THEME, AppTheme.DEFAULT.value)
|
||||||
if (theme == "black") {
|
if (theme == "black") {
|
||||||
setTheme(R.style.TuskyDialogActivityBlackTheme)
|
setTheme(R.style.TuskyDialogActivityBlackTheme)
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,9 +33,9 @@ import com.keylesspalace.tusky.R
|
||||||
import com.keylesspalace.tusky.appstore.EventHub
|
import com.keylesspalace.tusky.appstore.EventHub
|
||||||
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
|
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
|
||||||
import com.keylesspalace.tusky.databinding.ActivityPreferencesBinding
|
import com.keylesspalace.tusky.databinding.ActivityPreferencesBinding
|
||||||
|
import com.keylesspalace.tusky.settings.AppTheme
|
||||||
import com.keylesspalace.tusky.settings.PrefKeys
|
import com.keylesspalace.tusky.settings.PrefKeys
|
||||||
import com.keylesspalace.tusky.settings.PrefKeys.APP_THEME
|
import com.keylesspalace.tusky.settings.PrefKeys.APP_THEME
|
||||||
import com.keylesspalace.tusky.util.APP_THEME_DEFAULT
|
|
||||||
import com.keylesspalace.tusky.util.getNonNullString
|
import com.keylesspalace.tusky.util.getNonNullString
|
||||||
import com.keylesspalace.tusky.util.setAppNightMode
|
import com.keylesspalace.tusky.util.setAppNightMode
|
||||||
import dagger.android.DispatchingAndroidInjector
|
import dagger.android.DispatchingAndroidInjector
|
||||||
|
@ -147,7 +147,7 @@ class PreferencesActivity :
|
||||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
|
||||||
when (key) {
|
when (key) {
|
||||||
APP_THEME -> {
|
APP_THEME -> {
|
||||||
val theme = sharedPreferences.getNonNullString(APP_THEME, APP_THEME_DEFAULT)
|
val theme = sharedPreferences.getNonNullString(APP_THEME, AppTheme.DEFAULT.value)
|
||||||
Log.d("activeTheme", theme)
|
Log.d("activeTheme", theme)
|
||||||
setAppNightMode(theme)
|
setAppNightMode(theme)
|
||||||
|
|
||||||
|
|
|
@ -75,7 +75,7 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable {
|
||||||
makePreferenceScreen {
|
makePreferenceScreen {
|
||||||
preferenceCategory(R.string.pref_title_appearance_settings) {
|
preferenceCategory(R.string.pref_title_appearance_settings) {
|
||||||
listPreference {
|
listPreference {
|
||||||
setDefaultValue(AppTheme.NIGHT.value)
|
setDefaultValue(AppTheme.DEFAULT.value)
|
||||||
setEntries(R.array.app_theme_names)
|
setEntries(R.array.app_theme_names)
|
||||||
entryValues = AppTheme.stringValues()
|
entryValues = AppTheme.stringValues()
|
||||||
key = PrefKeys.APP_THEME
|
key = PrefKeys.APP_THEME
|
||||||
|
|
|
@ -10,6 +10,9 @@ enum class AppTheme(val value: String) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun stringValues() = values().map { it.value }.toTypedArray()
|
fun stringValues() = values().map { it.value }.toTypedArray()
|
||||||
|
|
||||||
|
@JvmField
|
||||||
|
val DEFAULT = AUTO_SYSTEM
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,20 +25,13 @@ import androidx.annotation.AttrRes
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import androidx.core.content.res.use
|
import androidx.core.content.res.use
|
||||||
import com.google.android.material.color.MaterialColors
|
import com.google.android.material.color.MaterialColors
|
||||||
|
import com.keylesspalace.tusky.settings.AppTheme
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides runtime compatibility to obtain theme information and re-theme views, especially where
|
* Provides runtime compatibility to obtain theme information and re-theme views, especially where
|
||||||
* the ability to do so is not supported in resource files.
|
* the ability to do so is not supported in resource files.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const val THEME_NIGHT = "night"
|
|
||||||
const val THEME_DAY = "day"
|
|
||||||
const val THEME_BLACK = "black"
|
|
||||||
const val THEME_AUTO = "auto"
|
|
||||||
const val THEME_SYSTEM = "auto_system"
|
|
||||||
const val THEME_SYSTEM_BLACK = "auto_system_black"
|
|
||||||
const val APP_THEME_DEFAULT = THEME_SYSTEM
|
|
||||||
|
|
||||||
fun getDimension(context: Context, @AttrRes attribute: Int): Int {
|
fun getDimension(context: Context, @AttrRes attribute: Int): Int {
|
||||||
return context.obtainStyledAttributes(intArrayOf(attribute)).use { array ->
|
return context.obtainStyledAttributes(intArrayOf(attribute)).use { array ->
|
||||||
array.getDimensionPixelSize(0, -1)
|
array.getDimensionPixelSize(0, -1)
|
||||||
|
@ -54,24 +47,24 @@ fun setDrawableTint(context: Context, drawable: Drawable, @AttrRes attribute: In
|
||||||
|
|
||||||
fun setAppNightMode(flavor: String?) {
|
fun setAppNightMode(flavor: String?) {
|
||||||
when (flavor) {
|
when (flavor) {
|
||||||
THEME_NIGHT, THEME_BLACK -> AppCompatDelegate.setDefaultNightMode(
|
AppTheme.NIGHT.value, AppTheme.BLACK.value -> AppCompatDelegate.setDefaultNightMode(
|
||||||
AppCompatDelegate.MODE_NIGHT_YES
|
AppCompatDelegate.MODE_NIGHT_YES
|
||||||
)
|
)
|
||||||
THEME_DAY -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
AppTheme.DAY.value -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
||||||
THEME_AUTO -> AppCompatDelegate.setDefaultNightMode(
|
AppTheme.AUTO.value -> AppCompatDelegate.setDefaultNightMode(
|
||||||
AppCompatDelegate.MODE_NIGHT_AUTO_TIME
|
AppCompatDelegate.MODE_NIGHT_AUTO_TIME
|
||||||
)
|
)
|
||||||
THEME_SYSTEM, THEME_SYSTEM_BLACK -> AppCompatDelegate.setDefaultNightMode(
|
AppTheme.AUTO_SYSTEM.value, AppTheme.AUTO_SYSTEM_BLACK.value -> AppCompatDelegate.setDefaultNightMode(
|
||||||
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
||||||
)
|
)
|
||||||
else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
|
else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isBlack(config: Configuration, theme: String?): Boolean {
|
fun isBlack(config: Configuration, theme: String?): Boolean {
|
||||||
return when (theme) {
|
return when (theme) {
|
||||||
THEME_BLACK -> true
|
AppTheme.BLACK.value -> true
|
||||||
THEME_SYSTEM_BLACK -> when (config.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
|
AppTheme.AUTO_SYSTEM_BLACK.value -> when (config.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
|
||||||
Configuration.UI_MODE_NIGHT_NO -> false
|
Configuration.UI_MODE_NIGHT_NO -> false
|
||||||
Configuration.UI_MODE_NIGHT_YES -> true
|
Configuration.UI_MODE_NIGHT_YES -> true
|
||||||
else -> false
|
else -> false
|
||||||
|
|
Loading…
Reference in a new issue