Skip to content

Commit

Permalink
Uses the system theme as default theme (#3813)
Browse files Browse the repository at this point in the history
Set the "System Design" as the default theme.

This ensures that the app's initial behaviour respect's the user's system-wide theme choice, while still allowing the user to adjust it later.

This is only done for new installs of Tusky. If the user is upgrading from a previous release and they did not have an explicit theme set then the dark theme is used, and the UX does not change.
  • Loading branch information
SpaceFox authored Aug 23, 2023
1 parent dd0cf9c commit 85888ac
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/com/keylesspalace/tusky/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

import javax.inject.Inject;

import static com.keylesspalace.tusky.settings.PrefKeys.APP_THEME;

public abstract class BaseActivity extends AppCompatActivity implements Injectable {
private static final String TAG = "BaseActivity";

Expand All @@ -74,7 +76,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
/* 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("appTheme", ThemeUtils.APP_THEME_DEFAULT);
String theme = preferences.getString(APP_THEME, ThemeUtils.APP_THEME_DEFAULT);
Log.d("activeTheme", theme);
if (theme.equals("black")) {
setTheme(R.style.TuskyBlackTheme);
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/com/keylesspalace/tusky/TuskyApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ 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.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
Expand Down Expand Up @@ -76,7 +79,7 @@ class TuskyApplication : Application(), HasAndroidInjector {
AppInjector.init(this)

// Migrate shared preference keys and defaults from version to version.
val oldVersion = sharedPreferences.getInt(PrefKeys.SCHEMA_VERSION, 0)
val oldVersion = sharedPreferences.getInt(PrefKeys.SCHEMA_VERSION, NEW_INSTALL_SCHEMA_VERSION)
if (oldVersion != SCHEMA_VERSION) {
upgradeSharedPreferences(oldVersion, SCHEMA_VERSION)
}
Expand All @@ -87,7 +90,7 @@ class TuskyApplication : Application(), HasAndroidInjector {
EmojiPackHelper.init(this, DefaultEmojiPackList.get(this), allowPackImports = false)

// init night mode
val theme = sharedPreferences.getString("appTheme", APP_THEME_DEFAULT)
val theme = sharedPreferences.getString(APP_THEME, APP_THEME_DEFAULT)
setAppNightMode(theme)

localeManager.setLocale()
Expand Down Expand Up @@ -136,6 +139,14 @@ class TuskyApplication : Application(), HasAndroidInjector {
editor.remove(PrefKeys.Deprecated.SHOW_NOTIFICATIONS_FILTER)
}

if (oldVersion != NEW_INSTALL_SCHEMA_VERSION && oldVersion < 2023082301) {
// Default value for appTheme is now THEME_SYSTEM. If the user is upgrading and
// 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.putInt(PrefKeys.SCHEMA_VERSION, newVersion)
editor.apply()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import com.keylesspalace.tusky.entity.Emoji
import com.keylesspalace.tusky.entity.NewPoll
import com.keylesspalace.tusky.entity.Status
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
Expand Down Expand Up @@ -208,7 +209,7 @@ class ComposeActivity :

activeAccount = accountManager.activeAccount ?: return

val theme = preferences.getString("appTheme", APP_THEME_DEFAULT)
val theme = preferences.getString(APP_THEME, APP_THEME_DEFAULT)
if (theme == "black") {
setTheme(R.style.TuskyDialogActivityBlackTheme)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
import com.keylesspalace.tusky.databinding.ActivityPreferencesBinding
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
Expand Down Expand Up @@ -145,8 +146,8 @@ class PreferencesActivity :

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
when (key) {
"appTheme" -> {
val theme = sharedPreferences.getNonNullString("appTheme", APP_THEME_DEFAULT)
APP_THEME -> {
val theme = sharedPreferences.getNonNullString(APP_THEME, APP_THEME_DEFAULT)
Log.d("activeTheme", theme)
setAppNightMode(theme)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ enum class AppTheme(val value: String) {
*
* - Adding a new preference that does not change the interpretation of an existing preference
*/
const val SCHEMA_VERSION = 2023072401
const val SCHEMA_VERSION = 2023082301

/** The schema version for fresh installs */
const val NEW_INSTALL_SCHEMA_VERSION = 0

object PrefKeys {
// Note: not all of these keys are actually used as SharedPreferences keys but we must give
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import com.google.android.material.color.MaterialColors
* the ability to do so is not supported in resource files.
*/

private const val THEME_NIGHT = "night"
private const val THEME_DAY = "day"
private const val THEME_BLACK = "black"
private const val THEME_AUTO = "auto"
private const val THEME_SYSTEM = "auto_system"
const val APP_THEME_DEFAULT = THEME_NIGHT
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 APP_THEME_DEFAULT = THEME_SYSTEM

fun getDimension(context: Context, @AttrRes attribute: Int): Int {
return context.obtainStyledAttributes(intArrayOf(attribute)).use { array ->
Expand Down

0 comments on commit 85888ac

Please sign in to comment.