|
| 1 | +// |
| 2 | +// RNMBXViewResolver.mm |
| 3 | +// |
| 4 | +// Implementation of cross-architecture view resolver utility |
| 5 | +// |
| 6 | + |
| 7 | +#import "RNMBXViewResolver.h" |
| 8 | + |
| 9 | +@implementation RNMBXViewResolver |
| 10 | + |
| 11 | ++ (void)withViewRef:(NSNumber *)viewRef |
| 12 | + delegate:(id<RNMBXViewResolverDelegate>)delegate |
| 13 | + expectedClass:(nullable Class)expectedClass |
| 14 | + block:(void (^)(UIView *view))block |
| 15 | + reject:(RCTPromiseRejectBlock)reject |
| 16 | + methodName:(NSString *)methodName { |
| 17 | + |
| 18 | + if (!delegate) { |
| 19 | + reject(@"no_delegate", @"Delegate is required", nil); |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + if (!viewRef) { |
| 24 | + reject(@"no_view_ref", @"View reference is required", nil); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + if (!block) { |
| 29 | + reject(@"no_block", @"Completion block is required", nil); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | +#ifdef RCT_NEW_ARCH_ENABLED |
| 34 | + [delegate.viewRegistry_DEPRECATED addUIBlock:^(RCTViewRegistry *viewRegistry) { |
| 35 | + [self resolveViewWithPolling:viewRef |
| 36 | + delegate:delegate |
| 37 | + expectedClass:expectedClass |
| 38 | + block:block |
| 39 | + reject:reject |
| 40 | + methodName:methodName |
| 41 | + attemptCount:0 |
| 42 | + startTime:nil]; |
| 43 | + }]; |
| 44 | +#else |
| 45 | + [delegate.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) { |
| 46 | + [self resolveViewWithPolling:viewRef |
| 47 | + delegate:delegate |
| 48 | + expectedClass:expectedClass |
| 49 | + block:block |
| 50 | + reject:reject |
| 51 | + methodName:methodName |
| 52 | + attemptCount:0 |
| 53 | + startTime:nil]; |
| 54 | + }]; |
| 55 | +#endif |
| 56 | +} |
| 57 | + |
| 58 | ++ (void)resolveViewWithPolling:(NSNumber *)viewRef |
| 59 | + delegate:(id<RNMBXViewResolverDelegate>)delegate |
| 60 | + expectedClass:(nullable Class)expectedClass |
| 61 | + block:(void (^)(UIView *view))block |
| 62 | + reject:(RCTPromiseRejectBlock)reject |
| 63 | + methodName:(NSString *)methodName |
| 64 | + attemptCount:(NSInteger)attemptCount |
| 65 | + startTime:(nullable NSDate *)startTime { |
| 66 | + |
| 67 | + |
| 68 | + UIView *view = [self resolveView:viewRef delegate:delegate]; |
| 69 | + |
| 70 | + if (view) { |
| 71 | + if (expectedClass && ![view isKindOfClass:expectedClass]) { |
| 72 | + reject(@"wrong_view_type", |
| 73 | + [NSString stringWithFormat:@"View with tag %@ is not of expected type %@ in %@. Found: %@", |
| 74 | + viewRef, NSStringFromClass(expectedClass), methodName, NSStringFromClass([view class])], |
| 75 | + nil); |
| 76 | + return; |
| 77 | + } |
| 78 | + block(view); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if (!startTime) { |
| 83 | + startTime = [NSDate date]; |
| 84 | + } |
| 85 | + |
| 86 | + NSTimeInterval elapsed = [[NSDate date] timeIntervalSinceDate:startTime]; |
| 87 | + if (elapsed >= 0.2) { // 200ms timeout |
| 88 | + NSString *errorMsg = [NSString stringWithFormat:@"Could not find view with tag %@ in %@ after %d attempts over %.1fms", |
| 89 | + viewRef, methodName, (int)attemptCount + 1, elapsed * 1000]; |
| 90 | + NSLog(@"%@", errorMsg); |
| 91 | + reject(@"view_not_found", errorMsg, nil); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + int64_t delay = (attemptCount == 0) ? (NSEC_PER_MSEC/5) : 10 * NSEC_PER_MSEC; |
| 96 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay), dispatch_get_main_queue(), ^{ |
| 97 | + [self resolveViewWithPolling:viewRef |
| 98 | + delegate:delegate |
| 99 | + expectedClass:expectedClass |
| 100 | + block:block |
| 101 | + reject:reject |
| 102 | + methodName:methodName |
| 103 | + attemptCount:attemptCount + 1 |
| 104 | + startTime:startTime]; |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +#pragma mark - Private Methods |
| 109 | + |
| 110 | ++ (UIView *)resolveView:(NSNumber *)viewRef delegate:(id<RNMBXViewResolverDelegate>)delegate { |
| 111 | +#ifdef RCT_NEW_ARCH_ENABLED |
| 112 | + if (delegate.viewRegistry_DEPRECATED) { |
| 113 | + UIView *componentView = [delegate.viewRegistry_DEPRECATED viewForReactTag:viewRef]; |
| 114 | + if (componentView) { |
| 115 | + if ([componentView respondsToSelector:@selector(contentView)]) { |
| 116 | + return [componentView performSelector:@selector(contentView)]; |
| 117 | + } |
| 118 | + return componentView; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if (delegate.bridge) { |
| 123 | + return [delegate.bridge.uiManager viewForReactTag:viewRef]; |
| 124 | + } |
| 125 | +#else |
| 126 | + if (delegate.bridge) { |
| 127 | + return [delegate.bridge.uiManager viewForReactTag:viewRef]; |
| 128 | + } |
| 129 | +#endif |
| 130 | + |
| 131 | + return nil; |
| 132 | +} |
| 133 | + |
| 134 | +@end |
0 commit comments