Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Badge threshold #859

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public void onTabReSelected(@IdRes int tabId) {
});

BottomBarTab nearby = bottomBar.getTabWithId(R.id.tab_nearby);
nearby.setBadgeCount(5);
nearby.setBadgeCount(900);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.test.annotation.UiThreadTest;
import android.support.test.filters.LargeTest;
import android.support.test.runner.AndroidJUnit4;
import android.text.TextUtils;

import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -33,6 +34,7 @@ public void setUp() {
bottomBar.setItems(com.roughike.bottombar.test.R.xml.dummy_tabs_three);
nearby = bottomBar.getTabWithId(com.roughike.bottombar.test.R.id.tab_nearby);
nearby.setBadgeCount(5);
nearby.setBadgeThreshold(20);
}

@Test
Expand Down Expand Up @@ -95,4 +97,19 @@ public void badgeRemovedProperly() {
assertNull(nearby.badge);
assertEquals(bottomBar.findViewById(R.id.bb_bottom_bar_item_container), nearby.getOuterView());
}

@Test
@UiThreadTest
public void badgeWithCounterBiggerThanThreshold(){
nearby.setBadgeCount(25);
assertTrue(TextUtils.equals(nearby.badge.getText(), "20+"));
}

@Test
@UiThreadTest
public void badgeWithCounterSmallerThanThreshold(){
nearby.setBadgeCount(2);
assertTrue(TextUtils.equals(nearby.badge.getText(), "2"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie
private boolean showShadow;
private float shadowElevation;
private View shadowView;
private int badgeThreshold;

private View backgroundOverlay;
private ViewGroup outerContainer;
Expand Down Expand Up @@ -212,6 +213,7 @@ private void populateAttributes(Context context, AttributeSet attrs, int defStyl
titleTextAppearance = ta.getResourceId(R.styleable.BottomBar_bb_titleTextAppearance, 0);
titleTypeFace = getTypeFaceFromAsset(ta.getString(R.styleable.BottomBar_bb_titleTypeFace));
showShadow = ta.getBoolean(R.styleable.BottomBar_bb_showShadow, true);
badgeThreshold = ta.getInt(R.styleable.BottomBar_bb_badgeThreshold, 99);
} finally {
ta.recycle();
}
Expand Down Expand Up @@ -321,6 +323,7 @@ private BottomBarTab.Config getTabConfig() {
.hideBadgeWhenSelected(hideBadgeWhenActive)
.titleTextAppearance(titleTextAppearance)
.titleTypeFace(titleTypeFace)
.setBadgeThreshold(badgeThreshold)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import android.graphics.drawable.ShapeDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.annotation.VisibleForTesting;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
Expand All @@ -29,12 +32,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class BottomBarBadge extends TextView {
class BottomBarBadge extends AppCompatTextView {

private int count;
private boolean isVisible = false;
private int countThreshold;
String countThresholdString;

BottomBarBadge(Context context) {
super(context);
countThresholdString = context.getString(R.string.badge_threshold);
}

public void setCountThreshold(int countThreshold) {
this.countThreshold = countThreshold;
}

/**
Expand All @@ -44,7 +55,12 @@ class BottomBarBadge extends TextView {
*/
void setCount(int count) {
this.count = count;
setText(String.valueOf(count));

if(count > countThreshold){
setText(String.format(countThresholdString, countThreshold));
}else{
setText(String.valueOf(count));
}
}

/**
Expand Down
23 changes: 23 additions & 0 deletions bottom-bar/src/main/java/com/roughike/bottombar/BottomBarTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class BottomBarTab extends LinearLayout {
private int indexInContainer;
private int titleTextAppearanceResId;
private Typeface titleTypeFace;
private int badgeThreshold;

BottomBarTab(Context context) {
super(context);
Expand All @@ -87,6 +88,7 @@ void setConfig(@NonNull Config config) {
setBadgeHidesWhenActive(config.badgeHidesWhenSelected);
setTitleTextAppearance(config.titleTextAppearance);
setTitleTypeface(config.titleTypeFace);
setBadgeThreshold(config.badgeThreshold);
}

void prepareLayout() {
Expand Down Expand Up @@ -329,6 +331,7 @@ public void setBadgeCount(int count) {
if (badge == null) {
badge = new BottomBarBadge(getContext());
badge.attachToTab(this, badgeBackgroundColor);
badge.setCountThreshold(badgeThreshold);
}

badge.setCount(count);
Expand Down Expand Up @@ -381,6 +384,18 @@ public Typeface getTitleTypeFace() {
return titleTypeFace;
}

public int getBadgeThreshold() {
return badgeThreshold;
}

public void setBadgeThreshold(int badgeThreshold) {
this.badgeThreshold = badgeThreshold;

if(badge != null){
badge.setCountThreshold(badgeThreshold);
}
}

void select(boolean animate) {
isActive = true;

Expand Down Expand Up @@ -650,6 +665,7 @@ public static class Config {
private final int titleTextAppearance;
private final Typeface titleTypeFace;
private boolean badgeHidesWhenSelected = true;
private final int badgeThreshold;

private Config(Builder builder) {
this.inActiveTabAlpha = builder.inActiveTabAlpha;
Expand All @@ -661,6 +677,7 @@ private Config(Builder builder) {
this.badgeHidesWhenSelected = builder.hidesBadgeWhenSelected;
this.titleTextAppearance = builder.titleTextAppearance;
this.titleTypeFace = builder.titleTypeFace;
this.badgeThreshold = builder.badgeThreshold;
}

public static class Builder {
Expand All @@ -673,6 +690,7 @@ public static class Builder {
private boolean hidesBadgeWhenSelected = true;
private int titleTextAppearance;
private Typeface titleTypeFace;
private int badgeThreshold;

public Builder inActiveTabAlpha(float alpha) {
this.inActiveTabAlpha = alpha;
Expand Down Expand Up @@ -719,6 +737,11 @@ public Builder titleTypeFace(Typeface titleTypeFace) {
return this;
}

public Builder setBadgeThreshold(int badgeThreshold) {
this.badgeThreshold = badgeThreshold;
return this;
}

public Config build() {
return new Config(this);
}
Expand Down
1 change: 1 addition & 0 deletions bottom-bar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
<attr name="bb_titleTextAppearance" format="reference" />
<attr name="bb_titleTypeFace" format="string" />
<attr name="bb_showShadow" format="boolean" />
<attr name="bb_badgeThreshold" format="integer" />
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions bottom-bar/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">BottomBar</string>
<string name="badge_threshold">%1$d+</string>
</resources>