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:
Konrad Pozniak 2023-10-14 14:20:20 +02:00 committed by GitHub
parent 8935b554d7
commit ff1c4a4b27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 25 deletions

View File

@ -47,6 +47,7 @@ import com.keylesspalace.tusky.db.AccountManager;
import com.keylesspalace.tusky.di.Injectable;
import com.keylesspalace.tusky.interfaces.AccountSelectionListener;
import com.keylesspalace.tusky.interfaces.PermissionRequester;
import com.keylesspalace.tusky.settings.AppTheme;
import com.keylesspalace.tusky.settings.PrefKeys;
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
* runtime, just individual activities. So, each activity has to set its theme before any
* 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);
if (ThemeUtils.isBlack(getResources().getConfiguration(), theme)) {
setTheme(R.style.TuskyBlackTheme);

View File

@ -25,13 +25,12 @@ import androidx.work.WorkManager
import autodispose2.AutoDisposePlugins
import com.keylesspalace.tusky.components.notifications.NotificationHelper
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.PrefKeys
import com.keylesspalace.tusky.settings.PrefKeys.APP_THEME
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.THEME_NIGHT
import com.keylesspalace.tusky.util.setAppNightMode
import com.keylesspalace.tusky.worker.PruneCacheWorker
import com.keylesspalace.tusky.worker.WorkerFactory
@ -90,7 +89,7 @@ class TuskyApplication : Application(), HasAndroidInjector {
EmojiPackHelper.init(this, DefaultEmojiPackList.get(this), allowPackImports = false)
// init night mode
val theme = sharedPreferences.getString(APP_THEME, APP_THEME_DEFAULT)
val theme = sharedPreferences.getString(APP_THEME, AppTheme.DEFAULT.value)
setAppNightMode(theme)
localeManager.setLocale()
@ -144,7 +143,7 @@ class TuskyApplication : Application(), HasAndroidInjector {
// didn't have an explicit preference set use the previous default, so the
// theme does not unexpectedly change.
if (!sharedPreferences.contains(APP_THEME)) {
editor.putString(APP_THEME, THEME_NIGHT)
editor.putString(APP_THEME, AppTheme.NIGHT.value)
}
}
editor.putInt(PrefKeys.SCHEMA_VERSION, newVersion)

View File

@ -93,9 +93,9 @@ import com.keylesspalace.tusky.entity.Attachment
import com.keylesspalace.tusky.entity.Emoji
import com.keylesspalace.tusky.entity.NewPoll
import com.keylesspalace.tusky.entity.Status
import com.keylesspalace.tusky.settings.AppTheme
import com.keylesspalace.tusky.settings.PrefKeys
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.PickMediaFiles
import com.keylesspalace.tusky.util.getInitialLanguages
@ -209,7 +209,7 @@ class ComposeActivity :
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") {
setTheme(R.style.TuskyDialogActivityBlackTheme)
}

View File

@ -33,9 +33,9 @@ import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
import com.keylesspalace.tusky.databinding.ActivityPreferencesBinding
import com.keylesspalace.tusky.settings.AppTheme
import com.keylesspalace.tusky.settings.PrefKeys
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.setAppNightMode
import dagger.android.DispatchingAndroidInjector
@ -147,7 +147,7 @@ class PreferencesActivity :
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
when (key) {
APP_THEME -> {
val theme = sharedPreferences.getNonNullString(APP_THEME, APP_THEME_DEFAULT)
val theme = sharedPreferences.getNonNullString(APP_THEME, AppTheme.DEFAULT.value)
Log.d("activeTheme", theme)
setAppNightMode(theme)

View File

@ -75,7 +75,7 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable {
makePreferenceScreen {
preferenceCategory(R.string.pref_title_appearance_settings) {
listPreference {
setDefaultValue(AppTheme.NIGHT.value)
setDefaultValue(AppTheme.DEFAULT.value)
setEntries(R.array.app_theme_names)
entryValues = AppTheme.stringValues()
key = PrefKeys.APP_THEME

View File

@ -10,6 +10,9 @@ enum class AppTheme(val value: String) {
companion object {
fun stringValues() = values().map { it.value }.toTypedArray()
@JvmField
val DEFAULT = AUTO_SYSTEM
}
}

View File

@ -25,20 +25,13 @@ import androidx.annotation.AttrRes
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.res.use
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
* 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 {
return context.obtainStyledAttributes(intArrayOf(attribute)).use { array ->
array.getDimensionPixelSize(0, -1)
@ -54,24 +47,24 @@ fun setDrawableTint(context: Context, drawable: Drawable, @AttrRes attribute: In
fun setAppNightMode(flavor: String?) {
when (flavor) {
THEME_NIGHT, THEME_BLACK -> AppCompatDelegate.setDefaultNightMode(
AppTheme.NIGHT.value, AppTheme.BLACK.value -> AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_YES
)
THEME_DAY -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
THEME_AUTO -> AppCompatDelegate.setDefaultNightMode(
AppTheme.DAY.value -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
AppTheme.AUTO.value -> AppCompatDelegate.setDefaultNightMode(
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
)
else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}
fun isBlack(config: Configuration, theme: String?): Boolean {
return when (theme) {
THEME_BLACK -> true
THEME_SYSTEM_BLACK -> when (config.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
AppTheme.BLACK.value -> true
AppTheme.AUTO_SYSTEM_BLACK.value -> when (config.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_NO -> false
Configuration.UI_MODE_NIGHT_YES -> true
else -> false