Convenience for setting up session and core object

This commit is contained in:
Pfeil, Scott Robert 2019-01-28 15:46:56 -05:00
parent 2510db98e0
commit ae1362ad7a
8 changed files with 58 additions and 15 deletions

View File

@ -14,11 +14,11 @@
@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
// 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.
@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;

View File

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

View File

@ -50,5 +50,12 @@
}
}
#pragma mark - logging delegate
- (void)handleDebugMessage:(nullable NSString *)message {
#ifdef DEBUG
NSLog(@"%@", message);
#endif
}
@end

View File

@ -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

View File

@ -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

View File

@ -27,10 +27,10 @@
@property (nullable, strong, nonatomic) MVMCoreSessionTimeHandler *sessionHandler;
// The delegates
@property (nullable, weak, nonatomic) id <MVMCoreGlobalLoadProtocol> globalLoadDelegate;
@property (nullable, weak, nonatomic) id <MVMCoreLoadingOverlayDelegateProtocol> loadingProtocol;
@property (nullable, weak, nonatomic) NSObject <MVMCoreLoggingDelegateProtocol> *loggingDelegate;
@property (nullable, weak, nonatomic) id <MVMCoreGlobalTopAlertDelegateProtocol> globalTopAlertDelegate;
@property (nullable, strong, nonatomic) id <MVMCoreGlobalLoadProtocol> globalLoadDelegate;
@property (nullable, strong, nonatomic) id <MVMCoreLoadingOverlayDelegateProtocol> loadingProtocol;
@property (nullable, strong, nonatomic) NSObject <MVMCoreLoggingDelegateProtocol> *loggingDelegate;
@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.
@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

View File

@ -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

View File

@ -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;
}