Skip to content

Commit

Permalink
Add bottomTabLongPressed event on iOS (#5784)
Browse files Browse the repository at this point in the history
* Add bottomTabLongPressed event on iOS

* Fix indent

Co-authored-by: Yogev Ben David <yogevbd@wix.com>
  • Loading branch information
N3TC4T and yogevbd committed Jan 12, 2020
1 parent 87af42a commit c425f83
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 23 deletions.
13 changes: 13 additions & 0 deletions docs/docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
16 changes: 15 additions & 1 deletion lib/ios/RNNBottomTabsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/ios/RNNEventEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
48 changes: 28 additions & 20 deletions lib/ios/RNNEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -21,19 +22,20 @@ @implementation RNNEventEmitter {
static NSString* const PreviewCompleted = @"RNN.PreviewCompleted";
static NSString* const ScreenPopped = @"RNN.ScreenPopped";

- (NSArray<NSString *> *)supportedEvents {
return @[AppLaunched,
CommandCompleted,
BottomTabSelected,
ComponentDidAppear,
ComponentDidDisappear,
NavigationButtonPressed,
ModalDismissed,
SearchBarUpdated,
SearchBarCancelPressed,
PreviewCompleted,
ScreenPopped,
ModalAttemptedToDismiss];
-(NSArray<NSString *> *)supportedEvents {
return @[AppLaunched,
CommandCompleted,
BottomTabSelected,
BottomTabLongPressed,
ComponentDidAppear,
ComponentDidDisappear,
NavigationButtonPressed,
ModalDismissed,
SearchBarUpdated,
SearchBarCancelPressed,
PreviewCompleted,
ScreenPopped,
ModalAttemptedToDismiss];
}

# pragma mark public
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/src/adapters/NativeEventsReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/src/events/EventsRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand All @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/interfaces/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export interface BottomTabSelectedEvent {
selectedTabIndex: number;
unselectedTabIndex: number;
}

export interface BottomTabLongPressedEvent {
selectedTabIndex: number;
}

0 comments on commit c425f83

Please sign in to comment.