From c425f837b1d99a8b27525d52b6eba37fb77cbded Mon Sep 17 00:00:00 2001 From: N3TC4T Date: Sun, 12 Jan 2020 11:51:06 +0100 Subject: [PATCH] Add bottomTabLongPressed event on iOS (#5784) * Add bottomTabLongPressed event on iOS * Fix indent Co-authored-by: Yogev Ben David --- docs/docs/events.md | 13 +++++++ lib/ios/RNNBottomTabsController.m | 16 +++++++- lib/ios/RNNEventEmitter.h | 2 + lib/ios/RNNEventEmitter.m | 48 ++++++++++++++---------- lib/src/adapters/NativeEventsReceiver.ts | 6 ++- lib/src/events/EventsRegistry.ts | 6 ++- lib/src/interfaces/Events.ts | 4 ++ 7 files changed, 72 insertions(+), 23 deletions(-) diff --git a/docs/docs/events.md b/docs/docs/events.md index 3ada0e780fc..b038f08f0fe 100644 --- a/docs/docs/events.md +++ b/docs/docs/events.md @@ -192,6 +192,19 @@ const bottomTabEventListener = Navigation.events().registerBottomTabSelectedList bottomTabEventListener.remove(); ``` +## registerBottomTabLongPressedListener +Invoked when a BottomTab is long pressed by the user. + +```js +// Subscribe +const bottomTabEventListener = Navigation.events().registerBottomTabLongPressedListener(({ selectedTabIndex }) => { + +}); +... +// Unsubscribe +bottomTabEventListener.remove(); +``` + | Parameter | Description | |:--------------------:|:-----| |**selectedTabIndex** | The index of the newly selected tab| diff --git a/lib/ios/RNNBottomTabsController.m b/lib/ios/RNNBottomTabsController.m index 465bce6cfa9..38a3306fe95 100644 --- a/lib/ios/RNNBottomTabsController.m +++ b/lib/ios/RNNBottomTabsController.m @@ -28,7 +28,14 @@ - (void)render { } - (void)viewDidLayoutSubviews { - [self.presenter viewDidLayoutSubviews]; + [self.presenter viewDidLayoutSubviews]; + + for (UIView *view in [[self tabBar] subviews]) { + UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleLongPress:)]; + if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) { + [view addGestureRecognizer: longPressGesture]; + } + } } - (UIViewController *)getCurrentChild { @@ -65,4 +72,11 @@ - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewCon _currentTabIndex = tabBarController.selectedIndex; } +- (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer { + if (recognizer.state == UIGestureRecognizerStateBegan) { + NSUInteger _index = [self.tabBar.subviews indexOfObject:(UIView *)recognizer.view]; + [self.eventEmitter sendBottomTabLongPressed:@(_index)]; + } +} + @end diff --git a/lib/ios/RNNEventEmitter.h b/lib/ios/RNNEventEmitter.h index 66d22efe67a..a05371670a6 100644 --- a/lib/ios/RNNEventEmitter.h +++ b/lib/ios/RNNEventEmitter.h @@ -14,6 +14,8 @@ - (void)sendBottomTabSelected:(NSNumber *)selectedTabIndex unselected:(NSNumber*)unselectedTabIndex; +- (void)sendBottomTabLongPressed:(NSNumber *)selectedTabIndex; + - (void)sendOnNavigationCommandCompletion:(NSString *)commandName commandId:(NSString *)commandId params:(NSDictionary*)params; - (void)sendOnSearchBarUpdated:(NSString *)componentId text:(NSString*)text isFocused:(BOOL)isFocused; diff --git a/lib/ios/RNNEventEmitter.m b/lib/ios/RNNEventEmitter.m index 35f2443bede..5ca911cdb0b 100644 --- a/lib/ios/RNNEventEmitter.m +++ b/lib/ios/RNNEventEmitter.m @@ -11,6 +11,7 @@ @implementation RNNEventEmitter { static NSString* const AppLaunched = @"RNN.AppLaunched"; static NSString* const CommandCompleted = @"RNN.CommandCompleted"; static NSString* const BottomTabSelected = @"RNN.BottomTabSelected"; +static NSString* const BottomTabLongPressed = @"RNN.BottomTabLongPressed"; static NSString* const ComponentDidAppear = @"RNN.ComponentDidAppear"; static NSString* const ComponentDidDisappear = @"RNN.ComponentDidDisappear"; static NSString* const NavigationButtonPressed = @"RNN.NavigationButtonPressed"; @@ -21,19 +22,20 @@ @implementation RNNEventEmitter { static NSString* const PreviewCompleted = @"RNN.PreviewCompleted"; static NSString* const ScreenPopped = @"RNN.ScreenPopped"; -- (NSArray *)supportedEvents { - return @[AppLaunched, - CommandCompleted, - BottomTabSelected, - ComponentDidAppear, - ComponentDidDisappear, - NavigationButtonPressed, - ModalDismissed, - SearchBarUpdated, - SearchBarCancelPressed, - PreviewCompleted, - ScreenPopped, - ModalAttemptedToDismiss]; +-(NSArray *)supportedEvents { + return @[AppLaunched, + CommandCompleted, + BottomTabSelected, + BottomTabLongPressed, + ComponentDidAppear, + ComponentDidDisappear, + NavigationButtonPressed, + ModalDismissed, + SearchBarUpdated, + SearchBarCancelPressed, + PreviewCompleted, + ScreenPopped, + ModalAttemptedToDismiss]; } # pragma mark public @@ -76,13 +78,19 @@ - (void)sendBottomTabSelected:(NSNumber *)selectedTabIndex unselected:(NSNumber* }]; } -- (void)sendOnNavigationCommandCompletion:(NSString *)commandName commandId:(NSString *)commandId params:(NSDictionary*)params { - [self send:CommandCompleted body:@{ - @"commandId":commandId, - @"commandName":commandName, - @"params": params, - @"completionTime": [RNNUtils getCurrentTimestamp] - }]; +- (void)sendBottomTabLongPressed:(NSNumber *)selectedTabIndex { + [self send:BottomTabLongPressed body:@{ + @"selectedTabIndex": selectedTabIndex + }]; +} + +-(void)sendOnNavigationCommandCompletion:(NSString *)commandName commandId:(NSString *)commandId params:(NSDictionary*)params { + [self send:CommandCompleted body:@{ + @"commandId":commandId, + @"commandName":commandName, + @"params": params, + @"completionTime": [RNNUtils getCurrentTimestamp] + }]; } - (void)sendOnSearchBarUpdated:(NSString *)componentId diff --git a/lib/src/adapters/NativeEventsReceiver.ts b/lib/src/adapters/NativeEventsReceiver.ts index eda39da28a3..2e82d865749 100644 --- a/lib/src/adapters/NativeEventsReceiver.ts +++ b/lib/src/adapters/NativeEventsReceiver.ts @@ -10,7 +10,7 @@ import { ScreenPoppedEvent, ModalAttemptedToDismissEvent } from '../interfaces/ComponentEvents'; -import { CommandCompletedEvent, BottomTabSelectedEvent } from '../interfaces/Events'; +import { CommandCompletedEvent, BottomTabSelectedEvent, BottomTabLongPressedEvent } from '../interfaces/Events'; export class NativeEventsReceiver { private emitter: EventEmitter; @@ -74,6 +74,10 @@ export class NativeEventsReceiver { return this.emitter.addListener('RNN.BottomTabSelected', callback); } + public registerBottomTabLongPressedListener(callback: (data: BottomTabLongPressedEvent) => void): EmitterSubscription { + return this.emitter.addListener('RNN.BottomTabLongPressed', callback); + } + public registerScreenPoppedListener(callback: (event: ScreenPoppedEvent) => void): EmitterSubscription { return this.emitter.addListener('RNN.ScreenPopped', callback); } diff --git a/lib/src/events/EventsRegistry.ts b/lib/src/events/EventsRegistry.ts index 86a5ad96d11..b5efe2ce16e 100644 --- a/lib/src/events/EventsRegistry.ts +++ b/lib/src/events/EventsRegistry.ts @@ -15,7 +15,7 @@ import { ScreenPoppedEvent, ModalAttemptedToDismissEvent } from '../interfaces/ComponentEvents'; -import { CommandCompletedEvent, BottomTabSelectedEvent } from '../interfaces/Events'; +import { CommandCompletedEvent, BottomTabSelectedEvent, BottomTabLongPressedEvent } from '../interfaces/Events'; export class EventsRegistry { constructor(private nativeEventsReceiver: NativeEventsReceiver, private commandsObserver: CommandsObserver, private componentEventsObserver: ComponentEventsObserver) { } @@ -40,6 +40,10 @@ export class EventsRegistry { return this.nativeEventsReceiver.registerBottomTabSelectedListener(callback); } + public registerBottomTabLongPressedListener(callback: (event: BottomTabLongPressedEvent) => void): EmitterSubscription { + return this.nativeEventsReceiver.registerBottomTabLongPressedListener(callback); + } + public registerNavigationButtonPressedListener(callback: (event: NavigationButtonPressedEvent) => void): EmitterSubscription { return this.nativeEventsReceiver.registerNavigationButtonPressedListener(callback); } diff --git a/lib/src/interfaces/Events.ts b/lib/src/interfaces/Events.ts index 8c2beed45a5..6da5adc0f90 100644 --- a/lib/src/interfaces/Events.ts +++ b/lib/src/interfaces/Events.ts @@ -9,3 +9,7 @@ export interface BottomTabSelectedEvent { selectedTabIndex: number; unselectedTabIndex: number; } + +export interface BottomTabLongPressedEvent { + selectedTabIndex: number; +}