From 27e604db1686a12bfec737a657ad5c033857a887 Mon Sep 17 00:00:00 2001 From: Yogev Ben David Date: Sun, 24 Oct 2021 16:43:20 +0300 Subject: [PATCH] Destroy buttons along with the main react view --- lib/ios/RNNBottomTabOptions.m | 3 ++- lib/ios/RNNButtonBuilder.m | 6 +++--- lib/ios/RNNButtonOptions.m | 8 ++++---- lib/ios/RNNDotIndicatorPresenter.m | 3 ++- lib/ios/RNNSegmentedControl.h | 2 +- lib/ios/RNNTabBarItemCreator.m | 5 +++-- lib/ios/RNNUIBarButtonItem.h | 4 +++- lib/ios/RNNUIBarButtonItem.m | 12 +++++++++--- lib/ios/TopBarPresenter.m | 12 ++++++++---- lib/ios/UIViewController+LayoutProtocol.m | 12 ++++++++++++ 10 files changed, 47 insertions(+), 20 deletions(-) diff --git a/lib/ios/RNNBottomTabOptions.m b/lib/ios/RNNBottomTabOptions.m index 0f24da1d61a..a164e64aae4 100644 --- a/lib/ios/RNNBottomTabOptions.m +++ b/lib/ios/RNNBottomTabOptions.m @@ -84,7 +84,8 @@ - (BOOL)hasValue { self.testID.hasValue || self.icon.hasValue || self.selectedIcon.hasValue || self.iconColor.hasValue || self.selectedIconColor.hasValue || self.selectedTextColor.hasValue || self.iconInsets.hasValue || self.textColor.hasValue || - self.visible.hasValue || self.selectTabOnPress.hasValue || self.sfSymbol.hasValue || self.sfSelectedSymbol.hasValue; + self.visible.hasValue || self.selectTabOnPress.hasValue || self.sfSymbol.hasValue || + self.sfSelectedSymbol.hasValue; } @end diff --git a/lib/ios/RNNButtonBuilder.m b/lib/ios/RNNButtonBuilder.m index ff2cad4fed8..24e3cb3589b 100644 --- a/lib/ios/RNNButtonBuilder.m +++ b/lib/ios/RNNButtonBuilder.m @@ -31,9 +31,9 @@ - (RNNUIBarButtonItem *)build:(RNNButtonOptions *)button return [[RNNUIBarButtonItem alloc] initCustomIcon:button iconCreator:_iconCreator onPress:onPress]; - } else if (button.sfSymbol.hasValue) { - return [[RNNUIBarButtonItem alloc] initWithSFSymbol:button onPress:onPress]; - } else if (button.icon.hasValue) { + } else if (button.sfSymbol.hasValue) { + return [[RNNUIBarButtonItem alloc] initWithSFSymbol:button onPress:onPress]; + } else if (button.icon.hasValue) { return [[RNNUIBarButtonItem alloc] initWithIcon:button onPress:onPress]; } else if (button.text.hasValue) { return [[RNNUIBarButtonItem alloc] initWithTitle:button onPress:onPress]; diff --git a/lib/ios/RNNButtonOptions.m b/lib/ios/RNNButtonOptions.m index 8f4d4ab69fa..a3119131d8d 100644 --- a/lib/ios/RNNButtonOptions.m +++ b/lib/ios/RNNButtonOptions.m @@ -11,7 +11,7 @@ - (instancetype)initWithDict:(NSDictionary *)dict { self.fontWeight = [TextParser parse:dict key:@"fontWeight"]; self.fontSize = [NumberParser parse:dict key:@"fontSize"]; self.text = [TextParser parse:dict key:@"text"]; - self.sfSymbol = [TextParser parse:dict key:@"sfSymbol"]; + self.sfSymbol = [TextParser parse:dict key:@"sfSymbol"]; self.testID = [TextParser parse:dict key:@"testID"]; self.accessibilityLabel = [TextParser parse:dict key:@"accessibilityLabel"]; self.color = [ColorParser parse:dict key:@"color"]; @@ -40,7 +40,7 @@ - (RNNButtonOptions *)copy { newOptions.color = self.color.copy; newOptions.disabledColor = self.disabledColor.copy; newOptions.icon = self.icon.copy; - newOptions.sfSymbol = self.sfSymbol.copy; + newOptions.sfSymbol = self.sfSymbol.copy; newOptions.iconInsets = self.iconInsets.copy; newOptions.enabled = self.enabled.copy; newOptions.selectTabOnPress = self.selectTabOnPress.copy; @@ -73,8 +73,8 @@ - (void)mergeOptions:(RNNButtonOptions *)options { self.disabledColor = options.disabledColor; if (options.icon.hasValue) self.icon = options.icon; - if (options.sfSymbol.hasValue) - self.sfSymbol = options.sfSymbol; + if (options.sfSymbol.hasValue) + self.sfSymbol = options.sfSymbol; if (options.enabled.hasValue) { self.enabled = options.enabled; [self.iconBackground setEnabled:self.enabled]; diff --git a/lib/ios/RNNDotIndicatorPresenter.m b/lib/ios/RNNDotIndicatorPresenter.m index 6869d071e20..635c1151f2a 100644 --- a/lib/ios/RNNDotIndicatorPresenter.m +++ b/lib/ios/RNNDotIndicatorPresenter.m @@ -92,7 +92,8 @@ - (BOOL)currentIndicatorEquals:(UIViewController *)child options:(DotIndicatorOp return NO; UIView *currentIndicator = [self getCurrentIndicator:child]; - return [[currentIndicator backgroundColor] isEqual:[options.color withDefault:[UIColor redColor]]]; + return + [[currentIndicator backgroundColor] isEqual:[options.color withDefault:[UIColor redColor]]]; } - (UIView *)getCurrentIndicator:(UIViewController *)child { diff --git a/lib/ios/RNNSegmentedControl.h b/lib/ios/RNNSegmentedControl.h index 41771612a73..64ddd8d14d4 100644 --- a/lib/ios/RNNSegmentedControl.h +++ b/lib/ios/RNNSegmentedControl.h @@ -1,5 +1,5 @@ -#import #import +#import @interface RNNSegmentedControl : HMSegmentedControl diff --git a/lib/ios/RNNTabBarItemCreator.m b/lib/ios/RNNTabBarItemCreator.m index b795501f568..140c7be8961 100644 --- a/lib/ios/RNNTabBarItemCreator.m +++ b/lib/ios/RNNTabBarItemCreator.m @@ -18,11 +18,12 @@ - (UITabBarItem *)createTabBarItem:(RNNBottomTabOptions *)bottomTabOptions if (@available(iOS 13.0, *)) { if (bottomTabOptions.sfSymbol.hasValue) { - icon = [UIImage systemImageNamed: [bottomTabOptions.sfSymbol withDefault:nil]]; + icon = [UIImage systemImageNamed:[bottomTabOptions.sfSymbol withDefault:nil]]; } if (bottomTabOptions.sfSelectedSymbol.hasValue) { - selectedIcon = [UIImage systemImageNamed: [bottomTabOptions.sfSelectedSymbol withDefault:nil]]; + selectedIcon = + [UIImage systemImageNamed:[bottomTabOptions.sfSelectedSymbol withDefault:nil]]; } } diff --git a/lib/ios/RNNUIBarButtonItem.h b/lib/ios/RNNUIBarButtonItem.h index bbffeea3222..0c44e4aad8d 100644 --- a/lib/ios/RNNUIBarButtonItem.h +++ b/lib/ios/RNNUIBarButtonItem.h @@ -15,7 +15,7 @@ typedef void (^RNNButtonPressCallback)(NSString *buttonId); iconCreator:(RNNIconCreator *)iconCreator onPress:(RNNButtonPressCallback)onPress; - (instancetype)initWithSFSymbol:(RNNButtonOptions *)buttonOptions - onPress:(RNNButtonPressCallback)onPress; + onPress:(RNNButtonPressCallback)onPress; - (instancetype)initWithIcon:(RNNButtonOptions *)buttonOptions onPress:(RNNButtonPressCallback)onPress; - (instancetype)initWithTitle:(RNNButtonOptions *)buttonOptions @@ -32,4 +32,6 @@ typedef void (^RNNButtonPressCallback)(NSString *buttonId); - (void)notifyDidAppear; - (void)notifyDidDisappear; +- (void)invalidate; + @end diff --git a/lib/ios/RNNUIBarButtonItem.m b/lib/ios/RNNUIBarButtonItem.m index 4f177c2cde0..6dd97d905f1 100644 --- a/lib/ios/RNNUIBarButtonItem.m +++ b/lib/ios/RNNUIBarButtonItem.m @@ -21,12 +21,12 @@ - (instancetype)init { } - (instancetype)initWithSFSymbol:(RNNButtonOptions *)buttonOptions - onPress:(RNNButtonPressCallback)onPress { + onPress:(RNNButtonPressCallback)onPress { UIImage *iconImage = [UIImage alloc]; - if (@available(iOS 13.0, *)) { + if (@available(iOS 13.0, *)) { iconImage = [UIImage systemImageNamed:[buttonOptions.sfSymbol withDefault:nil]]; - } + } self = [super initWithImage:iconImage style:UIBarButtonItemStylePlain @@ -196,6 +196,12 @@ - (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView { rootView.hidden = NO; } +- (void)invalidate { + if ([self.customView isKindOfClass:[RNNReactView class]]) { + [((RNNReactView *)self.customView) invalidate]; + } +} + - (void)onButtonPressed:(RNNUIBarButtonItem *)barButtonItem { self.onPress(self.buttonId); } diff --git a/lib/ios/TopBarPresenter.m b/lib/ios/TopBarPresenter.m index a5b5ca1bcc7..2c9c35fe6a6 100644 --- a/lib/ios/TopBarPresenter.m +++ b/lib/ios/TopBarPresenter.m @@ -160,17 +160,21 @@ - (void)setBackButtonOptions:(RNNBackButtonOptions *)backButtonOptions { UIBarButtonItem *backItem = [[RNNUIBarBackButtonItem alloc] initWithOptions:backButtonOptions]; UINavigationItem *previousNavigationItem = previousViewControllerInStack.navigationItem; - if (@available(iOS 13.0, *)) { UIImage *sfSymbol = [UIImage systemImageNamed:[backButtonOptions.sfSymbol withDefault:nil]]; if (backButtonOptions.sfSymbol.hasValue) { - icon = color ? [sfSymbol imageWithTintColor:color renderingMode:UIImageRenderingModeAlwaysOriginal] + icon = color ? [sfSymbol imageWithTintColor:color + renderingMode:UIImageRenderingModeAlwaysOriginal] : [sfSymbol imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } else { - icon = color ? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] : icon; + icon = color ? [[icon withTintColor:color] + imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] + : icon; } } else { - icon = color ? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] : icon; + icon = color ? [[icon withTintColor:color] + imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] + : icon; } [self setBackIndicatorImage:icon withColor:color]; diff --git a/lib/ios/UIViewController+LayoutProtocol.m b/lib/ios/UIViewController+LayoutProtocol.m index 43040206cc9..a155fd30631 100644 --- a/lib/ios/UIViewController+LayoutProtocol.m +++ b/lib/ios/UIViewController+LayoutProtocol.m @@ -1,4 +1,5 @@ +#import "RNNUIBarButtonItem.h" #import "UIViewController+LayoutProtocol.h" #import @@ -105,6 +106,17 @@ - (void)destroy { for (UIViewController *child in self.childViewControllers) { [child destroy]; } + + [self destroyButtons]; +} + +- (void)destroyButtons { + for (UIBarButtonItem *button in [self.navigationItem.leftBarButtonItems + arrayByAddingObjectsFromArray:self.navigationItem.rightBarButtonItems]) { + if ([button isKindOfClass:[RNNUIBarButtonItem class]]) { + [(RNNUIBarButtonItem *)button invalidate]; + } + } } - (void)destroyReactView {