-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathADJEvent.m
200 lines (166 loc) · 5.52 KB
/
ADJEvent.m
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
//
// ADJEvent.m
// adjust
//
// Created by Pedro Filipe on 15/10/14.
// Copyright (c) 2014 adjust GmbH. All rights reserved.
//
#import "ADJEvent.h"
#import "ADJAdjustFactory.h"
#import "ADJUtil.h"
@interface ADJEvent()
@property (nonatomic, weak) id<ADJLogger> logger;
@property (nonatomic, strong) NSMutableDictionary *callbackMutableParameters;
@property (nonatomic, strong) NSMutableDictionary *partnerMutableParameters;
@end
@implementation ADJEvent
- (id)initWithEventToken:(NSString *)eventToken {
self = [super init];
if (self == nil) {
return nil;
}
self.logger = ADJAdjustFactory.logger;
if (![self checkEventToken:eventToken]) {
return self;
}
_eventToken = [eventToken copy];
return self;
}
- (void)addCallbackParameter:(NSString *)key value:(NSString *)value {
@synchronized (self) {
NSString *immutableKey = [key copy];
NSString *immutableValue = [value copy];
if (![ADJUtil isValidParameter:immutableKey
attributeType:@"key"
parameterName:@"Callback"]) {
return;
}
if (![ADJUtil isValidParameter:immutableValue
attributeType:@"value"
parameterName:@"Callback"]) {
return;
}
if (self.callbackMutableParameters == nil) {
self.callbackMutableParameters = [[NSMutableDictionary alloc] init];
}
if ([self.callbackMutableParameters objectForKey:immutableKey]) {
[self.logger warn:@"Callback parameter key %@ was overwritten", immutableKey];
}
[self.callbackMutableParameters setObject:immutableValue forKey:immutableKey];
}
}
- (void)addPartnerParameter:(NSString *)key value:(NSString *)value {
@synchronized (self) {
NSString *immutableKey = [key copy];
NSString *immutableValue = [value copy];
if (![ADJUtil isValidParameter:immutableKey
attributeType:@"key"
parameterName:@"Partner"]) {
return;
}
if (![ADJUtil isValidParameter:immutableValue
attributeType:@"value"
parameterName:@"Partner"]) {
return;
}
if (self.partnerMutableParameters == nil) {
self.partnerMutableParameters = [[NSMutableDictionary alloc] init];
}
if ([self.partnerMutableParameters objectForKey:immutableKey]) {
[self.logger warn:@"Partner parameter key %@ was overwritten", immutableKey];
}
[self.partnerMutableParameters setObject:immutableValue forKey:immutableKey];
}
}
- (void)setRevenue:(double)amount currency:(NSString *)currency {
NSNumber *revenue = [NSNumber numberWithDouble:amount];
if (![self checkRevenue:revenue currency:currency]) {
return;
}
_revenue = revenue;
@synchronized (self) {
_currency = [currency copy];
}
}
- (void)setTransactionId:(NSString *)transactionId {
@synchronized (self) {
_transactionId = [transactionId copy];
}
}
- (void)setDeduplicationId:(NSString *)deduplicationId {
@synchronized (self) {
_deduplicationId = [deduplicationId copy];
}
}
- (void)setCallbackId:(NSString *)callbackId {
@synchronized (self) {
_callbackId = [callbackId copy];
}
}
- (NSDictionary *)callbackParameters {
@synchronized (self) {
return (NSDictionary *)self.callbackMutableParameters;
}
}
- (NSDictionary *)partnerParameters {
@synchronized (self) {
return (NSDictionary *)self.partnerMutableParameters;
}
}
- (void)setProductId:(NSString *)productId {
@synchronized (self) {
_productId = [productId copy];
}
}
- (BOOL)checkEventToken:(NSString *)eventToken {
if ([ADJUtil isNull:eventToken]) {
[self.logger error:@"Missing Event Token"];
return NO;
}
if ([eventToken length] <= 0) {
[self.logger error:@"Event Token can't be empty"];
return NO;
}
return YES;
}
- (BOOL)checkRevenue:(NSNumber *)revenue currency:(NSString *)currency {
if (![ADJUtil isNull:revenue]) {
double amount = [revenue doubleValue];
if (amount < 0.0) {
[self.logger error:@"Invalid amount %.5f", amount];
return NO;
}
if ([ADJUtil isNull:currency]) {
[self.logger error:@"Currency must be set with revenue"];
return NO;
}
if ([currency isEqualToString:@""]) {
[self.logger error:@"Currency is empty"];
return NO;
}
} else {
if ([ADJUtil isNotNull:currency]) {
[self.logger error:@"Revenue must be set with currency"];
return NO;
}
}
return YES;
}
- (BOOL)isValid {
return self.eventToken != nil;
}
- (id)copyWithZone:(NSZone *)zone {
ADJEvent *copy = [[[self class] allocWithZone:zone] init];
if (copy) {
copy->_eventToken = [self.eventToken copyWithZone:zone];
copy->_revenue = [self.revenue copyWithZone:zone];
copy->_currency = [self.currency copyWithZone:zone];
copy.callbackMutableParameters = [self.callbackMutableParameters copyWithZone:zone];
copy.partnerMutableParameters = [self.partnerMutableParameters copyWithZone:zone];
copy->_transactionId = [self.transactionId copyWithZone:zone];
copy->_deduplicationId = [self.deduplicationId copyWithZone:zone];
copy->_productId = [self.productId copyWithZone:zone];
}
return copy;
}
@end