-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathOSDynamicTriggerController.m
More file actions
158 lines (133 loc) · 7.02 KB
/
OSDynamicTriggerController.m
File metadata and controls
158 lines (133 loc) · 7.02 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
/**
* 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 "OSDynamicTriggerController.h"
#import "OSInAppMessagingDefines.h"
#import "OneSignalCommonDefines.h"
#import "OSMessagingController.h"
#import <OneSignalOutcomes/OneSignalOutcomes.h>
#import "OSMacros.h"
#import "OSSessionManager.h"
@interface OSDynamicTriggerController ()
/*
Maps messageId's to future scheduled time-based triggers
For example, a message might conceivably have a session_duration trigger
and an os_time trigger both scheduled for the future
This dictionary prevents the SDK from scheduling multiple duplicate timers
for the same messageId + trigger type
*/
@property (strong, nonatomic, nonnull) NSMutableSet<NSString *> *scheduledMessages;
@end
@implementation OSDynamicTriggerController
- (instancetype)init {
if (self = [super init]) {
self.scheduledMessages = [NSMutableSet new];
self.timeSinceLastMessage = [NSDate distantPast];
}
return self;
}
- (BOOL)dynamicTriggerShouldFire:(OSTrigger *)trigger withMessageId:(NSString *)messageId {
if (!trigger.value)
return false;
@synchronized (self.scheduledMessages) {
// All time-based trigger values should be numbers (either timestamps or offsets)
if (![trigger.value isKindOfClass:[NSNumber class]])
return false;
// Timer already set for this message trigger
if ([self.scheduledMessages containsObject:trigger.triggerId])
return false;
let requiredTimeValue = [trigger.value doubleValue];
// How long to set the timer for (if needed)
var offset = 0.0f;
// Check what type of trigger it is
if ([trigger.kind isEqualToString:OS_DYNAMIC_TRIGGER_KIND_SESSION_TIME]) {
let currentDuration = fabs([[OSSessionManager.sharedSessionManager sessionLaunchTime] timeIntervalSinceNow]);
if ([self evaluateTimeInterval:requiredTimeValue withCurrentValue:currentDuration forOperator:trigger.operatorType]) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"session time trigger completed: %@", trigger.triggerId]];
[self.delegate dynamicTriggerCompleted:trigger.triggerId];
//[self.delegate dynamicTriggerFired:trigger.triggerId];
return true;
}
offset = requiredTimeValue - currentDuration;
} else if ([trigger.kind isEqualToString:OS_DYNAMIC_TRIGGER_KIND_MIN_TIME_SINCE]) {
// Make sure no IAM are showng before handling "since_last_message" trigger kind
if (OSMessagingController.sharedInstance.isInAppMessageShowing)
return false;
let timestampSinceLastMessage = fabs([self.timeSinceLastMessage timeIntervalSinceNow]);
if ([self evaluateTimeInterval:requiredTimeValue withCurrentValue:timestampSinceLastMessage forOperator:trigger.operatorType]) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"time since last inapp trigger completed: %@", trigger.triggerId]];
return true;
}
offset = requiredTimeValue - timestampSinceLastMessage;
}
// Don't schedule timers for the past
if (offset <= 0.0f)
return false;
// If we reach this point, it means we need to return false and set up a timer for a future time
NSTimer *timer = [NSTimer timerWithTimeInterval:offset
target:self
selector:@selector(timerFiredForMessage:)
userInfo:@{@"trigger" : trigger}
repeats:false];
if (timer) {
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"timer added for triggerId: %@, messageId: %@", trigger.triggerId, messageId]];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
[self.scheduledMessages addObject:trigger.triggerId];
}
return false;
}
/*
Time-based triggers can use operators like < to trigger at specific times.
For example, the "session duration" trigger can be triggered
*/
- (BOOL)evaluateTimeInterval:(NSTimeInterval)timeInterval withCurrentValue:(NSTimeInterval)currentTimeInterval forOperator:(OSTriggerOperatorType)operator {
switch (operator) {
case OSTriggerOperatorTypeLessThan:
return currentTimeInterval < timeInterval;
case OSTriggerOperatorTypeLessThanOrEqualTo: // Due to potential floating point error, consider very small differences to be equal
return currentTimeInterval <= timeInterval || OS_ROUGHLY_EQUAL(timeInterval, currentTimeInterval);
case OSTriggerOperatorTypeGreaterThan:
return currentTimeInterval > timeInterval;
case OSTriggerOperatorTypeGreaterThanOrEqualTo: // Due to potential floating point error, consider very small differences to be equal
return currentTimeInterval >= timeInterval || OS_ROUGHLY_EQUAL(timeInterval, currentTimeInterval);
case OSTriggerOperatorTypeEqualTo:
return OS_ROUGHLY_EQUAL(timeInterval, currentTimeInterval);
case OSTriggerOperatorTypeNotEqualTo:
return !OS_ROUGHLY_EQUAL(timeInterval, currentTimeInterval);
default:
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"Attempted to apply an invalid operator on a time-based in-app-message trigger: %@", OS_OPERATOR_TO_STRING(operator)]];
return false;
}
}
- (void)timerFiredForMessage:(NSTimer *)timer {
@synchronized (self.scheduledMessages) {
let trigger = (OSTrigger *)timer.userInfo[@"trigger"];
[self.scheduledMessages removeObject:trigger.triggerId];
[self.delegate dynamicTriggerFired];
}
}
@end