-
-
Notifications
You must be signed in to change notification settings - Fork 933
Expand file tree
/
Copy pathRNMBXPointAnnotationComponentView.mm
More file actions
144 lines (113 loc) · 5.17 KB
/
RNMBXPointAnnotationComponentView.mm
File metadata and controls
144 lines (113 loc) · 5.17 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#import "RNMBXPointAnnotationComponentView.h"
#import "RNMBXFabricHelpers.h"
#import <React/RCTConversions.h>
#import <React/RCTFabricComponentsPlugins.h>
#import <react/renderer/components/rnmapbox_maps_specs/ComponentDescriptors.h>
#import <react/renderer/components/rnmapbox_maps_specs/EventEmitters.h>
#import <react/renderer/components/rnmapbox_maps_specs/Props.h>
#import <react/renderer/components/rnmapbox_maps_specs/RCTComponentViewHelpers.h>
#import "RNMBXFabricPropConvert.h"
using namespace facebook::react;
@interface RNMBXPointAnnotationComponentView () <RCTRNMBXPointAnnotationViewProtocol>
@end
@implementation RNMBXPointAnnotationComponentView {
RNMBXPointAnnotation *_view;
}
// Needed because of this: https://github.com/facebook/react-native/pull/37274
+ (void)load
{
[super load];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const RNMBXPointAnnotationProps>();
_props = defaultProps;
[self prepareView];
}
return self;
}
- (void)prepareView
{
_view = [[RNMBXPointAnnotation alloc] init];
self.contentView = _view;
// capture weak self reference to prevent retain cycle
__weak __typeof__(self) weakSelf = self;
[_view setOnDrag:^(NSDictionary* event) {
__typeof__(self) strongSelf = weakSelf;
if (strongSelf != nullptr && strongSelf->_eventEmitter != nullptr) {
const auto [type, json] = RNMBXStringifyEventData(event);
std::dynamic_pointer_cast<const facebook::react::RNMBXPointAnnotationEventEmitter>(strongSelf->_eventEmitter)->onMapboxPointAnnotationDrag({type, json});
}
}];
[_view setOnDragEnd:^(NSDictionary* event) {
__typeof__(self) strongSelf = weakSelf;
if (strongSelf != nullptr && strongSelf->_eventEmitter != nullptr) {
const auto [type, json] = RNMBXStringifyEventData(event);
std::dynamic_pointer_cast<const facebook::react::RNMBXPointAnnotationEventEmitter>(strongSelf->_eventEmitter)->onMapboxPointAnnotationDragEnd({type, json});
}
}];
[_view setOnDragStart:^(NSDictionary* event) {
__typeof__(self) strongSelf = weakSelf;
if (strongSelf != nullptr && strongSelf->_eventEmitter != nullptr) {
const auto [type, json] = RNMBXStringifyEventData(event);
std::dynamic_pointer_cast<const facebook::react::RNMBXPointAnnotationEventEmitter>(strongSelf->_eventEmitter)->onMapboxPointAnnotationDragStart({type, json});
}
}];
[_view setOnSelected:^(NSDictionary* event) {
__typeof__(self) strongSelf = weakSelf;
if (strongSelf != nullptr && strongSelf->_eventEmitter != nullptr) {
const auto [type, json] = RNMBXStringifyEventData(event);
std::dynamic_pointer_cast<const facebook::react::RNMBXPointAnnotationEventEmitter>(strongSelf->_eventEmitter)->onMapboxPointAnnotationSelected({type, json});
}
}];
[_view setOnDeselected:^(NSDictionary* event) {
__typeof__(self) strongSelf = weakSelf;
if (strongSelf != nullptr && strongSelf->_eventEmitter != nullptr) {
const auto [type, json] = RNMBXStringifyEventData(event);
std::dynamic_pointer_cast<const facebook::react::RNMBXPointAnnotationEventEmitter>(strongSelf->_eventEmitter)->onMapboxPointAnnotationDeselected({type, json});
}
}];
}
- (void)prepareForRecycle
{
[super prepareForRecycle];
[self prepareView];
}
#pragma mark - RCTComponentViewProtocol
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<RNMBXPointAnnotationComponentDescriptor>();
}
- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
if ([childComponentView isKindOfClass:[RCTViewComponentView class]] && ((RCTViewComponentView *)childComponentView).contentView != nil) {
[_view insertReactSubviewInternal:((RCTViewComponentView *)childComponentView).contentView at:index];
} else {
[_view insertReactSubviewInternal:childComponentView at:index];
}
}
- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
if ([childComponentView isKindOfClass:[RCTViewComponentView class]] && ((RCTViewComponentView *)childComponentView).contentView != nil) {
[_view removeReactSubviewInternal:((RCTViewComponentView *)childComponentView).contentView];
} else {
[_view removeReactSubviewInternal:childComponentView];
}
}
- (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps
{
const auto &oldViewProps = static_cast<const RNMBXPointAnnotationProps &>(*oldProps);
const auto &newViewProps = static_cast<const RNMBXPointAnnotationProps &>(*props);
RNMBX_OPTIONAL_PROP_NSString(coordinate)
RNMBX_OPTIONAL_PROP_BOOL(draggable)
RNMBX_OPTIONAL_PROP_NSString(id)
RNMBX_OPTIONAL_PROP_NSDictionary(anchor)
RNMBX_REMAP_OPTIONAL_PROP_BOOL(selected, reactSelected)
[super updateProps:props oldProps:oldProps];
}
@end
Class<RCTComponentViewProtocol> RNMBXPointAnnotationCls(void)
{
return RNMBXPointAnnotationComponentView.class;
}