1#import "AIRMapOverlay.h"
2
3#import <React/RCTBridge.h>
4#import <React/RCTEventDispatcher.h>
5#import <React/RCTImageLoaderProtocol.h>
6#import <React/RCTUtils.h>
7#import <React/UIView+React.h>
8
9@interface AIRMapOverlay()
10@property (nonatomic, strong, readwrite) UIImage *overlayImage;
11@end
12
13@implementation AIRMapOverlay {
14    RCTImageLoaderCancellationBlock _reloadImageCancellationBlock;
15    CLLocationCoordinate2D _southWest;
16    CLLocationCoordinate2D _northEast;
17    MKMapRect _mapRect;
18}
19
20- (void)setImageSrc:(NSString *)imageSrc
21{
22    NSLog(@">>> SET IMAGESRC: %@", imageSrc);
23    _imageSrc = imageSrc;
24
25    if (_reloadImageCancellationBlock) {
26        _reloadImageCancellationBlock();
27        _reloadImageCancellationBlock = nil;
28    }
29    __weak typeof(self) weakSelf = self;
30    _reloadImageCancellationBlock = [[_bridge moduleForName:@"ImageLoader"] loadImageWithURLRequest:[RCTConvert NSURLRequest:_imageSrc]
31                                                                            size:weakSelf.bounds.size
32                                                                           scale:RCTScreenScale()
33                                                                         clipped:YES
34                                                                      resizeMode:RCTResizeModeCenter
35                                                                   progressBlock:nil
36                                                                partialLoadBlock:nil
37                                                                 completionBlock:^(NSError *error, UIImage *image) {
38                                                                     if (error) {
39                                                                         NSLog(@"%@", error);
40                                                                     }
41                                                                     dispatch_async(dispatch_get_main_queue(), ^{
42                                                                         NSLog(@">>> IMAGE: %@", image);
43                                                                         weakSelf.overlayImage = image;
44                                                                         [weakSelf createOverlayRendererIfPossible];
45                                                                         [weakSelf update];
46                                                                     });
47                                                                 }];
48}
49
50- (void)setBoundsRect:(NSArray *)boundsRect {
51    _boundsRect = boundsRect;
52
53    _southWest = CLLocationCoordinate2DMake([boundsRect[0][0] doubleValue], [boundsRect[0][1] doubleValue]);
54    _northEast = CLLocationCoordinate2DMake([boundsRect[1][0] doubleValue], [boundsRect[1][1] doubleValue]);
55
56    MKMapPoint southWest = MKMapPointForCoordinate(_southWest);
57    MKMapPoint northEast = MKMapPointForCoordinate(_northEast);
58
59    _mapRect = MKMapRectMake(southWest.x, northEast.y, ABS(northEast.x - southWest.x), ABS(northEast.y - southWest.y));
60
61    [self update];
62}
63
64- (void)createOverlayRendererIfPossible
65{
66    if (MKMapRectIsEmpty(_mapRect) || !self.overlayImage) return;
67    __weak typeof(self) weakSelf = self;
68    self.renderer = [[AIRMapOverlayRenderer alloc] initWithOverlay:weakSelf];
69}
70
71- (void)update
72{
73    if (!_renderer) return;
74
75    if (_map == nil) return;
76    [_map removeOverlay:self];
77    [_map addOverlay:self];
78}
79
80
81#pragma mark MKOverlay implementation
82
83- (CLLocationCoordinate2D)coordinate
84{
85    return MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMidX(_mapRect), MKMapRectGetMidY(_mapRect)));
86}
87
88- (MKMapRect)boundingMapRect
89{
90    return _mapRect;
91}
92
93- (BOOL)intersectsMapRect:(MKMapRect)mapRect
94{
95    return MKMapRectIntersectsRect(_mapRect, mapRect);
96}
97
98- (BOOL)canReplaceMapContent
99{
100    return NO;
101}
102
103@end
104