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
* 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")) {

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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;
}
}

View File

@ -185,14 +185,6 @@
<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_custom_tabs">Używaj niestandardowych kart Chrome</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>
</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>

View File

@ -31,13 +31,9 @@
</string-array>
<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>day</item>
<item>auto</item>
</string-array>
</resources>

View File

@ -172,15 +172,9 @@
<string name="pref_title_app_theme">App Theme</string>
<string-array name="app_theme_names">
<item>Tusky</item>
<item>Remin</item>
</string-array>
<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>
<item>Dark</item>
<item>Light</item>
<item>Automatic at sunset</item>
</string-array>
<string name="pref_title_browser_settings">Browser</string>

View File

@ -105,14 +105,6 @@
</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">
<item name="android:tint">@color/image_button_light</item>
</style>

View File

@ -12,13 +12,6 @@
android:summary="%s"
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
android:defaultValue="medium"
android:entries="@array/status_text_size_names"