Skip to content
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
47 changes: 31 additions & 16 deletions ios/bottom-tabs/RNSBottomTabsScreenComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ @implementation RNSBottomTabsScreenComponentView {
#if !RCT_NEW_ARCH_ENABLED
BOOL _tabItemNeedsAppearanceUpdate;
BOOL _tabScreenOrientationNeedsUpdate;
BOOL _tabBarItemNeedsBaseChange;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When reading the code for the first time the base part made no sense to me. What we want to signal here is that the tab bar item needs to be recreated & requires allocating completely new instance. What about naming it _tabBarItemNeedsRecreation then? What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also rename the changeTabBarItem appropriately. createTabBarItem?

BOOL _tabBarItemNeedsUpdate;
BOOL _scrollEdgeEffectsNeedUpdate;
#endif // !RCT_NEW_ARCH_ENABLED
Expand Down Expand Up @@ -64,6 +65,7 @@ - (void)initState
#if !RCT_NEW_ARCH_ENABLED
_tabItemNeedsAppearanceUpdate = NO;
_tabScreenOrientationNeedsUpdate = NO;
_tabBarItemNeedsBaseChange = NO;
_tabBarItemNeedsUpdate = NO;
_scrollEdgeEffectsNeedUpdate = NO;
#endif
Expand Down Expand Up @@ -172,7 +174,7 @@ - (void)updateContentScrollViewEdgeEffectsIfExists

#pragma mark - Prop update utils

- (void)updateTabBarItem
- (void)changeBaseTabBarItem
{
UITabBarItem *tabBarItem = nil;
if (_systemItem != RNSBottomTabsScreenSystemItemNone) {
Expand All @@ -181,14 +183,24 @@ - (void)updateTabBarItem
tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:systemItem tag:0];
} else {
tabBarItem = [[UITabBarItem alloc] init];
tabBarItem.title = _title;
}

tabBarItem.badgeValue = _badgeValue;

_controller.tabBarItem = tabBarItem;
}

- (void)updateTabBarItem
{
UITabBarItem *tabBarItem = _controller.tabBarItem;

if (![tabBarItem.title isEqualToString:_title]) {
tabBarItem.title = _title;
}

if (![tabBarItem.badgeValue isEqualToString:_badgeValue]) {
tabBarItem.badgeValue = _badgeValue;
}
}

#pragma mark - RNSSafeAreaProviding

- (UIEdgeInsets)providerSafeAreaInsets
Expand Down Expand Up @@ -222,6 +234,7 @@ - (void)updateProps:(const facebook::react::Props::Shared &)props

bool tabItemNeedsAppearanceUpdate{false};
bool tabScreenOrientationNeedsUpdate{false};
bool tabBarItemNeedsBaseChange{false};
bool tabBarItemNeedsUpdate{false};
bool scrollEdgeEffectsNeedUpdate{false};

Expand Down Expand Up @@ -274,33 +287,28 @@ - (void)updateProps:(const facebook::react::Props::Shared &)props
if (newComponentProps.iconType != oldComponentProps.iconType) {
_iconType = rnscreens::conversion::RNSBottomTabsIconTypeFromIcon(newComponentProps.iconType);
tabItemNeedsAppearanceUpdate = YES;
tabBarItemNeedsUpdate = YES;
}

if (newComponentProps.iconImageSource != oldComponentProps.iconImageSource) {
_iconImageSource =
rnscreens::conversion::RCTImageSourceFromImageSourceAndIconType(&newComponentProps.iconImageSource, _iconType);
tabItemNeedsAppearanceUpdate = YES;
tabBarItemNeedsUpdate = YES;
}

if (newComponentProps.iconSfSymbolName != oldComponentProps.iconSfSymbolName) {
_iconSfSymbolName = RCTNSStringFromStringNilIfEmpty(newComponentProps.iconSfSymbolName);
tabItemNeedsAppearanceUpdate = YES;
tabBarItemNeedsUpdate = YES;
}

if (newComponentProps.selectedIconImageSource != oldComponentProps.selectedIconImageSource) {
_selectedIconImageSource = rnscreens::conversion::RCTImageSourceFromImageSourceAndIconType(
&newComponentProps.selectedIconImageSource, _iconType);
tabItemNeedsAppearanceUpdate = YES;
tabBarItemNeedsUpdate = YES;
}

if (newComponentProps.selectedIconSfSymbolName != oldComponentProps.selectedIconSfSymbolName) {
_selectedIconSfSymbolName = RCTNSStringFromStringNilIfEmpty(newComponentProps.selectedIconSfSymbolName);
tabItemNeedsAppearanceUpdate = YES;
tabBarItemNeedsUpdate = YES;
}

if (newComponentProps.specialEffects.repeatedTabSelection.popToRoot !=
Expand Down Expand Up @@ -334,7 +342,7 @@ - (void)updateProps:(const facebook::react::Props::Shared &)props
if (newComponentProps.systemItem != oldComponentProps.systemItem) {
_systemItem = rnscreens::conversion::RNSBottomTabsScreenSystemItemFromReactRNSBottomTabsScreenSystemItem(
newComponentProps.systemItem);
tabBarItemNeedsUpdate = YES;
tabBarItemNeedsBaseChange = YES;
}

if (newComponentProps.bottomScrollEdgeEffect != oldComponentProps.bottomScrollEdgeEffect) {
Expand Down Expand Up @@ -367,6 +375,11 @@ - (void)updateProps:(const facebook::react::Props::Shared &)props
scrollEdgeEffectsNeedUpdate = YES;
}

if (tabBarItemNeedsBaseChange) {
[self changeBaseTabBarItem];
tabBarItemNeedsUpdate = YES;
}

if (tabBarItemNeedsUpdate) {
[self updateTabBarItem];

Expand Down Expand Up @@ -445,6 +458,13 @@ - (void)didSetProps:(NSArray<NSString *> *)changedProps
// didSetProps will always be called because tabKey prop is required.
_isOverrideScrollViewContentInsetAdjustmentBehaviorSet = YES;

if (_tabBarItemNeedsBaseChange) {
[self changeBaseTabBarItem];
_tabBarItemNeedsBaseChange = NO;

_tabBarItemNeedsUpdate = YES;
}

if (_tabBarItemNeedsUpdate) {
[self updateTabBarItem];
_tabBarItemNeedsUpdate = NO;
Expand Down Expand Up @@ -501,35 +521,30 @@ - (void)setIconType:(RNSBottomTabsIconType)iconType
{
_iconType = iconType;
_tabItemNeedsAppearanceUpdate = YES;
_tabBarItemNeedsUpdate = YES;
}

- (void)setIconImageSource:(RCTImageSource *)iconImageSource
{
_iconImageSource = iconImageSource;
_tabItemNeedsAppearanceUpdate = YES;
_tabBarItemNeedsUpdate = YES;
}

- (void)setIconSfSymbolName:(NSString *)iconSfSymbolName
{
_iconSfSymbolName = [NSString rnscreens_stringOrNilIfEmpty:iconSfSymbolName];
_tabItemNeedsAppearanceUpdate = YES;
_tabBarItemNeedsUpdate = YES;
}

- (void)setSelectedIconImageSource:(RCTImageSource *)selectedIconImageSource
{
_selectedIconImageSource = selectedIconImageSource;
_tabItemNeedsAppearanceUpdate = YES;
_tabBarItemNeedsUpdate = YES;
}

- (void)setSelectedIconSfSymbolName:(NSString *)selectedIconSfSymbolName
{
_selectedIconSfSymbolName = [NSString rnscreens_stringOrNilIfEmpty:selectedIconSfSymbolName];
_tabItemNeedsAppearanceUpdate = YES;
_tabBarItemNeedsUpdate = YES;
}

- (void)setBottomScrollEdgeEffect:(RNSScrollEdgeEffect)bottomScrollEdgeEffect
Expand Down Expand Up @@ -596,7 +611,7 @@ - (void)setScrollEdgeAppearance:(NSDictionary *)scrollEdgeAppearanceProps
- (void)setSystemItem:(RNSBottomTabsScreenSystemItem)systemItem
{
_systemItem = systemItem;
_tabBarItemNeedsUpdate = YES;
_tabBarItemNeedsBaseChange = YES;
}

- (void)setOrientation:(RNSOrientation)orientation
Expand Down
10 changes: 10 additions & 0 deletions ios/bottom-tabs/RNSTabBarAppearanceCoordinator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,20 @@ - (void)setIconsForTabBarItem:(UITabBarItem *)tabBarItem
if (screenView.iconType == RNSBottomTabsIconTypeSfSymbol) {
if (screenView.iconSfSymbolName != nil) {
tabBarItem.image = [UIImage systemImageNamed:screenView.iconSfSymbolName];
} else if (screenView.systemItem != RNSBottomTabsScreenSystemItemNone) {
// Restore default system item icon
UITabBarSystemItem systemItem =
rnscreens::conversion::RNSBottomTabsScreenSystemItemToUITabBarSystemItem(screenView.systemItem);
tabBarItem.image = [[UITabBarItem alloc] initWithTabBarSystemItem:systemItem tag:0].image;
}

if (screenView.selectedIconSfSymbolName != nil) {
tabBarItem.selectedImage = [UIImage systemImageNamed:screenView.selectedIconSfSymbolName];
} else if (screenView.systemItem != RNSBottomTabsScreenSystemItemNone) {
// Restore default system item icon
UITabBarSystemItem systemItem =
rnscreens::conversion::RNSBottomTabsScreenSystemItemToUITabBarSystemItem(screenView.systemItem);
tabBarItem.selectedImage = [[UITabBarItem alloc] initWithTabBarSystemItem:systemItem tag:0].selectedImage;
Comment on lines +58 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this trick.

}
} else if (imageLoader != nil) {
bool isTemplate = screenView.iconType == RNSBottomTabsIconTypeTemplate;
Expand Down
Loading