Skip to content

Commit ae3d2df

Browse files
loading-googlematerial-automation
authored andcommitted
[MDCBottomNavigationBar] Add checks for nil when a new appearance is set and provide defaults. Based on failures observed in testing when removing deprecated badge properties, these values are nil by neglect and not nil by intent. Hence we will provide default values for badge appearance properties when they are nil and set on the nav bar.
PiperOrigin-RevId: 719333484
1 parent b5bbb66 commit ae3d2df

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

components/Badges/tests/unit/MDCBadgeViewTests.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
#import "MDCBadgeView.h"
1919

20+
NS_ASSUME_NONNULL_BEGIN
21+
2022
@interface MDCBadgeViewTests : XCTestCase
21-
@property(nonatomic, strong) MDCBadgeView *badge;
23+
@property(nonatomic, strong, nullable) MDCBadgeView *badge;
2224
@end
2325

2426
@implementation MDCBadgeViewTests
@@ -109,3 +111,5 @@ - (void)testLayoutSubviewsWontAdjustFrame {
109111
}
110112

111113
@end
114+
115+
NS_ASSUME_NONNULL_END

components/BottomNavigation/src/MDCBottomNavigationBar.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ typedef NS_ENUM(NSInteger, MDCBottomNavigationBarAlignment) {
127127
An array of MDCBottomNavigationBarItems that is used to populate bottom navigation bar content. It
128128
is strongly recommended the array contain at least three items and no more than five items --
129129
appearance may degrade outside of this range.
130+
131+
If a given item has no badge appearance set, then the itemBadgeAppearance is used.
132+
Additionally, if a given badge appearance has a `nil` for its textColor and/or font, then the
133+
values from the itemBadgeAppearance are used.
130134
*/
131135
@property(nonatomic, copy, nonnull) NSArray<MDCBottomNavigationBarItem *> *barItems;
132136

@@ -350,6 +354,10 @@ traitCollectionDidChange:. The block is called after the call to the superclass.
350354
If a given UITabBarItem has set a non-nil badgeColor, then that value will be used for that item
351355
view's badge instead of the backgroundColor associated with this appearance object.
352356
357+
If a given badge appearance has set a `nil` textColor, then the default `whiteColor` will be used.
358+
If a given badge appearance has set a `nil` font, then the default `systemFontOfSize:8` will be
359+
used.
360+
353361
Note that the individual itemBadge* properties will be deprecated and already act as proxies for
354362
modifying the itemBadgeAppearance of each badge directly.
355363
*/

components/BottomNavigation/src/MDCBottomNavigationBar.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,16 @@ - (void)updateBarItems {
824824
itemView.selectionIndicatorColor = self.selectionIndicatorColor;
825825
itemView.selectionIndicatorSize = self.selectionIndicatorSize;
826826
[self configureTitleStateForItemView:itemView];
827+
828+
// If a given badge appearance has a `nil` for its textColor and font, then the values from the
829+
// itemBadgeAppearance are used.
830+
if (!self.barItems[i].badgeAppearance.textColor) {
831+
self.barItems[i].badgeAppearance.textColor = _itemBadgeAppearance.textColor;
832+
}
833+
if (!self.barItems[i].badgeAppearance.font) {
834+
self.barItems[i].badgeAppearance.font = _itemBadgeAppearance.font;
835+
}
836+
827837
[self configureItemView:itemView
828838
withItem:self.barItems[i].item
829839
appearance:self.barItems[i].badgeAppearance];
@@ -1279,6 +1289,16 @@ - (void)setRippleColor:(nullable UIColor *)rippleColor {
12791289
- (void)setItemBadgeAppearance:(MDCBadgeAppearance *)itemBadgeAppearance {
12801290
_itemBadgeAppearance = [itemBadgeAppearance copy];
12811291

1292+
// Setting default values if `nil` is received for the appearance properties.
1293+
// The default value for the textColor is `white` and the default value for the font is
1294+
// `systemFontOfSize:8`.
1295+
if (!_itemBadgeAppearance.textColor) {
1296+
_itemBadgeAppearance.textColor = [UIColor whiteColor];
1297+
}
1298+
if (!_itemBadgeAppearance.font) {
1299+
_itemBadgeAppearance.font = [UIFont systemFontOfSize:kBadgeFontSize];
1300+
}
1301+
12821302
for (NSUInteger i = 0; i < [self itemCount]; ++i) {
12831303
MDCBottomNavigationItemView *itemView = self.itemViews[i];
12841304
if (self.barItems.count > 0 && self.barItems[i].badgeAppearance != nil) {

components/BottomNavigation/tests/snapshot/MDCBottomNavigationBarSnapshotTests.m

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -475,26 +475,25 @@ - (void)testCustomBadgeColorsOverrideDefaultBadgeAppearanceWhenSetBeforeBarItems
475475
[self generateAndVerifySnapshot];
476476
}
477477

478-
- (void)testClearBadgeColorsRendersClearBackgroundAndUILabelDefaultTextColor {
478+
- (void)testClearBadgeColorRendersClearBackgroundAndDefaultFont {
479479
// Given
480480
self.tabItem1.badgeValue = @"";
481481
self.tabItem2.badgeValue = @"Black on Clear";
482-
self.tabItem3.badgeValue = @"Black on Green";
483482
self.navigationBar.frame = CGRectMake(0, 0, MDCBottomNavigationBarTestWidthiPad,
484483
MDCBottomNavigationBarTestHeightTypical);
485484

486485
// When
487-
MDCBadgeAppearance *greenAppearance = [[MDCBadgeAppearance alloc] init];
488-
greenAppearance.font = [UIFont systemFontOfSize:8.0];
489-
greenAppearance.backgroundColor = UIColor.greenColor;
490-
greenAppearance.textColor = UIColor.blackColor;
486+
MDCBadgeAppearance *normalAppearance = [[MDCBadgeAppearance alloc] init];
487+
normalAppearance.font = nil;
488+
normalAppearance.backgroundColor = UIColor.clearColor;
489+
normalAppearance.textColor = UIColor.blackColor;
491490
MDCBottomNavigationBarItem *barItem1 =
492491
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem1];
493492
MDCBottomNavigationBarItem *barItem2 =
494-
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem2];
493+
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem2
494+
badgeAppearance:normalAppearance];
495495
MDCBottomNavigationBarItem *barItem3 =
496-
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem3
497-
badgeAppearance:greenAppearance];
496+
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem3];
498497
MDCBottomNavigationBarItem *barItem4 =
499498
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem4];
500499
MDCBottomNavigationBarItem *barItem5 =
@@ -510,10 +509,10 @@ - (void)testClearBadgeColorsRendersClearBackgroundAndUILabelDefaultTextColor {
510509
[self generateAndVerifySnapshot];
511510
}
512511

513-
- (void)testNilBadgeColorsRendersTintBackgroundAndUILabelDefaultTextColor {
512+
- (void)testNilBadgeColorsRendersTintBackgroundAndNavigationBarDefaultTextColor {
514513
// Given
515514
self.tabItem1.badgeValue = @"";
516-
self.tabItem2.badgeValue = @"Black on Tint Color";
515+
self.tabItem2.badgeValue = @"White on Tint Color";
517516
self.tabItem3.badgeValue = @"Black on Green";
518517
self.navigationBar.frame = CGRectMake(0, 0, MDCBottomNavigationBarTestWidthiPad,
519518
MDCBottomNavigationBarTestHeightTypical);
@@ -545,6 +544,24 @@ - (void)testNilBadgeColorsRendersTintBackgroundAndUILabelDefaultTextColor {
545544
[self generateAndVerifySnapshot];
546545
}
547546

547+
- (void)testDefaultBadgeTextFont {
548+
// Given
549+
self.tabItem1.badgeValue = @"";
550+
self.tabItem2.badgeValue = @"10";
551+
self.navigationBar.frame = CGRectMake(0, 0, MDCBottomNavigationBarTestWidthTypical,
552+
MDCBottomNavigationBarTestHeightTypical);
553+
554+
// When
555+
MDCBadgeAppearance *badgeAppearance = [[MDCBadgeAppearance alloc] init];
556+
badgeAppearance.font = nil;
557+
self.navigationBar.itemBadgeAppearance = badgeAppearance;
558+
self.navigationBar.items =
559+
@[ self.tabItem1, self.tabItem2, self.tabItem3, self.tabItem4, self.tabItem5 ];
560+
561+
// Then
562+
[self generateAndVerifySnapshot];
563+
}
564+
548565
- (void)testCustomBadgeTextFontSetBeforeItems {
549566
// Given
550567
self.tabItem1.badgeValue = @"";
@@ -563,7 +580,7 @@ - (void)testCustomBadgeTextFontSetBeforeItems {
563580
[self generateAndVerifySnapshot];
564581
}
565582

566-
- (void)testCustomBadgeTextFontSetAfterItems {
583+
- (void)testCustomBadgeTextFontSetAfterItemsUsesDefaultBadgeColor {
567584
// Given
568585
self.tabItem1.badgeValue = @"";
569586
self.tabItem2.badgeValue = @"10";

0 commit comments

Comments
 (0)