simplify theme handling

This commit is contained in:
Conny Duck 2018-02-03 23:26:53 +01:00
parent 92ae463b38
commit f08efec0b6
11 changed files with 24 additions and 129 deletions

View file

@ -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 /* 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[] themeFlavorPair = preferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT).split(":"); String theme = preferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT);
String appTheme = themeFlavorPair[0], themeFlavorPreference = themeFlavorPair[2]; ThemeUtils.setAppNightMode(theme);
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);
int style; int style;
switch(preferences.getString("statusTextSize", "medium")) { switch(preferences.getString("statusTextSize", "medium")) {

View file

@ -64,16 +64,8 @@ class LoginActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
preferences = PreferenceManager.getDefaultSharedPreferences(this) preferences = PreferenceManager.getDefaultSharedPreferences(this)
val themeFlavorPair = preferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT)!!.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() val theme = preferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT)
val appTheme = themeFlavorPair[0] ThemeUtils.setAppNightMode(theme)
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)
setContentView(R.layout.activity_login) setContentView(R.layout.activity_login)

View file

@ -27,7 +27,6 @@ import android.support.v7.widget.Toolbar;
import android.view.MenuItem; import android.view.MenuItem;
import com.keylesspalace.tusky.fragment.PreferencesFragment; import com.keylesspalace.tusky.fragment.PreferencesFragment;
import com.keylesspalace.tusky.util.ResourcesUtils;
import com.keylesspalace.tusky.util.ThemeUtils; import com.keylesspalace.tusky.util.ThemeUtils;
public class PreferencesActivity extends BaseActivity public class PreferencesActivity extends BaseActivity
@ -59,7 +58,6 @@ public class PreferencesActivity extends BaseActivity
actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayShowHomeEnabled(true);
} }
preferences.registerOnSharedPreferenceChangeListener(this); preferences.registerOnSharedPreferenceChangeListener(this);
if(savedInstanceState == null) { if(savedInstanceState == null) {
@ -71,28 +69,6 @@ public class PreferencesActivity extends BaseActivity
} }
showFragment(currentPreferences, currentTitle); 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) { 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) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
switch (key) { switch (key) {
case "appTheme": { case "appTheme": {
String[] themeFlavorPair = sharedPreferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT).split(":"); String theme = sharedPreferences.getString("appTheme", TuskyApplication.APP_THEME_DEFAULT);
String appTheme = themeFlavorPair[0]; ThemeUtils.setAppNightMode(theme);
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);
restartActivitiesOnExit = true; restartActivitiesOnExit = true;
// recreate() could be used instead, but it doesn't have an animation B). // recreate() could be used instead, but it doesn't have an animation B).
Intent intent = getIntent(); Intent intent = getIntent();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
@ -163,7 +117,6 @@ public class PreferencesActivity extends BaseActivity
startActivity(intent); startActivity(intent);
finish(); finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out); overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
break;
} }
case "statusTextSize": { case "statusTextSize": {
restartActivitiesOnExit = true; restartActivitiesOnExit = true;

View file

@ -28,10 +28,11 @@ import com.jakewharton.picasso.OkHttp3Downloader;
import com.keylesspalace.tusky.db.AccountManager; import com.keylesspalace.tusky.db.AccountManager;
import com.keylesspalace.tusky.db.AppDatabase; import com.keylesspalace.tusky.db.AppDatabase;
import com.keylesspalace.tusky.util.OkHttpUtils; import com.keylesspalace.tusky.util.OkHttpUtils;
import com.keylesspalace.tusky.util.ThemeUtils;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
public class TuskyApplication extends Application { 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 AppDatabase db;
private static AccountManager accountManager; private static AccountManager accountManager;

View file

@ -37,12 +37,9 @@ import com.keylesspalace.tusky.TuskyApplication;
* the ability to do so is not supported in resource files. * the ability to do so is not supported in resource files.
*/ */
public class ThemeUtils { public class ThemeUtils {
public static final String THEME_MODE_PREFER = "prefer"; public static final String THEME_NIGHT = "night";
public static final String THEME_MODE_ONLY = "only"; public static final String THEME_DAY = "day";
public static final String THEME_FLAVOR_NIGHT = "night"; public static final String THEME_AUTO = "auto";
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 Drawable getDrawable(Context context, @AttrRes int attribute, public static Drawable getDrawable(Context context, @AttrRes int attribute,
@DrawableRes int fallbackDrawable) { @DrawableRes int fallbackDrawable) {
@ -94,20 +91,19 @@ public class ThemeUtils {
drawable.setColorFilter(getColor(context, attribute), PorterDuff.Mode.SRC_IN); drawable.setColorFilter(getColor(context, attribute), PorterDuff.Mode.SRC_IN);
} }
public static boolean setAppNightMode(String flavor) { public static void setAppNightMode(String flavor) {
int mode; int mode;
switch (flavor) { switch (flavor) {
case THEME_FLAVOR_AUTO: default:
mode = UiModeManager.MODE_NIGHT_AUTO; case THEME_NIGHT:
break;
case THEME_FLAVOR_NIGHT:
mode = UiModeManager.MODE_NIGHT_YES; mode = UiModeManager.MODE_NIGHT_YES;
break; break;
case THEME_FLAVOR_DAY: case THEME_DAY:
mode = UiModeManager.MODE_NIGHT_NO; mode = UiModeManager.MODE_NIGHT_NO;
break; break;
default: case THEME_AUTO:
return false; mode = UiModeManager.MODE_NIGHT_AUTO;
break;
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
@ -116,6 +112,5 @@ public class ThemeUtils {
AppCompatDelegate.setDefaultNightMode(mode); AppCompatDelegate.setDefaultNightMode(mode);
} }
return true;
} }
} }

View file

@ -185,14 +185,6 @@
<string name="pref_title_app_theme">Motyw</string> <string name="pref_title_app_theme">Motyw</string>
<string name="pref_title_app_theme_flavor">Odmiana motywu</string>
<string-array name="app_theme_flavor_names">
<item>Pora dnia</item>
<item>Noc</item>
<item>Dzień</item>
</string-array>
<string name="pref_title_browser_settings">Przeglądarka</string> <string name="pref_title_browser_settings">Przeglądarka</string>
<string name="pref_title_custom_tabs">Używaj niestandardowych kart Chrome</string> <string name="pref_title_custom_tabs">Używaj niestandardowych kart Chrome</string>
<string name="pref_title_hide_follow_button">Ukryj przycisk śledzenia podczas przewijania</string> <string name="pref_title_hide_follow_button">Ukryj przycisk śledzenia podczas przewijania</string>

View file

@ -7,10 +7,4 @@
<item name="android:navigationBarDividerColor">@color/status_divider_light</item> <item name="android:navigationBarDividerColor">@color/status_divider_light</item>
</style> </style>
<style name="Remin" parent="ReminBase">
<item name="android:windowLightNavigationBar">true</item>
<item name="android:navigationBarColor">@color/color_background_light</item>
<item name="android:navigationBarDividerColor">@color/status_divider_light</item>
</style>
</resources> </resources>

View file

@ -31,13 +31,9 @@
</string-array> </string-array>
<string-array name="app_theme_values"> <string-array name="app_theme_values">
<item>AppTheme:prefer:night</item>
<item>Remin:only:day</item>
</string-array>
<string-array name="app_theme_flavor_values">
<item>auto</item>
<item>night</item> <item>night</item>
<item>day</item> <item>day</item>
<item>auto</item>
</string-array> </string-array>
</resources> </resources>

View file

@ -172,15 +172,9 @@
<string name="pref_title_app_theme">App Theme</string> <string name="pref_title_app_theme">App Theme</string>
<string-array name="app_theme_names"> <string-array name="app_theme_names">
<item>Tusky</item> <item>Dark</item>
<item>Remin</item> <item>Light</item>
</string-array> <item>Automatic at sunset</item>
<string name="pref_title_app_theme_flavor">Theme flavor</string>
<string-array name="app_theme_flavor_names">
<item>Sunset/Sunrise</item>
<item>Night</item>
<item>Day</item>
</string-array> </string-array>
<string name="pref_title_browser_settings">Browser</string> <string name="pref_title_browser_settings">Browser</string>

View file

@ -105,14 +105,6 @@
</style> </style>
<style name="Remin" parent="ReminBase"/>
<style name="ReminBase" parent="AppThemeBase">
<item name="colorAccent">#e5ac00</item>
<item name="colorButtonNormal">#e8c14e</item>
<item name="tab_icon_selected_tint">#e2b62f</item>
<item name="compose_hide_media_button_selected_color">#f4c842</item>
</style>
<style name="AppTheme.ImageButton.Light" parent="Widget.AppCompat.Button.Borderless.Colored"> <style name="AppTheme.ImageButton.Light" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="android:tint">@color/image_button_light</item> <item name="android:tint">@color/image_button_light</item>
</style> </style>

View file

@ -12,13 +12,6 @@
android:summary="%s" android:summary="%s"
android:title="@string/pref_title_app_theme" /> android:title="@string/pref_title_app_theme" />
<ListPreference
android:entries="@array/app_theme_flavor_names"
android:entryValues="@array/app_theme_flavor_values"
android:key="appThemeFlavor"
android:summary="%s"
android:title="@string/pref_title_app_theme_flavor" />
<ListPreference <ListPreference
android:defaultValue="medium" android:defaultValue="medium"
android:entries="@array/status_text_size_names" android:entries="@array/status_text_size_names"