2626#import " RCTDevMenu.h"
2727#endif
2828
29+ NSString *const RCTTVRemoteEventMenu = @" menu" ;
30+ NSString *const RCTTVRemoteEventPlayPause = @" playPause" ;
31+ NSString *const RCTTVRemoteEventSelect = @" select" ;
32+
33+ NSString *const RCTTVRemoteEventLongPlayPause = @" longPlayPause" ;
34+ NSString *const RCTTVRemoteEventLongSelect = @" longSelect" ;
35+
36+ NSString *const RCTTVRemoteEventLeft = @" left" ;
37+ NSString *const RCTTVRemoteEventRight = @" right" ;
38+ NSString *const RCTTVRemoteEventUp = @" up" ;
39+ NSString *const RCTTVRemoteEventDown = @" down" ;
40+
41+ NSString *const RCTTVRemoteEventSwipeLeft = @" swipeLeft" ;
42+ NSString *const RCTTVRemoteEventSwipeRight = @" swipeRight" ;
43+ NSString *const RCTTVRemoteEventSwipeUp = @" swipeUp" ;
44+ NSString *const RCTTVRemoteEventSwipeDown = @" swipeDown" ;
45+
46+
2947@implementation RCTTVRemoteHandler {
30- NSMutableArray < UIGestureRecognizer *> *_tvRemoteGestureRecognizers;
48+ NSMutableDictionary < NSString *, UIGestureRecognizer *> *_tvRemoteGestureRecognizers;
3149}
3250
3351- (instancetype )init
3452{
3553 if ((self = [super init ])) {
36- _tvRemoteGestureRecognizers = [NSMutableArray array ];
54+ _tvRemoteGestureRecognizers = [NSMutableDictionary dictionary ];
3755
3856 // Recognizers for Apple TV remote buttons
3957
4058 // Play/Pause
4159 [self addTapGestureRecognizerWithSelector: @selector (playPausePressed: )
42- pressType: UIPressTypePlayPause];
60+ pressType: UIPressTypePlayPause
61+ name: RCTTVRemoteEventPlayPause];
4362
4463 // Menu
4564 [self addTapGestureRecognizerWithSelector: @selector (menuPressed: )
46- pressType: UIPressTypeMenu];
65+ pressType: UIPressTypeMenu
66+ name: RCTTVRemoteEventMenu];
4767
4868 // Select
4969 [self addTapGestureRecognizerWithSelector: @selector (selectPressed: )
50- pressType: UIPressTypeSelect];
70+ pressType: UIPressTypeSelect
71+ name: RCTTVRemoteEventSelect];
5172
5273 // Up
5374 [self addTapGestureRecognizerWithSelector: @selector (swipedUp: )
54- pressType: UIPressTypeUpArrow];
75+ pressType: UIPressTypeUpArrow
76+ name: RCTTVRemoteEventUp];
5577
5678 // Down
5779 [self addTapGestureRecognizerWithSelector: @selector (swipedDown: )
58- pressType: UIPressTypeDownArrow];
80+ pressType: UIPressTypeDownArrow
81+ name: RCTTVRemoteEventDown];
5982
6083 // Left
6184 [self addTapGestureRecognizerWithSelector: @selector (swipedLeft: )
62- pressType: UIPressTypeLeftArrow];
85+ pressType: UIPressTypeLeftArrow
86+ name: RCTTVRemoteEventLeft];
6387
6488 // Right
6589 [self addTapGestureRecognizerWithSelector: @selector (swipedRight: )
66- pressType: UIPressTypeRightArrow];
90+ pressType: UIPressTypeRightArrow
91+ name: RCTTVRemoteEventRight];
6792
6893 // Recognizers for long button presses
6994 // We don't intercept long menu press -- that's used by the system to go to the home screen
7095
7196 [self addLongPressGestureRecognizerWithSelector: @selector (longPlayPausePressed: )
72- pressType: UIPressTypePlayPause];
97+ pressType: UIPressTypePlayPause
98+ name: RCTTVRemoteEventLongPlayPause];
7399
74100 [self addLongPressGestureRecognizerWithSelector: @selector (longSelectPressed: )
75- pressType: UIPressTypeSelect];
101+ pressType: UIPressTypeSelect
102+ name: RCTTVRemoteEventLongSelect];
76103
77104 // Recognizers for Apple TV remote trackpad swipes
78105
79106 // Up
80107 [self addSwipeGestureRecognizerWithSelector: @selector (swipedUp: )
81- direction: UISwipeGestureRecognizerDirectionUp];
108+ direction: UISwipeGestureRecognizerDirectionUp
109+ name: RCTTVRemoteEventSwipeUp];
82110
83111 // Down
84112 [self addSwipeGestureRecognizerWithSelector: @selector (swipedDown: )
85- direction: UISwipeGestureRecognizerDirectionDown];
113+ direction: UISwipeGestureRecognizerDirectionDown
114+ name: RCTTVRemoteEventSwipeDown];
86115
87116 // Left
88117 [self addSwipeGestureRecognizerWithSelector: @selector (swipedLeft: )
89- direction: UISwipeGestureRecognizerDirectionLeft];
118+ direction: UISwipeGestureRecognizerDirectionLeft
119+ name: RCTTVRemoteEventSwipeLeft];
90120
91121 // Right
92122 [self addSwipeGestureRecognizerWithSelector: @selector (swipedRight: )
93- direction: UISwipeGestureRecognizerDirectionRight];
123+ direction: UISwipeGestureRecognizerDirectionRight
124+ name: RCTTVRemoteEventSwipeRight];
94125
95126 }
96127
@@ -99,22 +130,22 @@ - (instancetype)init
99130
100131- (void )playPausePressed : (UIGestureRecognizer *)r
101132{
102- [self sendAppleTVEvent: @" playPause " toView: r.view];
133+ [self sendAppleTVEvent: RCTTVRemoteEventPlayPause toView: r.view];
103134}
104135
105136- (void )menuPressed : (UIGestureRecognizer *)r
106137{
107- [self sendAppleTVEvent: @" menu " toView: r.view];
138+ [self sendAppleTVEvent: RCTTVRemoteEventMenu toView: r.view];
108139}
109140
110141- (void )selectPressed : (UIGestureRecognizer *)r
111142{
112- [self sendAppleTVEvent: @" select " toView: r.view];
143+ [self sendAppleTVEvent: RCTTVRemoteEventSelect toView: r.view];
113144}
114145
115146- (void )longPlayPausePressed : (UIGestureRecognizer *)r
116147{
117- [self sendAppleTVEvent: @" longPlayPause " toView: r.view];
148+ [self sendAppleTVEvent: RCTTVRemoteEventLongPlayPause toView: r.view];
118149
119150#if __has_include("RCTDevMenu.h") && RCT_DEV
120151 // If shake to show is enabled on device, use long play/pause event to show dev menu
@@ -124,53 +155,53 @@ - (void)longPlayPausePressed:(UIGestureRecognizer *)r
124155
125156- (void )longSelectPressed : (UIGestureRecognizer *)r
126157{
127- [self sendAppleTVEvent: @" longSelect " toView: r.view];
158+ [self sendAppleTVEvent: RCTTVRemoteEventLongSelect toView: r.view];
128159}
129160
130161- (void )swipedUp : (UIGestureRecognizer *)r
131162{
132- [self sendAppleTVEvent: @" up " toView: r.view];
163+ [self sendAppleTVEvent: RCTTVRemoteEventUp toView: r.view];
133164}
134165
135166- (void )swipedDown : (UIGestureRecognizer *)r
136167{
137- [self sendAppleTVEvent: @" down " toView: r.view];
168+ [self sendAppleTVEvent: RCTTVRemoteEventDown toView: r.view];
138169}
139170
140171- (void )swipedLeft : (UIGestureRecognizer *)r
141172{
142- [self sendAppleTVEvent: @" left " toView: r.view];
173+ [self sendAppleTVEvent: RCTTVRemoteEventLeft toView: r.view];
143174}
144175
145176- (void )swipedRight : (UIGestureRecognizer *)r
146177{
147- [self sendAppleTVEvent: @" right " toView: r.view];
178+ [self sendAppleTVEvent: RCTTVRemoteEventRight toView: r.view];
148179}
149180
150181#pragma mark -
151182
152- - (void )addLongPressGestureRecognizerWithSelector : (nonnull SEL )selector pressType : (UIPressType)pressType
183+ - (void )addLongPressGestureRecognizerWithSelector : (nonnull SEL )selector pressType : (UIPressType)pressType name : ( NSString *) name
153184{
154185 UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc ] initWithTarget: self action: selector];
155186 recognizer.allowedPressTypes = @[@(pressType)];
156187
157- [ _tvRemoteGestureRecognizers addObject: recognizer] ;
188+ _tvRemoteGestureRecognizers[name] = recognizer;
158189}
159190
160- - (void )addTapGestureRecognizerWithSelector : (nonnull SEL )selector pressType : (UIPressType)pressType
191+ - (void )addTapGestureRecognizerWithSelector : (nonnull SEL )selector pressType : (UIPressType)pressType name : ( NSString *) name
161192{
162193 UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc ] initWithTarget: self action: selector];
163194 recognizer.allowedPressTypes = @[@(pressType)];
164195
165- [ _tvRemoteGestureRecognizers addObject: recognizer] ;
196+ _tvRemoteGestureRecognizers[name] = recognizer;
166197}
167198
168- - (void )addSwipeGestureRecognizerWithSelector : (nonnull SEL )selector direction : (UISwipeGestureRecognizerDirection)direction
199+ - (void )addSwipeGestureRecognizerWithSelector : (nonnull SEL )selector direction : (UISwipeGestureRecognizerDirection)direction name : ( NSString *) name
169200{
170201 UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc ] initWithTarget: self action: selector];
171202 recognizer.direction = direction;
172203
173- [ _tvRemoteGestureRecognizers addObject: recognizer] ;
204+ _tvRemoteGestureRecognizers[name] = recognizer;
174205}
175206
176207- (void )sendAppleTVEvent : (NSString *)eventType toView : (__unused UIView *)v
0 commit comments