Skip to content

Commit

Permalink
Moved the Config class to BottomBarTab, since it's more logical.
Browse files Browse the repository at this point in the history
  • Loading branch information
roughike committed Aug 22, 2016
1 parent 9fffb99 commit b3b5126
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public void setItems(@XmlRes int xmlRes) {
throw new RuntimeException("No items specified for the BottomBar!");
}

TabParser.Config config = new TabParser.Config.Builder()
BottomBarTab.Config config = new BottomBarTab.Config.Builder()
.inActiveTabAlpha(inActiveTabAlpha)
.activeTabAlpha(activeTabAlpha)
.inActiveTabColor(inActiveTabColor)
Expand Down
128 changes: 114 additions & 14 deletions bottom-bar/src/main/java/com/roughike/bottombar/BottomBarTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.ColorInt;
import android.support.annotation.VisibleForTesting;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
Expand Down Expand Up @@ -77,23 +78,21 @@ enum Type {
sixteenDps = MiscUtils.dpToPixel(context, 16);
}

void setConfig(Config config) {
setInActiveAlpha(config.inActiveTabAlpha);
setActiveAlpha(config.activeTabAlpha);
setInActiveColor(config.inActiveTabColor);
setActiveColor(config.activeTabColor);
setBarColorWhenSelected(config.barColorWhenSelected);
setBadgeBackgroundColor(config.badgeBackgroundColor);
setTitleTextAppearance(config.titleTextAppearance);
titleTypeFace(config.titleTypeFace);
}

void prepareLayout() {
int layoutResource;

switch (type) {
case FIXED:
layoutResource = R.layout.bb_bottom_bar_item_fixed;
break;
case SHIFTING:
layoutResource = R.layout.bb_bottom_bar_item_shifting;
break;
case TABLET:
layoutResource = R.layout.bb_bottom_bar_item_fixed_tablet;
break;
default:
// should never happen
throw new RuntimeException("Unknown BottomBarTab type.");
}
layoutResource = getLayoutResource();

inflate(getContext(), layoutResource, this);
setOrientation(VERTICAL);
Expand All @@ -112,6 +111,26 @@ void prepareLayout() {
initCustomFont();
}

@VisibleForTesting
int getLayoutResource() {
int layoutResource;
switch (type) {
case FIXED:
layoutResource = R.layout.bb_bottom_bar_item_fixed;
break;
case SHIFTING:
layoutResource = R.layout.bb_bottom_bar_item_shifting;
break;
case TABLET:
layoutResource = R.layout.bb_bottom_bar_item_fixed_tablet;
break;
default:
// should never happen
throw new RuntimeException("Unknown BottomBarTab type.");
}
return layoutResource;
}

@SuppressWarnings("deprecation")
private void initCustomTextAppearance() {
if (type == Type.TABLET || titleTextAppearanceResId == 0) {
Expand Down Expand Up @@ -468,4 +487,85 @@ public void onRestoreInstanceState(Parcelable state) {
}
super.onRestoreInstanceState(state);
}

static class Config {
private final float inActiveTabAlpha;
private final float activeTabAlpha;
private final int inActiveTabColor;
private final int activeTabColor;
private final int barColorWhenSelected;
private final int badgeBackgroundColor;
private final int titleTextAppearance;
private final Typeface titleTypeFace;

private Config(Builder builder) {
this.inActiveTabAlpha = builder.inActiveTabAlpha;
this.activeTabAlpha = builder.activeTabAlpha;
this.inActiveTabColor = builder.inActiveTabColor;
this.activeTabColor = builder.activeTabColor;
this.barColorWhenSelected = builder.barColorWhenSelected;
this.badgeBackgroundColor = builder.badgeBackgroundColor;
this.titleTextAppearance = builder.titleTextAppearance;
this.titleTypeFace = builder.titleTypeFace;
}

static class Builder {
private float inActiveTabAlpha;
private float activeTabAlpha;
private int inActiveTabColor;
private int activeTabColor;
private int barColorWhenSelected;
private int badgeBackgroundColor;
private int titleTextAppearance;
private Typeface titleTypeFace;

Builder inActiveTabAlpha(float alpha) {
this.inActiveTabAlpha = alpha;
return this;
}

Builder activeTabAlpha(float alpha) {
this.activeTabAlpha = alpha;
return this;
}

Builder inActiveTabColor(@ColorInt int color) {
this.inActiveTabColor = color;
return this;
}

Builder activeTabColor(@ColorInt int color) {
this.activeTabColor = color;
return this;
}

Builder barColorWhenSelected(@ColorInt int color) {
this.barColorWhenSelected = color;
return this;
}

Builder badgeBackgroundColor(@ColorInt int color) {
this.badgeBackgroundColor = color;
return this;
}

Builder titleTextAppearance(int titleTextAppearance) {
this.titleTextAppearance = titleTextAppearance;
return this;
}

Builder titleTypeFace(Context context, String titleTypeFace) {
if (titleTypeFace != null) {
this.titleTypeFace = Typeface.createFromAsset(
context.getAssets(), titleTypeFace);
}

return this;
}

Config build() {
return new Config(this);
}
}
}
}
95 changes: 3 additions & 92 deletions bottom-bar/src/main/java/com/roughike/bottombar/TabParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.annotation.ColorInt;
import android.support.annotation.XmlRes;
import android.support.v4.content.ContextCompat;

Expand All @@ -19,13 +17,13 @@
*/
class TabParser {
private final Context context;
private final Config config;
private final BottomBarTab.Config config;
private final XmlResourceParser parser;

private ArrayList<BottomBarTab> tabs;
private BottomBarTab workingTab;

TabParser(Context context, Config config, @XmlRes int tabsXmlResId) {
TabParser(Context context, BottomBarTab.Config config, @XmlRes int tabsXmlResId) {
this.context = context;
this.config = config;

Expand Down Expand Up @@ -113,14 +111,7 @@ private void parseNewTab(XmlResourceParser parser) {

private BottomBarTab tabWithDefaults() {
BottomBarTab tab = new BottomBarTab(context);
tab.setInActiveAlpha(config.inActiveTabAlpha);
tab.setActiveAlpha(config.activeTabAlpha);
tab.setInActiveColor(config.inActiveTabColor);
tab.setActiveColor(config.activeTabColor);
tab.setBarColorWhenSelected(config.barColorWhenSelected);
tab.setBadgeBackgroundColor(config.badgeBackgroundColor);
tab.setTitleTextAppearance(config.titleTextAppearance);
tab.titleTypeFace(config.titleTypeFace);
tab.setConfig(config);

return tab;
}
Expand Down Expand Up @@ -153,84 +144,4 @@ List<BottomBarTab> getTabs() {
return tabs;
}

static class Config {
private final float inActiveTabAlpha;
private final float activeTabAlpha;
private final int inActiveTabColor;
private final int activeTabColor;
private final int barColorWhenSelected;
private final int badgeBackgroundColor;
private final int titleTextAppearance;
private final Typeface titleTypeFace;

private Config(Builder builder) {
this.inActiveTabAlpha = builder.inActiveTabAlpha;
this.activeTabAlpha = builder.activeTabAlpha;
this.inActiveTabColor = builder.inActiveTabColor;
this.activeTabColor = builder.activeTabColor;
this.barColorWhenSelected = builder.barColorWhenSelected;
this.badgeBackgroundColor = builder.badgeBackgroundColor;
this.titleTextAppearance = builder.titleTextAppearance;
this.titleTypeFace = builder.titleTypeFace;
}

static class Builder {
private float inActiveTabAlpha;
private float activeTabAlpha;
private int inActiveTabColor;
private int activeTabColor;
private int barColorWhenSelected;
private int badgeBackgroundColor;
private int titleTextAppearance;
private Typeface titleTypeFace;

Builder inActiveTabAlpha(float alpha) {
this.inActiveTabAlpha = alpha;
return this;
}

Builder activeTabAlpha(float alpha) {
this.activeTabAlpha = alpha;
return this;
}

Builder inActiveTabColor(@ColorInt int color) {
this.inActiveTabColor = color;
return this;
}

Builder activeTabColor(@ColorInt int color) {
this.activeTabColor = color;
return this;
}

Builder barColorWhenSelected(@ColorInt int color) {
this.barColorWhenSelected = color;
return this;
}

Builder badgeBackgroundColor(@ColorInt int color) {
this.badgeBackgroundColor = color;
return this;
}

Builder titleTextAppearance(int titleTextAppearance) {
this.titleTextAppearance = titleTextAppearance;
return this;
}

Builder titleTypeFace(Context context, String titleTypeFace) {
if (titleTypeFace != null) {
this.titleTypeFace = Typeface.createFromAsset(
context.getAssets(), titleTypeFace);
}

return this;
}

Config build() {
return new Config(this);
}
}
}
}

0 comments on commit b3b5126

Please sign in to comment.