forked from Shuangzuan/RCTRefreshControl
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDropRefreshControl.m
108 lines (79 loc) · 3.55 KB
/
DropRefreshControl.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
#import "DropRefreshControl.h"
#import "ODRefreshControl.h"
#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
#import <React/RCTScrollView.h>
#import <React/RCTUIManager.h>
#import <React/RCTEventDispatcher.h>
@implementation DropRefreshControl
#pragma mark -
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
- (dispatch_queue_t)methodQueue {
return self.bridge.uiManager.methodQueue;
}
RCT_EXPORT_METHOD(configure:(nonnull NSNumber *)reactTag
options:(NSDictionary *)options
callback:(RCTResponseSenderBlock)callback) {
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) {
UIView *view = viewRegistry[reactTag];
if (!view) {
RCTLogError(@"Cannot find view with tag #%@", reactTag);
return;
}
UIScrollView *scrollView = ((RCTScrollView *)view).scrollView;
ODRefreshControl *refreshControl = [[ODRefreshControl alloc] initInScrollView:scrollView];
refreshControl.tag = [reactTag integerValue]; // Maybe something better
UIColor *tintColor = options[@"tintColor"];
// TODO: activityIndicatorViewStyle
UIColor *activityIndicatorViewColor = options[@"activityIndicatorViewColor"];
if (tintColor) refreshControl.tintColor = [RCTConvert UIColor:tintColor];
if (activityIndicatorViewColor) refreshControl.activityIndicatorViewColor = [RCTConvert UIColor:activityIndicatorViewColor];
[refreshControl addTarget:self action:@selector(dropViewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];
callback(@[[NSNull null], reactTag]);
}];
}
RCT_EXPORT_METHOD(beginRefreshing:(nonnull NSNumber *)reactTag) {
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) {
UIView *view = viewRegistry[reactTag];
if (!view) {
RCTLogError(@"Cannot find view with tag #%@", reactTag);
return;
}
UIScrollView *scrollView = ((RCTScrollView *)view).scrollView;
ODRefreshControl *refreshControl = (ODRefreshControl *)[scrollView viewWithTag:[reactTag integerValue]];
dispatch_async(dispatch_get_main_queue(), ^{
[refreshControl beginRefreshing];
});
}];
}
RCT_EXPORT_METHOD(endRefreshing:(nonnull NSNumber *)reactTag) {
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) {
UIView *view = viewRegistry[reactTag];
if (!view) {
RCTLogError(@"Cannot find view with tag #%@", reactTag);
return;
}
UIScrollView *scrollView = ((RCTScrollView *)view).scrollView;
ODRefreshControl *refreshControl = (ODRefreshControl *)[scrollView viewWithTag:[reactTag integerValue]];
dispatch_async(dispatch_get_main_queue(), ^{
[refreshControl endRefreshing];
});
}];
}
- (NSDictionary *)constantsToExport {
return @{@"UIActivityIndicatorViewStyleWhiteLarge": @(UIActivityIndicatorViewStyleWhiteLarge),
@"UIActivityIndicatorViewStyleWhite": @(UIActivityIndicatorViewStyleWhite),
@"UIActivityIndicatorViewStyleGray": @(UIActivityIndicatorViewStyleGray)};
}
- (void)dropViewDidBeginRefreshing:(ODRefreshControl *)refreshControl {
[self.bridge.eventDispatcher sendDeviceEventWithName:@"dropViewDidBeginRefreshing"
body:@(refreshControl.tag)];
/*
double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[refreshControl endRefreshing];
}); */
}
@end