- Fix Vari
This commit is contained in:
1 parent
f569c8545c
commit
7e5a6149a8
28 files changed
+1162
-862
No files matched your search
@@ -12,7 +12,7 @@
|
||||
#import "PNObjectConfig.h"
|
||||
#import "PNObjectModel.h"
|
||||
#import <AFNetworking/AFNetworking.h>
|
||||
#import "PNObject.m"
|
||||
#import "PNObject+Protected.h"
|
||||
|
||||
@implementation PNObject (PNObjectConnection)
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
NSLogDebug(@"JSON: %@", responseObject);
|
||||
NSLogDebug(@"JSON: %@", [responseObject class]);
|
||||
|
||||
_JSON = [[NSDictionary alloc] initWithDictionary:responseObject];
|
||||
self.JSON = [[NSDictionary alloc] initWithDictionary:responseObject];
|
||||
|
||||
[self populateObjectFromJSON:responseObject];
|
||||
|
||||
@@ -38,10 +38,6 @@
|
||||
NSLogDebug(@"Error: %@", error);
|
||||
|
||||
}];
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// PNObject+Protected.h
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 25/01/16.
|
||||
//
|
||||
//
|
||||
|
||||
#import <PNObject/PNObject.h>
|
||||
#import <objc/runtime.h>
|
||||
|
||||
@interface PNObject (Protected)
|
||||
|
||||
@property (nonatomic, strong) PNObjectModel * _Nonnull objectModel;
|
||||
|
||||
@property (nonatomic, strong) NSDictionary * _Nonnull JSON;
|
||||
|
||||
@property (nonatomic, strong) NSString * _Nonnull endPoint;
|
||||
|
||||
@property (nonatomic) BOOL singleInstance;
|
||||
|
||||
- (void)populateObjectFromJSON:(id _Nullable)JSON;
|
||||
|
||||
- (BOOL)isStringNull:(NSString * _Nullable)str;
|
||||
|
||||
- (BOOL)isObjNull:(id _Nullable)obj;
|
||||
|
||||
+ (NSArray * _Nonnull) protectedProperties;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,150 @@
|
||||
//
|
||||
// PNObject+Protected.m
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 25/01/16.
|
||||
//
|
||||
//
|
||||
|
||||
#import "PNObject+Protected.h"
|
||||
#import <PNObjectProperty.h>
|
||||
#import "PNObjectConstants.h"
|
||||
#import <NSDate_Utils/NSDate+NSDate_Util.h>
|
||||
|
||||
@implementation PNObject (Protected)
|
||||
|
||||
@dynamic endPoint;
|
||||
@dynamic objectModel;
|
||||
@dynamic JSON;
|
||||
@dynamic singleInstance;
|
||||
|
||||
+ (NSArray * _Nonnull) protectedProperties {
|
||||
return @[@"JSON",@"subClassDelegate",@"objectModel",@"objectMapping"];
|
||||
}
|
||||
|
||||
- (void)populateObjectFromJSON:(id _Nullable)JSON
|
||||
{
|
||||
NSDictionary *properties = [PNObjectProperty 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;
|
||||
}
|
||||
|
||||
// Check if there is mapping for the property
|
||||
if([self isObjNull:mappedJSONKey]) {
|
||||
// No mapping so just continue
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Get JSON value for the mapped key
|
||||
id value = [JSON valueForKeyPath:propertyName];
|
||||
if([self isObjNull:value]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
((void (^)())@{
|
||||
@"c" : ^{
|
||||
char val = [value charValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"d" : ^{
|
||||
double val = [value doubleValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"f" : ^{
|
||||
float val = [value floatValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"i" : ^{
|
||||
int val = [value intValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"l" : ^{
|
||||
long val = [value longValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"s" : ^{
|
||||
short val = [value shortValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"B" : ^{
|
||||
BOOL val = [value boolValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"NSString" : ^{
|
||||
NSString *val = [NSString stringWithFormat:@"%@", value];
|
||||
if (![self isObjNull:val]) {
|
||||
[self setValue:val forKey:propertyName];
|
||||
}
|
||||
|
||||
},
|
||||
@"NSNumber" : ^{
|
||||
NSInteger val = [value integerValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"NSDate" : ^{
|
||||
NSString *str = [NSString stringWithFormat:@"%@", value];
|
||||
NSDate *val = [[NSDate dateFromString:str withFormat:kNSDateHelperFormatSQLDateWithTime] toLocalTime];
|
||||
if (![self isObjNull:val]) {
|
||||
[self setValue:val forKey:propertyName];
|
||||
}
|
||||
},
|
||||
@"NSArray" : ^{
|
||||
NSMutableArray *arr = [NSMutableArray array];
|
||||
for(id JSONObject in value) {
|
||||
PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject];
|
||||
[arr addObject:val];
|
||||
}
|
||||
|
||||
[self setValue:arr forKey:propertyName];
|
||||
},
|
||||
@"NSMutableArray" : ^{
|
||||
NSMutableArray *arr = [NSMutableArray array];
|
||||
for(id JSONObject in value) {
|
||||
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];
|
||||
}
|
||||
else {
|
||||
NSString *errorStr = [NSString stringWithFormat:@"Property '%@' could not be assigned any value.", propertyName];
|
||||
NSLogDebug(@"%@",errorStr);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
- (BOOL)isObjNull:(id _Nullable)obj
|
||||
{
|
||||
if(!obj || nil == obj || NSNull.null == obj || ([obj isKindOfClass:[NSString class]] && [obj isEqualToString:@"(null)"]) || [obj isEqual:[NSNull null]])
|
||||
return YES;
|
||||
else
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -27,7 +27,7 @@
|
||||
@interface PNObject : NSObject
|
||||
|
||||
@property (nonatomic, strong) NSString * _Nonnull objID;
|
||||
@property (nonatomic, strong) NSDate * _Nonnull createdDate;
|
||||
@property (nonatomic, strong) NSDate * _Nonnull createdAt;
|
||||
@property (nonatomic, strong, getter=getObject) NSDictionary * _Nonnull objectMapping;
|
||||
@property (nonatomic, assign) id<PNObjectSubclassing> _Nonnull subClassDelegate;
|
||||
|
||||
@@ -35,6 +35,4 @@
|
||||
|
||||
- (id _Nonnull) saveLocally;
|
||||
|
||||
- (void) saveLocallyInBackGroundWithBlock:(id _Nonnull) object inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id _Nullable responseObject, NSError * _Nullable error)) responseBlock;
|
||||
|
||||
@end
|
||||
+115
-177
@@ -12,18 +12,18 @@
|
||||
#import <AFNetworking/AFNetworking.h>
|
||||
#import "PNObject/PNUser.h"
|
||||
#import "PNObjectConstants.h"
|
||||
|
||||
#import "PNObject+Protected.h"
|
||||
|
||||
#define PNOBJECT_DIR @"PNObjects"
|
||||
|
||||
|
||||
@interface PNObject() <PNObjectPersistency>
|
||||
|
||||
@property (nonatomic, strong) PNObjectModel *objectModel;
|
||||
@property (nonatomic, strong) PNObjectModel * _Nonnull objectModel;
|
||||
|
||||
@property (nonatomic, strong) NSDictionary *JSON;
|
||||
@property (nonatomic, strong) NSMutableDictionary * _Nonnull JSON;
|
||||
|
||||
@property (nonatomic, strong) NSString *endPoint;
|
||||
@property (nonatomic, strong) NSString * _Nonnull endPoint;
|
||||
|
||||
@property (nonatomic) BOOL singleInstance;
|
||||
|
||||
@@ -31,15 +31,26 @@
|
||||
|
||||
@implementation PNObject
|
||||
|
||||
- (NSDictionary *) PNObjectMapping {
|
||||
NSDictionary *mapping = @{@"objID":@"objID",
|
||||
@"createdAt":@"created_at",
|
||||
};
|
||||
return mapping;
|
||||
}
|
||||
|
||||
- (instancetype) init {
|
||||
- (_Nullable instancetype) init {
|
||||
self = [super init];
|
||||
|
||||
if (self) {
|
||||
if ([[self class] isSubclassOfClass:[PNObject class]]) {
|
||||
NSAssert([[self class] conformsToProtocol:@protocol(PNObjectSubclassing)], @"Subclass object must conform to PNObjectSubclassing");
|
||||
|
||||
_objectMapping = [[self class] objcetMapping];
|
||||
_objID = [[NSProcessInfo processInfo] globallyUniqueString];
|
||||
|
||||
NSMutableDictionary * objectDict = [[NSMutableDictionary alloc] initWithDictionary:[[self class] objcetMapping]];
|
||||
[objectDict addEntriesFromDictionary:[self PNObjectMapping]];
|
||||
|
||||
_objectMapping = objectDict;
|
||||
|
||||
NSAssert(_objectMapping, @"You must create objectMapping");
|
||||
|
||||
@@ -47,40 +58,26 @@
|
||||
|
||||
_objectModel = [PNObjectModel sharedInstance];
|
||||
[_objectModel setPersistencyDelegate:self];
|
||||
|
||||
_createdAt = [[NSDate date] toLocalTime];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype) initWithJSON:(NSDictionary*) JSON {
|
||||
- (_Nullable instancetype) initWithJSON:( NSDictionary * _Nonnull) JSON {
|
||||
self = [self init];
|
||||
if (self) {
|
||||
|
||||
NSAssert(_objectMapping, @"You must create objectMapping");
|
||||
_JSON = [[NSDictionary alloc] initWithDictionary:JSON];
|
||||
_JSON = [[NSMutableDictionary alloc] initWithDictionary:JSON];
|
||||
|
||||
[self populateObjectFromJSON:JSON];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)isStringNull:(NSString *)str
|
||||
{
|
||||
if(nil == str || NSNull.null == (id)str)
|
||||
return YES;
|
||||
else
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL)isObjNull:(id)obj
|
||||
{
|
||||
if(nil == obj || NSNull.null == obj)
|
||||
return YES;
|
||||
else
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (NSDictionary *)reverseMapping
|
||||
- (NSDictionary * _Nonnull)reverseMapping
|
||||
{
|
||||
NSMutableDictionary *JSON = [NSMutableDictionary dictionary];
|
||||
|
||||
@@ -103,20 +100,66 @@
|
||||
|
||||
id value = [self valueForKey:propertyName];
|
||||
|
||||
// NSDate
|
||||
if([propertyType isEqualToString:@"NSDate"]) {
|
||||
|
||||
value = [NSDate stringFromDate:value];
|
||||
}
|
||||
// NSURL
|
||||
else if([propertyType isEqualToString:@"NSURL"]) {
|
||||
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];
|
||||
},
|
||||
|
||||
@"UIImage" : ^{
|
||||
UIImage *image = [UIImage imageWithData:value];
|
||||
[JSON setValue:image forKey:propertyName];
|
||||
},
|
||||
@"NSURL" : ^{
|
||||
NSURL *url = value;
|
||||
value = [url absoluteString];
|
||||
}
|
||||
// NSArray, NSMutableArray
|
||||
else if([propertyType isEqualToString:@"NSArray"] ||
|
||||
[propertyType isEqualToString:@"NSMutableArray"]) {
|
||||
|
||||
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(@"getObject");
|
||||
@@ -130,13 +173,14 @@
|
||||
[arr addObject:returnValue];
|
||||
}
|
||||
|
||||
value = arr;
|
||||
|
||||
}
|
||||
// Other PNObject or an unidentified value
|
||||
else {
|
||||
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
|
||||
if(isPNObjectSubclass) {
|
||||
[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(@"getObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
@@ -144,17 +188,31 @@
|
||||
[invocation invoke];
|
||||
NSDictionary *returnValue;
|
||||
[invocation getReturnValue:&returnValue];
|
||||
|
||||
value = returnValue;
|
||||
|
||||
}
|
||||
else {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
[JSON setValue:arr forKey:propertyName];
|
||||
}
|
||||
}[propertyType] ?: ^{
|
||||
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
|
||||
if(isPNObjectSubclass) {
|
||||
SEL selector = NSSelectorFromString(@"getObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:value];
|
||||
[invocation invoke];
|
||||
NSDictionary *returnValue;
|
||||
[invocation getReturnValue:&returnValue];
|
||||
|
||||
[JSON setValue:returnValue forKey:propertyName];
|
||||
}
|
||||
else {
|
||||
// do nothing
|
||||
}
|
||||
})();
|
||||
|
||||
|
||||
|
||||
|
||||
[JSON setValue:value forKey:propertyName];
|
||||
}
|
||||
|
||||
_JSON = JSON;
|
||||
@@ -162,121 +220,16 @@
|
||||
return _JSON;
|
||||
}
|
||||
|
||||
- (NSDictionary*) getObject {
|
||||
- (NSDictionary* _Nonnull) getObject {
|
||||
return [self reverseMapping];
|
||||
}
|
||||
|
||||
- (void)populateObjectFromJSON:(id)JSON
|
||||
{
|
||||
|
||||
NSDictionary *properties = [PNObjectProperty propertiesForClass:self.class];
|
||||
|
||||
for(NSString *propertyName in properties) {
|
||||
|
||||
if([propertyName isEqualToString:@"mappingError"])
|
||||
continue;
|
||||
|
||||
NSString *mappedJSONKey;
|
||||
NSString *mappedJSONType;
|
||||
|
||||
NSString *propertyType = [properties valueForKey:propertyName];
|
||||
|
||||
id mappingValue = [_objectMapping valueForKey:propertyName];
|
||||
|
||||
if([mappingValue isKindOfClass:NSDictionary.class]) {
|
||||
mappedJSONKey = [mappingValue valueForKey:@"key"];
|
||||
mappedJSONType = [mappingValue valueForKey:@"type"];
|
||||
} else {
|
||||
mappedJSONKey = mappingValue;
|
||||
}
|
||||
|
||||
// Check if there is mapping for the property
|
||||
if([self isStringNull:mappedJSONKey]) {
|
||||
// No mapping so just continue
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Get JSON value for the mapped key
|
||||
id value = [JSON valueForKeyPath:mappedJSONKey];
|
||||
if([self isObjNull:value]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
((void (^)())@{
|
||||
@"c" : ^{
|
||||
char val = [value charValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"d" : ^{
|
||||
double val = [value doubleValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"f" : ^{
|
||||
float val = [value floatValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"i" : ^{
|
||||
int val = [value intValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"l" : ^{
|
||||
long val = [value longValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"s" : ^{
|
||||
short val = [value shortValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"NSString" : ^{
|
||||
NSString *val = [NSString stringWithFormat:@"%@", value];
|
||||
[self setValue:val forKey:propertyName];
|
||||
},
|
||||
@"NSNumber" : ^{
|
||||
NSInteger val = [value integerValue];
|
||||
[self setValue:@(val) forKey:propertyName];
|
||||
},
|
||||
@"NSDate" : ^{
|
||||
NSString *str = [NSString stringWithFormat:@"%@", value];
|
||||
NSDate *val = [[NSDate dateFromString:str withFormat:kNSDateHelperFormatSQLDateWithTime] toLocalTime];
|
||||
[self setValue:val forKey:propertyName];
|
||||
},
|
||||
@"NSArray" : ^{
|
||||
NSMutableArray *arr = [NSMutableArray array];
|
||||
for(id JSONObject in value) {
|
||||
PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject];
|
||||
[arr addObject:val];
|
||||
}
|
||||
|
||||
[self setValue:arr forKey:propertyName];
|
||||
},
|
||||
@"NSMutableArray" : ^{
|
||||
NSMutableArray *arr = [NSMutableArray array];
|
||||
for(id JSONObject in value) {
|
||||
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];
|
||||
}
|
||||
else {
|
||||
NSString *errorStr = [NSString stringWithFormat:@"Property '%@' could not be assigned any value.", propertyName];
|
||||
NSLogDebug(@"%@",errorStr);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
- (NSString* _Nonnull) description {
|
||||
return [_JSON description];
|
||||
}
|
||||
|
||||
- (NSString*) description {
|
||||
return [_JSON description];
|
||||
- (void) setSingleInstance:(BOOL)singleInstance {
|
||||
_singleInstance = singleInstance;
|
||||
}
|
||||
|
||||
#pragma mark PNObjectPersistency protocol
|
||||
@@ -307,22 +260,7 @@
|
||||
|
||||
- (id _Nonnull) saveLocally {
|
||||
|
||||
if (_singleInstance) {
|
||||
return [_objectModel saveLocally:self];
|
||||
}
|
||||
else {
|
||||
return [_objectModel pushObjectAndSaveLocally:self];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) saveLocallyInBackGroundWithBlock:(id _Nonnull) object inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id _Nullable responseObject, NSError * _Nullable error)) responseBlock; {
|
||||
|
||||
if (_singleInstance) {
|
||||
return [_objectModel saveLocally:self inBackGroundWithBlock:responseBlock];
|
||||
}
|
||||
else {
|
||||
return [_objectModel pushObjectAndSaveLocally:self inBackGroundWithBlock:responseBlock];
|
||||
}
|
||||
return [_objectModel saveLocally:self];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@@ -32,14 +32,6 @@
|
||||
*/
|
||||
- (id _Nonnull) saveLocally:(id _Nonnull) object;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
*
|
||||
* @param object <#object description#>
|
||||
* @param responseBlock <#responseBlock description#>
|
||||
*/
|
||||
- (void) saveLocally:(id _Nonnull) object inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id _Nullable responseObject, NSError * _Nullable error)) responseBlock;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
*
|
||||
@@ -47,14 +39,8 @@
|
||||
*
|
||||
* @return <#return value description#>
|
||||
*/
|
||||
- (id _Nonnull) pushObjectAndSaveLocally:(id _Nonnull) object;
|
||||
- (id _Nonnull) removeObjectAndSaveLocally:(id _Nonnull) object;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
*
|
||||
* @param object <#object description#>
|
||||
* @param responseBlock <#responseBlock description#>
|
||||
*/
|
||||
- (void) pushObjectAndSaveLocally:(id _Nonnull) object inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id _Nullable responseObject, NSError * _Nullable error)) responseBlock;
|
||||
- (id _Nonnull) fetchObjectsWithClass:(Class _Nonnull) class;
|
||||
|
||||
@end
|
||||
+124
-46
@@ -7,7 +7,7 @@
|
||||
//
|
||||
|
||||
#import "PNObjectModel.h"
|
||||
#import "PNObject.h"
|
||||
#import "PNObject+Protected.h"
|
||||
#import "PEARFileManager.h"
|
||||
#import "PNObjectConstants.h"
|
||||
|
||||
@@ -45,17 +45,19 @@ static bool isFirstAccess = YES;
|
||||
if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) {
|
||||
|
||||
NSLogDebug(@"%@",[object subClassDelegate]);
|
||||
|
||||
NSString *className;
|
||||
//if ([[object subClassDelegate] respondsToSelector:@selector(objectClassName)]) {
|
||||
|
||||
@try {
|
||||
return (NSString *)[[object class] performSelector:@selector(objectClassName)];
|
||||
|
||||
className = (NSString *)[[object class] performSelector:@selector(objectClassName)];
|
||||
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
return nil;
|
||||
}
|
||||
@finally {
|
||||
|
||||
return className;
|
||||
}
|
||||
|
||||
|
||||
@@ -121,6 +123,32 @@ static bool isFirstAccess = YES;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id _Nonnull) fetchObjectsWithClass:(Class _Nonnull) class {
|
||||
BOOL isPNObjectSubclass = [class isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
NSString *className;
|
||||
|
||||
@try {
|
||||
|
||||
className = (NSString *)[class performSelector:@selector(objectClassName)];
|
||||
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
|
||||
}
|
||||
@finally {
|
||||
|
||||
if ([_fileManager checkPath:className]) {
|
||||
return [NSKeyedUnarchiver unarchiveObjectWithData:[_fileManager fetchFileDataWithPath:className]];
|
||||
}
|
||||
else
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (id _Nonnull) saveLocally:(id _Nonnull) object {
|
||||
|
||||
@@ -130,33 +158,95 @@ static bool isFirstAccess = YES;
|
||||
|
||||
if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) {
|
||||
|
||||
SEL selector = NSSelectorFromString(@"getObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:object];
|
||||
[invocation invoke];
|
||||
|
||||
NSDictionary *objectDict;
|
||||
[invocation getReturnValue:&objectDict];
|
||||
|
||||
NSLogDebug(@"%@",objectDict);
|
||||
|
||||
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objectDict];
|
||||
|
||||
if ([self issetPNObjectModelForObject:object]) {
|
||||
if ([_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
return object;
|
||||
if ([(PNObject*) object singleInstance]) {
|
||||
|
||||
SEL selector = NSSelectorFromString(@"getObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:object];
|
||||
[invocation invoke];
|
||||
|
||||
NSDictionary *objectDict;
|
||||
[invocation getReturnValue:&objectDict];
|
||||
|
||||
NSLogDebug(@"%@",objectDict);
|
||||
|
||||
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objectDict];
|
||||
|
||||
if ([self issetPNObjectModelForObject:object]) {
|
||||
if ([_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
return object;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Object cannot be updated", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Object cannot be updated", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
if ([_fileManager createFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
return object;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Object cannot be created", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ([_fileManager createFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
return object;
|
||||
if ([self issetPNObjectModelForObject:object]) {
|
||||
|
||||
NSData * data = [_fileManager fetchFileDataWithPath:[self objectName:object]];
|
||||
|
||||
NSMutableArray *objects = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
|
||||
|
||||
|
||||
SEL selector = NSSelectorFromString(@"getObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:object];
|
||||
[invocation invoke];
|
||||
|
||||
NSDictionary *objectDict;
|
||||
[invocation getReturnValue:&objectDict];
|
||||
|
||||
NSLogDebug(@"%@",objectDict);
|
||||
|
||||
[objects addObject:objectDict];
|
||||
|
||||
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objects];
|
||||
|
||||
if ([_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
|
||||
return objects;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Objects list cannot be updated", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Object cannot be created", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
|
||||
NSMutableArray *objects = [[NSMutableArray alloc] init];
|
||||
|
||||
|
||||
SEL selector = NSSelectorFromString(@"getObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:object];
|
||||
[invocation invoke];
|
||||
|
||||
NSDictionary *objectDict;
|
||||
[invocation getReturnValue:&objectDict];
|
||||
|
||||
NSLogDebug(@"%@",objectDict);
|
||||
|
||||
[objects addObject:objectDict];
|
||||
|
||||
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objects];
|
||||
|
||||
if ([_fileManager createFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)]) {
|
||||
return object;
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"Objects list cannot be created", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,31 +259,19 @@ static bool isFirstAccess = YES;
|
||||
}
|
||||
}
|
||||
|
||||
- (void) saveLocally:(id _Nonnull) object inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id _Nullable responseObject, NSError * _Nullable error)) responseBlock {
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
- (id _Nonnull) removeObjectAndSaveLocally:(id _Nonnull) object {
|
||||
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
__weak id responseObject = [self saveLocally:object];
|
||||
if ([responseObject isKindOfClass:[NSError class]]) {
|
||||
if (responseBlock) {
|
||||
responseBlock(NO, nil, responseObject);
|
||||
if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) {
|
||||
|
||||
if ([(PNObject*) object singleInstance]) {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
else if ([[responseObject class] isSubclassOfClass:[PNObject class]]){
|
||||
if (responseBlock) {
|
||||
responseBlock(YES,responseObject, nil);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
- (id _Nonnull) pushObjectAndSaveLocally:(id _Nonnull) object {
|
||||
|
||||
}
|
||||
|
||||
- (void) pushObjectAndSaveLocally:(id _Nonnull) object inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id _Nullable responseObject, NSError * _Nullable error)) responseBlock {
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "PNObject.h"
|
||||
|
||||
@interface PNObjectProperty : NSObject
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#import "PNObjectProperty.h"
|
||||
#import "objc/runtime.h"
|
||||
#import "PNObjectConstants.h"
|
||||
#import "PNObject+Protected.h"
|
||||
|
||||
@implementation PNObjectProperty
|
||||
|
||||
@@ -38,6 +39,9 @@ static BOOL property_getTypeString( objc_property_t property, char *buffer )
|
||||
}
|
||||
|
||||
NSMutableDictionary *results = [NSMutableDictionary dictionary];
|
||||
if ([PNObjClass isSubclassOfClass:[PNObject class]] && PNObjClass != [PNObject class]) {
|
||||
[results addEntriesFromDictionary:[self propertiesForClass:[PNObject class]]];
|
||||
}
|
||||
|
||||
unsigned int outCount, i;
|
||||
objc_property_t *properties = class_copyPropertyList(PNObjClass, &outCount);
|
||||
@@ -64,9 +68,11 @@ static BOOL property_getTypeString( objc_property_t property, char *buffer )
|
||||
propertyType = [propertyType substringWithRange:subStrRange];
|
||||
}
|
||||
|
||||
NSLogDebug(@"Prop type & name: %@ -- %@", propertyType, propertyName);
|
||||
//NSLogDebug(@"Prop type & name: %@ -- %@", propertyType, propertyName);
|
||||
|
||||
[results setObject:propertyType forKey:propertyName];
|
||||
if (![[PNObject protectedProperties] containsObject:propertyName]) {
|
||||
[results setObject:propertyType forKey:propertyName];
|
||||
}
|
||||
}
|
||||
}
|
||||
free(properties);
|
||||
|
||||
@@ -26,8 +26,7 @@
|
||||
@property (nonatomic, strong) NSString * _Nonnull username;
|
||||
@property (nonatomic, strong) NSString * _Nonnull password;
|
||||
@property (nonatomic) BOOL publicProfile;
|
||||
@property (nonatomic) NSInteger loginCount;
|
||||
@property (strong, nonatomic) NSDate * _Nonnull createdAt;
|
||||
@property (nonatomic, strong) NSNumber * _Nonnull loginCount;
|
||||
@property (nonatomic, strong) NSString * _Nullable facebookId;
|
||||
@property (nonatomic, strong) NSString * _Nullable facebookAccessToken;
|
||||
|
||||
@@ -41,6 +40,6 @@
|
||||
|
||||
- (BOOL) isValidPassword:(NSString* _Nonnull) password;
|
||||
|
||||
- (void) setPassword:(NSString * _Nonnull)password withBlock:(id _Nonnull) object inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id _Nullable responseObject, NSError * _Nullable error)) responseBlock;
|
||||
//- (void) setPassword:(NSString * _Nonnull)password inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id _Nullable responseObject, NSError * _Nullable error)) responseBlock;
|
||||
|
||||
@end
|
||||
+24
-13
@@ -9,6 +9,7 @@
|
||||
#import "PNUser.h"
|
||||
#import "NSString+Helper.h"
|
||||
#import "PNObjectConstants.h"
|
||||
#import "PNObject+Protected.h"
|
||||
|
||||
|
||||
@interface PNUser() <PNObjectSubclassing>
|
||||
@@ -17,6 +18,8 @@
|
||||
|
||||
@implementation PNUser
|
||||
|
||||
@synthesize password = _password;
|
||||
|
||||
static PNUser *SINGLETON = nil;
|
||||
|
||||
static bool isFirstAccess = YES;
|
||||
@@ -25,17 +28,18 @@ static bool isFirstAccess = YES;
|
||||
|
||||
+ (instancetype)sharedInstance
|
||||
{
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
SINGLETON = [[super allocWithZone:NULL] init];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
return [self currentUser];
|
||||
}
|
||||
|
||||
+ (instancetype) currentUser {
|
||||
return [self sharedInstance];
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
|
||||
SINGLETON = [[super allocWithZone:NULL] init];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
}
|
||||
|
||||
#pragma mark - Life Cycle
|
||||
@@ -76,6 +80,12 @@ static bool isFirstAccess = YES;
|
||||
self = [super init];
|
||||
if (self) {
|
||||
[self setSubClassDelegate:self];
|
||||
|
||||
super.JSON = [super.objectModel fetchObjectsWithClass:[self class]];
|
||||
|
||||
if(super.JSON){
|
||||
[super populateObjectFromJSON:super.JSON];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -89,12 +99,13 @@ static bool isFirstAccess = YES;
|
||||
|
||||
- (void) setPassword:(NSString *)password {
|
||||
if ([password length] >= [[PNObjectConfig sharedInstance] minPasswordLenght]) {
|
||||
self.password = password;
|
||||
_password = password;
|
||||
}
|
||||
else {
|
||||
NSLogDebug(@"Inserted Passord is not valid.Lenght must be >= %ld",(long)[[PNObjectConfig sharedInstance] minPasswordLenght]);
|
||||
}
|
||||
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;
|
||||
@@ -111,7 +122,8 @@ static bool isFirstAccess = YES;
|
||||
|
||||
+ (NSDictionary *)objcetMapping {
|
||||
|
||||
NSDictionary *mapping = @{@"userId":@"id",
|
||||
NSDictionary *mapping = @{
|
||||
@"userId":@"id",
|
||||
@"firstName":@"firstName",
|
||||
@"lastName":@"lastName",
|
||||
@"profileImage":@"profileImage",
|
||||
@@ -127,7 +139,6 @@ static bool isFirstAccess = YES;
|
||||
@"username":@"username",
|
||||
@"publicProfile":@"public_profile",
|
||||
@"loginCount":@"login_count",
|
||||
@"createdAt":@"created_at",
|
||||
@"facebookId":@"facebookId",
|
||||
@"facebookAccessToken":@"facebookAccessToken",
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user