forked from davidohayon669/react-native-youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCTYouTubeManager.m
121 lines (107 loc) · 4.7 KB
/
RCTYouTubeManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#import "RCTYouTubeManager.h"
#import "RCTYouTube.h"
#if __has_include(<React/RCTAssert.h>)
#import <React/RCTUIManager.h>
#else // backwards compatibility for RN < 0.40
#import "RCTUIManager.h"
#endif
@implementation RCTYouTubeManager
RCT_EXPORT_MODULE();
@synthesize bridge = _bridge;
- (UIView *)view {
return [[RCTYouTube alloc] initWithBridge:self.bridge];
}
- (dispatch_queue_t)methodQueue {
return _bridge.uiManager.methodQueue;
}
RCT_EXPORT_VIEW_PROPERTY(playerParams, NSDictionary);
RCT_EXPORT_VIEW_PROPERTY(videoId, NSString);
RCT_EXPORT_VIEW_PROPERTY(videoIds, NSArray);
RCT_EXPORT_VIEW_PROPERTY(playlistId, NSString);
RCT_EXPORT_VIEW_PROPERTY(play, BOOL);
RCT_EXPORT_VIEW_PROPERTY(loopProp, BOOL);
RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onReady, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onChangeState, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onChangeQuality, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onChangeFullscreen, RCTDirectEventBlock);
RCT_EXPORT_VIEW_PROPERTY(onProgress, RCTDirectEventBlock);
RCT_EXPORT_METHOD(seekTo:(nonnull NSNumber *)reactTag seconds:(nonnull NSNumber *)seconds)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RCTYouTube *youtube = (RCTYouTube*)viewRegistry[reactTag];
if ([youtube isKindOfClass:[RCTYouTube class]]) {
[youtube seekToSeconds:seconds.floatValue allowSeekAhead:YES];
} else {
RCTLogError(@"Cannot seekTo: %@ (tag #%@) is not RCTYouTube", youtube, reactTag);
}
}];
}
RCT_EXPORT_METHOD(nextVideo:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RCTYouTube *youtube = (RCTYouTube*)viewRegistry[reactTag];
if ([youtube isKindOfClass:[RCTYouTube class]]) {
[youtube nextVideo];
} else {
RCTLogError(@"Cannot nextVideo: %@ (tag #%@) is not RCTYouTube", youtube, reactTag);
}
}];
}
RCT_EXPORT_METHOD(previousVideo:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RCTYouTube *youtube = (RCTYouTube*)viewRegistry[reactTag];
if ([youtube isKindOfClass:[RCTYouTube class]]) {
[youtube previousVideo];
} else {
RCTLogError(@"Cannot previousVideo: %@ (tag #%@) is not RCTYouTube", youtube, reactTag);
}
}];
}
RCT_EXPORT_METHOD(playVideoAt:(nonnull NSNumber *)reactTag index:(nonnull NSNumber *)index)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RCTYouTube *youtube = (RCTYouTube*)viewRegistry[reactTag];
if ([youtube isKindOfClass:[RCTYouTube class]]) {
[youtube playVideoAt:(int)[index integerValue]];
} else {
RCTLogError(@"Cannot playVideoAt: %@ (tag #%@) is not RCTYouTube", youtube, reactTag);
}
}];
}
RCT_EXPORT_METHOD(videosIndex:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RCTYouTube *youtube = (RCTYouTube*)viewRegistry[reactTag];
if ([youtube isKindOfClass:[RCTYouTube class]]) {
NSNumber *index = [NSNumber numberWithInt:[youtube playlistIndex]];
if (index) {
resolve(index);
} else {
NSError *error = nil;
reject(@"Error getting index of video from RCTYouTube", @"", error);
}
} else {
RCTLogError(@"Cannot videosIndex: %@ (tag #%@) is not RCTYouTube", youtube, reactTag);
}
}];
}
RCT_EXPORT_METHOD(currentTime:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RCTYouTube *youtube = (RCTYouTube*)viewRegistry[reactTag];
if ([youtube isKindOfClass:[RCTYouTube class]]) {
NSNumber *index = [NSNumber numberWithInt:[youtube currentTime]];
if (index) {
resolve(index);
} else {
NSError *error = nil;
reject(@"Error getting current time of video from RCTYouTube", @"", error);
}
} else {
RCTLogError(@"Cannot currentTime: %@ (tag #%@) is not RCTYouTube", youtube, reactTag);
}
}];
}
@end