4646#import " LPRequestFactory.h"
4747#import " LPFileTransferManager.h"
4848#import " LPRequestSender.h"
49- #import " LPAPIConfig.h"
5049#import " LPOperationQueue.h"
5150#import " LPDeferMessageManager.h"
5251#include < sys/sysctl.h>
@@ -132,6 +131,16 @@ + (NotificationsManager*)notificationsManager
132131 return managerInstance;
133132}
134133
134+ + (User*)user
135+ {
136+ static User *userInstance = nil ;
137+ static dispatch_once_t onceUserInstanceToken;
138+ dispatch_once (&onceUserInstanceToken, ^{
139+ userInstance = [User new ];
140+ });
141+ return userInstance;
142+ }
143+
135144+ (void )throwError : (NSString *)reason
136145{
137146 if ([LPConstantsState sharedState ].isDevelopmentModeEnabled ) {
@@ -153,24 +162,24 @@ + (void)_initPush
153162}
154163
155164+ (void )setApiHostName : (NSString *)hostName
156- withServletName : (NSString *)servletName
165+ withPath : (NSString *)apiPath
157166 usingSsl : (BOOL )ssl
158167{
159168 if ([LPUtils isNullOrEmpty: hostName]) {
160- [self throwError: @" [Leanplum setApiHostName:withServletName :usingSsl:] Empty hostname "
169+ [self throwError: @" [Leanplum setApiHostName:withPath :usingSsl:] Empty hostname "
161170 @" parameter provided." ];
162171 return ;
163172 }
164- if ([LPUtils isNullOrEmpty: servletName ]) {
165- [self throwError: @" [Leanplum setApiHostName:withServletName :usingSsl:] Empty servletName "
173+ if ([LPUtils isNullOrEmpty: apiPath ]) {
174+ [self throwError: @" [Leanplum setApiHostName:withPath :usingSsl:] Empty apiPath "
166175 @" parameter provided." ];
167176 return ;
168177 }
169178
170179 LP_TRY
171- [LPConstantsState sharedState ].apiHostName = hostName;
172- [LPConstantsState sharedState ]. apiServlet = servletName ;
173- [LPConstantsState sharedState ].apiSSL = ssl;
180+ [ApiConfig shared ].apiHostName = hostName;
181+ [ApiConfig shared ]. apiPath = apiPath ;
182+ [ApiConfig shared ].apiSSL = ssl;
174183 LP_END_TRY
175184}
176185
@@ -182,8 +191,12 @@ + (void)setSocketHostName:(NSString *)hostName withPortNumber:(int)port
182191 return ;
183192 }
184193
185- [LPConstantsState sharedState ].socketHost = hostName;
186- [LPConstantsState sharedState ].socketPort = port;
194+ if (![[ApiConfig shared ].socketHost isEqualToString: hostName] ||
195+ [ApiConfig shared ].socketPort != port) {
196+ [ApiConfig shared ].socketHost = hostName;
197+ [ApiConfig shared ].socketPort = port;
198+ [[LeanplumSocket sharedSocket ] connectToNewSocket ];
199+ }
187200}
188201
189202+ (void )setClient : (NSString *)client withVersion : (NSString *)version
@@ -325,7 +338,7 @@ + (void)setAppId:(NSString *)appId withDevelopmentKey:(NSString *)accessKey
325338
326339 LP_TRY
327340 [LPConstantsState sharedState ].isDevelopmentModeEnabled = YES ;
328- [[LPAPIConfig sharedConfig ] setAppId: appId withAccessKey : accessKey];
341+ [[ApiConfig shared ] setAppId: appId accessKey : accessKey];
329342 LP_END_TRY
330343}
331344
@@ -350,7 +363,7 @@ + (void)setAppId:(NSString *)appId withProductionKey:(NSString *)accessKey
350363
351364 LP_TRY
352365 [LPConstantsState sharedState ].isDevelopmentModeEnabled = NO ;
353- [[LPAPIConfig sharedConfig ] setAppId: appId withAccessKey : accessKey];
366+ [[ApiConfig shared ] setAppId: appId accessKey : accessKey];
354367 LP_END_TRY
355368}
356369
@@ -377,10 +390,10 @@ + (void)setDeviceId:(NSString *)deviceId
377390 LP_TRY
378391 // If Leanplum start has been called already, changing the deviceId results in a new device
379392 // Ensure the id is updated and the new device has all attributes set
380- if ([LPInternalState sharedState ].hasStarted && ![[LPAPIConfig sharedConfig ].deviceId isEqualToString: deviceId]) {
393+ if ([LPInternalState sharedState ].hasStarted && ![[Leanplum user ].deviceId isEqualToString: deviceId]) {
381394 [self setDeviceIdInternal: deviceId];
382395 } else {
383- [[LPAPIConfig sharedConfig ] setDeviceId: deviceId];
396+ [[Leanplum user ] setDeviceId: deviceId];
384397 }
385398 LP_END_TRY
386399}
@@ -398,7 +411,7 @@ +(void)setDeviceIdInternal:(NSString *)deviceId
398411 LP_PARAM_DEVICE_ID: deviceId
399412 } mutableCopy];
400413
401- NSString *pushToken = [Leanplum notificationsManager ].pushToken ;
414+ NSString *pushToken = [Leanplum user ].pushToken ;
402415 if (pushToken) {
403416 params[LP_PARAM_DEVICE_PUSH_TOKEN] = pushToken;
404417 }
@@ -411,20 +424,19 @@ +(void)setDeviceIdInternal:(NSString *)deviceId
411424 }
412425
413426 // Clean UserDefaults before changing deviceId because it is used to generate key
414- [Leanplum notificationsManager ].pushToken = nil ;
427+ [Leanplum user ].pushToken = nil ;
415428 [[Leanplum notificationsManager ] removeNotificationSettings ];
416429
417- // Change the LPAPIConfig after getting the push token and settings
430+ // Change the User deviceId after getting the push token and settings
418431 // and after cleaning UserDefaults
419- // The LPAPIConfig value is used in retrieving them
420- [[LPAPIConfig sharedConfig ] setDeviceId: deviceId];
432+ // The User userId and deviceId are used in retrieving them
433+ [[Leanplum user ] setDeviceId: deviceId];
421434 [[LPVarCache sharedCache ] saveDiffs ];
422435
423436 // Update the token and settings now that the key is different
424- [Leanplum notificationsManager ].pushToken = pushToken;
437+ [Leanplum user ].pushToken = pushToken;
425438 [[Leanplum notificationsManager ] saveNotificationSettings: settings];
426439
427-
428440 LPRequest *request = [LPRequestFactory setDeviceAttributesWithParams: params];
429441 [[LPRequestSender sharedInstance ] send: request];
430442 }];
@@ -783,7 +795,7 @@ + (void)startWithUserId:(NSString *)userId
783795{
784796 [[Leanplum notificationsManager ].proxy setupNotificationSwizzling ];
785797
786- if ([LPAPIConfig sharedConfig ].appId == nil ) {
798+ if ([ApiConfig shared ].appId == nil ) {
787799 [self throwError: @" Please provide your app ID using one of the [Leanplum setAppId:] "
788800 @" methods." ];
789801 return ;
@@ -852,7 +864,6 @@ + (void)startWithUserId:(NSString *)userId
852864 });
853865 state.actionManager = [LPActionManager sharedManager ];
854866
855- [[LPAPIConfig sharedConfig ] loadToken ];
856867 [[LPVarCache sharedCache ] setSilent: YES ];
857868 [[LPVarCache sharedCache ] loadDiffs ];
858869 [[LPVarCache sharedCache ] setSilent: NO ];
@@ -871,7 +882,7 @@ + (void)startWithUserId:(NSString *)userId
871882 }];
872883
873884 // Set device ID.
874- NSString *deviceId = [LPAPIConfig sharedConfig ].deviceId ;
885+ NSString *deviceId = [Leanplum user ].deviceId ;
875886 // This is the device ID set when the MAC address is used on iOS 7.
876887 // This is to allow apps who upgrade to the new ID to forget the old one.
877888 if ([deviceId isEqualToString: @" 0f607264fc6318a92b9e13c65db7cd3c" ]) {
@@ -882,17 +893,17 @@ + (void)startWithUserId:(NSString *)userId
882893 if (!deviceId) {
883894 deviceId = [[UIDevice currentDevice ] leanplum_uniqueGlobalDeviceIdentifier ];
884895 }
885- [[LPAPIConfig sharedConfig ] setDeviceId: deviceId];
896+ [[Leanplum user ] setDeviceId: deviceId];
886897 }
887898
888899 // Set user ID.
889900 if (!userId) {
890- userId = [LPAPIConfig sharedConfig ].userId ;
901+ userId = [Leanplum user ].userId ;
891902 if (!userId) {
892- userId = [LPAPIConfig sharedConfig ].deviceId ;
903+ userId = [Leanplum user ].deviceId ;
893904 }
894905 }
895- [[LPAPIConfig sharedConfig ] setUserId: userId];
906+ [[Leanplum user ] setUserId: userId];
896907
897908 // Setup parameters.
898909 NSString *versionName = [self appVersion ];
@@ -944,7 +955,7 @@ + (void)startWithUserId:(NSString *)userId
944955 params[LP_PARAM_INBOX_MESSAGES] = [self .inbox messagesIds ];
945956
946957 // Push token.
947- NSString *pushToken = [[Leanplum notificationsManager ] pushToken ];
958+ NSString *pushToken = [[Leanplum user ] pushToken ];
948959 if (pushToken) {
949960 params[LP_PARAM_DEVICE_PUSH_TOKEN] = pushToken;
950961 }
@@ -971,8 +982,9 @@ + (void)startWithUserId:(NSString *)userId
971982 NSString *varsJson = [LPJSON stringFromJSON: [response valueForKey: LP_KEY_VARS]];
972983 NSString *varsSignature = response[LP_KEY_VARS_SIGNATURE];
973984 NSArray *localCaps = response[LP_KEY_LOCAL_CAPS];
974- [[LPAPIConfig sharedConfig ] setToken: token];
975- [[LPAPIConfig sharedConfig ] saveToken ];
985+ if (token) {
986+ [[ApiConfig shared ] setToken: token];
987+ }
976988 [[LPVarCache sharedCache ] applyVariableDiffs: values
977989 messages: messages
978990 variants: variants
@@ -1027,8 +1039,8 @@ + (void)startWithUserId:(NSString *)userId
10271039 [[LPVarCache sharedCache ] setDevModeValuesFromServer: valuesFromCode
10281040 fileAttributes: fileAttributes
10291041 actionDefinitions: actionDefinitions];
1030- [[LeanplumSocket sharedSocket ] connectToAppId: [LPAPIConfig sharedConfig ].appId
1031- deviceId: [LPAPIConfig sharedConfig ].deviceId];
1042+ [[LeanplumSocket sharedSocket ] connectToAppId: [ApiConfig shared ].appId
1043+ deviceId: [Leanplum user ].deviceId];
10321044 if ([response[LP_KEY_IS_REGISTERED] boolValue ]) {
10331045 [Leanplum onHasStartedAndRegisteredAsDeveloper ];
10341046 }
@@ -2206,13 +2218,13 @@ + (void)setUserIdInternal:(NSString *)userId withAttributes:(NSDictionary *)attr
22062218
22072219 LPRequest *request = [LPRequestFactory setUserAttributesWithParams: @{
22082220 LP_PARAM_USER_ATTRIBUTES: attributes ? [LPJSON stringFromJSON: attributes] : @" " ,
2209- LP_PARAM_USER_ID: [LPAPIConfig sharedConfig ].userId ?: @" " ,
2221+ LP_PARAM_USER_ID: [Leanplum user ].userId ?: @" " ,
22102222 LP_PARAM_NEW_USER_ID: userId ?: @" "
22112223 }];
22122224 [[LPRequestSender sharedInstance ] send: request];
22132225
22142226 if (userId.length ) {
2215- [[LPAPIConfig sharedConfig ] setUserId: userId];
2227+ [[Leanplum user ] setUserId: userId];
22162228 if ([LPInternalState sharedState ].hasStarted ) {
22172229 [[LPVarCache sharedCache ] saveDiffs ];
22182230 }
@@ -2648,7 +2660,7 @@ + (NSString *)deviceId
26482660 [self throwError: @" [Leanplum start] must be called before calling deviceId" ];
26492661 return nil ;
26502662 }
2651- return [LPAPIConfig sharedConfig ].deviceId ;
2663+ return [Leanplum user ].deviceId ;
26522664 LP_END_TRY
26532665 return nil ;
26542666}
@@ -2660,7 +2672,7 @@ + (NSString *)userId
26602672 [self throwError: @" [Leanplum start] must be called before calling userId" ];
26612673 return nil ;
26622674 }
2663- return [LPAPIConfig sharedConfig ].userId ;
2675+ return [Leanplum user ].userId ;
26642676 LP_END_TRY
26652677 return nil ;
26662678}
0 commit comments