Skip to content

Commit

Permalink
[Bottom Navigation] Remove deprecated badge properties and update tes…
Browse files Browse the repository at this point in the history
…ts to reflect new behavior.

PiperOrigin-RevId: 702506989
  • Loading branch information
Janette James authored and material-automation committed Dec 4, 2024
1 parent 3694092 commit decc506
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 120 deletions.
39 changes: 4 additions & 35 deletions components/BottomNavigation/src/MDCBottomNavigationBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#import "MaterialShadow.h"
#import "MaterialShadowElevations.h"

NS_ASSUME_NONNULL_BEGIN

@protocol MDCBottomNavigationBarDelegate;

/** States used to configure bottom navigation on when to show item titles. */
Expand Down Expand Up @@ -353,21 +355,6 @@ traitCollectionDidChange:. The block is called after the call to the superclass.
*/
@property(nonatomic, copy, nonnull) MDCBadgeAppearance *itemBadgeAppearance;

/**
Default background color for badges.
If a given UITabBarItem's `badgeColor` is non-nil, then the item's `badgeColor` is used instead of
this value.
This property is a proxy for itemBadgeAppearance.backgroundColor, with the exception that assigning
nil will be treated as [UIColor clearColor].
Default is a red color.
*/
@property(nonatomic, copy, nullable)
UIColor *itemBadgeBackgroundColor API_DEPRECATED_WITH_REPLACEMENT(
"itemBadgeAppearance.backgroundColor", ios(12, 12));

/**
X-offset for Badge position.
Expand All @@ -381,26 +368,6 @@ traitCollectionDidChange:. The block is called after the call to the superclass.
*/
@property(nonatomic, assign) CGFloat itemBadgeHorizontalOffset;

/**
Text color for badges.
This property is a proxy for itemBadgeAppearance.textColor.
Default is white.
*/
@property(nonatomic, copy, nullable) UIColor *itemBadgeTextColor API_DEPRECATED_WITH_REPLACEMENT(
"itemBadgeAppearance.textColor", ios(12, 12));

/**
Text font for badges.
This property is a proxy for itemBadgeAppearance.font.
Default is a small system font.
*/
@property(nonatomic, copy, nullable) UIFont *itemBadgeTextFont API_DEPRECATED_WITH_REPLACEMENT(
"itemBadgeAppearance.font", ios(12, 12));

@end

#if MDC_AVAILABLE_SDK_IOS(13_0) && !TARGET_OS_TV
Expand All @@ -424,3 +391,5 @@ API_UNAVAILABLE(tvos, watchos)
UILargeContentViewerInteractionDelegate>
@end
#endif // MDC_AVAILABLE_SDK_IOS(13_0)

NS_ASSUME_NONNULL_END
40 changes: 4 additions & 36 deletions components/BottomNavigation/src/MDCBottomNavigationBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ - (void)commonMDCBottomNavigationBarInit {
_rippleEnabled = YES;
_itemsAlignmentInVerticalMode = MDCNavigationBarItemsVerticalAlignmentCenter;
_itemBadgeAppearance = [[MDCBadgeAppearance alloc] init];
_itemBadgeAppearance.textColor = UIColor.whiteColor;
_itemBadgeAppearance.font = [UIFont systemFontOfSize:kBadgeFontSize];
_itemBadgeAppearance.backgroundColor = MDCPalette.redPalette.tint700;

// TODO(featherless): Delete once everyone has migrated to itemBadgeAppearance.
_itemBadgeTextColor = UIColor.whiteColor;
_itemBadgeTextFont = [UIFont systemFontOfSize:kBadgeFontSize];
_itemBadgeBackgroundColor = MDCPalette.redPalette.tint700;
_itemsHorizontalPadding = kDefaultItemHorizontalPadding;
_showsSelectionIndicator = NO;
_selectionIndicatorColor = [UIColor colorWithRed:195.f / 255.f
Expand Down Expand Up @@ -914,6 +913,7 @@ - (void)setItems:(NSArray<UITabBarItem *> *)items {
itemView.selected = NO;
itemView.displayTitleInVerticalLayout = self.displayItemTitlesInVerticalLayout;
itemView.enableVerticalLayout = self.enableVerticalLayout;
itemView.badgeAppearance = _itemBadgeAppearance;

itemView.selectionIndicatorColor = self.selectionIndicatorColor;
itemView.selectionIndicatorSize = self.selectionIndicatorSize;
Expand Down Expand Up @@ -1305,38 +1305,6 @@ - (void)setItemBadgeHorizontalOffset:(CGFloat)itemBadgeHorizontalOffset {
}
}

// TODO(featherless): Delete once everyone has migrated to itemBadgeAppearance.
- (void)setItemBadgeBackgroundColor:(nullable UIColor *)itemBadgeBackgroundColor {
_itemBadgeBackgroundColor = itemBadgeBackgroundColor;

for (NSUInteger i = 0; i < self.items.count; ++i) {
UITabBarItem *item = self.items[i];
if (item.badgeColor) {
continue;
}
MDCBottomNavigationItemView *itemView = self.itemViews[i];
itemView.badgeColor = itemBadgeBackgroundColor;
}
}

// TODO(featherless): Delete once everyone has migrated to itemBadgeAppearance.
- (void)setItemBadgeTextColor:(nullable UIColor *)itemBadgeTextColor {
_itemBadgeTextColor = itemBadgeTextColor;

for (MDCBottomNavigationItemView *itemView in self.itemViews) {
itemView.badgeTextColor = itemBadgeTextColor;
}
}

// TODO(featherless): Delete once everyone has migrated to itemBadgeAppearance.
- (void)setItemBadgeTextFont:(nullable UIFont *)itemBadgeTextFont {
_itemBadgeTextFont = itemBadgeTextFont;

for (MDCBottomNavigationItemView *itemView in self.itemViews) {
itemView.badgeFont = itemBadgeTextFont;
}
}

// TODO(b/244765238): Remove branching layout logic after GM3 migrations
// Assume that setting itemBadgeHorizontalOffset to any value other than its default (0) should
// activate the new anchored/GM3 layout, and assume that anyone applying GM3 branding for Bottom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@

#import <UIKit/UIKit.h>

#import "MDCAvailability.h"
#import "MDCBottomNavigationBar.h"
#import "MDCBottomNavigationBar+ItemView.h"
#import "MDCBottomNavigationBar+Private.h"
#import "MDCBottomNavigationItemView.h"

NS_ASSUME_NONNULL_BEGIN

@implementation MDCBottomNavigationBar (ItemViewConfiguration)

#pragma mark - Public
Expand All @@ -37,21 +38,11 @@ - (void)configureAppearanceForItemView:(MDCBottomNavigationItemView *)itemView {
itemView.selectionIndicatorSize = self.selectionIndicatorSize;
itemView.showsSelectionIndicator = self.showsSelectionIndicator;

// TODO(b/234850214): Delete once everyone has migrated to itemBadgeAppearance.
[self configureBadgeForItemViewLegacy:itemView];

[self configureColorsForItemView:itemView];
[self configureMarginForItemView:itemView];
[self configureTitleForItemView:itemView];
}

// TODO(b/234850214): Delete once everyone has migrated to itemBadgeAppearance.
- (void)configureBadgeForItemViewLegacy:(MDCBottomNavigationItemView *)itemView {
itemView.badgeColor = self.itemBadgeBackgroundColor;
itemView.badgeFont = self.itemBadgeTextFont;
itemView.badgeTextColor = self.itemBadgeTextColor;
}

- (void)configureColorsForItemView:(MDCBottomNavigationItemView *)itemView {
// rippleColor must be set before selectedItemTintColor because selectedItemTintColor's behavior
// depends on the value of rippleColor.
Expand Down Expand Up @@ -113,3 +104,5 @@ - (void)configurePointerInteractionForItemView:(MDCBottomNavigationItemView *)it
}

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "MDCBadgeAppearance.h"

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wprivate-header"
#import "MDCBottomNavigationBarItem.h"
#import "MDCBottomNavigationItemView.h"
#pragma clang diagnostic pop

#import "supplemental/MDCBottomNavigationSnapshotTestMutableTraitCollection.h"
#import "supplemental/MDCBottomNavigationSnapshotTestUtilities.h"
#import "supplemental/MDCFakeBottomNavigationBar.h"
#import "MDCAvailability.h"
#import "MDCBadgeAppearance.h"
#import "MDCBottomNavigationBar.h"
#import "MDCRippleTouchController.h"
#import "MDCRippleView.h"
Expand Down Expand Up @@ -401,7 +404,7 @@ - (void)testShadowColorRespondsToDynamicColor {

#pragma mark - Badging

- (void)testCustomBadgeColorsSetAfterItems {
- (void)testCustomBadgeColorsOverrideDefaultBadgeAppearanceWhenSetAfterBarItems {
// Given
self.tabItem1.badgeValue = @"";
self.tabItem2.badgeValue = @"Gray on Yellow";
Expand All @@ -410,15 +413,34 @@ - (void)testCustomBadgeColorsSetAfterItems {
MDCBottomNavigationBarTestHeightTypical);

// When
self.tabItem3.badgeColor = UIColor.greenColor;
self.navigationBar.itemBadgeBackgroundColor = UIColor.yellowColor;
self.navigationBar.itemBadgeTextColor = UIColor.darkGrayColor;
MDCBadgeAppearance *greenAppearance = [[MDCBadgeAppearance alloc] init];
greenAppearance.font = [UIFont systemFontOfSize:8.0];
greenAppearance.backgroundColor = UIColor.greenColor;
greenAppearance.textColor = UIColor.darkGrayColor;
MDCBottomNavigationBarItem *barItem1 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem1];
MDCBottomNavigationBarItem *barItem2 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem2];
MDCBottomNavigationBarItem *barItem3 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem3
badgeAppearance:greenAppearance];
MDCBottomNavigationBarItem *barItem4 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem4];
MDCBottomNavigationBarItem *barItem5 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem5];
self.navigationBar.barItems = @[ barItem1, barItem2, barItem3, barItem4, barItem5 ];

MDCBadgeAppearance *badgeAppearance = [[MDCBadgeAppearance alloc] init];
badgeAppearance.font = [UIFont systemFontOfSize:8.0];
badgeAppearance.backgroundColor = UIColor.yellowColor;
badgeAppearance.textColor = UIColor.darkGrayColor;
self.navigationBar.itemBadgeAppearance = badgeAppearance;

// Then
[self generateAndVerifySnapshot];
}

- (void)testCustomBadgeColorsSetBeforeItems {
- (void)testCustomBadgeColorsOverrideDefaultBadgeAppearanceWhenSetBeforeBarItems {
// Given
self.tabItem1.badgeValue = @"";
self.tabItem2.badgeValue = @"Gray on Yellow";
Expand All @@ -427,11 +449,27 @@ - (void)testCustomBadgeColorsSetBeforeItems {
MDCBottomNavigationBarTestHeightTypical);

// When
self.tabItem3.badgeColor = UIColor.greenColor;
self.navigationBar.itemBadgeBackgroundColor = UIColor.yellowColor;
self.navigationBar.itemBadgeTextColor = UIColor.darkGrayColor;
self.navigationBar.items =
@[ self.tabItem1, self.tabItem2, self.tabItem3, self.tabItem4, self.tabItem5 ];
MDCBadgeAppearance *badgeAppearance = [[MDCBadgeAppearance alloc] init];
badgeAppearance.font = [UIFont systemFontOfSize:8.0];
badgeAppearance.backgroundColor = UIColor.yellowColor;
badgeAppearance.textColor = UIColor.darkGrayColor;
self.navigationBar.itemBadgeAppearance = badgeAppearance;
MDCBadgeAppearance *greenAppearance = [[MDCBadgeAppearance alloc] init];
greenAppearance.font = [UIFont systemFontOfSize:8.0];
greenAppearance.backgroundColor = UIColor.greenColor;
greenAppearance.textColor = UIColor.darkGrayColor;
MDCBottomNavigationBarItem *barItem1 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem1];
MDCBottomNavigationBarItem *barItem2 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem2];
MDCBottomNavigationBarItem *barItem3 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem3
badgeAppearance:greenAppearance];
MDCBottomNavigationBarItem *barItem4 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem4];
MDCBottomNavigationBarItem *barItem5 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem5];
self.navigationBar.barItems = @[ barItem1, barItem2, barItem3, barItem4, barItem5 ];

// Then
[self generateAndVerifySnapshot];
Expand All @@ -446,9 +484,27 @@ - (void)testClearBadgeColorsRendersClearBackgroundAndUILabelDefaultTextColor {
MDCBottomNavigationBarTestHeightTypical);

// When
self.tabItem3.badgeColor = UIColor.greenColor;
self.navigationBar.itemBadgeBackgroundColor = [UIColor clearColor];
self.navigationBar.itemBadgeTextColor = nil;
MDCBadgeAppearance *greenAppearance = [[MDCBadgeAppearance alloc] init];
greenAppearance.font = [UIFont systemFontOfSize:8.0];
greenAppearance.backgroundColor = UIColor.greenColor;
greenAppearance.textColor = UIColor.blackColor;
MDCBottomNavigationBarItem *barItem1 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem1];
MDCBottomNavigationBarItem *barItem2 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem2];
MDCBottomNavigationBarItem *barItem3 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem3
badgeAppearance:greenAppearance];
MDCBottomNavigationBarItem *barItem4 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem4];
MDCBottomNavigationBarItem *barItem5 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem5];
self.navigationBar.barItems = @[ barItem1, barItem2, barItem3, barItem4, barItem5 ];
MDCBadgeAppearance *badgeAppearance = [[MDCBadgeAppearance alloc] init];
badgeAppearance.font = [UIFont systemFontOfSize:8.0];
badgeAppearance.backgroundColor = UIColor.clearColor;
badgeAppearance.textColor = nil;
self.navigationBar.itemBadgeAppearance = badgeAppearance;

// Then
[self generateAndVerifySnapshot];
Expand All @@ -463,9 +519,27 @@ - (void)testNilBadgeColorsRendersTintBackgroundAndUILabelDefaultTextColor {
MDCBottomNavigationBarTestHeightTypical);

// When
self.tabItem3.badgeColor = UIColor.greenColor;
self.navigationBar.itemBadgeBackgroundColor = nil;
self.navigationBar.itemBadgeTextColor = nil;
MDCBadgeAppearance *greenAppearance = [[MDCBadgeAppearance alloc] init];
greenAppearance.font = [UIFont systemFontOfSize:8.0];
greenAppearance.backgroundColor = UIColor.greenColor;
greenAppearance.textColor = UIColor.blackColor;
MDCBottomNavigationBarItem *barItem1 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem1];
MDCBottomNavigationBarItem *barItem2 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem2];
MDCBottomNavigationBarItem *barItem3 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem3
badgeAppearance:greenAppearance];
MDCBottomNavigationBarItem *barItem4 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem4];
MDCBottomNavigationBarItem *barItem5 =
[[MDCBottomNavigationBarItem alloc] initWithBarItem:self.tabItem5];
self.navigationBar.barItems = @[ barItem1, barItem2, barItem3, barItem4, barItem5 ];
MDCBadgeAppearance *badgeAppearance = [[MDCBadgeAppearance alloc] init];
badgeAppearance.font = [UIFont systemFontOfSize:8.0];
badgeAppearance.backgroundColor = nil;
badgeAppearance.textColor = nil;
self.navigationBar.itemBadgeAppearance = badgeAppearance;

// Then
[self generateAndVerifySnapshot];
Expand All @@ -479,7 +553,9 @@ - (void)testCustomBadgeTextFontSetBeforeItems {
MDCBottomNavigationBarTestHeightTypical);

// When
self.navigationBar.itemBadgeTextFont = [UIFont systemFontOfSize:10.0];
MDCBadgeAppearance *badgeAppearance = [[MDCBadgeAppearance alloc] init];
badgeAppearance.font = [UIFont systemFontOfSize:10.0];
self.navigationBar.itemBadgeAppearance = badgeAppearance;
self.navigationBar.items =
@[ self.tabItem1, self.tabItem2, self.tabItem3, self.tabItem4, self.tabItem5 ];

Expand All @@ -497,7 +573,9 @@ - (void)testCustomBadgeTextFontSetAfterItems {
// When
self.navigationBar.items =
@[ self.tabItem1, self.tabItem2, self.tabItem3, self.tabItem4, self.tabItem5 ];
self.navigationBar.itemBadgeTextFont = [UIFont systemFontOfSize:10.0];
MDCBadgeAppearance *badgeAppearance = [[MDCBadgeAppearance alloc] init];
badgeAppearance.font = [UIFont systemFontOfSize:10.0];
self.navigationBar.itemBadgeAppearance = badgeAppearance;

// Then
[self generateAndVerifySnapshot];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#pragma clang diagnostic pop
#import "MDCAvailability.h"
#import "MDCBottomNavigationBar.h"
#import "MDCPalettes.h"
#import "MDCShadowElevations.h"

NS_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -66,25 +65,6 @@ - (void)tearDown {
self.bottomNavBar = nil;
}

- (void)testDefaultValues {
// When
MDCBottomNavigationBar *bar = [[MDCBottomNavigationBar alloc] init];

// Then
XCTAssertEqualObjects(bar.backgroundColor, UIColor.whiteColor);
XCTAssertFalse(bar.isBackgroundBlurEnabled);
XCTAssertEqual(bar.backgroundBlurEffectStyle, UIBlurEffectStyleExtraLight);
XCTAssertEqualWithAccuracy(self.bottomNavBar.elevation, MDCShadowElevationBottomNavigationBar,
0.001);
XCTAssertEqualWithAccuracy(self.bottomNavBar.mdc_currentElevation, self.bottomNavBar.elevation,
0.001);
XCTAssertLessThan(self.bottomNavBar.mdc_overrideBaseElevation, 0);
XCTAssertNil(self.bottomNavBar.mdc_elevationDidChangeBlock);
XCTAssertEqualObjects(self.bottomNavBar.itemBadgeTextColor, UIColor.whiteColor);
XCTAssertEqualObjects(self.bottomNavBar.itemBadgeTextFont, [UIFont systemFontOfSize:8.0]);
XCTAssertEqualObjects(self.bottomNavBar.itemBadgeBackgroundColor, MDCPalette.redPalette.tint700);
}

#pragma mark - Fonts

- (void)testItemTitleFontDefault {
Expand Down

0 comments on commit decc506

Please sign in to comment.