forked from urbanairship/react-native-airship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTNAirshipMessageView.mm
183 lines (157 loc) · 5.95 KB
/
RTNAirshipMessageView.mm
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* Copyright Airship and Contributors */
#import "RTNAirshipMessageView.h"
#if __has_include(<react_native_airship/react_native_airship-Swift.h>)
#import <react_native_airship/react_native_airship-Swift.h>
#else
#import "react_native_airship-Swift.h"
#endif
#ifdef RCT_NEW_ARCH_ENABLED
#import <React/RCTConversions.h>
#import <React/RCTFabricComponentsPlugins.h>
#import <react/renderer/components/RTNAirshipComponents/ComponentDescriptors.h>
#import <react/renderer/components/RTNAirshipComponents/Props.h>
using namespace facebook::react;
#endif
@interface RTNAirshipMessageView()<RTNAirshipMessageWebViewWrapperDelegate>
@property (nonatomic, strong) RTNAirshipMessageWebViewWrapper *wrapper;
@end
NSString *const RTNAirshipMessageViewErrorMessageNotAvailable = @"MESSAGE_NOT_AVAILABLE";
NSString *const RTNAirshipMessageViewErrorFailedToFetchMessage = @"FAILED_TO_FETCH_MESSAGE";
NSString *const RTNAirshipMessageViewErrorMessageLoadFailed = @"MESSAGE_LOAD_FAILED";
NSString *const RTNAirshipMessageViewMessageIDKey = @"messageId";
NSString *const RTNAirshipMessageViewRetryableKey = @"retryable";
NSString *const RTNAirshipMessageViewErrorKey = @"error";
@implementation RTNAirshipMessageView
#ifdef RCT_NEW_ARCH_ENABLED
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const RTNAirshipMessageViewProps>();
_props = defaultProps;
}
return self;
}
#endif
- (instancetype) init {
self = [self initWithFrame:CGRectZero];
if (self) {
self.wrapper = [[RTNAirshipMessageWebViewWrapper alloc] initWithFrame:self.bounds];
self.wrapper.delegate = self;
[self addSubview:self.wrapper.webView];
}
return self;
}
#ifdef RCT_NEW_ARCH_ENABLED
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<RTNAirshipMessageViewComponentDescriptor>();
}
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
const auto &newProps = *std::static_pointer_cast<const RTNAirshipMessageViewProps>(props);
self.messageID = [NSString stringWithUTF8String:newProps.messageId.c_str()];
[super updateProps:props oldProps:oldProps];
}
#endif
- (void)layoutSubviews {
[super layoutSubviews];
self.wrapper.webView.frame = self.bounds;
}
- (void)setMessageID:(NSString *)messageID {
_messageID = messageID;
__weak RTNAirshipMessageView *weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.wrapper loadMessageWithMessageID:messageID];
});
}
- (void)onCloseWithMessageID:(NSString *)messageID {
[self dispatchOnCloseEvent:messageID];
}
- (void)onLoadStartedWithMessageID:(NSString *)messageID {
[self dispatchOnLoadStartedEvent:messageID];
}
- (void)onLoadFinishedWithMessageID:(NSString *)messageID {
[self dispatchOnLoadFinishedEvent:messageID];
}
- (void)onMessageGoneWithMessageID:(NSString *)messageID {
[self dispatchOnLoadErrorEvent:messageID
withErrorMessage:RTNAirshipMessageViewErrorMessageNotAvailable
retryable:NO];
}
- (void)onMessageLoadFailedWithMessageID:(NSString *)messageID {
[self dispatchOnLoadErrorEvent:messageID
withErrorMessage:RTNAirshipMessageViewErrorFailedToFetchMessage
retryable:YES];
}
- (void)onMessageBodyLoadFailedWithMessageID:(NSString *)messageID {
[self dispatchOnLoadErrorEvent:messageID
withErrorMessage:RTNAirshipMessageViewErrorMessageLoadFailed
retryable:YES];
}
- (void)dispatchOnLoadStartedEvent: (NSString*)messageID
{
#ifdef RCT_NEW_ARCH_ENABLED
std::dynamic_pointer_cast<const facebook::react::RTNAirshipMessageViewEventEmitter>(_eventEmitter)
->onLoadStarted(facebook::react::RTNAirshipMessageViewEventEmitter::OnLoadStarted{
.messageId = std::string([messageID UTF8String])
});
#else
if (self.onLoadStarted) {
self.onLoadStarted(@{
RTNAirshipMessageViewMessageIDKey: messageID
});
}
#endif
}
- (void)dispatchOnLoadErrorEvent: (NSString*)messageID
withErrorMessage: (NSString*)errorMessage
retryable: (BOOL)retryable
{
#ifdef RCT_NEW_ARCH_ENABLED
std::dynamic_pointer_cast<const facebook::react::RTNAirshipMessageViewEventEmitter>(_eventEmitter)
->onLoadError(facebook::react::RTNAirshipMessageViewEventEmitter::OnLoadError{
.messageId = std::string([messageID UTF8String]),
.error = std::string([errorMessage UTF8String]),
.retryable = retryable
});
#else
if (self.onLoadError) {
self.onLoadError(@{ RTNAirshipMessageViewMessageIDKey: messageID,
RTNAirshipMessageViewErrorKey: errorMessage,
RTNAirshipMessageViewRetryableKey: @(retryable) });
}
#endif
}
- (void)dispatchOnLoadFinishedEvent: (NSString*)messageID
{
#ifdef RCT_NEW_ARCH_ENABLED
std::dynamic_pointer_cast<const facebook::react::RTNAirshipMessageViewEventEmitter>(_eventEmitter)
->onLoadFinished(facebook::react::RTNAirshipMessageViewEventEmitter::OnLoadFinished{
.messageId = std::string([messageID UTF8String])
});
#else
if (self.onLoadFinished) {
self.onLoadFinished(@{ RTNAirshipMessageViewMessageIDKey: messageID });
}
#endif
}
- (void)dispatchOnCloseEvent: (NSString*)messageID
{
#ifdef RCT_NEW_ARCH_ENABLED
std::dynamic_pointer_cast<const facebook::react::RTNAirshipMessageViewEventEmitter>(_eventEmitter)
->onClose(facebook::react::RTNAirshipMessageViewEventEmitter::OnClose{
.messageId = std::string([messageID UTF8String])
});
#else
if (self.onClose) {
self.onClose(@{ RTNAirshipMessageViewMessageIDKey: messageID });
}
#endif
}
@end
#ifdef RCT_NEW_ARCH_ENABLED
Class<RCTComponentViewProtocol> RTNAirshipMessageViewCls(void)
{
return RTNAirshipMessageView.class;
}
#endif