forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountlyUserDetails.m
171 lines (138 loc) · 3.92 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
// 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, strong) NSMutableDictionary* modifications;
@end
NSString* const kCountlyLocalPicturePath = @"kCountlyLocalPicturePath";
@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;
}
- (void)recordUserDetails
{
[self save];
}
- (NSString *)serializedUserDetails
{
NSMutableDictionary* userDictionary = NSMutableDictionary.new;
if (self.name)
userDictionary[@"name"] = self.name;
if (self.username)
userDictionary[@"username"] = self.username;
if (self.email)
userDictionary[@"email"] = self.email;
if (self.organization)
userDictionary[@"organization"] = self.organization;
if (self.phone)
userDictionary[@"phone"] = self.phone;
if (self.gender)
userDictionary[@"gender"] = self.gender;
if (self.pictureURL)
userDictionary[@"picture"] = self.pictureURL;
if (self.birthYear)
userDictionary[@"byear"] = self.birthYear;
if (self.custom)
userDictionary[@"custom"] = 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];
}
#pragma mark -
- (void)set:(NSString *)key value:(NSString *)value
{
self.modifications[key] = value;
}
- (void)setOnce:(NSString *)key value:(NSString *)value
{
self.modifications[key] = @{@"$setOnce":value};
}
- (void)unSet:(NSString *)key
{
self.modifications[key] = NSNull.null;
}
- (void)increment:(NSString *)key
{
[self incrementBy:key value:@1];
}
- (void)incrementBy:(NSString *)key value:(NSNumber *)value
{
self.modifications[key] = @{@"$inc":value};
}
- (void)multiply:(NSString *)key value:(NSNumber *)value
{
self.modifications[key] = @{@"$mul":value};
}
- (void)max:(NSString *)key value:(NSNumber *)value
{
self.modifications[key] = @{@"$max":value};
}
- (void)min:(NSString *)key value:(NSNumber *)value
{
self.modifications[key] = @{@"$min":value};
}
- (void)push:(NSString *)key value:(NSString *)value
{
self.modifications[key] = @{@"$push":value};
}
- (void)push:(NSString *)key values:(NSArray *)value
{
self.modifications[key] = @{@"$push":value};
}
- (void)pushUnique:(NSString *)key value:(NSString *)value
{
self.modifications[key] = @{@"$addToSet":value};
}
- (void)pushUnique:(NSString *)key values:(NSArray *)value
{
self.modifications[key] = @{@"$addToSet":value};
}
- (void)pull:(NSString *)key value:(NSString *)value
{
self.modifications[key] = @{@"$pull":value};
}
- (void)pull:(NSString *)key values:(NSArray *)value
{
self.modifications[key] = @{@"$pull":value};
}
- (void)save
{
NSString* userDetails = [CountlyUserDetails.sharedInstance 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:[@{@"custom":self.modifications} cly_JSONify]];
[self clearUserDetails];
}
@end