- Important fix in PNObject

This commit is contained in:
Giuseppe Nucifora 2016-01-27 17:07:06 +01:00
parent fad171373e
commit 7fc6f135bd
7 changed files with 205 additions and 81 deletions

View File

@ -29,11 +29,10 @@
PNUser *user = [PNUser currentUser]; PNUser *user = [PNUser currentUser];
//NSLog(@"user : %@",[user getJSONObject]);
NSLog(@"user : %@",[user getJSONObject]);
//[user autoRemoveLocally]; //[user autoRemoveLocally];
/*[user setFirstName:@"Giuseppe2"]; [user setFirstName:@"Giuseppe2"];
[user setLastName:@"Nucifora2"]; [user setLastName:@"Nucifora2"];
[user setEmail:@"giuseppe.nucifora@giuseppenucifora.com"]; [user setEmail:@"giuseppe.nucifora@giuseppenucifora.com"];
[user setSex:@"M"]; [user setSex:@"M"];
@ -44,9 +43,14 @@
[user setPhone:@"+393485904995"]; [user setPhone:@"+393485904995"];
[user setUserId:@"blablabla"]; [user setUserId:@"blablabla"];
[user saveLocally]; [user saveLocally];
*/
//NSLog(@"%@",[user getObject]);
[user setFirstName:@"Angela"];
[user setLastName:@"Sapienza"];
[user getJSONObject];
//NSLog(@"%@",userDictionary);
/* /*

View File

@ -17,8 +17,18 @@
* @return singleton * @return singleton
*/ */
+ (instancetype _Nonnull) currentUser; + (instancetype _Nonnull) currentUser;
/**
* <#Description#>
*
* @param password <#password description#>
*
* @return <#return value description#>
*/
- (BOOL) isValidPassword:(NSString* _Nonnull) password; - (BOOL) isValidPassword:(NSString* _Nonnull) password;
/**
* <#Description#>
*/
- (void) logout;
//- (void) setPassword:(NSString * _Nonnull)password inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id responseObject, NSError * error)) responseBlock; //- (void) setPassword:(NSString * _Nonnull)password inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id responseObject, NSError * error)) responseBlock;
@ -29,15 +39,15 @@
/** /**
* <#Description#> * <#Description#>
*/ */
@property (strong, nonatomic, nonnull) NSString * userId; @property (strong, nonatomic, nullable) NSString * userId;
/** /**
* <#Description#> * <#Description#>
*/ */
@property (strong, nonatomic, nonnull) NSString * firstName; @property (strong, nonatomic, nullable) NSString * firstName;
/** /**
* <#Description#> * <#Description#>
*/ */
@property (strong, nonatomic, nonnull) NSString * lastName; @property (strong, nonatomic, nullable) NSString * lastName;
/** /**
* <#Description#> * <#Description#>
*/ */
@ -73,15 +83,15 @@
/** /**
* <#Description#> * <#Description#>
*/ */
@property (nonatomic, strong, nonnull) NSString * email; @property (nonatomic, strong, nullable) NSString * email;
/** /**
* <#Description#> * <#Description#>
*/ */
@property (nonatomic, strong, nonnull) NSString * username; @property (nonatomic, strong, nullable) NSString * username;
/** /**
* <#Description#> * <#Description#>
*/ */
@property (nonatomic, strong, nonnull) NSString * password; @property (nonatomic, strong, nullable) NSString * password;
/** /**
* <#Description#> * <#Description#>
*/ */
@ -89,7 +99,7 @@
/** /**
* <#Description#> * <#Description#>
*/ */
@property (nonatomic, strong, nonnull) NSNumber * loginCount; @property (nonatomic, strong, nullable) NSNumber * loginCount;
/** /**
* <#Description#> * <#Description#>
*/ */

View File

@ -91,8 +91,10 @@ static bool isFirstAccess = YES;
if ([email isValidEmail]) { if ([email isValidEmail]) {
_email = email; _email = email;
} }
else {
NSLogDebug(@"insertedEmail is not valid."); NSLogDebug(@"insertedEmail is not valid.");
} }
}
- (void) setPassword:(NSString *)password { - (void) setPassword:(NSString *)password {
if ([self isValidPassword:password]) { if ([self isValidPassword:password]) {
@ -115,6 +117,11 @@ static bool isFirstAccess = YES;
return @"password is not readble"; return @"password is not readble";
} }
- (void)logout {
[self autoRemoveLocally];
[self resetObject];
}
#pragma mark PNObjectSubclassing Protocol #pragma mark PNObjectSubclassing Protocol
+ (NSDictionary *)objcetMapping { + (NSDictionary *)objcetMapping {

View File

@ -38,6 +38,7 @@
*/ */
+ (NSDictionary * _Nullable)propertiesForClass:(Class _Nonnull)PNObjClass; + (NSDictionary * _Nullable)propertiesForClass:(Class _Nonnull)PNObjClass;
- (void)resetObject;
///-------------------------------------- ///--------------------------------------
#pragma mark - PNObject (Protected) Properties #pragma mark - PNObject (Protected) Properties

View File

@ -26,6 +26,10 @@
{ {
NSDictionary *properties = [PNObject propertiesForClass:self.class]; NSDictionary *properties = [PNObject propertiesForClass:self.class];
for (NSString *propertyName in properties) {
[self addObserver:self forKeyPath:propertyName options:NSKeyValueObservingOptionNew context:nil];
}
for(NSString *propertyName in properties) { for(NSString *propertyName in properties) {
if([propertyName isEqualToString:@"mappingError"]) if([propertyName isEqualToString:@"mappingError"])
@ -132,6 +136,94 @@
} }
} }
- (void)resetObject
{
NSDictionary *properties = [PNObject propertiesForClass:self.class];
for(NSString *propertyName in properties) {
if([propertyName isEqualToString:@"mappingError"])
continue;
NSString *mappedJSONKey;
NSString *mappedJSONType;
NSString *propertyType = [properties valueForKey:propertyName];
id mappingValue = [self.objectMapping valueForKey:propertyName];
if([mappingValue isKindOfClass:NSDictionary.class]) {
mappedJSONKey = [mappingValue valueForKey:@"key"];
mappedJSONType = [mappingValue valueForKey:@"type"];
} else {
mappedJSONKey = mappingValue;
}
if ([[PNObject protectedProperties] containsObject:propertyName]
|| [propertyName isEqualToString:@"description"]
|| [propertyName isEqualToString:@"debugDescription"]) {
continue;
}
// Get JSON value for the mapped key
((void (^)())@{
@"c" : ^{
char val = '\0';
[self setValue:@(val) forKey:propertyName];
},
@"d" : ^{
double val = 0.0;
[self setValue:@(val) forKey:propertyName];
},
@"f" : ^{
float val = 0.0;
[self setValue:@(val) forKey:propertyName];
},
@"i" : ^{
int val = 0;
[self setValue:@(val) forKey:propertyName];
},
@"l" : ^{
long val = 0;
[self setValue:@(val) forKey:propertyName];
},
@"s" : ^{
short val = 0;
[self setValue:@(val) forKey:propertyName];
},
@"B" : ^{
[self setValue:@(NO) forKey:propertyName];
},
@"NSString" : ^{
[self setValue:[[NSString alloc] init] forKey:propertyName];
},
@"NSNumber" : ^{
[self setValue:[[NSNumber alloc] init] forKey:propertyName];
},
@"NSDate" : ^{
[self setValue:[[NSDate alloc] init] forKey:propertyName];
},
@"NSArray" : ^{
[self setValue:[[NSArray alloc] init] forKey:propertyName];
},
@"NSMutableArray" : ^{
[self setValue:[[NSMutableArray alloc] init] forKey:propertyName];
}
}[propertyType] ?: ^{
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
[self setValue:@"" forKey:propertyName];
}
else {
NSString *errorStr = [NSString stringWithFormat:@"Property '%@' could not be assigned any value.", propertyName];
NSLogDebug(@"%@",errorStr);
}
})();
}
}
- (BOOL)isObjNull:(id _Nullable)obj - (BOOL)isObjNull:(id _Nullable)obj
{ {
if(!obj || nil == obj || NSNull.null == obj || ([obj isKindOfClass:[NSString class]] && [obj isEqualToString:@"(null)"]) || [obj isEqual:[NSNull null]]) if(!obj || nil == obj || NSNull.null == obj || ([obj isKindOfClass:[NSString class]] && [obj isEqualToString:@"(null)"]) || [obj isEqual:[NSNull null]])

View File

@ -12,6 +12,7 @@
#import "PNObject/PNUser.h" #import "PNObject/PNUser.h"
#import "PNObjectConstants.h" #import "PNObjectConstants.h"
#import "PNObject+Protected.h" #import "PNObject+Protected.h"
#import "objc/runtime.h"
#define PNOBJECT_DIR @"PNObjects" #define PNOBJECT_DIR @"PNObjects"
@ -242,10 +243,6 @@
// do nothing // do nothing
} }
})(); })();
} }
_JSON = JSON; _JSON = JSON;
@ -254,12 +251,17 @@
} }
- (NSDictionary* _Nonnull) getJSONObject { - (NSDictionary* _Nonnull) getJSONObject {
if (!_JSON) {
return [self reverseMapping]; return [self reverseMapping];
} }
else {
return _JSON;
}
}
- (NSString* _Nonnull) description { - (NSString* _Nonnull) description {
if (!_JSON) { if (!_JSON) {
return [[self reverseMapping] description]; [self reverseMapping];
} }
return [_JSON description]; return [_JSON description];
} }
@ -305,4 +307,16 @@
#pragma mark - #pragma mark -
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[_JSON setObject:[change objectForKey:@"new"] forKey:keyPath];
}
- (void)dealloc
{
unsigned int propertyCount;
objc_property_t *properties = class_copyPropertyList([self class], &propertyCount);
for(int i = 0; i < propertyCount; i++)[self removeObserver:self forKeyPath:[NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding]];
}
@end @end

View File

@ -170,8 +170,6 @@ static bool isFirstAccess = YES;
NSDictionary *objectDict; NSDictionary *objectDict;
[invocation getReturnValue:&objectDict]; [invocation getReturnValue:&objectDict];
NSLogDebug(@"%@",objectDict);
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objectDict]; NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objectDict];
if ([self issetPNObjectModelForObject:object]) { if ([self issetPNObjectModelForObject:object]) {
@ -236,8 +234,6 @@ static bool isFirstAccess = YES;
NSDictionary *objectDict; NSDictionary *objectDict;
[invocation getReturnValue:&objectDict]; [invocation getReturnValue:&objectDict];
NSLogDebug(@"%@",objectDict);
[objects addObject:objectDict]; [objects addObject:objectDict];
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objects]; NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objects];