-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathOSInAppMessageInternal.m
More file actions
260 lines (203 loc) · 8.38 KB
/
Copy pathOSInAppMessageInternal.m
File metadata and controls
260 lines (203 loc) · 8.38 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* Modified MIT License
*
* Copyright 2017 OneSignal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* 1. The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 2. All copies of substantial portions of the Software may only be used in connection
* with services provided by OneSignal.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import "OSInAppMessageInternal.h"
#import "NSDateFormatter+OneSignal.h"
#import "OneSignalCommonDefines.h"
#import <OneSignalCore/OSMacros.h>
@implementation OSInAppMessage
- (NSDictionary *)jsonRepresentation {
NSMutableDictionary *json = [NSMutableDictionary new];
json[@"messageId"] = self.messageId;
return json;
}
@end
@interface OSInAppMessageInternal ()
@property (strong, nonatomic, nonnull) NSMutableSet <NSString *> *clickedClickIds;
@property (strong, nonatomic, nonnull) NSMutableSet <NSString *> *viewedPageIds;
@end
@implementation OSInAppMessageInternal
- (instancetype)init {
if (self = [super init]) {
self.clickedClickIds = [[NSMutableSet alloc] init];
self.viewedPageIds = [NSMutableSet new];
self.isTriggerChanged = false;
}
return self;
}
- (BOOL)isBanner {
return self.position == OSInAppMessageDisplayPositionTop || self.position == OSInAppMessageDisplayPositionBottom;
}
- (BOOL)takeActionAsUnique {
if (self.actionTaken)
return false;
return self.actionTaken = true;
}
- (BOOL)isClickAvailable:(NSString *)clickId {
return ![_clickedClickIds containsObject:clickId];
}
- (void)clearClickIds {
_clickedClickIds = [NSMutableSet new];
}
- (void)addClickId:(NSString *)clickId {
[_clickedClickIds addObject:clickId];
}
- (NSSet<NSString *> *)getClickedClickIds {
return _clickedClickIds;
}
+ (instancetype)instanceWithData:(NSData *)data {
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error || !json) {
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"Unable to decode in-app message JSON: %@", error.description ?: @"No Data"]];
return nil;
}
return [self instanceWithJson:json];
}
+ (instancetype)instanceWithJson:(NSDictionary * _Nonnull)json {
let message = [OSInAppMessageInternal new];
// "id" is expected instead of "messageId" when parsing JSON from the backend
if (json[@"id"] && [json[@"id"] isKindOfClass:[NSString class]])
message.messageId = json[@"id"];
else
return nil;
if (json[@"variants"] && [json[@"variants"] isKindOfClass:[NSDictionary class]])
message.variants = json[@"variants"];
else
return nil;
if (json[@"redisplay"] && [json[@"redisplay"] isKindOfClass:[NSDictionary class]])
message.displayStats = [OSInAppMessageDisplayStats instanceWithJson:json[@"redisplay"]];
else
message.displayStats = [[OSInAppMessageDisplayStats alloc] init];
if (json[@"end_time"] && [json[@"end_time"] isKindOfClass:[NSString class]]) {
NSString *stringEndTime = json[@"end_time"];
NSDateFormatter *dateFormatter = [NSDateFormatter iso8601DateFormatter];
NSDate *endTime = [dateFormatter dateFromString:stringEndTime];
message.endTime = endTime;
}
if (json[@"has_liquid"]) {
message.hasLiquid = YES;
}
if (json[@"is_preview"]) {
message.isPreview = YES;
}
if (json[@"triggers"] && [json[@"triggers"] isKindOfClass:[NSArray class]]) {
let triggers = [NSMutableArray new];
for (NSArray *list in (NSArray *)json[@"triggers"]) {
let subTriggers = [NSMutableArray new];
for (NSDictionary *triggerJson in list) {
let trigger = [OSTrigger instanceWithJson:triggerJson];
if (trigger)
[subTriggers addObject:trigger];
else {
[OneSignalLog onesignalLog:ONE_S_LL_WARN message:[NSString stringWithFormat:@"Trigger JSON is invalid: %@", triggerJson]];
return nil;
}
}
[triggers addObject:subTriggers];
}
message.triggers = triggers;
}
else
return nil;
return message;
}
+ (instancetype)instancePreviewFromNotification:(OSNotification *)notification {
let message = [OSInAppMessageInternal new];
message.messageId = [notification additionalData][ONESIGNAL_IAM_PREVIEW];
message.isPreview = true;
return message;
}
- (NSDictionary *)jsonRepresentationInternal {
let json = [NSMutableDictionary new];
json[@"messageId"] = self.messageId;
json[@"variants"] = self.variants;
let triggers = [NSMutableArray new];
for (NSArray *andBlock in self.triggers) {
let andConditions = [NSMutableArray new];
for (OSTrigger *trigger in andBlock)
[andConditions addObject:trigger.jsonRepresentation];
[triggers addObject:andConditions];
}
json[@"triggers"] = triggers;
if ([_displayStats isRedisplayEnabled]) {
json[@"redisplay"] = [_displayStats jsonRepresentation];
}
json[@"end_time"] = [[NSDateFormatter iso8601DateFormatter] stringFromDate:self.endTime];
if (self.hasLiquid) {
json[@"has_liquid"] = @true;
}
if (self.isPreview) {
json[@"is_preview"] = @true;
}
return json;
}
- (NSString *)description {
return [NSString stringWithFormat:@"OSInAppMessage: \nmessageId: %@ \ntriggers: %@ \ndisplayed_in_session: %@ \ndisplayStats: %@ \nendTime: %@ \nhasLiquid: %@", self.messageId, self.triggers, self.isDisplayedInSession ? @"YES" : @"NO", self.displayStats, self.endTime, self.hasLiquid ? @"YES" : @"NO"];
}
- (BOOL)isEqual:(id)object {
if (self == object) {
return YES;
}
if (![object isKindOfClass:[OSInAppMessageInternal class]]) {
return NO;
}
OSInAppMessageInternal *iam = object;
return [self.messageId isEqualToString:iam.messageId];
}
- (NSUInteger)hash {
return [self.messageId hash];
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.messageId forKey:@"messageId"];
[encoder encodeObject:_variants forKey:@"variants"];
[encoder encodeObject:_triggers forKey:@"triggers"];
[encoder encodeObject:_displayStats forKey:@"displayStats"];
//TODO: This will need to be changed when we add core data or database to iOS, see android implementation for reference
[encoder encodeBool:_isDisplayedInSession forKey:@"displayed_in_session"];
[encoder encodeObject:_endTime forKey:@"endTime"];
[encoder encodeBool:_hasLiquid forKey:@"hasLiquid"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
self.messageId = [decoder decodeObjectForKey:@"messageId"];
_variants = [decoder decodeObjectForKey:@"variants"];
_triggers = [decoder decodeObjectForKey:@"triggers"];
_displayStats = [decoder decodeObjectForKey:@"displayStats"];
//TODO: This will need to be changed when we add core data or database to iOS, see android implementation for reference
_isDisplayedInSession = [decoder decodeBoolForKey:@"displayed_in_session"];
_endTime = [decoder decodeObjectForKey:@"endTime"];
_hasLiquid = [decoder decodeBoolForKey:@"hasLiquid"];
}
return self;
}
- (BOOL)isFinished {
if (!self.endTime) {
return NO;
}
return [self.endTime compare:[NSDate date]] == NSOrderedAscending;
}
@end