no message
This commit is contained in:
1 parent
f25ede069d
commit
54797fafcd
43 files changed
+4320
-1774
No files matched your search
+23
-5
@@ -7,17 +7,35 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "PNObjectSubclassing.h"
|
||||
#import "PNObjectConfig.h"
|
||||
#import "PNObjectModel.h"
|
||||
|
||||
@protocol PNObjectSubclassing <NSObject>
|
||||
|
||||
@required
|
||||
|
||||
+ (NSString * _Nonnull ) objectClassName;
|
||||
|
||||
+ (NSDictionary * _Nonnull) objcetMapping;
|
||||
|
||||
+ (BOOL) singleInstance;
|
||||
|
||||
|
||||
@end
|
||||
|
||||
@interface PNObject : NSObject
|
||||
|
||||
@property (nonatomic, strong) NSString *objID;
|
||||
@property (nonatomic, strong) NSDate *createdDate;
|
||||
@property (nonatomic, strong, getter=getObject) NSDictionary *objectMapping;
|
||||
@property (nonatomic, strong) NSString * _Nonnull objID;
|
||||
@property (nonatomic, strong) NSDate * _Nonnull createdDate;
|
||||
@property (nonatomic, strong, getter=getObject) NSDictionary * _Nonnull objectMapping;
|
||||
@property (nonatomic, assign) id<PNObjectSubclassing> _Nonnull subClassDelegate;
|
||||
|
||||
- (instancetype) initWithJSON:(NSDictionary*) JSON;
|
||||
- (_Nullable instancetype) initWithJSON:( NSDictionary * _Nonnull) JSON;
|
||||
|
||||
- (id _Nonnull) saveLocally;
|
||||
|
||||
- (void) saveLocallyInBackGroundWithBlock:(id _Nonnull) object inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id _Nullable responseObject, NSError * _Nullable error)) responseBlock;
|
||||
|
||||
+ (void) get;
|
||||
|
||||
@end
|
||||
+73
-20
@@ -12,37 +12,39 @@
|
||||
#import <AFNetworking/AFNetworking.h>
|
||||
#import "PNObject/PNUser.h"
|
||||
|
||||
@interface PNObject()
|
||||
#define PNOBJECT_DIR @"PNObjects"
|
||||
|
||||
|
||||
@interface PNObject() <PNObjectPersistency>
|
||||
|
||||
@property (nonatomic, strong) PNObjectModel *objectModel;
|
||||
|
||||
@property (nonatomic, strong) NSDictionary *JSON;
|
||||
|
||||
@property (nonatomic, strong) NSString *endPoint;
|
||||
|
||||
@property (nonatomic) BOOL singleInstance;
|
||||
|
||||
@end
|
||||
|
||||
@implementation PNObject
|
||||
|
||||
|
||||
+ (void) get {
|
||||
|
||||
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||
manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
|
||||
manager.securityPolicy.allowInvalidCertificates = YES;
|
||||
|
||||
[manager GET:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",@"User"] parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||
[[[PNObjectConfig sharedInstance] manager] GET:[[[PNObjectConfig sharedInstance] PNObjEndpoint] stringByAppendingFormat:@"%@",@"User"] parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||
|
||||
|
||||
NSLog(@"JSON: %@", responseObject);
|
||||
NSLog(@"JSON: %@", [responseObject class]);
|
||||
NSLogDebug(@"JSON: %@", responseObject);
|
||||
NSLogDebug(@"JSON: %@", [responseObject class]);
|
||||
|
||||
PNUser *user = [[PNUser alloc] initWithJSON:responseObject];
|
||||
|
||||
NSLog(@"%@",user);
|
||||
NSLogDebug(@"%@",user);
|
||||
|
||||
|
||||
} failure:^(NSURLSessionTask *operation, NSError *error) {
|
||||
|
||||
NSLog(@"Error: %@", error);
|
||||
NSLogDebug(@"Error: %@", error);
|
||||
|
||||
}];
|
||||
}
|
||||
@@ -58,6 +60,11 @@
|
||||
_objectMapping = [[self class] objcetMapping];
|
||||
|
||||
NSAssert(_objectMapping, @"You must create objectMapping");
|
||||
|
||||
_singleInstance = [[self class] singleInstance];
|
||||
|
||||
_objectModel = [PNObjectModel sharedInstance];
|
||||
[_objectModel setPersistencyDelegate:self];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
@@ -129,12 +136,11 @@
|
||||
[propertyType isEqualToString:@"NSMutableArray"]) {
|
||||
|
||||
NSMutableArray *arr = [NSMutableArray array];
|
||||
for(id LLObject in value) {
|
||||
SEL selector = NSSelectorFromString(@"reverseMapping");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
|
||||
[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
for(id PNObject in value) {
|
||||
SEL selector = NSSelectorFromString(@"getObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: [[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:LLObject];
|
||||
[invocation setTarget:PNObject];
|
||||
[invocation invoke];
|
||||
NSDictionary *returnValue;
|
||||
[invocation getReturnValue:&returnValue];
|
||||
@@ -145,13 +151,12 @@
|
||||
value = arr;
|
||||
|
||||
}
|
||||
// Other LLModel or an unidentified value
|
||||
// Other PNObject or an unidentified value
|
||||
else {
|
||||
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
|
||||
if(isPNObjectSubclass) {
|
||||
SEL selector = NSSelectorFromString(@"getObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
|
||||
[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:value];
|
||||
[invocation invoke];
|
||||
@@ -281,7 +286,7 @@
|
||||
}
|
||||
else {
|
||||
NSString *errorStr = [NSString stringWithFormat:@"Property '%@' could not be assigned any value.", propertyName];
|
||||
NSLog(@"%@",errorStr);
|
||||
NSLogDebug(@"%@",errorStr);
|
||||
}
|
||||
})();
|
||||
}
|
||||
@@ -292,4 +297,52 @@
|
||||
return [_JSON description];
|
||||
}
|
||||
|
||||
#pragma mark PNObjectPersistency protocol
|
||||
|
||||
- (BOOL) initObjectPersistency {
|
||||
|
||||
BOOL isDir=YES;
|
||||
NSError *error;
|
||||
|
||||
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
|
||||
|
||||
NSFileManager *fileManager= [NSFileManager defaultManager];
|
||||
|
||||
if(![fileManager fileExistsAtPath:[docDir stringByAppendingFormat:@"/%@",PNOBJECT_DIR] isDirectory:&isDir]) {
|
||||
|
||||
if (![[NSFileManager defaultManager] createDirectoryAtPath:[docDir stringByAppendingFormat:@"/%@",PNOBJECT_DIR] withIntermediateDirectories:NO attributes:nil error:&error]) {
|
||||
#ifdef DEBUG
|
||||
NSLogDebug(@"Create directory error: %@", error);
|
||||
#endif
|
||||
return NO;
|
||||
}
|
||||
}
|
||||
#ifdef DEBUG
|
||||
NSLogDebug(@"%@",[docDir stringByAppendingFormat:@"/%@",PNOBJECT_DIR]);
|
||||
#endif
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (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];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@end
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <AFNetworking/AFNetworking.h>
|
||||
|
||||
typedef NS_ENUM(NSInteger, Environment) {
|
||||
Development,
|
||||
@@ -20,7 +21,7 @@ extern NSString* const EnvironmentDevelopment;
|
||||
|
||||
@interface PNObjectConfig : NSObject
|
||||
|
||||
|
||||
@property (nonatomic, strong) AFHTTPSessionManager *manager;
|
||||
|
||||
/**
|
||||
* gets singleton object.
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// PNObjectModel.h
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 16/01/16.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@protocol PNObjectPersistency <NSObject>
|
||||
|
||||
@required
|
||||
|
||||
- (BOOL) initObjectPersistency;
|
||||
|
||||
@end
|
||||
|
||||
@interface PNObjectModel : NSObject
|
||||
|
||||
@property (nonatomic, assign) id<PNObjectPersistency> _Nonnull persistencyDelegate;
|
||||
|
||||
+ (_Nonnull instancetype) sharedInstance;
|
||||
|
||||
/**
|
||||
* <#Description#>
|
||||
*
|
||||
* @param object PNObject Subclass to save
|
||||
*
|
||||
* @return return It return the object if the save was successful.
|
||||
* @return return NSError
|
||||
*/
|
||||
- (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#>
|
||||
*
|
||||
* @param object PNObject Subclass add list objects and save the list
|
||||
*
|
||||
* @return <#return value description#>
|
||||
*/
|
||||
- (id _Nonnull) pushObjectAndSaveLocally:(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;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,162 @@
|
||||
//
|
||||
// PNObjectModel.m
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 16/01/16.
|
||||
//
|
||||
//
|
||||
|
||||
#import "PNObjectModel.h"
|
||||
#import "PNObject.h"
|
||||
#import "PEARFileManager.h"
|
||||
|
||||
#define DEF_DOCUMENT_ROOT @"Documents"
|
||||
|
||||
@interface PNObjectModel()
|
||||
|
||||
@property (nonatomic, strong) PEARFileManager *fileManager;
|
||||
|
||||
@end
|
||||
|
||||
@implementation PNObjectModel
|
||||
|
||||
static PNObjectModel *SINGLETON = nil;
|
||||
|
||||
static bool isFirstAccess = YES;
|
||||
|
||||
#pragma mark Public Method
|
||||
|
||||
+ (instancetype) sharedInstance
|
||||
{
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
isFirstAccess = NO;
|
||||
SINGLETON = [[super allocWithZone:NULL] init];
|
||||
});
|
||||
|
||||
return SINGLETON;
|
||||
}
|
||||
|
||||
- (NSString * _Nullable) objectName:(id _Nonnull) object {
|
||||
NSString *className;
|
||||
|
||||
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)] && [(PNObject*)[object subClassDelegate] respondsToSelector:@selector(objectClassName)]) {
|
||||
|
||||
return className = (NSString *)[[(PNObject*)object class] performSelector:@selector(objectClassName)];
|
||||
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (BOOL) issetPNObjectModelForObject:(id _Nonnull) object {
|
||||
|
||||
NSString *className = [self objectName:object];
|
||||
|
||||
if (!className) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
return [_fileManager checkPath:className];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
+ (id) allocWithZone:(NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
+ (id)copyWithZone:(struct _NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
+ (id)mutableCopyWithZone:(struct _NSZone *)zone
|
||||
{
|
||||
return [self sharedInstance];
|
||||
}
|
||||
|
||||
- (id)copy
|
||||
{
|
||||
return [[PNObjectModel alloc] init];
|
||||
}
|
||||
|
||||
- (id)mutableCopy
|
||||
{
|
||||
return [[PNObjectModel alloc] init];
|
||||
}
|
||||
|
||||
- (id) init
|
||||
{
|
||||
if(SINGLETON){
|
||||
return SINGLETON;
|
||||
}
|
||||
if (isFirstAccess) {
|
||||
[self doesNotRecognizeSelector:_cmd];
|
||||
}
|
||||
self = [super init];
|
||||
|
||||
if (self) {
|
||||
_fileManager = [PEARFileManager sharedInstatnce];
|
||||
|
||||
[_fileManager setRootDirectory:k_ROOT_DIR_DOCUMENTS];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
- (id _Nonnull) saveLocally:(id _Nonnull) object {
|
||||
|
||||
BOOL isPNObjectSubclass = [[object class] isSubclassOfClass:[PNObject class]];
|
||||
|
||||
if(isPNObjectSubclass) {
|
||||
|
||||
if ([[object class] conformsToProtocol:@protocol(PNObjectSubclassing)]) {
|
||||
|
||||
if ([self issetPNObjectModelForObject:object]) {
|
||||
|
||||
|
||||
id value;
|
||||
|
||||
SEL selector = NSSelectorFromString(@"getObject");
|
||||
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[PNObject class] instanceMethodSignatureForSelector:selector]];
|
||||
[invocation setSelector:selector];
|
||||
[invocation setTarget:value];
|
||||
[invocation invoke];
|
||||
|
||||
NSDictionary *objectDict;
|
||||
[invocation getReturnValue:&objectDict];
|
||||
|
||||
NSLogDebug(@"%@",objectDict);
|
||||
|
||||
NSData *objectData = [NSKeyedArchiver archivedDataWithRootObject:objectDict];
|
||||
|
||||
[_fileManager updateFileWithData:objectData filePath:[self objectName:object] permisson:@(0755)];
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return [NSError errorWithDomain:NSLocalizedString(@"", @"") code:<#(NSInteger)#> userInfo:<#(nullable NSDictionary *)#>]
|
||||
}
|
||||
}
|
||||
|
||||
- (void) saveLocally:(id _Nonnull) object inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id _Nullable responseObject, NSError * _Nullable error)) responseBlock {
|
||||
|
||||
}
|
||||
|
||||
- (id _Nonnull) pushObjectAndSaveLocally:(id _Nonnull) object {
|
||||
|
||||
}
|
||||
|
||||
- (void) pushObjectAndSaveLocally:(id _Nonnull) object inBackGroundWithBlock:(nullable void (^)(BOOL saveStatus, id _Nullable responseObject, NSError * _Nullable error)) responseBlock {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -13,18 +13,18 @@
|
||||
|
||||
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';
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ static BOOL property_getTypeString( objc_property_t property, char *buffer )
|
||||
propertyType = [propertyType substringWithRange:subStrRange];
|
||||
}
|
||||
|
||||
//NSLog(@"Prop type & name: %@ -- %@", propertyType, propertyName);
|
||||
NSLogDebug(@"Prop type & name: %@ -- %@", propertyType, propertyName);
|
||||
|
||||
[results setObject:propertyType forKey:propertyName];
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
//
|
||||
// PNObjectSubclassing.h
|
||||
// Pods
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 08/01/16.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@protocol PNObjectSubclassing <NSObject>
|
||||
|
||||
@required
|
||||
|
||||
+ (NSString *) objectClassName;
|
||||
|
||||
+ (NSDictionary *) objcetMapping;
|
||||
|
||||
|
||||
@end
|
||||
@@ -26,4 +26,8 @@
|
||||
return mapping;
|
||||
}
|
||||
|
||||
+ (BOOL)singleInstance {
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -22,4 +22,8 @@
|
||||
return mapping;
|
||||
}
|
||||
|
||||
+ (BOOL)singleInstance {
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -9,7 +9,7 @@
|
||||
#import "PNObject.h"
|
||||
#import "PNAddress.h"
|
||||
|
||||
@interface PNUser : PNObject <PNObjectSubclassing>
|
||||
@interface PNUser : PNObject
|
||||
|
||||
@property (strong, nonatomic) NSString *userId;
|
||||
@property (strong, nonatomic) NSString *firstName;
|
||||
@@ -31,14 +31,6 @@
|
||||
@property (nonatomic, strong) NSString *facebookAccessToken;
|
||||
@property (nonatomic, strong) PNAddress *address;
|
||||
|
||||
/**
|
||||
* gets singleton object.
|
||||
*
|
||||
* @return singleton
|
||||
*/
|
||||
+ (instancetype)sharedInstance;
|
||||
|
||||
|
||||
/**
|
||||
* gets singleton object of current user session.
|
||||
*
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
|
||||
#import "PNUser.h"
|
||||
|
||||
@interface PNUser() <PNObjectSubclassing>
|
||||
|
||||
@end
|
||||
|
||||
@implementation PNUser
|
||||
|
||||
static PNUser *SINGLETON = nil;
|
||||
@@ -70,7 +74,10 @@ static bool isFirstAccess = YES;
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark PNObjectSubclassing Protocol
|
||||
|
||||
+ (NSDictionary *)objcetMapping {
|
||||
|
||||
NSDictionary *mapping = @{@"userId":@"id",
|
||||
@"firstName":@"firstName",
|
||||
@"lastName":@"lastName",
|
||||
@@ -98,6 +105,11 @@ static bool isFirstAccess = YES;
|
||||
return @"User";
|
||||
}
|
||||
|
||||
+ (BOOL) singleInstance {
|
||||
return YES;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user