-
Notifications
You must be signed in to change notification settings - Fork 244
/
CountlyUserDetails.m
484 lines (378 loc) · 13.3 KB
/
CountlyUserDetails.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// CountlyUserDetails.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
@interface CountlyUserDetails ()
@property (nonatomic) NSMutableDictionary* modifications;
@end
NSString* const kCountlyLocalPicturePath = @"kCountlyLocalPicturePath";
NSString* const kCountlyUDKeyName = @"name";
NSString* const kCountlyUDKeyUsername = @"username";
NSString* const kCountlyUDKeyEmail = @"email";
NSString* const kCountlyUDKeyOrganization = @"organization";
NSString* const kCountlyUDKeyPhone = @"phone";
NSString* const kCountlyUDKeyGender = @"gender";
NSString* const kCountlyUDKeyPicture = @"picture";
NSString* const kCountlyUDKeyBirthyear = @"byear";
NSString* const kCountlyUDKeyCustom = @"custom";
NSString* const kCountlyUDKeyModifierSetOnce = @"$setOnce";
NSString* const kCountlyUDKeyModifierIncrement = @"$inc";
NSString* const kCountlyUDKeyModifierMultiply = @"$mul";
NSString* const kCountlyUDKeyModifierMax = @"$max";
NSString* const kCountlyUDKeyModifierMin = @"$min";
NSString* const kCountlyUDKeyModifierPush = @"$push";
NSString* const kCountlyUDKeyModifierAddToSet = @"$addToSet";
NSString* const kCountlyUDKeyModifierPull = @"$pull";
@implementation CountlyUserDetails
+ (instancetype)sharedInstance
{
static CountlyUserDetails *s_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{s_sharedInstance = self.new;});
return s_sharedInstance;
}
- (instancetype)init
{
if (self = [super init])
{
self.modifications = NSMutableDictionary.new;
}
return self;
}
- (NSString *)serializedUserDetails
{
NSMutableDictionary* userDictionary = NSMutableDictionary.new;
if (self.name)
userDictionary[kCountlyUDKeyName] =
![self.name isKindOfClass:NSString.class] ? self.name :
[(NSString *)self.name cly_truncatedValue:@"User details name"];
if (self.username)
userDictionary[kCountlyUDKeyUsername] =
![self.username isKindOfClass:NSString.class] ? self.username :
[(NSString *)self.username cly_truncatedValue:@"User details username"];
if (self.email)
userDictionary[kCountlyUDKeyEmail] =
![self.email isKindOfClass:NSString.class] ? self.email :
[(NSString *)self.email cly_truncatedValue:@"User details email"];
if (self.organization)
userDictionary[kCountlyUDKeyOrganization] =
![self.organization isKindOfClass:NSString.class] ? self.organization :
[(NSString *)self.organization cly_truncatedValue:@"User details organization"];
if (self.phone)
userDictionary[kCountlyUDKeyPhone] =
![self.phone isKindOfClass:NSString.class] ? self.phone :
[(NSString *)self.phone cly_truncatedValue:@"User details phone"];
if (self.gender)
userDictionary[kCountlyUDKeyGender] =
![self.gender isKindOfClass:NSString.class] ? self.gender :
[(NSString *)self.gender cly_truncatedValue:@"User details gender"];
if (self.pictureURL)
userDictionary[kCountlyUDKeyPicture] = self.pictureURL;
if (self.birthYear)
userDictionary[kCountlyUDKeyBirthyear] = self.birthYear;
if ([self.custom isKindOfClass:NSDictionary.class])
{
NSDictionary* customTruncated = [((NSDictionary *)self.custom) cly_truncated:@"User details custom dictionary"];
self.custom = [customTruncated cly_limited:@"User details custom dictionary"];
}
if (self.custom)
userDictionary[kCountlyUDKeyCustom] = self.custom;
if (userDictionary.allKeys.count)
return [userDictionary cly_JSONify];
return nil;
}
- (void)clearUserDetails
{
self.name = nil;
self.username = nil;
self.email = nil;
self.organization = nil;
self.phone = nil;
self.gender = nil;
self.pictureURL = nil;
self.pictureLocalPath = nil;
self.birthYear = nil;
self.custom = nil;
[self.modifications removeAllObjects];
}
- (BOOL)hasUnsyncedChanges
{
NSArray<NSNumber *> *userDetailsFlags = @[
@(self.name != nil),
@(self.username != nil),
@(self.email != nil),
@(self.organization != nil),
@(self.phone != nil),
@(self.gender != nil),
@(self.pictureURL != nil),
@(self.pictureLocalPath != nil),
@(self.birthYear != nil),
@(self.custom != nil)
];
__block BOOL userDetailsChanged = NO;
[userDetailsFlags enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.boolValue) {
userDetailsChanged = YES;
*stop = YES;
}
}];
return userDetailsChanged || self.modifications.count > 0;
}
#pragma mark -
- (void)set:(NSString *)key value:(NSString *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
self.modifications[key] = value.copy;
}
- (void)set:(NSString *)key numberValue:(NSNumber *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
self.modifications[key] = value.copy;
}
- (void)set:(NSString *)key boolValue:(BOOL)value
{
CLY_LOG_I(@"%s %@ %d", __FUNCTION__, key, value);
self.modifications[key] = @(value);
}
- (void)setOnce:(NSString *)key value:(NSString *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierSetOnce: value.copy};
}
- (void)setOnce:(NSString *)key numberValue:(NSNumber *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierSetOnce: value.copy};
}
- (void)setOnce:(NSString *)key boolValue:(BOOL)value;
{
CLY_LOG_I(@"%s %@ %d", __FUNCTION__, key, value);
self.modifications[key] = @{kCountlyUDKeyModifierSetOnce: @(value)};
}
- (void)unSet:(NSString *)key
{
CLY_LOG_I(@"%s %@", __FUNCTION__, key);
self.modifications[key] = NSNull.null;
}
- (void)increment:(NSString *)key
{
CLY_LOG_I(@"%s %@", __FUNCTION__, key);
[self incrementBy:key value:@1];
}
- (void)incrementBy:(NSString *)key value:(NSNumber *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierIncrement: value};
}
- (void)multiply:(NSString *)key value:(NSNumber *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierMultiply: value};
}
- (void)max:(NSString *)key value:(NSNumber *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierMax: value};
}
- (void)min:(NSString *)key value:(NSNumber *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierMin: value};
}
- (void)push:(NSString *)key value:(NSString *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierPush: value.copy};
}
- (void)push:(NSString *)key numberValue:(NSNumber *)value;
{
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierPush: value.copy};
}
- (void)push:(NSString *)key boolValue:(BOOL)value
{
CLY_LOG_I(@"%s %@ %d", __FUNCTION__, key, value);
self.modifications[key] = @{kCountlyUDKeyModifierPush: @(value)};
}
- (void)push:(NSString *)key values:(NSArray *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierPush: value.copy};
}
- (void)pushUnique:(NSString *)key value:(NSString *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierAddToSet: value.copy};
}
- (void)pushUnique:(NSString *)key numberValue:(NSNumber *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierAddToSet: value.copy};
}
- (void)pushUnique:(NSString *)key boolValue:(BOOL)value
{
CLY_LOG_I(@"%s %@ %d", __FUNCTION__, key, value);
self.modifications[key] = @{kCountlyUDKeyModifierAddToSet: @(value)};
}
- (void)pushUnique:(NSString *)key values:(NSArray *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierAddToSet: value.copy};
}
- (void)pull:(NSString *)key value:(NSString *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierPull: value.copy};
}
- (void)pull:(NSString *)key numberValue:(NSNumber *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierPull: value.copy};
}
- (void)pull:(NSString *)key boolValue:(BOOL)value
{
CLY_LOG_I(@"%s %@ %d", __FUNCTION__, key, value);
self.modifications[key] = @{kCountlyUDKeyModifierPull: @(value)};
}
- (void)pull:(NSString *)key values:(NSArray *)value
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, key, value);
if (!value)
{
CLY_LOG_W(@"%s call will be ignored as value is nil!", __FUNCTION__);
return;
}
self.modifications[key] = @{kCountlyUDKeyModifierPull: value.copy};
}
- (void)save
{
CLY_LOG_I(@"%s", __FUNCTION__);
if (!CountlyCommon.sharedInstance.hasStarted)
return;
if (!CountlyConsentManager.sharedInstance.consentForUserDetails)
return;
// Returns early if user properties values are not changed
if (![self hasUnsyncedChanges])
return;
[CountlyConnectionManager.sharedInstance sendEvents];
NSString* userDetails = [self serializedUserDetails];
if (userDetails)
[CountlyConnectionManager.sharedInstance sendUserDetails:userDetails];
if (self.pictureLocalPath && !self.pictureURL)
[CountlyConnectionManager.sharedInstance sendUserDetails:[@{kCountlyLocalPicturePath: self.pictureLocalPath} cly_JSONify]];
if (self.modifications.count)
{
[CountlyConnectionManager.sharedInstance sendUserDetails:[@{kCountlyUDKeyCustom: [self truncateModifications]} cly_JSONify]];
}
[self clearUserDetails];
}
- (NSDictionary *)truncateModifications
{
NSMutableDictionary* truncatedDict = self.modifications.mutableCopy;
[self.modifications enumerateKeysAndObjectsUsingBlock:^(NSString * key, id obj, BOOL * stop)
{
NSString* truncatedKey = [key cly_truncatedKey:@"User details modifications key"];
if (![truncatedKey isEqualToString:key])
{
truncatedDict[truncatedKey] = obj;
[truncatedDict removeObjectForKey:key];
}
if ([obj isKindOfClass:NSString.class])
{
NSString* truncatedValue = [obj cly_truncatedValue:@"User details modifications value"];
if (![truncatedValue isEqualToString:obj])
{
truncatedDict[truncatedKey] = truncatedValue;
}
}
else if ([obj isKindOfClass:NSDictionary.class])
{
NSMutableDictionary* truncatedValueDict = ((NSDictionary *)obj).mutableCopy;
[(NSDictionary *)obj enumerateKeysAndObjectsUsingBlock:^(NSString * key, id value, BOOL * stop)
{
if ([value isKindOfClass:NSString.class])
{
NSString* truncatedValue = [value cly_truncatedValue:@"User details modifications value"];
if (![truncatedValue isEqualToString:value])
{
truncatedValueDict[key] = truncatedValue;
truncatedDict[truncatedKey] = truncatedValueDict;
}
}
}];
}
}];
return truncatedDict.copy;
}
@end