Преглед на файлове

Add bottomTabLongPressed event on iOS (#5784)

* Add bottomTabLongPressed event on iOS

* Fix indent

Co-authored-by: Yogev Ben David <yogevbd@wix.com>
N3TC4T преди 5 години
родител
ревизия
c425f837b1

+ 13
- 0
docs/docs/events.md Целия файл

@@ -192,6 +192,19 @@ const bottomTabEventListener = Navigation.events().registerBottomTabSelectedList
192 192
 bottomTabEventListener.remove();
193 193
 ```
194 194
 
195
+## registerBottomTabLongPressedListener
196
+Invoked when a BottomTab is long pressed by the user.
197
+
198
+```js
199
+// Subscribe
200
+const bottomTabEventListener = Navigation.events().registerBottomTabLongPressedListener(({ selectedTabIndex }) => {
201
+
202
+});
203
+...
204
+// Unsubscribe
205
+bottomTabEventListener.remove();
206
+```
207
+
195 208
 |       Parameter         | Description |
196 209
 |:--------------------:|:-----|
197 210
 |**selectedTabIndex** | The index of the newly selected tab|

+ 15
- 1
lib/ios/RNNBottomTabsController.m Целия файл

@@ -28,7 +28,14 @@
28 28
 }
29 29
 
30 30
 - (void)viewDidLayoutSubviews {
31
-	[self.presenter viewDidLayoutSubviews];
31
+    [self.presenter viewDidLayoutSubviews];
32
+    
33
+    for (UIView *view in [[self tabBar] subviews]) {
34
+         UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleLongPress:)];
35
+          if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) {
36
+              [view addGestureRecognizer: longPressGesture];
37
+          }
38
+    }
32 39
 }
33 40
 
34 41
 - (UIViewController *)getCurrentChild {
@@ -65,4 +72,11 @@
65 72
 	_currentTabIndex = tabBarController.selectedIndex;
66 73
 }
67 74
 
75
+- (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer {
76
+    if (recognizer.state == UIGestureRecognizerStateBegan) {
77
+        NSUInteger _index = [self.tabBar.subviews indexOfObject:(UIView *)recognizer.view];
78
+        [self.eventEmitter sendBottomTabLongPressed:@(_index)];
79
+    }
80
+}
81
+
68 82
 @end

+ 2
- 0
lib/ios/RNNEventEmitter.h Целия файл

@@ -14,6 +14,8 @@
14 14
 
15 15
 - (void)sendBottomTabSelected:(NSNumber *)selectedTabIndex unselected:(NSNumber*)unselectedTabIndex;
16 16
 
17
+- (void)sendBottomTabLongPressed:(NSNumber *)selectedTabIndex;
18
+
17 19
 - (void)sendOnNavigationCommandCompletion:(NSString *)commandName commandId:(NSString *)commandId params:(NSDictionary*)params;
18 20
 
19 21
 - (void)sendOnSearchBarUpdated:(NSString *)componentId text:(NSString*)text isFocused:(BOOL)isFocused;

+ 28
- 20
lib/ios/RNNEventEmitter.m Целия файл

@@ -11,6 +11,7 @@ RCT_EXPORT_MODULE();
11 11
 static NSString* const AppLaunched				= @"RNN.AppLaunched";
12 12
 static NSString* const CommandCompleted			= @"RNN.CommandCompleted";
13 13
 static NSString* const BottomTabSelected		= @"RNN.BottomTabSelected";
14
+static NSString* const BottomTabLongPressed     = @"RNN.BottomTabLongPressed";
14 15
 static NSString* const ComponentDidAppear		= @"RNN.ComponentDidAppear";
15 16
 static NSString* const ComponentDidDisappear	= @"RNN.ComponentDidDisappear";
16 17
 static NSString* const NavigationButtonPressed	= @"RNN.NavigationButtonPressed";
@@ -21,19 +22,20 @@ static NSString* const SearchBarCancelPressed 	= @"RNN.SearchBarCancelPressed";
21 22
 static NSString* const PreviewCompleted         = @"RNN.PreviewCompleted";
22 23
 static NSString* const ScreenPopped             = @"RNN.ScreenPopped";
23 24
 
24
-- (NSArray<NSString *> *)supportedEvents {
25
-    return @[AppLaunched,
26
-             CommandCompleted,
27
-             BottomTabSelected,
28
-             ComponentDidAppear,
29
-             ComponentDidDisappear,
30
-             NavigationButtonPressed,
31
-             ModalDismissed,
32
-             SearchBarUpdated,
33
-             SearchBarCancelPressed,
34
-             PreviewCompleted,
35
-             ScreenPopped,
36
-             ModalAttemptedToDismiss];
25
+-(NSArray<NSString *> *)supportedEvents {
26
+	return @[AppLaunched,
27
+			 CommandCompleted,
28
+			 BottomTabSelected,
29
+       BottomTabLongPressed,
30
+			 ComponentDidAppear,
31
+			 ComponentDidDisappear,
32
+			 NavigationButtonPressed,
33
+			 ModalDismissed,
34
+			 SearchBarUpdated,
35
+			 SearchBarCancelPressed,
36
+			 PreviewCompleted,
37
+       ScreenPopped,
38
+       ModalAttemptedToDismiss];
37 39
 }
38 40
 
39 41
 # pragma mark public
@@ -76,13 +78,19 @@ static NSString* const ScreenPopped             = @"RNN.ScreenPopped";
76 78
     }];
77 79
 }
78 80
 
79
-- (void)sendOnNavigationCommandCompletion:(NSString *)commandName commandId:(NSString *)commandId params:(NSDictionary*)params {
80
-    [self send:CommandCompleted body:@{
81
-        @"commandId":commandId,
82
-        @"commandName":commandName,
83
-        @"params": params,
84
-        @"completionTime": [RNNUtils getCurrentTimestamp]
85
-    }];
81
+- (void)sendBottomTabLongPressed:(NSNumber *)selectedTabIndex {
82
+    [self send:BottomTabLongPressed body:@{
83
+                                        				  @"selectedTabIndex": selectedTabIndex
84
+                                        				  }];
85
+}
86
+
87
+-(void)sendOnNavigationCommandCompletion:(NSString *)commandName commandId:(NSString *)commandId params:(NSDictionary*)params {
88
+	[self send:CommandCompleted body:@{
89
+									   @"commandId":commandId,
90
+									   @"commandName":commandName,
91
+									   @"params": params,
92
+									   @"completionTime": [RNNUtils getCurrentTimestamp]
93
+									   }];
86 94
 }
87 95
 
88 96
 - (void)sendOnSearchBarUpdated:(NSString *)componentId

+ 5
- 1
lib/src/adapters/NativeEventsReceiver.ts Целия файл

@@ -10,7 +10,7 @@ import {
10 10
   ScreenPoppedEvent,
11 11
   ModalAttemptedToDismissEvent
12 12
 } from '../interfaces/ComponentEvents';
13
-import { CommandCompletedEvent, BottomTabSelectedEvent } from '../interfaces/Events';
13
+import { CommandCompletedEvent, BottomTabSelectedEvent, BottomTabLongPressedEvent } from '../interfaces/Events';
14 14
 
15 15
 export class NativeEventsReceiver {
16 16
   private emitter: EventEmitter;
@@ -74,6 +74,10 @@ export class NativeEventsReceiver {
74 74
     return this.emitter.addListener('RNN.BottomTabSelected', callback);
75 75
   }
76 76
 
77
+  public registerBottomTabLongPressedListener(callback: (data: BottomTabLongPressedEvent) => void): EmitterSubscription {
78
+    return this.emitter.addListener('RNN.BottomTabLongPressed', callback);
79
+  }
80
+
77 81
   public registerScreenPoppedListener(callback: (event: ScreenPoppedEvent) => void): EmitterSubscription {
78 82
     return this.emitter.addListener('RNN.ScreenPopped', callback);
79 83
   }

+ 5
- 1
lib/src/events/EventsRegistry.ts Целия файл

@@ -15,7 +15,7 @@ import {
15 15
   ScreenPoppedEvent,
16 16
   ModalAttemptedToDismissEvent
17 17
 } from '../interfaces/ComponentEvents';
18
-import { CommandCompletedEvent, BottomTabSelectedEvent } from '../interfaces/Events';
18
+import { CommandCompletedEvent, BottomTabSelectedEvent, BottomTabLongPressedEvent } from '../interfaces/Events';
19 19
 
20 20
 export class EventsRegistry {
21 21
   constructor(private nativeEventsReceiver: NativeEventsReceiver, private commandsObserver: CommandsObserver, private componentEventsObserver: ComponentEventsObserver) { }
@@ -40,6 +40,10 @@ export class EventsRegistry {
40 40
     return this.nativeEventsReceiver.registerBottomTabSelectedListener(callback);
41 41
   }
42 42
 
43
+  public registerBottomTabLongPressedListener(callback: (event: BottomTabLongPressedEvent) => void): EmitterSubscription {
44
+    return this.nativeEventsReceiver.registerBottomTabLongPressedListener(callback);
45
+  }
46
+
43 47
   public registerNavigationButtonPressedListener(callback: (event: NavigationButtonPressedEvent) => void): EmitterSubscription {
44 48
     return this.nativeEventsReceiver.registerNavigationButtonPressedListener(callback);
45 49
   }

+ 4
- 0
lib/src/interfaces/Events.ts Целия файл

@@ -9,3 +9,7 @@ export interface BottomTabSelectedEvent {
9 9
   selectedTabIndex: number;
10 10
   unselectedTabIndex: number;
11 11
 }
12
+
13
+export interface BottomTabLongPressedEvent {
14
+  selectedTabIndex: number;
15
+}