Merge pull request #50 in BPHVB/mvm_core from feature/template to develop

* commit 'ae1362ad7acf51775697c97b59bfe5e362b463a8':
  Convenience for setting up session and core object
  default presentation view controller. Re-use function
  remove old code
  fix to class check for nil convenience functions init functions
This commit is contained in:
Pfeil, Scott Robert 2019-01-29 10:06:46 -05:00
commit 6b6e21e6ab
15 changed files with 78 additions and 36 deletions

View File

@ -36,8 +36,7 @@ NSString * const KeyActionTypeOpen = @"openPage";
@implementation MVMCoreActionHandler @implementation MVMCoreActionHandler
+ (nullable instancetype)sharedActionHandler { + (nullable instancetype)sharedActionHandler {
[MVMCoreActionUtility classIsInstanceTypeOfClass:[MVMCoreObject sharedInstance].actionHandler.class otherClass:self throwException:YES]; return [MVMCoreActionUtility initializerClassCheck:[MVMCoreObject sharedInstance].actionHandler classToVerify:self];
return [MVMCoreObject sharedInstance].actionHandler;
} }
- (void)handleActionWithDictionary:(nullable NSDictionary *)dictionary additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject <MVMCoreLoadDelegateProtocol, MVMCorePresentationDelegateProtocol,MVMCoreActionDelegateProtocol>*)delegate { - (void)handleActionWithDictionary:(nullable NSDictionary *)dictionary additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject <MVMCoreLoadDelegateProtocol, MVMCorePresentationDelegateProtocol,MVMCoreActionDelegateProtocol>*)delegate {

View File

@ -14,11 +14,11 @@
@protocol MVMCoreGlobalLoadProtocol <NSObject> @protocol MVMCoreGlobalLoadProtocol <NSObject>
// Provides the desired error screen for the native error. If null, will fail silently. Called on the main thread.
- (nullable UIViewController <MVMCoreViewControllerProtocol> *)getNativeScreenForRequestError:(nonnull MVMCoreErrorObject *)errorObject requestObject:(nonnull MVMCoreRequestParameters *)requestObject;
@optional @optional
// Provides the desired error screen for the native error. Called on the main thread. If set, any native errors or NSURLErrorDomain errors will use a native screen.
- (nullable UIViewController <MVMCoreViewControllerProtocol> *)getNativeScreenForRequestError:(nonnull MVMCoreErrorObject *)errorObject requestObject:(nonnull MVMCoreRequestParameters *)requestObject;
/* Can choose to handle global error scenarios here... This will happen for every request so only put things in here that aren't controller specific. Must call completion handler. /* Can choose to handle global error scenarios here... This will happen for every request so only put things in here that aren't controller specific. Must call completion handler.
@param completionHandler must be called when finished handling errors. Pass if we should continue or not, and any error object that you want to be handled. */ @param completionHandler must be called when finished handling errors. Pass if we should continue or not, and any error object that you want to be handled. */
- (void)handleGlobalErrorsForLoadObject:(nonnull MVMCoreLoadObject *)loadObject completionHandler:(nonnull void (^)(BOOL shouldContinueLoad, MVMCoreErrorObject * _Nullable error))completionHandler; - (void)handleGlobalErrorsForLoadObject:(nonnull MVMCoreLoadObject *)loadObject completionHandler:(nonnull void (^)(BOOL shouldContinueLoad, MVMCoreErrorObject * _Nullable error))completionHandler;

View File

@ -42,8 +42,7 @@
static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
+ (nullable instancetype)sharedCache { + (nullable instancetype)sharedCache {
[MVMCoreActionUtility classIsInstanceTypeOfClass:[MVMCoreObject sharedInstance].cache.class otherClass:self throwException:YES]; return [MVMCoreActionUtility initializerClassCheck:[MVMCoreObject sharedInstance].cache classToVerify:self];
return [MVMCoreObject sharedInstance].cache;
} }
- (nullable instancetype)init { - (nullable instancetype)init {

View File

@ -10,12 +10,13 @@
#import <MVMCore/MVMCoreErrorObject.h> #import <MVMCore/MVMCoreErrorObject.h>
#import <MVMCore/MVMCoreViewControllerProtocol.h> #import <MVMCore/MVMCoreViewControllerProtocol.h>
#import <MVMCore/MVMCoreLoadObject.h> #import <MVMCore/MVMCoreLoadObject.h>
#import <MVMCore/MVMCoreLoggingDelegateProtocol.h>
@class MVMCoreAlertController; @class MVMCoreAlertController;
#define MVMCoreLog(fmt, ...) \ #define MVMCoreLog(fmt, ...) \
[MVMCoreLoggingHandler logDebugMessageWithDelegate:[NSString stringWithFormat:(@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__]]; [MVMCoreLoggingHandler logDebugMessageWithDelegate:[NSString stringWithFormat:(@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__]];
@interface MVMCoreLoggingHandler : NSObject @interface MVMCoreLoggingHandler : NSObject <MVMCoreLoggingDelegateProtocol>
+ (nullable instancetype)sharedLoggingHandler; + (nullable instancetype)sharedLoggingHandler;

View File

@ -13,7 +13,7 @@
@implementation MVMCoreLoggingHandler @implementation MVMCoreLoggingHandler
+ (nullable instancetype)sharedLoggingHandler { + (nullable instancetype)sharedLoggingHandler {
if ([MVMCoreActionUtility classIsInstanceTypeOfClass:[MVMCoreObject sharedInstance].loggingDelegate.class otherClass:self throwException:NO]) { if ([MVMCoreObject sharedInstance].loggingDelegate && [MVMCoreActionUtility classIsInstanceTypeOfClass:[MVMCoreObject sharedInstance].loggingDelegate.class otherClass:self throwException:NO]) {
return (MVMCoreLoggingHandler *)[MVMCoreObject sharedInstance].loggingDelegate; return (MVMCoreLoggingHandler *)[MVMCoreObject sharedInstance].loggingDelegate;
} else { } else {
return nil; return nil;
@ -50,5 +50,12 @@
} }
} }
#pragma mark - logging delegate
- (void)handleDebugMessage:(nullable NSString *)message {
#ifdef DEBUG
NSLog(@"%@", message);
#endif
}
@end @end

View File

@ -276,12 +276,13 @@
- (void)presentViewController:(nonnull UIViewController *)viewController animated:(BOOL)animated delegate:(nullable NSObject<MVMCorePresentationDelegateProtocol>*)delegate completionHandler:(nullable void (^)(void))completionBlock { - (void)presentViewController:(nonnull UIViewController *)viewController animated:(BOOL)animated delegate:(nullable NSObject<MVMCorePresentationDelegateProtocol>*)delegate completionHandler:(nullable void (^)(void))completionBlock {
if (!self.viewControllerToPresentOn) { UIViewController *controllerToPresentOn = self.viewControllerToPresentOn ?: [UIApplication sharedApplication].keyWindow.rootViewController;
if (!controllerToPresentOn) {
// Log if we are attempting to add an alert to nothing. // Log if we are attempting to add an alert to nothing.
MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:@"The viewControllerToPresent on property is nil and an attempt was made to present." code:ErrorCodeNoViewControllerToPresentOn domain:ErrorDomainNative location:[NSString stringWithFormat:@"ViewController:%@|Delegate:%@",viewController.description,delegate]]; MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:@"The viewControllerToPresent on property is nil and an attempt was made to present." code:ErrorCodeNoViewControllerToPresentOn domain:ErrorDomainNative location:[NSString stringWithFormat:@"ViewController:%@|Delegate:%@",viewController.description,delegate]];
[MVMCoreLoggingHandler addErrorToLog:error]; [MVMCoreLoggingHandler addErrorToLog:error];
} else { } else {
MVMCorePresentViewControllerOperation *operation = [[MVMCorePresentViewControllerOperation alloc] initWithPresentingViewController:self.viewControllerToPresentOn presentedViewController:viewController animated:animated]; MVMCorePresentViewControllerOperation *operation = [[MVMCorePresentViewControllerOperation alloc] initWithPresentingViewController:controllerToPresentOn presentedViewController:viewController animated:animated];
operation.delegate = delegate; operation.delegate = delegate;
operation.completionBlock = completionBlock; operation.completionBlock = completionBlock;
[self.presentationQueue addOperation:operation]; [self.presentationQueue addOperation:operation];

View File

@ -26,6 +26,9 @@
// Returns the shared instance of this singleton // Returns the shared instance of this singleton
+ (nullable instancetype)sharedGlobal; + (nullable instancetype)sharedGlobal;
// Creates the nsurlsession with default configuration and no delegate. Can subclass for different session.
- (nonnull NSURLSession *)createNSURLSession;
// Gets inital parameters for request parameters. // Gets inital parameters for request parameters.
- (nullable NSDictionary *)getInitialParameters; - (nullable NSDictionary *)getInitialParameters;
// Gets inital parameters for request parameters excluding any items given a list of key names. // Gets inital parameters for request parameters excluding any items given a list of key names.
@ -37,7 +40,7 @@
// Redirect, leaving the current app experience. // Redirect, leaving the current app experience.
- (void)redirectWithInfo:(nullable NSDictionary *)dictionary; - (void)redirectWithInfo:(nullable NSDictionary *)dictionary;
// Clears the session singleton. // Clears the session singleton. Creates a new session NSURLSession also.
- (void)clearSessionObject; - (void)clearSessionObject;
@end @end

View File

@ -12,9 +12,19 @@
@implementation MVMCoreSessionObject @implementation MVMCoreSessionObject
- (instancetype)init {
if (self = [super init]) {
self.session = [self createNSURLSession];
}
return self;
}
+ (nullable instancetype)sharedGlobal { + (nullable instancetype)sharedGlobal {
[MVMCoreActionUtility classIsInstanceTypeOfClass:[MVMCoreObject sharedInstance].session.class otherClass:self throwException:YES]; return [MVMCoreActionUtility initializerClassCheck:[MVMCoreObject sharedInstance].session classToVerify:self];
return [MVMCoreObject sharedInstance].session; }
- (nonnull NSURLSession *)createNSURLSession {
return [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:nil delegateQueue:nil];
} }
- (nullable NSDictionary *)getInitialParameters { - (nullable NSDictionary *)getInitialParameters {
@ -37,6 +47,8 @@
} }
- (void)clearSessionObject { - (void)clearSessionObject {
[self.session invalidateAndCancel];
self.session = [self createNSURLSession];
} }
@end @end

View File

@ -47,8 +47,7 @@
@implementation MVMCoreSessionTimeHandler @implementation MVMCoreSessionTimeHandler
+ (nullable instancetype)sharedSessionHandler { + (nullable instancetype)sharedSessionHandler {
[MVMCoreActionUtility classIsInstanceTypeOfClass:[MVMCoreObject sharedInstance].sessionHandler.class otherClass:self throwException:YES]; return [MVMCoreActionUtility initializerClassCheck:[MVMCoreObject sharedInstance].sessionHandler classToVerify:self];
return [MVMCoreObject sharedInstance].sessionHandler;
} }
- (instancetype)init { - (instancetype)init {

View File

@ -27,10 +27,10 @@
@property (nullable, strong, nonatomic) MVMCoreSessionTimeHandler *sessionHandler; @property (nullable, strong, nonatomic) MVMCoreSessionTimeHandler *sessionHandler;
// The delegates // The delegates
@property (nullable, weak, nonatomic) id <MVMCoreGlobalLoadProtocol> globalLoadDelegate; @property (nullable, strong, nonatomic) id <MVMCoreGlobalLoadProtocol> globalLoadDelegate;
@property (nullable, weak, nonatomic) id <MVMCoreLoadingOverlayDelegateProtocol> loadingProtocol; @property (nullable, strong, nonatomic) id <MVMCoreLoadingOverlayDelegateProtocol> loadingProtocol;
@property (nullable, weak, nonatomic) NSObject <MVMCoreLoggingDelegateProtocol> *loggingDelegate; @property (nullable, strong, nonatomic) NSObject <MVMCoreLoggingDelegateProtocol> *loggingDelegate;
@property (nullable, weak, nonatomic) id <MVMCoreGlobalTopAlertDelegateProtocol> globalTopAlertDelegate; @property (nullable, strong, nonatomic) id <MVMCoreGlobalTopAlertDelegateProtocol> globalTopAlertDelegate;
// A reference to the calling application delegate that should be set. For a normal app, could be the UIApplicationDelegate. For watch, could be WKExtensionDelegate. For iMessage, could be MSMessagesAppViewController. etc, etc. Useful for the framework to call delegate specific functions. // A reference to the calling application delegate that should be set. For a normal app, could be the UIApplicationDelegate. For watch, could be WKExtensionDelegate. For iMessage, could be MSMessagesAppViewController. etc, etc. Useful for the framework to call delegate specific functions.
@property (nullable, weak, nonatomic) id applicationDelegate; @property (nullable, weak, nonatomic) id applicationDelegate;
@ -38,4 +38,7 @@
// A singleton. // A singleton.
+ (nullable instancetype)sharedInstance; + (nullable instancetype)sharedInstance;
// Sets up with a default session, cache, view controller mapping, action handler, session handler, and logging delegate.
- (void)defaultInitialSetup;
@end @end

View File

@ -19,4 +19,13 @@
return sharedInstance; return sharedInstance;
} }
- (void)defaultInitialSetup {
self.session = [[MVMCoreSessionObject alloc] init];
self.cache = [[MVMCoreCache alloc] init];
self.viewControllerMapping = [[MVMCoreViewControllerMappingObject alloc] init];
self.sessionHandler = [[MVMCoreSessionTimeHandler alloc] init];
self.actionHandler = [[MVMCoreActionHandler alloc] init];
self.loggingDelegate = [[MVMCoreLoggingHandler alloc] init];
}
@end @end

View File

@ -26,4 +26,7 @@
// returns if the class is not the same or a subclass of the other class. Can pass throw an exception as well // returns if the class is not the same or a subclass of the other class. Can pass throw an exception as well
+ (BOOL)classIsInstanceTypeOfClass:(Class)theClass otherClass:(Class)otherClass throwException:(BOOL)throwException; + (BOOL)classIsInstanceTypeOfClass:(Class)theClass otherClass:(Class)otherClass throwException:(BOOL)throwException;
// Calls the above function with throw exception yes if the object is not nil. Returns the object. Convenience function for one liner in initializer.
+ (id)initializerClassCheck:(nullable NSObject *)object classToVerify:(Class)classToVerify;
@end @end

View File

@ -101,14 +101,7 @@
} }
+ (void)openURL:(nullable NSURL *)url completionHandler:(void (^ __nullable)(BOOL success))completion{ + (void)openURL:(nullable NSURL *)url completionHandler:(void (^ __nullable)(BOOL success))completion{
if (@available(iOS 10.0, *)) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:completion];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:completion];
} else {
BOOL successful = [[UIApplication sharedApplication] openURL:url];
if (completion != nil){
completion(successful);
}
}
} }
+ (void)displayViewController:(nonnull UIViewController <MVMCoreViewControllerProtocol> *)viewController forLoadObject:(nullable MVMCoreLoadObject *)loadObject presentationDelegate:(nullable NSObject<MVMCorePresentationDelegateProtocol>*)delegate completionHandler:(nullable void (^)(void))completionBlock { + (void)displayViewController:(nonnull UIViewController <MVMCoreViewControllerProtocol> *)viewController forLoadObject:(nullable MVMCoreLoadObject *)loadObject presentationDelegate:(nullable NSObject<MVMCorePresentationDelegateProtocol>*)delegate completionHandler:(nullable void (^)(void))completionBlock {
@ -147,4 +140,11 @@
return YES; return YES;
} }
+ (id)initializerClassCheck:(nullable NSObject *)object classToVerify:(Class)classToVerify {
if (object) {
[self classIsInstanceTypeOfClass:object.class otherClass:classToVerify throwException:YES];
}
return object;
}
@end @end

View File

@ -7,6 +7,7 @@
// //
#import "MVMCoreErrorObject.h" #import "MVMCoreErrorObject.h"
#import "MVMCoreObject.h"
#import "MVMCoreErrorConstants.h" #import "MVMCoreErrorConstants.h"
#import "MVMCoreGetterUtility.h" #import "MVMCoreGetterUtility.h"
#import "NSDictionary+MFConvenience.h" #import "NSDictionary+MFConvenience.h"
@ -32,8 +33,10 @@
self.logError = YES; self.logError = YES;
// Native and system errors have an error screen. // Native and system errors have an error screen.
self.errorScreenError = YES; if ([[MVMCoreObject sharedInstance].globalLoadDelegate respondsToSelector:@selector(getNativeScreenForRequestError:requestObject:)]) {
self.nativeDrivenErrorScreen = YES; self.errorScreenError = YES;
self.nativeDrivenErrorScreen = YES;
}
} else { } else {
self.logError = NO; self.logError = NO;
} }
@ -58,8 +61,10 @@
self.logError = YES; self.logError = YES;
// Native and system errors have an error screen. // Native and system errors have an error screen.
self.errorScreenError = YES; if ([[MVMCoreObject sharedInstance].globalLoadDelegate respondsToSelector:@selector(getNativeScreenForRequestError:requestObject:)]) {
self.nativeDrivenErrorScreen = YES; self.errorScreenError = YES;
self.nativeDrivenErrorScreen = YES;
}
} else { } else {
self.logError = NO; self.logError = NO;
} }
@ -98,9 +103,11 @@
MVMCoreErrorObject *errorObject = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[error localizedDescription] code:[error code] domain:ErrorDomainSystem location:location]; MVMCoreErrorObject *errorObject = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[error localizedDescription] code:[error code] domain:ErrorDomainSystem location:location];
if ([error.domain isEqualToString:NSURLErrorDomain]) { if ([error.domain isEqualToString:NSURLErrorDomain]) {
errorObject.errorScreenError = YES;
errorObject.nativeDrivenErrorScreen = YES;
errorObject.systemDomain = error.domain; errorObject.systemDomain = error.domain;
if ([[MVMCoreObject sharedInstance].globalLoadDelegate respondsToSelector:@selector(getNativeScreenForRequestError:requestObject:)]) {
errorObject.errorScreenError = YES;
errorObject.nativeDrivenErrorScreen = YES;
}
} }
return errorObject; return errorObject;
} }

View File

@ -27,8 +27,7 @@
@implementation MVMCoreViewControllerMappingObject @implementation MVMCoreViewControllerMappingObject
+ (nullable instancetype)sharedViewControllerMappingObject { + (nullable instancetype)sharedViewControllerMappingObject {
[MVMCoreActionUtility classIsInstanceTypeOfClass:[MVMCoreObject sharedInstance].viewControllerMapping.class otherClass:self throwException:YES]; return [MVMCoreActionUtility initializerClassCheck:[MVMCoreObject sharedInstance].viewControllerMapping classToVerify:self];
return [MVMCoreObject sharedInstance].viewControllerMapping;
} }
#pragma mark - View Controller Mapping #pragma mark - View Controller Mapping