forked from davidohayon669/react-native-youtube
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RCTYouTubeManager.m
117 lines (95 loc) · 4.29 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
#import "RCTYouTubeManager.h"
#import "RCTYouTube.h"
#import <React/RCTUIManager.h>
@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];
[youtube seekToSeconds:seconds.floatValue allowSeekAhead:YES];
}];
}
RCT_EXPORT_METHOD(nextVideo:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RCTYouTube *youtube = (RCTYouTube*)viewRegistry[reactTag];
[youtube nextVideo];
}];
}
RCT_EXPORT_METHOD(previousVideo:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
RCTYouTube *youtube = (RCTYouTube*)viewRegistry[reactTag];
[youtube previousVideo];
}];
}
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];
[youtube playVideoAt:(int)[index integerValue]];
}];
}
RCT_EXPORT_METHOD(getVideosIndex:(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];
[youtube getPlaylistIndex:^(int response, NSError * _Nullable error) {
if (error) {
reject(@"Error getting index of video from RCTYouTube", @"", error);
} else {
NSNumber *index = [NSNumber numberWithInt:response];
resolve(index);
}
}];
}];
}
RCT_EXPORT_METHOD(getCurrentTime:(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];
[youtube getCurrentTime:^(float response, NSError * _Nullable error) {
if (error) {
reject(@"Error getting current time of video from RCTYouTube", @"", error);
} else {
NSNumber *currentTime = [NSNumber numberWithInt:response];
resolve(currentTime);
}
}];
}];
}
RCT_EXPORT_METHOD(getDuration:(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];
[youtube getDuration:^(NSTimeInterval response, NSError * _Nullable error) {
if (error) {
reject(@"Error getting duration of video from RCTYouTube", @"", error);
} else {
NSNumber *duration = [NSNumber numberWithInt:response];
resolve(duration);
}
}];
}];
}
@end