-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathOSInAppMessageDisplayStats.m
More file actions
150 lines (123 loc) · 5.19 KB
/
OSInAppMessageDisplayStats.m
File metadata and controls
150 lines (123 loc) · 5.19 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
/**
* Modified MIT License
*
* Copyright 2020 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 "OSInAppMessageDisplayStats.h"
#import "OSMacros.h"
@interface OSInAppMessageDisplayStats ()
@property (nonatomic, readwrite) BOOL redisplayEnabled;
@end
@implementation OSInAppMessageDisplayStats
- (instancetype)init
{
self = [super init];
if (self) {
self.displayLimit = NSIntegerMax;
self.displayDelay = 0;
self.displayQuantity = 0;
self.lastDisplayTime = -1;
self.redisplayEnabled = false;
}
return self;
}
+ (instancetype)instanceWithDisplayQuantity:(NSInteger)displayQuantity lastDisplayTime:(NSTimeInterval)lastDisplayTime {
let displayStats = [OSInAppMessageDisplayStats new];
displayStats.displayQuantity = displayQuantity;
displayStats.lastDisplayTime = lastDisplayTime;
return displayStats;
}
+ (instancetype _Nullable)instanceWithData:(NSData * _Nonnull)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 display state message JSON: %@", error.description ?: @"No Data"]];
return nil;
}
return [self instanceWithJson:json];
}
+ (instancetype)instanceWithJson:(NSDictionary * _Nonnull)json {
let displayStats = [OSInAppMessageDisplayStats new];
if (json[@"limit"] && [json[@"limit"] isKindOfClass:[NSNumber class]])
displayStats.displayLimit = [json[@"limit"] integerValue];
else
displayStats.displayLimit = NSIntegerMax;
if (json[@"delay"] && [json[@"delay"] isKindOfClass:[NSNumber class]])
displayStats.displayDelay = [json[@"delay"] doubleValue];
else
displayStats.displayDelay = 0;
displayStats.displayQuantity = 0;
displayStats.lastDisplayTime = -1;
displayStats.redisplayEnabled = YES;
return displayStats;
}
+ (instancetype)instancePreviewFromNotification:(OSNotification *)notification {
return [OSInAppMessageDisplayStats new];
}
-(NSDictionary *)jsonRepresentation {
let json = [NSMutableDictionary new];
json[@"limit"] = @(_displayLimit);
json[@"delay"] = @(_displayDelay);
return json;
}
- (BOOL)isDelayTimeSatisfied:(NSTimeInterval)date {
if (_lastDisplayTime < 0) {
return true;
}
//Calculate gap between display times
let diffInSeconds = date - _lastDisplayTime;
return diffInSeconds >= _displayDelay;
}
- (BOOL)shouldDisplayAgain {
BOOL result = _displayQuantity < _displayLimit;
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"In app message shouldDisplayAgain: %hhu", result]];
return result;
}
- (void)incrementDisplayQuantity {
_displayQuantity++;
}
- (BOOL)isRedisplayEnabled {
return _redisplayEnabled;
}
- (NSString *)description {
return [NSString stringWithFormat:@"OSInAppMessageDisplayStats: redisplayEnabled: %@ \nlastDisplayTime: %f \ndisplayDelay: %f \ndisplayQuantity: %ld \ndisplayLimit: %ld", self.redisplayEnabled ? @"YES" : @"NO", self.lastDisplayTime, self.displayDelay, (long)self.displayQuantity, (long)self.displayLimit];
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeInteger:_displayLimit forKey:@"displayLimit"];
[encoder encodeInteger:_displayQuantity forKey:@"displayQuantity"];
[encoder encodeDouble:_displayDelay forKey:@"displayDelay"];
[encoder encodeDouble:_lastDisplayTime forKey:@"lastDisplayTime"];
[encoder encodeBool:_redisplayEnabled forKey:@"redisplayEnabled"];
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
_displayLimit = [decoder decodeIntegerForKey:@"displayLimit"];
_displayQuantity = [decoder decodeIntegerForKey:@"displayQuantity"];
_displayDelay = [decoder decodeDoubleForKey:@"displayDelay"];
_lastDisplayTime = [decoder decodeDoubleForKey:@"lastDisplayTime"];
_redisplayEnabled = [decoder decodeBoolForKey:@"redisplayEnabled"];
}
return self;
}
@end