From 351fb483ef2a8d1747b0267d23f4a01e2dd22483 Mon Sep 17 00:00:00 2001 From: Frank Wang Date: Fri, 22 Nov 2024 14:29:25 -0800 Subject: [PATCH] [Bottom Navigation] Remove deprecated badge properties and update tests to reflect new behavior. PiperOrigin-RevId: 699289362 --- .../src/MDCBottomNavigationBar.h | 39 +----- .../src/MDCBottomNavigationBar.m | 40 +----- .../private/MDCBottomNavigationBar+ItemView.m | 15 +-- .../MDCBottomNavigationBarSnapshotTests.m | 114 +++++++++++++++--- .../tests/unit/MDCBottomNavigationBarTests.m | 20 --- 5 files changed, 108 insertions(+), 120 deletions(-) diff --git a/components/BottomNavigation/src/MDCBottomNavigationBar.h b/components/BottomNavigation/src/MDCBottomNavigationBar.h index 3d5f51e7736..9f3aa1faa9c 100644 --- a/components/BottomNavigation/src/MDCBottomNavigationBar.h +++ b/components/BottomNavigation/src/MDCBottomNavigationBar.h @@ -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. */ @@ -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. @@ -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 @@ -424,3 +391,5 @@ API_UNAVAILABLE(tvos, watchos) UILargeContentViewerInteractionDelegate> @end #endif // MDC_AVAILABLE_SDK_IOS(13_0) + +NS_ASSUME_NONNULL_END diff --git a/components/BottomNavigation/src/MDCBottomNavigationBar.m b/components/BottomNavigation/src/MDCBottomNavigationBar.m index 0ab4c471a90..e2962ee2189 100644 --- a/components/BottomNavigation/src/MDCBottomNavigationBar.m +++ b/components/BottomNavigation/src/MDCBottomNavigationBar.m @@ -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 @@ -914,6 +913,7 @@ - (void)setItems:(NSArray *)items { itemView.selected = NO; itemView.displayTitleInVerticalLayout = self.displayItemTitlesInVerticalLayout; itemView.enableVerticalLayout = self.enableVerticalLayout; + itemView.badgeAppearance = _itemBadgeAppearance; itemView.selectionIndicatorColor = self.selectionIndicatorColor; itemView.selectionIndicatorSize = self.selectionIndicatorSize; @@ -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 diff --git a/components/BottomNavigation/src/private/MDCBottomNavigationBar+ItemView.m b/components/BottomNavigation/src/private/MDCBottomNavigationBar+ItemView.m index 273016fb1e4..499e97a0939 100644 --- a/components/BottomNavigation/src/private/MDCBottomNavigationBar+ItemView.m +++ b/components/BottomNavigation/src/private/MDCBottomNavigationBar+ItemView.m @@ -14,12 +14,13 @@ #import -#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 @@ -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. @@ -113,3 +104,5 @@ - (void)configurePointerInteractionForItemView:(MDCBottomNavigationItemView *)it } @end + +NS_ASSUME_NONNULL_END diff --git a/components/BottomNavigation/tests/snapshot/MDCBottomNavigationBarSnapshotTests.m b/components/BottomNavigation/tests/snapshot/MDCBottomNavigationBarSnapshotTests.m index 0a99be10ace..3b38edfb9ec 100644 --- a/components/BottomNavigation/tests/snapshot/MDCBottomNavigationBarSnapshotTests.m +++ b/components/BottomNavigation/tests/snapshot/MDCBottomNavigationBarSnapshotTests.m @@ -14,9 +14,11 @@ #import #import +#import "MDCBadgeAppearance.h" #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wprivate-header" +#import "MDCBottomNavigationBarItem.h" #import "MDCBottomNavigationItemView.h" #pragma clang diagnostic pop @@ -24,6 +26,7 @@ #import "supplemental/MDCBottomNavigationSnapshotTestUtilities.h" #import "supplemental/MDCFakeBottomNavigationBar.h" #import "MDCAvailability.h" +#import "MDCBadgeAppearance.h" #import "MDCBottomNavigationBar.h" #import "MDCRippleTouchController.h" #import "MDCRippleView.h" @@ -401,7 +404,7 @@ - (void)testShadowColorRespondsToDynamicColor { #pragma mark - Badging -- (void)testCustomBadgeColorsSetAfterItems { +- (void)testCustomBadgeColorsOverrideDefaultBadgeAppearanceWhenSetAfterBarItems { // Given self.tabItem1.badgeValue = @""; self.tabItem2.badgeValue = @"Gray on Yellow"; @@ -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"; @@ -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]; @@ -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]; @@ -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]; @@ -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 ]; @@ -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]; diff --git a/components/BottomNavigation/tests/unit/MDCBottomNavigationBarTests.m b/components/BottomNavigation/tests/unit/MDCBottomNavigationBarTests.m index 6c9d85ada83..48fa21ca41a 100644 --- a/components/BottomNavigation/tests/unit/MDCBottomNavigationBarTests.m +++ b/components/BottomNavigation/tests/unit/MDCBottomNavigationBarTests.m @@ -21,7 +21,6 @@ #pragma clang diagnostic pop #import "MDCAvailability.h" #import "MDCBottomNavigationBar.h" -#import "MDCPalettes.h" #import "MDCShadowElevations.h" NS_ASSUME_NONNULL_BEGIN @@ -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 {