diff --git a/Example/PNObject/PNObjectViewController.m b/Example/PNObject/PNObjectViewController.m index dcf3247..7191d91 100644 --- a/Example/PNObject/PNObjectViewController.m +++ b/Example/PNObject/PNObjectViewController.m @@ -102,7 +102,7 @@ - (void) apiCallAction { - PNObjcPassword *password = [PNObjcPassword new]; + /*PNObjcPassword *password = [PNObjcPassword new]; [password setPassword:@"asdasdasd"]; [password setConfirmPassword:@"asdasdasd"]; @@ -113,10 +113,10 @@ [user setPassword:password]; [user setHasAcceptedNewsletter:YES]; [user setHasAcceptedPrivacy:YES]; - +*/ //[user saveLocally]; - NSLog(@"%@",[user JSONFormObject]); + NSLog(@"%@",[[User currentUser] JSONFormObject]); //NSLog(@"%@",[user JSONObjectMap]); /*[user registerWithBlockSuccess:^(PNUser * _Nullable responseObject) { @@ -138,10 +138,24 @@ [User loginCurrentUserWithEmail:@"packman@giuseppenucifora.com" password:@"asdasdasd" withBlockSuccess:^(PNUser * _Nullable responseObject) { NSLog(@"response : %@",responseObject); + + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [responseObject reloadFormServer]; + }); } failure:^(NSError * _Nonnull error) { NSLog(@"response : %@",error); }]; + + + /*[User resetPasswordForEmail:@"packman@giuseppenucifora.com" Progress:^(NSProgress * _Nonnull uploadProgress) { + + } Success:^(NSDictionary * _Nullable responseObject) { + + } failure:^(NSError * _Nonnull error) { + + }];*/ + } - (void)didReceiveMemoryWarning diff --git a/Pod/Classes/PNClasses/PNInstallation.m b/Pod/Classes/PNClasses/PNInstallation.m index 9b46a96..6e09159 100644 --- a/Pod/Classes/PNClasses/PNInstallation.m +++ b/Pod/Classes/PNClasses/PNInstallation.m @@ -95,7 +95,7 @@ static bool isFirstAccess = YES; NSDictionary *savedInstallation = [[PNObjectModel sharedInstance] fetchObjectsWithClass:[self class]]; if (savedInstallation) { - self = [super initWithJSON:savedInstallation]; + self = [super initWithLocalJSON:savedInstallation]; } else { self = [super init]; diff --git a/Pod/Classes/PNClasses/PNUser.m b/Pod/Classes/PNClasses/PNUser.m index 0b4f9bb..e9deadd 100644 --- a/Pod/Classes/PNClasses/PNUser.m +++ b/Pod/Classes/PNClasses/PNUser.m @@ -75,7 +75,7 @@ static bool isFirstAccess = YES; NSDictionary *savedUser = [[PNObjectModel sharedInstance] fetchObjectsWithClass:[self class]]; if (savedUser) { - USER = [super initWithJSON:savedUser]; + USER = [super initWithLocalJSON:savedUser]; } if (USER) { @@ -136,11 +136,15 @@ static bool isFirstAccess = YES; ///api/v1/user/profile [self autoLoginWithBlockSuccess:^(BOOL loginSuccess) { - [self GETWithEndpointAction:@"user/profile" + [[self class] GETWithEndpointAction:@"user/profile" progress:nil success:^(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject) { - NSLogDebug(@"%@",responseObject); + NSLogDebug(@"%@",[responseObject objectForKey:@"user"]); + + [self populateObjectFromJSON:[responseObject objectForKey:@"user"]]; + [self saveLocally]; + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLogDebug(@"%@",error); }]; @@ -152,7 +156,7 @@ static bool isFirstAccess = YES; - (void) registerWithBlockSuccess:(nullable void (^)(PNUser * _Nullable responseObject))success failure:(nullable void (^)(NSError * _Nonnull error))failure { - [self POSTWithEndpointAction:@"registration/register" parameters:[self JSONFormObject] + [[self class] POSTWithEndpointAction:@"registration/register" parameters:[self JSONFormObject] progress:nil success:^(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject) { NSLog(@"response %@",responseObject); @@ -295,23 +299,23 @@ static bool isFirstAccess = YES; NSDictionary *mapping = @{ @"userId":@"uuid", - @"firstName":@"firstName", - @"lastName":@"lastName", - @"profileImage":@"profileImage", + @"firstName":@"first_name", + @"lastName":@"last_name", + @"profileImage":@"profile_image", @"sex":@"sex", - @"birthDate":@"birthDate", + @"birthDate":@"birth_year", @"phone":@"phone", @"password":@{@"key":@"password",@"type":@"PNObjcPassword"}, - @"hasAcceptedPrivacy":@"hasAcceptedPrivacy", - @"hasAcceptedNewsletter":@"hasAcceptedNewsletter", - @"hasVerifiedEmail":@"hasVerifiedEmail", + @"hasAcceptedPrivacy":@"has_accepted_privacy", + @"hasAcceptedNewsletter":@"has_accepted_newsletter", + @"hasVerifiedEmail":@"has_verified_email", @"hasVerifiedPhone":@"has_verified_phone", - @"emailVerifiedDate":@"emailVerifiedDate", + @"emailVerifiedDate":@"email_verified_date", @"email":@"email", @"username":@"username", @"publicProfile":@"publicProfile", @"loginCount":@"login_count", - @"facebookId":@"facebookId", + @"facebookId":@"facebook_id", @"facebookAccessToken":@"facebookAccessToken", @"isFacebookUser":@"isFacebookUser", @"registeredAt":@"registeredAt", diff --git a/Pod/Classes/PNObject+PNObjectConnection.h b/Pod/Classes/PNObject+PNObjectConnection.h index 0dc08a2..86390aa 100644 --- a/Pod/Classes/PNObject+PNObjectConnection.h +++ b/Pod/Classes/PNObject+PNObjectConnection.h @@ -11,14 +11,14 @@ @interface PNObject (PNObjectConnection) -- (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint - progress:(nullable void (^)(NSProgress * _Nonnull downloadProgress)) downloadProgress ++ (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint + progress:(nullable void (^)(NSProgress * _Nullable downloadProgress)) downloadProgress success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure; -- (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint - parameters:(NSDictionary * _Nonnull) parameters - progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress ++ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint + parameters:(NSDictionary * _Nullable) parameters + progress:(nullable void (^)(NSProgress * _Nullable uploadProgress)) uploadProgress success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure; diff --git a/Pod/Classes/PNObject+PNObjectConnection.m b/Pod/Classes/PNObject+PNObjectConnection.m index a232a5c..b01df47 100644 --- a/Pod/Classes/PNObject+PNObjectConnection.m +++ b/Pod/Classes/PNObject+PNObjectConnection.m @@ -16,7 +16,7 @@ @implementation PNObject (PNObjectConnection) -- (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint ++ (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint progress:(nullable void (^)(NSProgress * _Nonnull downloadProgress)) downloadProgress success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure { @@ -48,8 +48,8 @@ } } -- (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint - parameters:(NSDictionary * _Nonnull) parameters ++ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint + parameters:(NSDictionary * _Nullable) parameters progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure { @@ -91,14 +91,14 @@ if (response) { if ([response isKindOfClass:[NSDictionary class]] && [[response allKeys] count] > 0) { - PNObjectResponse = [[[self class] alloc] initWithJSON:[response copy]]; + PNObjectResponse = [[[self class] alloc] initWithRemoteJSON:[response copy]]; } else if ([response isKindOfClass:[NSArray class]] && [response count] > 0){ NSMutableArray * resposeArray = [[NSMutableArray alloc] init]; for (id singleObjectDict in response) { if ([singleObjectDict isKindOfClass:[NSDictionary class]]) { - id singleObject = [[[self class] alloc] initWithJSON:singleObjectDict]; + id singleObject = [[[self class] alloc] initWithRemoteJSON:singleObjectDict]; [resposeArray addObject:singleObject]; } } diff --git a/Pod/Classes/PNObject+Protected.h b/Pod/Classes/PNObject+Protected.h index e44b445..093c423 100644 --- a/Pod/Classes/PNObject+Protected.h +++ b/Pod/Classes/PNObject+Protected.h @@ -23,6 +23,15 @@ extern NSString* _Nonnull const PNObjectMappingSelector; * @param JSON <#JSON description#> */ - (void)populateObjectFromJSON:(id _Nullable)JSON; + +/** + * <#Description#> + * + * @param JSON <#JSON description#> + * @param fromLocal <#fromLocal description#> + */ +- (void) populateObjectFromJSON:(id _Nullable)JSON fromLocal:(BOOL) fromLocal; + /** * <#Description#> * @@ -31,12 +40,14 @@ extern NSString* _Nonnull const PNObjectMappingSelector; * @return <#return value description#> */ - (BOOL)isObjNull:(id _Nullable)obj; + /** * <#Description#> * * @return <#return value description#> */ + (NSArray * _Nonnull) protectedProperties; + /** * <#Description#> * diff --git a/Pod/Classes/PNObject+Protected.m b/Pod/Classes/PNObject+Protected.m index d8cf852..e34cac1 100644 --- a/Pod/Classes/PNObject+Protected.m +++ b/Pod/Classes/PNObject+Protected.m @@ -30,8 +30,12 @@ NSString * const PNObjectMappingSelector = @"PNObjectLocalNotificationRefreshTok return @[@"JSON",@"objectModel",@"objectMapping",@"singleInstance"]; } -- (void)populateObjectFromJSON:(id _Nullable)JSON -{ +- (void) populateObjectFromJSON:(id)JSON { + return [self populateObjectFromJSON:JSON fromLocal:NO]; +} + +- (void) populateObjectFromJSON:(id _Nullable)JSON fromLocal:(BOOL) fromLocal { + NSDictionary *properties = [PNObject propertiesForClass:self.class]; for(NSString *propertyName in properties) { @@ -46,7 +50,6 @@ NSString * const PNObjectMappingSelector = @"PNObjectLocalNotificationRefreshTok id mappingValue = [[[self class] objcetMapping] valueForKey:propertyName]; - if([mappingValue isKindOfClass:NSDictionary.class]) { mappedJSONKey = [mappingValue valueForKey:@"key"]; mappedJSONType = [mappingValue valueForKey:@"type"]; @@ -54,13 +57,23 @@ NSString * const PNObjectMappingSelector = @"PNObjectLocalNotificationRefreshTok mappedJSONKey = mappingValue; } + /*if (fromLocal) { + propertyName = mappedJSONKey; + }*/ if ([[PNObject protectedProperties] containsObject:propertyName] || [self isObjNull:mappedJSONKey]) { continue; } // Get JSON value for the mapped key - id value = [JSON valueForKeyPath:mappedJSONKey]; + id value; + if (fromLocal) { + value = [JSON valueForKeyPath:propertyName]; + } + else { + value = [JSON valueForKeyPath:mappedJSONKey]; + } + if([self isObjNull:value]) { continue; @@ -121,7 +134,7 @@ NSString * const PNObjectMappingSelector = @"PNObjectLocalNotificationRefreshTok @"NSArray" : ^{ NSMutableArray *arr = [NSMutableArray array]; for(id JSONObject in value) { - PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject]; + PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject fromLocal:fromLocal]; [arr addObject:val]; } @@ -130,7 +143,7 @@ NSString * const PNObjectMappingSelector = @"PNObjectLocalNotificationRefreshTok @"NSMutableArray" : ^{ NSMutableArray *arr = [NSMutableArray array]; for(id JSONObject in value) { - PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject]; + PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject fromLocal:fromLocal]; [arr addObject:val]; } @@ -142,7 +155,7 @@ NSString * const PNObjectMappingSelector = @"PNObjectLocalNotificationRefreshTok if(isPNObjectSubclass) { - PNObject *val = [[NSClassFromString(propertyType) alloc] initWithJSON:value]; + PNObject *val = [[NSClassFromString(propertyType) alloc] initWithJSON:value fromLocal:fromLocal]; [self setValue:val forKey:propertyName]; } else { diff --git a/Pod/Classes/PNObject.h b/Pod/Classes/PNObject.h index f829cd8..75952b6 100644 --- a/Pod/Classes/PNObject.h +++ b/Pod/Classes/PNObject.h @@ -32,9 +32,13 @@ + (NSString * _Nonnull) PNObjEndPoint; -+ (NSArray * _Nonnull) batch:(id _Nonnull)JSON; ++ (NSArray * _Nonnull) batch:(id _Nonnull)JSON fromLocal:(BOOL) fromLocal; -- (_Nullable instancetype) initWithJSON:( NSDictionary * _Nonnull) JSON; +- (_Nullable instancetype) initWithLocalJSON:( NSDictionary * _Nonnull) JSON; + +- (_Nullable instancetype) initWithRemoteJSON:( NSDictionary * _Nonnull) JSON; + +- (_Nullable instancetype) initWithJSON:( NSDictionary * _Nonnull) JSON fromLocal:(BOOL) fromLocal; - (id _Nonnull) saveLocally; diff --git a/Pod/Classes/PNObject.m b/Pod/Classes/PNObject.m index 64c6150..5d1b6d8 100644 --- a/Pod/Classes/PNObject.m +++ b/Pod/Classes/PNObject.m @@ -114,7 +114,16 @@ return self; } -- (_Nullable instancetype) initWithJSON:( NSDictionary * _Nonnull) JSON { +- (_Nullable instancetype) initWithRemoteJSON:( NSDictionary * _Nonnull) JSON { + return [self initWithJSON:JSON fromLocal:NO]; +} + +- (_Nullable instancetype) initWithLocalJSON:( NSDictionary * _Nonnull) JSON { + return [self initWithJSON:JSON fromLocal:YES]; +} + + +- (_Nullable instancetype) initWithJSON:( NSDictionary * _Nonnull) JSON fromLocal:(BOOL) fromLocal { self = [super init]; if (self) { if ([[self class] isSubclassOfClass:[PNObject class]]) { @@ -150,14 +159,14 @@ NSAssert(self.JSONObjectMap, @"You must create objectMapping"); self.JSON = [[NSMutableDictionary alloc] initWithDictionary:JSON]; - [self populateObjectFromJSON:self.JSON]; + [self populateObjectFromJSON:self.JSON fromLocal:fromLocal]; } return self; } - (NSDictionary * _Nonnull) reverseMapping { - NSMutableDictionary *JSON = [NSMutableDictionary dictionary]; + NSMutableDictionary *JSON = [[NSMutableDictionary alloc] init]; NSString *mappedJSONKey; NSString *mappedJSONType; @@ -350,14 +359,14 @@ return [self.objectModel removeObjectLocally:self]; } -+ (NSArray *)batch:(id)JSON ++ (NSArray *)batch:(id)JSON fromLocal:(BOOL) fromLocal { NSString *className = NSStringFromClass([self class]); NSMutableArray *batch = [NSMutableArray array]; for(id objectJSON in JSON) { - PNObject *val = [[NSClassFromString(className) alloc] initWithJSON:objectJSON]; + PNObject *val = [[NSClassFromString(className) alloc] initWithJSON:objectJSON fromLocal:fromLocal]; [batch addObject:val]; } diff --git a/Pod/Classes/PNObjectModel.m b/Pod/Classes/PNObjectModel.m index 6f1f4be..2280d2f 100644 --- a/Pod/Classes/PNObjectModel.m +++ b/Pod/Classes/PNObjectModel.m @@ -172,7 +172,6 @@ static bool isFirstAccess = YES; NSData *objectData = [RNCryptor encryptData:[NSKeyedArchiver archivedDataWithRootObject:objectDict] password:[[PNObjectConfig sharedInstance] encrypKey]]; - if ([self issetPNObjectModelForObject:object]) { if ([_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) { return object;