Add changing of the toolbar color

filtering
Floens 10 years ago
parent 915faf731b
commit a0191c0203
  1. 35
      Clover/app/src/main/java/org/floens/chan/core/settings/ChanSettings.java
  2. 99
      Clover/app/src/main/java/org/floens/chan/ui/controller/ThemeSettingsController.java
  3. 2
      Clover/app/src/main/java/org/floens/chan/ui/theme/Theme.java
  4. 91
      Clover/app/src/main/java/org/floens/chan/ui/theme/ThemeHelper.java
  5. 22
      Clover/app/src/main/res/layout/controller_theme.xml
  6. 1
      Clover/app/src/main/res/values/strings.xml

@ -27,7 +27,7 @@ import org.floens.chan.utils.AndroidUtils;
import java.io.File;
public class ChanSettings {
public static final StringSetting theme;
private static final StringSetting theme;
public static final StringSetting fontSize;
public static final BooleanSetting openLinkConfirmation;
public static final BooleanSetting autoRefreshThread;
@ -126,4 +126,37 @@ public class ChanSettings {
public static boolean passLoggedIn() {
return passId.get().length() > 0;
}
public static ThemeColor getThemeAndColor() {
String themeRaw = ChanSettings.theme.get();
String theme = themeRaw;
String color = null;
String[] splitted = themeRaw.split(",");
if (splitted.length == 2) {
theme = splitted[0];
color = splitted[1];
}
return new ThemeColor(theme, color);
}
public static void setThemeAndColor(ThemeColor themeColor) {
if (themeColor.color != null) {
ChanSettings.theme.set(themeColor.theme + "," + themeColor.color);
} else {
ChanSettings.theme.set(themeColor.theme);
}
}
public static class ThemeColor {
public String theme;
public String color;
public ThemeColor(String theme, String color) {
this.theme = theme;
this.color = color;
}
}
}

@ -17,14 +17,19 @@
*/
package org.floens.chan.ui.controller;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPager;
import android.support.v7.internal.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.floens.chan.R;
import org.floens.chan.chan.ChanParser;
@ -39,13 +44,17 @@ import org.floens.chan.ui.theme.Theme;
import org.floens.chan.ui.theme.ThemeHelper;
import org.floens.chan.ui.toolbar.NavigationItem;
import org.floens.chan.ui.toolbar.Toolbar;
import org.floens.chan.ui.view.FloatingMenu;
import org.floens.chan.ui.view.FloatingMenuItem;
import org.floens.chan.ui.view.ThumbnailView;
import org.floens.chan.ui.view.ViewPagerAdapter;
import org.floens.chan.utils.AndroidUtils;
import org.floens.chan.utils.Time;
import java.util.ArrayList;
import java.util.List;
import static org.floens.chan.utils.AndroidUtils.dp;
import static org.floens.chan.utils.AndroidUtils.getAttrColor;
public class ThemeSettingsController extends Controller implements View.OnClickListener {
@ -55,6 +64,8 @@ public class ThemeSettingsController extends Controller implements View.OnClickL
private Adapter adapter;
private ThemeHelper themeHelper;
private List<ThemeHelper.PrimaryColor> colors = new ArrayList<>();
private PostCell.PostCellCallback DUMMY_POST_CALLBACK;
private Toolbar.ToolbarCallback DUMMY_TOOLBAR_CALLBACK;
@ -131,21 +142,25 @@ public class ThemeSettingsController extends Controller implements View.OnClickL
adapter = new Adapter();
pager.setAdapter(adapter);
String currentTheme = ChanSettings.theme.get();
ChanSettings.ThemeColor currentSettingsTheme = ChanSettings.getThemeAndColor();
for (int i = 0; i < themeHelper.getThemes().size(); i++) {
Theme theme = themeHelper.getThemes().get(i);
if (theme.name.equals(currentTheme)) {
ThemeHelper.PrimaryColor color = theme.primaryColor;
if (theme.name.equals(currentSettingsTheme.theme)) {
pager.setCurrentItem(i, false);
break;
if (currentSettingsTheme.color != null) {
color = themeHelper.getColor(currentSettingsTheme.color);
}
}
colors.add(color);
}
}
@Override
public void onClick(View v) {
if (v == done) {
Theme currentTheme = themeHelper.getThemes().get(pager.getCurrentItem());
ChanSettings.theme.set(currentTheme.name);
Theme theme = themeHelper.getThemes().get(pager.getCurrentItem());
themeHelper.changeTheme(theme, colors.get(pager.getCurrentItem()));
((StartActivity) context).restart();
}
}
@ -163,7 +178,7 @@ public class ThemeSettingsController extends Controller implements View.OnClickL
}
@Override
public View getView(int position, ViewGroup parent) {
public View getView(final int position, ViewGroup parent) {
Theme theme = themes.get(position);
Context themeContext = new ContextThemeWrapper(context, theme.resValue);
@ -188,14 +203,43 @@ public class ThemeSettingsController extends Controller implements View.OnClickL
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundColor(getAttrColor(themeContext, R.attr.backcolor));
Toolbar toolbar = new Toolbar(themeContext);
final Toolbar toolbar = new Toolbar(themeContext);
toolbar.setCallback(DUMMY_TOOLBAR_CALLBACK);
toolbar.setBackgroundColor(theme.primaryColor.color);
NavigationItem item = new NavigationItem();
final NavigationItem item = new NavigationItem();
item.title = theme.displayName;
item.hasBack = false;
toolbar.setNavigationItem(false, true, item);
toolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<FloatingMenuItem> items = new ArrayList<>();
for (ThemeHelper.PrimaryColor color : themeHelper.getColors()) {
items.add(new FloatingMenuItem(color, color.displayName));
}
FloatingMenu menu = new FloatingMenu(context);
menu.setItems(items);
menu.setAdapter(new ColorsAdapter(items));
menu.setAnchor(toolbar, Gravity.CENTER, 0, dp(5));
menu.setPopupWidth(toolbar.getWidth());
menu.setCallback(new FloatingMenu.FloatingMenuCallback() {
@Override
public void onFloatingMenuItemClicked(FloatingMenu menu, FloatingMenuItem item) {
ThemeHelper.PrimaryColor primaryColor = (ThemeHelper.PrimaryColor) item.getId();
colors.set(position, primaryColor);
toolbar.setBackgroundColor(primaryColor.color);
}
@Override
public void onFloatingMenuDismissed(FloatingMenu menu) {
}
});
menu.show();
}
});
linearLayout.addView(toolbar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
themeContext.getResources().getDimensionPixelSize(R.dimen.toolbar_height)));
@ -211,4 +255,43 @@ public class ThemeSettingsController extends Controller implements View.OnClickL
return themes.size();
}
}
private class ColorsAdapter extends BaseAdapter {
private List<FloatingMenuItem> items;
public ColorsAdapter(List<FloatingMenuItem> items) {
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
@SuppressLint("ViewHolder")
TextView textView = (TextView) LayoutInflater.from(context).inflate(R.layout.toolbar_menu_item, parent, false);
textView.setText(getItem(position));
textView.setTypeface(AndroidUtils.ROBOTO_MEDIUM);
ThemeHelper.PrimaryColor color = (ThemeHelper.PrimaryColor) items.get(position).getId();
textView.setBackgroundColor(color.color);
boolean lightColor = (Color.red(color.color) * 0.299f) + (Color.green(color.color) * 0.587f) + (Color.blue(color.color) * 0.114f) > 125f;
textView.setTextColor(lightColor ? 0xff000000 : 0xffffffff);
return textView;
}
@Override
public int getCount() {
return items.size();
}
@Override
public String getItem(int position) {
return items.get(position).getText();
}
@Override
public long getItemId(int position) {
return position;
}
}
}

@ -35,8 +35,8 @@ public class Theme {
public final String displayName;
public final String name;
public final int resValue;
public final ThemeHelper.PrimaryColor primaryColor;
public boolean isLightTheme = true;
public ThemeHelper.PrimaryColor primaryColor;
public int quoteColor;
public int highlightQuoteColor;

@ -26,6 +26,7 @@ import org.floens.chan.core.settings.ChanSettings;
import org.floens.chan.utils.Logger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ThemeHelper {
@ -53,17 +54,30 @@ public class ThemeHelper {
themes.add(new Theme("Yotsuba B", "yotsuba_b", R.style.Chan_Theme_YotsubaB, PrimaryColor.RED));
themes.add(new Theme("Photon", "photon", R.style.Chan_Theme_Photon, PrimaryColor.ORANGE));
themes.add(new DarkTheme("Tomorrow", "tomorrow", R.style.Chan_Theme_Tomorrow, PrimaryColor.DARK));
ChanSettings.ThemeColor settingTheme = ChanSettings.getThemeAndColor();
if (settingTheme.color != null) {
for (Theme theme : themes) {
if (theme.name.equals(settingTheme.theme)) {
theme.primaryColor = getColor(settingTheme.color);
break;
}
}
}
updateCurrentTheme();
}
public List<Theme> getThemes() {
return themes;
public void changeTheme(Theme theme, PrimaryColor primaryColor) {
ChanSettings.setThemeAndColor(new ChanSettings.ThemeColor(theme.name, primaryColor.name));
theme.primaryColor = primaryColor;
updateCurrentTheme();
}
public void updateCurrentTheme() {
String settingTheme = ChanSettings.theme.get();
ChanSettings.ThemeColor settingTheme = ChanSettings.getThemeAndColor();
for (Theme theme : themes) {
if (theme.name.equals(settingTheme)) {
if (theme.name.equals(settingTheme.theme)) {
this.theme = theme;
return;
}
@ -77,6 +91,10 @@ public class ThemeHelper {
return theme;
}
public List<Theme> getThemes() {
return themes;
}
public void setupContext(Activity context) {
updateCurrentTheme();
context.setTheme(theme.resValue);
@ -88,35 +106,52 @@ public class ThemeHelper {
}
}
public enum PrimaryColor {
RED("red", 0xFFF44336, 0xFFD32F2F),
PINK("pink", 0xFFE91E63, 0xFFC2185B),
PURPLE("purple", 0xFF9C27B0, 0xFF7B1FA2),
DEEP_PURPLE("deep purple", 0xFF673AB7, 0xFF512DA8),
INDIGO("indigo", 0xFF3F51B5, 0xFF303F9F),
BLUE("blue", 0xFF2196F3, 0xFF1976D2),
LIGHT_BLUE("light blue", 0xFF03A9F4, 0xFF0288D1),
CYAN("cyan", 0xFF00BCD4, 0xFF0097A7),
TEAL("teal", 0xFF009688, 0xFF00796B),
GREEN("green", 0xFF4CAF50, 0xFF388E3C),
LIGHT_GREEN("light green", 0xFF8BC34A, 0xFF689F38),
LIME("lime", 0xFFCDDC39, 0xFFAFB42B),
YELLOW("yellow", 0xFFFFEB3B, 0xFFFBC02D),
AMBER("amber", 0xFFFFC107, 0xFFFFA000),
ORANGE("orange", 0xFFFF9800, 0xFFF57C00),
DEEP_ORANGE("deep orange", 0xFFFF5722, 0xFFE64A19),
BROWN("brown", 0xFF795548, 0xFF5D4037),
GREY("grey", 0xFF9E9E9E, 0xFF616161),
BLUE_GREY("blue grey", 0xFF607D8B, 0xFF455A64),
DARK("dark", 0xff212121, 0xff000000),
BLACK("black", 0xff000000, 0xff000000);
public PrimaryColor getColor(String name) {
for (PrimaryColor primaryColor : PrimaryColor.values()) {
if (primaryColor.name.equals(name)) {
return primaryColor;
}
}
Logger.e(TAG, "No color found for setting " + name + ", using a default color");
return PrimaryColor.BLACK;
}
public List<PrimaryColor> getColors() {
return Arrays.asList(PrimaryColor.values());
}
public enum PrimaryColor {
RED("Red", "red", 0xFFF44336, 0xFFD32F2F),
PINK("Pink", "pink", 0xFFE91E63, 0xFFC2185B),
PURPLE("Purple", "purple", 0xFF9C27B0, 0xFF7B1FA2),
DEEP_PURPLE("Deep purple", "deep_purple", 0xFF673AB7, 0xFF512DA8),
INDIGO("Indigo", "indigo", 0xFF3F51B5, 0xFF303F9F),
BLUE("Blue", "blue", 0xFF2196F3, 0xFF1976D2),
LIGHT_BLUE("Light blue", "light_blue", 0xFF03A9F4, 0xFF0288D1),
CYAN("Cyan", "cyan", 0xFF00BCD4, 0xFF0097A7),
TEAL("Teal", "teal", 0xFF009688, 0xFF00796B),
GREEN("Green", "green", 0xFF4CAF50, 0xFF388E3C),
LIGHT_GREEN("Light green", "light_green", 0xFF8BC34A, 0xFF689F38),
LIME("Lime", "lime", 0xFFCDDC39, 0xFFAFB42B),
YELLOW("Yellow", "yellow", 0xFFFFEB3B, 0xFFFBC02D),
AMBER("Amber", "amber", 0xFFFFC107, 0xFFFFA000),
ORANGE("Orange", "orange", 0xFFFF9800, 0xFFF57C00),
DEEP_ORANGE("Deep orange", "deep_orange", 0xFFFF5722, 0xFFE64A19),
BROWN("Brown", "brown", 0xFF795548, 0xFF5D4037),
GREY("Grey", "grey", 0xFF9E9E9E, 0xFF616161),
BLUE_GREY("Blue grey", "blue_grey", 0xFF607D8B, 0xFF455A64),
DARK("Dark", "dark", 0xff212121, 0xff000000),
BLACK("Black", "black", 0xff000000, 0xff000000);
public final String displayName;
public final String name;
public final int color;
public final int dark;
PrimaryColor(String name, int color, int dark) {
PrimaryColor(String displayName, String name, int color, int dark) {
this.displayName = displayName;
this.name = name;
this.color = color;
this.dark = dark;

@ -21,14 +21,28 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
android:background="?backcolor"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/setting_theme_explanation" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:clipToPadding="false"
android:paddingLeft="56dp"
android:paddingRight="56dp"
android:paddingBottom="88dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:paddingLeft="56dp"
android:paddingRight="56dp" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/done"

@ -170,6 +170,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<string name="settings_group_browsing">Browsing</string>
<string name="setting_theme">Theme</string>
<string name="setting_theme_explanation">Swipe to change the theme.\nTap the toolbar to change its color.</string>
<string name="setting_font_size">Font size</string>
<string name="setting_font_size_default">(default)</string>
<string name="setting_open_link_confirmation">Ask before opening links</string>

Loading…
Cancel
Save