no message

This commit is contained in:
giuseppenuc committed 2016-02-15 09:31:31 +01:00
1 parent bb38a40ea9
commit 549b27d6de
41 files changed
+7383 -3942

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
//
// PNObjcPassword.h
// Pods
//
// Created by Giuseppe Nucifora on 13/01/16.
//
//
#import "PNObject.h"
@interface PNObjcPassword : PNObject <PNObjectSubclassing>
+ (BOOL) validateMinimumRequirenment:(NSString * _Nullable) passString;
- (BOOL) isValid;
///--------------------------------------
#pragma mark - PNObjcPassword Properties
///--------------------------------------
/**
* <#Description#>
*/
@property (nonatomic, strong, nullable) NSString *password;
/**
* <#Description#>
*/
@property (nonatomic, strong, nullable) NSString *confirmPassword;
@end
+81
View File
@@ -0,0 +1,81 @@
//
// PNObjcPassword.m
// Pods
//
// Created by Giuseppe Nucifora on 13/01/16.
//
//
#import "PNObjcPassword.h"
#import "PNUser.h"
#import "PNObjectConstants.h"
@implementation PNObjcPassword
+ (NSString *) objectClassName {
return @"Password";
}
+(NSString *)objectEndPoint {
return @"Password";
}
+ (NSDictionary *) objcetMapping {
NSDictionary *mapping = @{@"password":@"password",
@"confirmPassword":@"confirmPassword",
};
return mapping;
}
+ (BOOL)singleInstance {
return NO;
}
- (void) setPassword:(NSString *)password {
if ([self validateMinimumRequirenment:password]) {
_password = password;
}
else {
NSLogDebug(@"Inserted Passord is not valid.Lenght must be >= %ld",(long)[[PNObjectConfig sharedInstance] minPasswordLenght]);
}
}
- (void) setConfirmPassword:(NSString *)confirmPassword {
if ([self validateMinimumRequirenment:confirmPassword]) {
if ([confirmPassword isEqualToString:_password]) {
_confirmPassword = confirmPassword;
}
else {
NSLogDebug(@"Inserted Passord is not same password.");
}
}
else {
NSLogDebug(@"Inserted Passord is not valid.Lenght must be >= %ld",(long)[[PNObjectConfig sharedInstance] minPasswordLenght]);
}
}
- (BOOL)isValid {
if ([self validateMinimumRequirenment:_password] && [self validateMinimumRequirenment:_confirmPassword] && [_confirmPassword isEqualToString:_password]) {
return YES;
}
return NO;
}
- (BOOL) validateMinimumRequirenment:(NSString * _Nullable) passString {
if ([passString length] >= [[PNObjectConfig sharedInstance] minPasswordLenght]) {
return YES;
}
return NO;
}
+ (BOOL) validateMinimumRequirenment:(NSString * _Nullable) passString {
if ([passString length] >= [[PNObjectConfig sharedInstance] minPasswordLenght]) {
return YES;
}
return NO;
}
@end
+6 -15
View File
@@ -7,6 +7,7 @@
//
#import "PNObject.h"
#import "PNObjcPassword.h"
@interface PNUser : PNObject
@@ -33,15 +34,6 @@
*/
+ (BOOL) isValidPassword:(NSString* _Nonnull) password;
/**
* <#Description#>
*
* @param password <#password description#>
*
* @return <#return value description#>
*/
- (BOOL) isValidPassword:(NSString* _Nonnull) password;
/**
* <#Description#>
*/
@@ -159,11 +151,7 @@
/**
* <#Description#>
*/
@property (nonatomic, strong, nullable) NSString * password;
/**
* <#Description#>
*/
@property (nonatomic, strong, nullable) NSString * confirmPassword;
@property (nonatomic, strong, nullable) PNObjcPassword * password;
/**
* <#Description#>
*/
@@ -184,6 +172,9 @@
* <#Description#>
*/
@property (nonatomic, strong, nullable) NSString * facebookAccessToken;
/**
* <#Description#>
*/
@property (nonatomic, strong, nullable) NSDate * registeredAt;
@end
+12 -42
View File
@@ -15,6 +15,7 @@
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <FBSDKShareKit/FBSDKShareKit.h>
#import "PNObjcPassword.h"
@interface PNUser() <PNObjectSubclassing>
@@ -98,56 +99,23 @@ static bool isFirstAccess = YES;
}
}
- (void) setPassword:(NSString *)password {
if ([self isValidPassword:password]) {
_password = password;
}
else {
NSLogDebug(@"Inserted Passord is not valid.Lenght must be >= %ld",(long)[[PNObjectConfig sharedInstance] minPasswordLenght]);
}
}
- (void) setConfirmPassword:(NSString *)confirmPassword {
if ([self isValidPassword:confirmPassword]) {
if ([confirmPassword isEqualToString:_password]) {
_confirmPassword = confirmPassword;
}
else {
NSLogDebug(@"Inserted Passord is not same password.");
}
}
else {
NSLogDebug(@"Inserted Passord is not valid.Lenght must be >= %ld",(long)[[PNObjectConfig sharedInstance] minPasswordLenght]);
}
}
- (BOOL) isValidPassword:(NSString* _Nonnull) password {
if ([password length] >= [[PNObjectConfig sharedInstance] minPasswordLenght]) {
return YES;
}
return NO;
}
+ (BOOL) isValidPassword:(NSString* _Nonnull) password {
if ([password length] >= [[PNObjectConfig sharedInstance] minPasswordLenght]) {
return YES;
}
return NO;
}
- (void)logout {
[self autoRemoveLocally];
[self resetObject];
}
- (BOOL) hasValidEmailAndPasswordData {
if(self.email && [self.email isValidEmail] && self.password && [self isValidPassword:[self password]]){
if(self.email && [self.email isValidEmail] && self.password && [self.password isValid]){
return YES;
}
return NO;
}
+ (BOOL) isValidPassword:(NSString* _Nonnull) password {
return [PNObjcPassword validateMinimumRequirenment:password];
}
- (void) autoLogin {
[self autoLoginWithBlockSuccess:nil failure:nil];
}
@@ -300,10 +268,13 @@ static bool isFirstAccess = YES;
[[PNObjectConfig sharedInstance] refreshTokenForUserWithEmail:email password:password withBlockSuccess:^(BOOL refreshSuccess) {
if (refreshSuccess) {
PNObjcPassword *objectPassword = [PNObjcPassword new];
[objectPassword setPassword:password];
[objectPassword setConfirmPassword:password];
[self setAuthenticated:YES];
[self setEmail:email];
[self setPassword:password];
[self setConfirmPassword:password];
[self setPassword:objectPassword];
[self saveLocally];
if (success) {
@@ -331,8 +302,7 @@ static bool isFirstAccess = YES;
@"sex":@"sex",
@"birthDate":@"birthDate",
@"phone":@"phone",
@"password":@"plainPassword[first]",
@"confirmPassword":@"plainPassword[second]",
@"password":@{@"key":@"password",@"type":@"PNObjcPassword"},
@"hasAcceptedPrivacy":@"hasAcceptedPrivacy",
@"hasAcceptedNewsletter":@"hasAcceptedNewsletter",
@"hasVerifiedEmail":@"hasVerifiedEmail",
+20 -3
View File
@@ -6,7 +6,15 @@
//
//
#import <PNObject/PNObject.h>
#import "PNObject.h"
#pragma mark MappingSelector Keys
extern NSString* _Nonnull const PNObjectMappingKey;
extern NSString* _Nonnull const PNObjectMappingType;
extern NSString* _Nonnull const PNObjectMappingSelector;
#pragma mark -
@interface PNObject (Protected)
/**
@@ -38,6 +46,15 @@
*/
+ (NSDictionary * _Nullable)propertiesForClass:(Class _Nonnull)PNObjClass;
/**
* <#Description#>
*
* @param mappingSelector <#mappingSelector description#>
*
* @return <#return value description#>
*/
- (NSDictionary* _Nonnull) getFormObject:(SEL _Nonnull) dictionaryMappingSelector;
- (void)resetObject;
///--------------------------------------
@@ -50,11 +67,11 @@
/**
* <#Description#>
*/
@property (nonatomic, strong, nonnull) NSDictionary * JSON;
@property (nonatomic, strong, nullable) NSMutableDictionary * JSON;
/**
* <#Description#>
*/
@property (nonatomic, strong, nonnull) NSString * endPoint;
@property (nonatomic, strong, nullable) NSString * endPoint;
/**
* <#Description#>
*/
+305 -39
View File
@@ -11,6 +11,14 @@
#import <NSDate_Utils/NSDate+NSDate_Util.h>
#import "objc/runtime.h"
extern NSString* _Nonnull const PNObjectMappingKey;
extern NSString* _Nonnull const PNObjectMappingType;
extern NSString* _Nonnull const PNObjectMappingSelector;
NSString * const PNObjectMappingKey = @"PNObjectLocalNotificationRefreshTokenClientCredentialSuccess";
NSString * const PNObjectMappingType = @"PNObjectLocalNotificationRefreshTokenClientCredentialFail";
NSString * const PNObjectMappingSelector = @"PNObjectLocalNotificationRefreshTokenClientCredentialFail";
@implementation PNObject (Protected)
@dynamic endPoint;
@@ -25,38 +33,39 @@
- (void)populateObjectFromJSON:(id _Nullable)JSON
{
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];
NSLogDebug(@"%@",self.JSONObject);
NSLogDebug(@"%@",[[self class] objcetMapping]);
id mappingValue = [[[self class] objcetMapping] valueForKey:propertyName];
if([mappingValue isKindOfClass:NSDictionary.class]) {
mappedJSONKey = [mappingValue valueForKey:@"key"];
mappedJSONType = [mappingValue valueForKey:@"type"];
} else {
mappedJSONKey = mappingValue;
}
if ([[PNObject protectedProperties] containsObject:propertyName]) {
if ([[PNObject protectedProperties] containsObject:propertyName] || [self isObjNull:mappedJSONKey]) {
continue;
}
// Get JSON value for the mapped key
id value = [JSON valueForKeyPath:mappedJSONKey];
if([self isObjNull:value]) {
continue;
}
((void (^)())@{
@"c" : ^{
char val = [value charValue];
@@ -90,13 +99,10 @@
if (![self isObjNull:value]) {
[self setValue:value forKey:propertyName];
}
},
@"NSNumber" : ^{
NSInteger val = 0;
if (value && ![value isKindOfClass:[NSNull class]]) {
val = [value integerValue];
}
NSInteger val = [value integerValue];
[self setValue:@(val) forKey:propertyName];
},
@@ -106,6 +112,11 @@
if (![self isObjNull:val]) {
[self setValue:val forKey:propertyName];
}
},
@"NSURL" : ^{
NSString *str = [NSString stringWithFormat:@"%@", value];
NSURL *val = [NSURL URLWithString:str];
[self setValue:val forKey:propertyName];
},
@"NSArray" : ^{
NSMutableArray *arr = [NSMutableArray array];
@@ -113,7 +124,7 @@
PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject];
[arr addObject:val];
}
[self setValue:arr forKey:propertyName];
},
@"NSMutableArray" : ^{
@@ -122,12 +133,15 @@
PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject];
[arr addObject:val];
}
[self setValue:arr forKey:propertyName];
}
}[propertyType] ?: ^{
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
PNObject *val = [[NSClassFromString(propertyType) alloc] initWithJSON:value];
[self setValue:val forKey:propertyName];
}
@@ -142,35 +156,35 @@
- (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.JSONObject valueForKey:propertyName];
id mappingValue = [self.JSONObjectMap 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';
@@ -227,6 +241,258 @@
}
}
- (NSDictionary* _Nonnull) getFormObject:(SEL _Nonnull) dictionaryMappingSelector
{
NSMutableDictionary *JSON = [NSMutableDictionary dictionary];
if ([[self class] respondsToSelector:dictionaryMappingSelector]) {
NSDictionary *properties = [PNObject propertiesForClass:self.class];
NSDictionary *formMapping = [[self class] performSelector:dictionaryMappingSelector];
for (NSString *formMappingKey in formMapping) {
if ([[properties allKeys] containsObject:formMappingKey] && [properties objectForKey:formMappingKey]) {
id property = [self valueForKey:formMappingKey];
id formMappingValue = [formMapping objectForKey:formMappingKey];
NSString *mappedKey;
NSString *mappedType;
NSDictionary *mappedValues;
if ([formMappingValue isKindOfClass:[NSDictionary class]]) {
mappedKey = [formMappingValue valueForKey:@"key"];
mappedType = [formMappingValue valueForKey:@"type"];
mappedValues = [formMappingValue valueForKey:@"values"];
}
else {
mappedKey = formMappingKey;
}
((void (^)())@{
@"c" : ^{
char val = [property charValue];
[JSON setValue:@(val) forKey:mappedKey];
},
@"d" : ^{
double val = [property doubleValue];
[JSON setValue:@(val) forKey:mappedKey];
},
@"f" : ^{
float val = [property floatValue];
[JSON setValue:@(val) forKey:mappedKey];
},
@"i" : ^{
int val = [property intValue];
[JSON setValue:@(val) forKey:mappedKey];
},
@"l" : ^{
long val = [property longValue];
[JSON setValue:@(val) forKey:mappedKey];
},
@"s" : ^{
short val = [property shortValue];
[JSON setValue:@(val) forKey:mappedKey];
},
@"B" : ^{
BOOL val = [property boolValue];
[JSON setValue:@(val) forKey:mappedKey];
},
@"UIImage" : ^{
UIImage *image = [UIImage imageWithData:property];
[JSON setValue:image forKey:mappedKey];
},
@"NSURL" : ^{
NSURL *url = property;
if (![self isObjNull:url]) {
[JSON setValue:[url absoluteString] forKey:mappedKey];
}
},
@"NSString" : ^{
NSString *val = [NSString stringWithFormat:@"%@", property];
if (![self isObjNull:val]) {
[JSON setValue:val forKey:mappedKey];
}
},
@"NSNumber" : ^{
NSInteger val = [property integerValue];
[JSON setValue:@(val) forKey:mappedKey];
},
@"NSDate" : ^{
NSDate *val = [property toLocalTime];
if (![self isObjNull:val]) {
[JSON setValue:val forKey:mappedKey];
}
},
@"NSArray" : ^{
NSMutableArray *arr = [NSMutableArray array];
for(id object in property) {
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
NSDictionary *objectDict = [(PNObject*) object getFormObject:dictionaryMappingSelector];
[arr addObject:objectDict];
}
}
[JSON setValue:arr forKey:mappedKey];
},
@"NSMutableArray" : ^{
NSMutableArray *arr = [NSMutableArray array];
for(id object in property) {
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
NSDictionary *objectDict = [(PNObject*) object getFormObject:dictionaryMappingSelector];
[arr addObject:objectDict];
}
}
[JSON setValue:arr forKey:mappedKey];
}
}[[property class]] ?: ^{
BOOL isPNObjectSubclass = [[property class] isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
NSDictionary *objectDict = [(PNObject*)property getFormObject:dictionaryMappingSelector];
[JSON setValue:objectDict forKey:mappedKey];
}
else {
// do nothing
}
})();
}
}
/*for (NSString* propertyName in self.JSONObjectMap) {
id mappingValue = [self.JSONObjectMap objectForKey:propertyName];
if([mappingValue isKindOfClass:NSDictionary.class]) {
mappedJSONKey = [mappingValue valueForKey:@"key"];
mappedJSONType = [mappingValue valueForKey:@"type"];
} else {
mappedJSONKey = mappingValue;
}
NSString *propertyType = [properties valueForKey:propertyName];
if (![[formMapping allKeys] containsObject:propertyName]) {
continue;
}
id value = [self valueForKey:propertyName];
//TODO: forse è da sostituire propertyName con il valore de
((void (^)())@{
@"c" : ^{
char val = [value charValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"d" : ^{
double val = [value doubleValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"f" : ^{
float val = [value floatValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"i" : ^{
int val = [value intValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"l" : ^{
long val = [value longValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"s" : ^{
short val = [value shortValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"B" : ^{
BOOL val = [value boolValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"UIImage" : ^{
UIImage *image = [UIImage imageWithData:value];
[JSON setValue:image forKey:propertyName];
},
@"NSURL" : ^{
NSURL *url = value;
if (![self isObjNull:url]) {
[JSON setValue:[url absoluteString] forKey:propertyName];
}
},
@"NSString" : ^{
NSString *val = [NSString stringWithFormat:@"%@", value];
if (![self isObjNull:val]) {
[JSON setValue:val forKey:propertyName];
}
},
@"NSNumber" : ^{
NSInteger val = [value integerValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"NSDate" : ^{
NSDate *val = [value toLocalTime];
if (![self isObjNull:val]) {
[JSON setValue:val forKey:propertyName];
}
},
@"NSArray" : ^{
NSMutableArray *arr = [NSMutableArray array];
for(id object in value) {
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
NSDictionary *objectDict = [(PNObject*) object reverseMapping];
[arr addObject:objectDict];
}
}
[JSON setValue:arr forKey:propertyName];
},
@"NSMutableArray" : ^{
NSMutableArray *arr = [NSMutableArray array];
for(id object in value) {
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
NSDictionary *objectDict = [(PNObject*) object reverseMapping];
[arr addObject:objectDict];
}
}
[JSON setValue:arr forKey:propertyName];
}
}[propertyType] ?: ^{
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
NSDictionary *objectDict = [(PNObject*)value reverseMapping];
[JSON setValue:objectDict forKey:propertyName];
}
else {
// do nothing
}
})();
}*/
}
return JSON;
}
- (BOOL)isObjNull:(id _Nullable)obj
{
if(!obj || nil == obj || NSNull.null == obj || ([obj isKindOfClass:[NSString class]] && [obj isEqualToString:@"(null)"]) || [obj isEqual:[NSNull null]])
@@ -240,15 +506,15 @@ static BOOL property_getTypeString( objc_property_t property, char *buffer )
const char * attrs = property_getAttributes( property );
if ( attrs == NULL )
return NO;
const char * e = strchr( attrs, ',' );
if ( e == NULL )
return NO;
int len = (int)(e - attrs);
memcpy( buffer, attrs, len );
buffer[len] = '\0';
return YES;
}
@@ -259,13 +525,13 @@ static BOOL property_getTypeString( objc_property_t property, char *buffer )
if (PNObjClass == NULL) {
return nil;
}
NSMutableDictionary *results = [NSMutableDictionary dictionary];
if ([PNObjClass isSubclassOfClass:[PNObject class]] && PNObjClass != [PNObject class]) {
[results addEntriesFromDictionary:[self propertiesForClass:class_getSuperclass(PNObjClass)]];
}
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(PNObjClass, &outCount);
for (i = 0; i < outCount; i++) {
@@ -279,7 +545,7 @@ static BOOL property_getTypeString( objc_property_t property, char *buffer )
}
NSString *propertyName = [NSString stringWithUTF8String:propName];
NSString *propertyType = [NSString stringWithUTF8String:propType];
NSRange range = [propertyType rangeOfString:@"T@\""];
NSRange range2 = [propertyType rangeOfString:@"T"];
if (range.location != NSNotFound) {
+5 -1
View File
@@ -32,6 +32,8 @@
+ (NSString * _Nonnull) PNObjEndPoint;
+ (NSArray * _Nonnull) batch:(id _Nonnull)JSON;
- (_Nullable instancetype) initWithJSON:( NSDictionary * _Nonnull) JSON;
- (id _Nonnull) saveLocally;
@@ -40,6 +42,8 @@
- (NSDictionary * _Nonnull) JSONFormObject;
- (NSDictionary * _Nonnull) reverseMapping;
///--------------------------------------
#pragma mark - PNObject Properties
///--------------------------------------
@@ -55,7 +59,7 @@
/**
* <#Description#>
*/
@property (nonatomic, strong, nonnull) NSDictionary * JSONObject;
@property (nonatomic, strong, nonnull) NSDictionary * JSONObjectMap;
@end
+162 -157
View File
@@ -28,6 +28,7 @@
@property (nonatomic) BOOL singleInstance;
@end
@implementation PNObject
@@ -84,22 +85,22 @@
_objID = [[NSProcessInfo processInfo] globallyUniqueString];
_objectModel = [PNObjectModel sharedInstance];
[_objectModel setPersistencyDelegate:self];
self.objectModel = [PNObjectModel sharedInstance];
[self.objectModel setPersistencyDelegate:self];
NSMutableDictionary * objectDict = [[NSMutableDictionary alloc] initWithDictionary:[[self class] objcetMapping]];
[objectDict addEntriesFromDictionary:[PNObject objcetMapping]];
_JSONObject = objectDict;
self.JSONObjectMap = objectDict;
NSAssert(_JSONObject, @"You must create objectMapping");
NSAssert(self.JSONObjectMap, @"You must create objectMapping");
_singleInstance = [[self class] singleInstance];
self.singleInstance = [[self class] singleInstance];
_createdAt = [[NSDate date] toLocalTime];
self.createdAt = [[NSDate date] toLocalTime];
}
_JSON = [[NSMutableDictionary alloc] init];
self.JSON = [[NSMutableDictionary alloc] init];
NSDictionary *properties = [PNObject propertiesForClass:self.class];
@@ -121,17 +122,17 @@
_objID = [[NSProcessInfo processInfo] globallyUniqueString];
_objectModel = [PNObjectModel sharedInstance];
[_objectModel setPersistencyDelegate:self];
self.objectModel = [PNObjectModel sharedInstance];
[self.objectModel setPersistencyDelegate:self];
NSMutableDictionary * objectDict = [[NSMutableDictionary alloc] initWithDictionary:[[self class] objcetMapping]];
[objectDict addEntriesFromDictionary:[PNObject objcetMapping]];
_JSONObject = objectDict;
self.JSONObjectMap = objectDict;
NSAssert(_JSONObject, @"You must create objectMapping");
NSAssert(self.JSONObjectMap, @"You must create objectMapping");
_singleInstance = [[self class] singleInstance];
self.singleInstance = [[self class] singleInstance];
_createdAt = [[NSDate date] toLocalTime];
@@ -146,15 +147,15 @@
[self addObserver:self forKeyPath:propertyName options:NSKeyValueObservingOptionNew context:nil];
}
NSAssert(_JSONObject, @"You must create objectMapping");
_JSON = [[NSMutableDictionary alloc] initWithDictionary:JSON];
NSAssert(self.JSONObjectMap, @"You must create objectMapping");
self.JSON = [[NSMutableDictionary alloc] initWithDictionary:JSON];
[self populateObjectFromJSON:_JSON];
[self populateObjectFromJSON:self.JSON];
}
return self;
}
- (NSDictionary * _Nonnull)reverseMapping
- (NSDictionary * _Nonnull) reverseMapping
{
NSMutableDictionary *JSON = [NSMutableDictionary dictionary];
@@ -163,8 +164,8 @@
NSDictionary *properties = [PNObject propertiesForClass:self.class];
for (NSString* propertyName in _JSONObject) {
id mappingValue = [_JSONObject objectForKey:propertyName];
for (NSString* propertyName in self.JSONObjectMap) {
id mappingValue = [self.JSONObjectMap objectForKey:propertyName];
if([mappingValue isKindOfClass:NSDictionary.class]) {
mappedJSONKey = [mappingValue valueForKey:@"key"];
@@ -173,124 +174,119 @@
mappedJSONKey = mappingValue;
}
NSString *propertyType = [properties valueForKey:propertyName];
id value = [self valueForKey:propertyName];
NSLog(@"PropertyName PropertyType: %@ - %@",propertyName,propertyType);
NSLog(@"PropertyName PropertyType Value: %@ - %@ - %@",propertyName,propertyType,value);
id value;
((void (^)())@{
@"c" : ^{
char val = [value charValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"d" : ^{
double val = [value doubleValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"f" : ^{
float val = [value floatValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"i" : ^{
int val = [value intValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"l" : ^{
long val = [value longValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"s" : ^{
short val = [value shortValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"B" : ^{
BOOL val = [value boolValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"UIImage" : ^{
UIImage *image = [UIImage imageWithData:value];
[JSON setValue:image forKey:propertyName];
},
@"NSURL" : ^{
NSURL *url = value;
if (![self isObjNull:url]) {
[JSON setValue:[url absoluteString] forKey:propertyName];
}
},
@"NSString" : ^{
NSString *val = [NSString stringWithFormat:@"%@", value];
if (![self isObjNull:val]) {
[JSON setValue:val forKey:propertyName];
}
},
@"NSNumber" : ^{
NSInteger val = [value integerValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"NSDate" : ^{
NSDate *val = [value toLocalTime];
if (![self isObjNull:val]) {
[JSON setValue:val forKey:propertyName];
}
},
@"NSArray" : ^{
NSMutableArray *arr = [NSMutableArray array];
for(id PNObject in value) {
SEL selector = NSSelectorFromString(@"JSONObject");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: [[PNObject class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:PNObject];
[invocation invoke];
NSDictionary *returnValue;
[invocation getReturnValue:&returnValue];
[arr addObject:returnValue];
}
[JSON setValue:arr forKey:propertyName];
},
@"NSMutableArray" : ^{
NSMutableArray *arr = [NSMutableArray array];
for(id JSONObject in value) {
PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject];
[arr addObject:val];
SEL selector = NSSelectorFromString(@"JSONObject");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:value];
[invocation invoke];
NSDictionary *returnValue;
[invocation getReturnValue:&returnValue];
}
[JSON setValue:arr forKey:propertyName];
@try {
value = [self valueForKey:propertyName];
}
}[propertyType] ?: ^{
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
SEL selector = NSSelectorFromString(@"JSONObject");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:value];
[invocation invoke];
NSDictionary *returnValue;
[invocation getReturnValue:&returnValue];
@catch (NSException *exception) {
continue;
}
@finally {
[JSON setValue:returnValue forKey:propertyName];
}
else {
// do nothing
}
})();
//NSLog(@"PropertyName PropertyType Value: %@ - %@ - %@",propertyName,propertyType,value);
((void (^)())@{
@"c" : ^{
char val = [value charValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"d" : ^{
double val = [value doubleValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"f" : ^{
float val = [value floatValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"i" : ^{
int val = [value intValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"l" : ^{
long val = [value longValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"s" : ^{
short val = [value shortValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"B" : ^{
BOOL val = [value boolValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"NSURL" : ^{
NSURL *url = value;
if (![self isObjNull:url]) {
[JSON setValue:[url absoluteString] forKey:propertyName];
}
},
@"NSString" : ^{
NSString *val = [NSString stringWithFormat:@"%@", value];
if (![self isObjNull:val]) {
[JSON setValue:val forKey:propertyName];
}
},
@"NSNumber" : ^{
NSInteger val = [value integerValue];
[JSON setValue:@(val) forKey:propertyName];
},
@"NSDate" : ^{
NSDate *val = [value toLocalTime];
if (![self isObjNull:val]) {
[JSON setValue:val forKey:propertyName];
}
},
@"NSArray" : ^{
NSMutableArray *arr = [NSMutableArray array];
for(id object in value) {
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
NSDictionary *objectDict = [(PNObject*) object reverseMapping];
[arr addObject:objectDict];
}
}
[JSON setValue:arr forKey:propertyName];
},
@"NSMutableArray" : ^{
NSMutableArray *arr = [NSMutableArray array];
for(id object in value) {
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
NSDictionary *objectDict = [(PNObject*) object reverseMapping];
[arr addObject:objectDict];
}
}
[JSON setValue:arr forKey:propertyName];
}
}[propertyType] ?: ^{
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
NSDictionary *objectDict = [(PNObject*)value reverseMapping];
[JSON setValue:objectDict forKey:propertyName];
}
else {
// do nothing
}
})();
}
}
_JSON = JSON;
return _JSON;
return JSON;
}
- (NSDictionary * _Nonnull) JSONFormObject {
@@ -301,28 +297,16 @@
for (NSString *key in JSONMap) {
if ([_JSON objectForKey:[JSONMap objectForKey:key]]) {
[JSONFormObject setObject:[_JSON objectForKey:[JSONMap objectForKey:key]] forKey:[JSONMap objectForKey:key]];
if ([self.JSON objectForKey:[JSONMap objectForKey:key]]) {
[JSONFormObject setObject:[self.JSON objectForKey:[JSONMap objectForKey:key]] forKey:[JSONMap objectForKey:key]];
}
}
return JSONFormObject;
}
- (NSDictionary* _Nonnull) JSONObject {
if (!_JSON) {
return [self reverseMapping];
}
else {
return _JSON;
}
}
- (NSString* _Nonnull) description {
if (!_JSON) {
[self reverseMapping];
}
return [_JSON description];
return [[self reverseMapping] description];
}
- (void) setSingleInstance:(BOOL)singleInstance {
@@ -359,11 +343,25 @@
__weak id weakSelf = self;
return [_objectModel saveLocally:weakSelf];
return [self.objectModel saveLocally:weakSelf];
}
- (BOOL) autoRemoveLocally {
return [_objectModel removeObjectLocally:self];
return [self.objectModel removeObjectLocally:self];
}
+ (NSArray *)batch:(id)JSON
{
NSString *className = NSStringFromClass([self class]);
NSMutableArray *batch = [NSMutableArray array];
for(id objectJSON in JSON) {
PNObject *val = [[NSClassFromString(className) alloc] initWithJSON:objectJSON];
[batch addObject:val];
}
return batch;
}
#pragma mark -
@@ -372,45 +370,52 @@
{
@try {
if ([change objectForKey:@"new"]) {
[_JSON setObject:[change objectForKey:@"new"] forKey:[[[self class] objcetMapping] objectForKey:keyPath]];
if ([[[change objectForKey:@"new"] class] isSubclassOfClass:[PNObject class]]) {
NSDictionary *objectDict = [(PNObject*)[change objectForKey:@"new"] reverseMapping];
[self.JSON setValue:objectDict forKey:[[[self class] objcetMapping] objectForKey:keyPath]];
}
else {
[self.JSON setObject:[change objectForKey:@"new"] forKey:[[[self class] objcetMapping] objectForKey:keyPath]];
}
}
else {
[_JSON removeObjectForKey:[[[self class] objcetMapping] objectForKey:keyPath]];
[self.JSON removeObjectForKey:[[[self class] objcetMapping] objectForKey:keyPath]];
}
}
@catch (NSException *exception) {
}
@finally {
}
}
- (void)dealloc
{
NSDictionary *properties = [PNObject propertiesForClass:self.class];
for (NSString *propertyName in properties) {
if ([propertyName isEqualToString:@"description"] || [propertyName isEqualToString:@"debugDescription"]) {
continue;
}
@try {
[self removeObserver:self forKeyPath:propertyName];
}
@catch (NSException *exception) {
}
@finally {
}
}
_JSON = nil;
_JSONObject = nil;
self.JSON = nil;
_JSONObjectMap = nil;
_objID = nil;
_createdAt = nil;
}
+5 -1
View File
@@ -8,7 +8,7 @@
#import <Foundation/Foundation.h>
#import <AFNetworking/AFNetworking.h>
#import "HTTPStatusCodes.h"
//#import "HTTPStatusCodes"
#import "AFOAuth2Manager.h"
#import "AFHTTPRequestSerializer+OAuth2.h"
@@ -175,5 +175,9 @@ extern NSString* _Nonnull const Client_Secret;
* <#Description#>
*/
@property (nonatomic) NSInteger minPasswordLenght;
/**
* <#Description#>
*/
@property (nonatomic, strong, readonly) NSString *encrypKey;
@end
+22 -2
View File
@@ -24,6 +24,8 @@ NSString * const PNObjectLocalNotificationRefreshTokenUserFail = @"PNObjectLocal
NSInteger const minPassLenght = 4;
NSString * const PNObjectEncryptionKey = @"PNObjectConfigEncryptionKey";
NSString * const PNObjectServiceCredentialIdentifier = @"PNObjectServiceCredentialIdentifier";
NSString* const EnvironmentProduction = @"PNObjectConfigEnvProduction";
@@ -146,6 +148,15 @@ static bool isFirstAccess = YES;
_headerFields = [[NSMutableDictionary alloc] init];
if ([[NSUserDefaults standardUserDefaults] objectForKey:PNObjectEncryptionKey]) {
_encrypKey = [[NSUserDefaults standardUserDefaults] objectForKey:PNObjectEncryptionKey];
}
else {
_encrypKey = [[NSProcessInfo processInfo] globallyUniqueString];
[[NSUserDefaults standardUserDefaults] setObject:_encrypKey forKey:PNObjectEncryptionKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
return self;
@@ -218,7 +229,7 @@ static bool isFirstAccess = YES;
[[_manager requestSerializer] setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
}
else {
[self refreshTokenForClientCredential];
[self refreshToken];
}
}
@@ -234,6 +245,15 @@ static bool isFirstAccess = YES;
return NO;
}
- (void) refreshToken {
if([PNUser currentUser] && [[PNUser currentUser] hasValidEmailAndPasswordData]) {
[self refreshTokenForUser];
}
else {
[self refreshTokenForClientCredential];
}
}
- (void) refreshTokenForUser {
[self refreshTokenForUserWithBlockSuccess:nil failure:nil];
@@ -243,7 +263,7 @@ static bool isFirstAccess = YES;
failure:(nullable void (^)(NSError * _Nonnull error))failure {
if([PNUser currentUser] && [[PNUser currentUser] hasValidEmailAndPasswordData]) {
[_manager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] username:[[PNUser currentUser] email] password:[[PNUser currentUser] password] scope:nil success:^(AFOAuthCredential * _Nonnull credential) {
[_manager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] username:[[PNUser currentUser] email] password:[[[PNUser currentUser] password] password] scope:nil success:^(AFOAuthCredential * _Nonnull credential) {
_currentOauthCredential = credential;
[AFOAuthCredential storeCredential:_currentOauthCredential withIdentifier:PNObjectServiceCredentialIdentifier];
+16 -34
View File
@@ -11,6 +11,8 @@
#import "PEARFileManager.h"
#import "PNObjectConstants.h"
@import RNCryptor;
@interface PNObjectModel()
@property (nonatomic, strong) PEARFileManager *fileManager;
@@ -161,16 +163,10 @@ static bool isFirstAccess = YES;
if ([(PNObject*) object singleInstance]) {
SEL selector = NSSelectorFromString(@"JSONObject");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:object];
[invocation invoke];
NSDictionary *objectDict = [(PNObject*) object reverseMapping];
NSDictionary *objectDict;
[invocation getReturnValue:&objectDict];
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objectDict];
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)]) {
@@ -192,25 +188,19 @@ static bool isFirstAccess = YES;
else {
if ([self issetPNObjectModelForObject:object]) {
NSData * data = [_fileManager fetchFileDataWithPath:[self objectName:object]];
//NSData * data = [_fileManager fetchFileDataWithPath:[self objectName:object]];
NSError *error = nil;
NSData *data = [RNCryptor decryptData:[_fileManager fetchFileDataWithPath:[self objectName:object]] password:[[PNObjectConfig sharedInstance] encrypKey] error:&error];
NSMutableArray *objects = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
SEL selector = NSSelectorFromString(@"JSONObject");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:object];
[invocation invoke];
NSDictionary *objectDict;
[invocation getReturnValue:&objectDict];
NSLogDebug(@"%@",objectDict);
NSDictionary *objectDict = [(PNObject*)object reverseMapping];
[objects addObject:objectDict];
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objects];
NSData *objectData = [RNCryptor encryptData:[NSKeyedArchiver archivedDataWithRootObject:objects] password:[[PNObjectConfig sharedInstance] encrypKey]];
if ([_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
@@ -223,20 +213,12 @@ static bool isFirstAccess = YES;
else {
NSMutableArray *objects = [[NSMutableArray alloc] init];
SEL selector = NSSelectorFromString(@"JSONObject");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:object];
[invocation invoke];
NSDictionary *objectDict;
[invocation getReturnValue:&objectDict];
NSDictionary *objectDict = [(PNObject*)object reverseMapping];
[objects addObject:objectDict];
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objects];
NSData *objectData = [RNCryptor encryptData:[NSKeyedArchiver archivedDataWithRootObject:objects] password:[[PNObjectConfig sharedInstance] encrypKey]];
if ([_fileManager createFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
return object;