From f08efec0b6f22245690de44feb3ab3086b3e36cb Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Sat, 3 Feb 2018 23:26:53 +0100 Subject: [PATCH] simplify theme handling --- .../com/keylesspalace/tusky/BaseActivity.java | 11 +--- .../com/keylesspalace/tusky/LoginActivity.kt | 12 +---- .../tusky/PreferencesActivity.java | 53 ++----------------- .../keylesspalace/tusky/TuskyApplication.java | 3 +- .../keylesspalace/tusky/util/ThemeUtils.java | 25 ++++----- app/src/main/res/values-pl/strings.xml | 8 --- app/src/main/res/values-v27/styles.xml | 6 --- app/src/main/res/values/donottranslate.xml | 8 +-- app/src/main/res/values/strings.xml | 12 ++--- app/src/main/res/values/styles.xml | 8 --- app/src/main/res/xml/preferences.xml | 7 --- 11 files changed, 24 insertions(+), 129 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index e6ca9986c..04f2f646a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -64,15 +64,8 @@ public abstract class BaseActivity extends AppCompatActivity { /* 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[] themeFlavorPair = preferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT).split(":"); - String appTheme = themeFlavorPair[0], themeFlavorPreference = themeFlavorPair[2]; - - setTheme(ResourcesUtils.getResourceIdentifier(this, "style", appTheme)); - - String flavor = preferences.getString("appThemeFlavor", ThemeUtils.THEME_FLAVOR_DEFAULT); - if (flavor.equals(ThemeUtils.THEME_FLAVOR_DEFAULT)) - flavor = themeFlavorPreference; - ThemeUtils.setAppNightMode(flavor); + String theme = preferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT); + ThemeUtils.setAppNightMode(theme); int style; switch(preferences.getString("statusTextSize", "medium")) { diff --git a/app/src/main/java/com/keylesspalace/tusky/LoginActivity.kt b/app/src/main/java/com/keylesspalace/tusky/LoginActivity.kt index f50565f2f..e668b56f1 100644 --- a/app/src/main/java/com/keylesspalace/tusky/LoginActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/LoginActivity.kt @@ -64,16 +64,8 @@ class LoginActivity : AppCompatActivity() { super.onCreate(savedInstanceState) preferences = PreferenceManager.getDefaultSharedPreferences(this) - val themeFlavorPair = preferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT)!!.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - val appTheme = themeFlavorPair[0] - val themeFlavorPreference = themeFlavorPair[2] - - setTheme(ResourcesUtils.getResourceIdentifier(this, "style", appTheme)) - - var flavor = preferences.getString("appThemeFlavor", ThemeUtils.THEME_FLAVOR_DEFAULT) - if (flavor == ThemeUtils.THEME_FLAVOR_DEFAULT) - flavor = themeFlavorPreference - ThemeUtils.setAppNightMode(flavor) + val theme = preferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT) + ThemeUtils.setAppNightMode(theme) setContentView(R.layout.activity_login) diff --git a/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java b/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java index 057edd0d7..df7ccd67e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java @@ -27,7 +27,6 @@ import android.support.v7.widget.Toolbar; import android.view.MenuItem; import com.keylesspalace.tusky.fragment.PreferencesFragment; -import com.keylesspalace.tusky.util.ResourcesUtils; import com.keylesspalace.tusky.util.ThemeUtils; public class PreferencesActivity extends BaseActivity @@ -59,7 +58,6 @@ public class PreferencesActivity extends BaseActivity actionBar.setDisplayShowHomeEnabled(true); } - preferences.registerOnSharedPreferenceChangeListener(this); if(savedInstanceState == null) { @@ -71,28 +69,6 @@ public class PreferencesActivity extends BaseActivity } showFragment(currentPreferences, currentTitle); - PreferencesFragment preferencesFragment = (PreferencesFragment)getFragmentManager().findFragmentById(R.id.fragment_container); - String[] themeFlavorPair = preferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT).split(":"); - String appTheme = themeFlavorPair[0], themeFlavorMode = themeFlavorPair[1], themeFlavorPreference = themeFlavorPair[2]; - - setTheme(ResourcesUtils.getResourceIdentifier(this, "style", appTheme)); - - if (preferencesFragment.findPreference("appThemeFlavor") != null) { - boolean lockFlavor = themeFlavorMode.equals(ThemeUtils.THEME_MODE_ONLY); - preferencesFragment.findPreference("appThemeFlavor").setEnabled(!lockFlavor); - } - - String flavor = preferences.getString("appThemeFlavor", ThemeUtils.THEME_FLAVOR_DEFAULT); - if (flavor.equals(ThemeUtils.THEME_FLAVOR_DEFAULT)) { - flavor = themeFlavorPreference; - - preferences.edit() - .putString("appThemeFlavor", flavor) - .apply(); - } - - // Set theme based on preference - setTheme(ResourcesUtils.getResourceIdentifier(this, "style", appTheme)); } public void showFragment(@XmlRes int preferenceId, @StringRes int title) { @@ -128,32 +104,10 @@ public class PreferencesActivity extends BaseActivity public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { switch (key) { case "appTheme": { - String[] themeFlavorPair = sharedPreferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT).split(":"); - String appTheme = themeFlavorPair[0]; - - setTheme(ResourcesUtils.getResourceIdentifier(this, "style", appTheme)); - - sharedPreferences.edit() - .remove("appThemeFlavor") - .apply(); - } - case "appThemeFlavor": { - String[] themeFlavorPair = sharedPreferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT).split(":"); - String appTheme = themeFlavorPair[0], themeFlavorPreference = themeFlavorPair[2]; - - setTheme(ResourcesUtils.getResourceIdentifier(this, "style", appTheme)); - - String flavor = sharedPreferences.getString("appThemeFlavor", ThemeUtils.THEME_FLAVOR_DEFAULT); - if (flavor.equals(ThemeUtils.THEME_FLAVOR_DEFAULT)) { - flavor = themeFlavorPreference; - - sharedPreferences.edit() - .putString("appThemeFlavor", flavor) - .apply(); - } - ThemeUtils.setAppNightMode(flavor); - + String theme = sharedPreferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT); + ThemeUtils.setAppNightMode(theme); restartActivitiesOnExit = true; + // recreate() could be used instead, but it doesn't have an animation B). Intent intent = getIntent(); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); @@ -163,7 +117,6 @@ public class PreferencesActivity extends BaseActivity startActivity(intent); finish(); overridePendingTransition(R.anim.fade_in, R.anim.fade_out); - break; } case "statusTextSize": { restartActivitiesOnExit = true; diff --git a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java index 47efb83d1..7c2ae988a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java +++ b/app/src/main/java/com/keylesspalace/tusky/TuskyApplication.java @@ -28,10 +28,11 @@ import com.jakewharton.picasso.OkHttp3Downloader; import com.keylesspalace.tusky.db.AccountManager; import com.keylesspalace.tusky.db.AppDatabase; import com.keylesspalace.tusky.util.OkHttpUtils; +import com.keylesspalace.tusky.util.ThemeUtils; import com.squareup.picasso.Picasso; public class TuskyApplication extends Application { - public static final String APP_THEME_DEFAULT = "AppTheme:prefer:night"; + public static final String APP_THEME_DEFAULT = ThemeUtils.THEME_NIGHT; private static AppDatabase db; private static AccountManager accountManager; diff --git a/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.java b/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.java index 7281f4f48..f2c1a2502 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.java +++ b/app/src/main/java/com/keylesspalace/tusky/util/ThemeUtils.java @@ -37,12 +37,9 @@ import com.keylesspalace.tusky.TuskyApplication; * the ability to do so is not supported in resource files. */ public class ThemeUtils { - public static final String THEME_MODE_PREFER = "prefer"; - public static final String THEME_MODE_ONLY = "only"; - public static final String THEME_FLAVOR_NIGHT = "night"; - public static final String THEME_FLAVOR_DAY = "day"; - public static final String THEME_FLAVOR_AUTO = "auto"; - public static final String THEME_FLAVOR_DEFAULT = "preferred"; + public static final String THEME_NIGHT = "night"; + public static final String THEME_DAY = "day"; + public static final String THEME_AUTO = "auto"; public static Drawable getDrawable(Context context, @AttrRes int attribute, @DrawableRes int fallbackDrawable) { @@ -94,20 +91,19 @@ public class ThemeUtils { drawable.setColorFilter(getColor(context, attribute), PorterDuff.Mode.SRC_IN); } - public static boolean setAppNightMode(String flavor) { + public static void setAppNightMode(String flavor) { int mode; switch (flavor) { - case THEME_FLAVOR_AUTO: - mode = UiModeManager.MODE_NIGHT_AUTO; - break; - case THEME_FLAVOR_NIGHT: + default: + case THEME_NIGHT: mode = UiModeManager.MODE_NIGHT_YES; break; - case THEME_FLAVOR_DAY: + case THEME_DAY: mode = UiModeManager.MODE_NIGHT_NO; break; - default: - return false; + case THEME_AUTO: + mode = UiModeManager.MODE_NIGHT_AUTO; + break; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { @@ -116,6 +112,5 @@ public class ThemeUtils { AppCompatDelegate.setDefaultNightMode(mode); } - return true; } } diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index f1395ccfb..870d264f0 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -185,14 +185,6 @@ Motyw - Odmiana motywu - - Pora dnia - Noc - Dzień - - - Przeglądarka Używaj niestandardowych kart Chrome Ukryj przycisk śledzenia podczas przewijania diff --git a/app/src/main/res/values-v27/styles.xml b/app/src/main/res/values-v27/styles.xml index fb234eb79..1b03dc9d9 100644 --- a/app/src/main/res/values-v27/styles.xml +++ b/app/src/main/res/values-v27/styles.xml @@ -7,10 +7,4 @@ @color/status_divider_light - - diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index 080fb8701..aea818fe6 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -31,13 +31,9 @@ - AppTheme:prefer:night - Remin:only:day - - - - auto night day + auto + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a64099223..69afd0dfc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -172,15 +172,9 @@ App Theme - Tusky - Remin - - - Theme flavor - - Sunset/Sunrise - Night - Day + Dark + Light + Automatic at sunset Browser diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 192ce0db9..53a7dd816 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -105,14 +105,6 @@ - - diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 0eb9fbd4a..aabfe3212 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -12,13 +12,6 @@ android:summary="%s" android:title="@string/pref_title_app_theme" /> - -