Skip to content

Commit 82f1124

Browse files
authored
refactor(iOS): cleanup no. 1 in RNSScreenStackHeaderConfig.updateViewController:withConfig:animated: (#2798)
## Description This method is very hard to read & it is very scary to refactor since as far as I remember, sometimes even call order matters here. Therefore, I've decided to split the refactoring process into many small PRs. This PR improves local variable naming & extracts LTR handling to separate method. ## Test code and steps to reproduce Nothing should change. LTR works as earlier. Can be tested in Example by toggling RTL mode (there is a button for it). ## Checklist - [ ] Ensured that CI passes
1 parent ac7154c commit 82f1124

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

ios/RNSScreenStackHeaderConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@interface NSString (RNSStringUtil)
1414

15-
+ (BOOL)RNSisBlank:(nullable NSString *)string;
15+
+ (BOOL)rnscreens_isBlankOrNull:(nullable NSString *)string;
1616

1717
@end
1818

ios/RNSScreenStackHeaderConfig.mm

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ @interface RCTImageLoader (Private)
4949

5050
@implementation NSString (RNSStringUtil)
5151

52-
+ (BOOL)RNSisBlank:(NSString *)string
52+
+ (BOOL)rnscreens_isBlankOrNull:(NSString *)string
5353
{
5454
if (string == nil) {
5555
return YES;
@@ -613,38 +613,23 @@ + (void)updateViewController:(UIViewController *)vc
613613

614614
[navctr setNavigationBarHidden:shouldHide animated:animated];
615615

616-
if ((config.direction == UISemanticContentAttributeForceLeftToRight ||
617-
config.direction == UISemanticContentAttributeForceRightToLeft) &&
618-
// iOS 12 cancels swipe gesture when direction is changed. See #1091
619-
navctr.view.semanticContentAttribute != config.direction) {
620-
// This is needed for swipe back gesture direction
621-
navctr.view.semanticContentAttribute = config.direction;
622-
623-
// This is responsible for the direction of the navigationBar and its contents
624-
navctr.navigationBar.semanticContentAttribute = config.direction;
625-
[[UIButton appearanceWhenContainedInInstancesOfClasses:@[ navctr.navigationBar.class ]]
626-
setSemanticContentAttribute:config.direction];
627-
[[UIView appearanceWhenContainedInInstancesOfClasses:@[ navctr.navigationBar.class ]]
628-
setSemanticContentAttribute:config.direction];
629-
[[UISearchBar appearanceWhenContainedInInstancesOfClasses:@[ navctr.navigationBar.class ]]
630-
setSemanticContentAttribute:config.direction];
631-
}
616+
[config applySemanticContentAttributeIfNeededToNavCtrl:navctr];
632617

633618
if (shouldHide) {
634619
navitem.title = config.title;
635620
return;
636621
}
637622

638623
#if !TARGET_OS_TV
639-
const auto isBackTitleBlank = [NSString RNSisBlank:config.backTitle] == YES;
624+
const auto isBackTitleBlank = [NSString rnscreens_isBlankOrNull:config.backTitle] == YES;
640625
NSString *resolvedBackTitle = isBackTitleBlank ? prevItem.title : config.backTitle;
641626
RNSUIBarButtonItem *backBarButtonItem = [[RNSUIBarButtonItem alloc] initWithTitle:resolvedBackTitle
642627
style:UIBarButtonItemStylePlain
643628
target:nil
644629
action:nil];
645630
[backBarButtonItem setMenuHidden:config.disableBackButtonMenu];
646631

647-
auto isBackButtonCustomized = !isBackTitleBlank || config.disableBackButtonMenu;
632+
auto shouldUseCustomBackBarButtonItem = !isBackTitleBlank || config.disableBackButtonMenu;
648633

649634
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_14_0) && \
650635
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
@@ -661,7 +646,7 @@ + (void)updateViewController:(UIViewController *)vc
661646
// See: https://github.com/software-mansion/react-native-screens/pull/2105#discussion_r1565222738
662647
![config.backTitleFontFamily isEqual:@"System"]) ||
663648
config.backTitleFontSize) {
664-
isBackButtonCustomized = YES;
649+
shouldUseCustomBackBarButtonItem = YES;
665650
NSMutableDictionary *attrs = [NSMutableDictionary new];
666651
NSNumber *size = config.backTitleFontSize ?: @17;
667652
if (config.backTitleFontFamily) {
@@ -684,15 +669,15 @@ + (void)updateViewController:(UIViewController *)vc
684669
// When backBarButtonItem's title is null, back menu will use value
685670
// of backButtonTitle
686671
[backBarButtonItem setTitle:nil];
687-
isBackButtonCustomized = YES;
672+
shouldUseCustomBackBarButtonItem = YES;
688673
prevItem.backButtonTitle = resolvedBackTitle;
689674
}
690675

691676
// Prevent unnecessary assignment of backBarButtonItem if it is not customized,
692677
// as assigning one will override the native behavior of automatically shortening
693678
// the title to "Back" or hide the back title if there's not enough space.
694679
// See: https://github.com/software-mansion/react-native-screens/issues/1589
695-
if (isBackButtonCustomized) {
680+
if (shouldUseCustomBackBarButtonItem) {
696681
prevItem.backBarButtonItem = backBarButtonItem;
697682
}
698683

@@ -848,6 +833,26 @@ + (void)updateViewController:(UIViewController *)vc
848833
}
849834
}
850835

836+
- (void)applySemanticContentAttributeIfNeededToNavCtrl:(UINavigationController *)navCtrl
837+
{
838+
if ((self.direction == UISemanticContentAttributeForceLeftToRight ||
839+
self.direction == UISemanticContentAttributeForceRightToLeft) &&
840+
// iOS 12 cancels swipe gesture when direction is changed. See #1091
841+
navCtrl.view.semanticContentAttribute != self.direction) {
842+
// This is needed for swipe back gesture direction
843+
navCtrl.view.semanticContentAttribute = self.direction;
844+
845+
// This is responsible for the direction of the navigationBar and its contents
846+
navCtrl.navigationBar.semanticContentAttribute = self.direction;
847+
[[UIButton appearanceWhenContainedInInstancesOfClasses:@[ navCtrl.navigationBar.class ]]
848+
setSemanticContentAttribute:self.direction];
849+
[[UIView appearanceWhenContainedInInstancesOfClasses:@[ navCtrl.navigationBar.class ]]
850+
setSemanticContentAttribute:self.direction];
851+
[[UISearchBar appearanceWhenContainedInInstancesOfClasses:@[ navCtrl.navigationBar.class ]]
852+
setSemanticContentAttribute:self.direction];
853+
}
854+
}
855+
851856
RNS_IGNORE_SUPER_CALL_BEGIN
852857
- (void)insertReactSubview:(RNSScreenStackHeaderSubview *)subview atIndex:(NSInteger)atIndex
853858
{

0 commit comments

Comments
 (0)