From ae1362ad7acf51775697c97b59bfe5e362b463a8 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Mon, 28 Jan 2019 15:46:56 -0500 Subject: [PATCH] Convenience for setting up session and core object --- .../MainProtocols/MVMCoreGlobalLoadProtocol.h | 6 +++--- .../OtherHandlers/MVMCoreLoggingHandler.h | 3 ++- .../OtherHandlers/MVMCoreLoggingHandler.m | 7 +++++++ .../MVMCore/Session/MVMCoreSessionObject.h | 5 ++++- .../MVMCore/Session/MVMCoreSessionObject.m | 13 +++++++++++++ MVMCore/MVMCore/Singletons/MVMCoreObject.h | 11 +++++++---- MVMCore/MVMCore/Singletons/MVMCoreObject.m | 9 +++++++++ MVMCore/MVMCore/Utility/MVMCoreErrorObject.m | 19 +++++++++++++------ 8 files changed, 58 insertions(+), 15 deletions(-) diff --git a/MVMCore/MVMCore/MainProtocols/MVMCoreGlobalLoadProtocol.h b/MVMCore/MVMCore/MainProtocols/MVMCoreGlobalLoadProtocol.h index fbca045..ddf3db9 100644 --- a/MVMCore/MVMCore/MainProtocols/MVMCoreGlobalLoadProtocol.h +++ b/MVMCore/MVMCore/MainProtocols/MVMCoreGlobalLoadProtocol.h @@ -14,11 +14,11 @@ @protocol MVMCoreGlobalLoadProtocol -// Provides the desired error screen for the native error. If null, will fail silently. Called on the main thread. -- (nullable UIViewController *)getNativeScreenForRequestError:(nonnull MVMCoreErrorObject *)errorObject requestObject:(nonnull MVMCoreRequestParameters *)requestObject; - @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 *)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. @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; diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.h b/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.h index 557ac8e..4cb1301 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.h +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.h @@ -10,12 +10,13 @@ #import #import #import +#import @class MVMCoreAlertController; #define MVMCoreLog(fmt, ...) \ [MVMCoreLoggingHandler logDebugMessageWithDelegate:[NSString stringWithFormat:(@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__]]; -@interface MVMCoreLoggingHandler : NSObject +@interface MVMCoreLoggingHandler : NSObject + (nullable instancetype)sharedLoggingHandler; diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.m index b96127e..84865ad 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreLoggingHandler.m @@ -50,5 +50,12 @@ } } +#pragma mark - logging delegate + +- (void)handleDebugMessage:(nullable NSString *)message { +#ifdef DEBUG + NSLog(@"%@", message); +#endif +} @end diff --git a/MVMCore/MVMCore/Session/MVMCoreSessionObject.h b/MVMCore/MVMCore/Session/MVMCoreSessionObject.h index c2a85e6..ceb4e23 100644 --- a/MVMCore/MVMCore/Session/MVMCoreSessionObject.h +++ b/MVMCore/MVMCore/Session/MVMCoreSessionObject.h @@ -26,6 +26,9 @@ // Returns the shared instance of this singleton + (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. - (nullable NSDictionary *)getInitialParameters; // 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. - (void)redirectWithInfo:(nullable NSDictionary *)dictionary; -// Clears the session singleton. +// Clears the session singleton. Creates a new session NSURLSession also. - (void)clearSessionObject; @end diff --git a/MVMCore/MVMCore/Session/MVMCoreSessionObject.m b/MVMCore/MVMCore/Session/MVMCoreSessionObject.m index 484eb37..d094b0b 100644 --- a/MVMCore/MVMCore/Session/MVMCoreSessionObject.m +++ b/MVMCore/MVMCore/Session/MVMCoreSessionObject.m @@ -12,10 +12,21 @@ @implementation MVMCoreSessionObject +- (instancetype)init { + if (self = [super init]) { + self.session = [self createNSURLSession]; + } + return self; +} + + (nullable instancetype)sharedGlobal { return [MVMCoreActionUtility initializerClassCheck:[MVMCoreObject sharedInstance].session classToVerify:self]; } +- (nonnull NSURLSession *)createNSURLSession { + return [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:nil delegateQueue:nil]; +} + - (nullable NSDictionary *)getInitialParameters { return [self getInitialParametersExcludingSections:nil]; } @@ -36,6 +47,8 @@ } - (void)clearSessionObject { + [self.session invalidateAndCancel]; + self.session = [self createNSURLSession]; } @end diff --git a/MVMCore/MVMCore/Singletons/MVMCoreObject.h b/MVMCore/MVMCore/Singletons/MVMCoreObject.h index ebdf435..26013a3 100644 --- a/MVMCore/MVMCore/Singletons/MVMCoreObject.h +++ b/MVMCore/MVMCore/Singletons/MVMCoreObject.h @@ -27,10 +27,10 @@ @property (nullable, strong, nonatomic) MVMCoreSessionTimeHandler *sessionHandler; // The delegates -@property (nullable, weak, nonatomic) id globalLoadDelegate; -@property (nullable, weak, nonatomic) id loadingProtocol; -@property (nullable, weak, nonatomic) NSObject *loggingDelegate; -@property (nullable, weak, nonatomic) id globalTopAlertDelegate; +@property (nullable, strong, nonatomic) id globalLoadDelegate; +@property (nullable, strong, nonatomic) id loadingProtocol; +@property (nullable, strong, nonatomic) NSObject *loggingDelegate; +@property (nullable, strong, nonatomic) id 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. @property (nullable, weak, nonatomic) id applicationDelegate; @@ -38,4 +38,7 @@ // A singleton. + (nullable instancetype)sharedInstance; +// Sets up with a default session, cache, view controller mapping, action handler, session handler, and logging delegate. +- (void)defaultInitialSetup; + @end diff --git a/MVMCore/MVMCore/Singletons/MVMCoreObject.m b/MVMCore/MVMCore/Singletons/MVMCoreObject.m index 6a44fde..40d0732 100644 --- a/MVMCore/MVMCore/Singletons/MVMCoreObject.m +++ b/MVMCore/MVMCore/Singletons/MVMCoreObject.m @@ -19,4 +19,13 @@ 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 diff --git a/MVMCore/MVMCore/Utility/MVMCoreErrorObject.m b/MVMCore/MVMCore/Utility/MVMCoreErrorObject.m index 552893a..9354326 100644 --- a/MVMCore/MVMCore/Utility/MVMCoreErrorObject.m +++ b/MVMCore/MVMCore/Utility/MVMCoreErrorObject.m @@ -7,6 +7,7 @@ // #import "MVMCoreErrorObject.h" +#import "MVMCoreObject.h" #import "MVMCoreErrorConstants.h" #import "MVMCoreGetterUtility.h" #import "NSDictionary+MFConvenience.h" @@ -32,8 +33,10 @@ self.logError = YES; // Native and system errors have an error screen. - self.errorScreenError = YES; - self.nativeDrivenErrorScreen = YES; + if ([[MVMCoreObject sharedInstance].globalLoadDelegate respondsToSelector:@selector(getNativeScreenForRequestError:requestObject:)]) { + self.errorScreenError = YES; + self.nativeDrivenErrorScreen = YES; + } } else { self.logError = NO; } @@ -58,8 +61,10 @@ self.logError = YES; // Native and system errors have an error screen. - self.errorScreenError = YES; - self.nativeDrivenErrorScreen = YES; + if ([[MVMCoreObject sharedInstance].globalLoadDelegate respondsToSelector:@selector(getNativeScreenForRequestError:requestObject:)]) { + self.errorScreenError = YES; + self.nativeDrivenErrorScreen = YES; + } } else { 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]; if ([error.domain isEqualToString:NSURLErrorDomain]) { - errorObject.errorScreenError = YES; - errorObject.nativeDrivenErrorScreen = YES; errorObject.systemDomain = error.domain; + if ([[MVMCoreObject sharedInstance].globalLoadDelegate respondsToSelector:@selector(getNativeScreenForRequestError:requestObject:)]) { + errorObject.errorScreenError = YES; + errorObject.nativeDrivenErrorScreen = YES; + } } return errorObject; }