move module from page to mfviewcontroller

This commit is contained in:
Pfeil, Scott Robert 2019-06-27 10:03:24 -04:00
parent bd365d60d2
commit 868593aabe
5 changed files with 59 additions and 50 deletions

View File

@ -131,6 +131,9 @@
// Returns an array of modules to observe for when we receive a response with JSON. Subclass this to have the ui update when the returned page types are cached. // Returns an array of modules to observe for when we receive a response with JSON. Subclass this to have the ui update when the returned page types are cached.
- (nullable NSArray *)modulesToListenFor; - (nullable NSArray *)modulesToListenFor;
/// Aggregated with modulesToListenFor. Returns an array of modules to observe based on the page itself. Subclass this to go into the page logic and see if it dictates any modules are needed. Important: This also updates in the requiredModules in the view controller mapping, so that future page loads account for these modules.
- (nullable NSArray *)requiredModulesFromPageJSON:(nullable NSDictionary *)page;
// The function that gets called by the notification center when the JSON is updated. // The function that gets called by the notification center when the JSON is updated.
- (void)responseJSONUpdated:(nonnull NSNotification *)notification; - (void)responseJSONUpdated:(nonnull NSNotification *)notification;

View File

@ -93,6 +93,7 @@
#pragma mark - Functions To Subclass #pragma mark - Functions To Subclass
- (BOOL)shouldFinishProcessingLoad:(nonnull MVMCoreLoadObject *)loadObject error:(MVMCoreErrorObject *_Nonnull *_Nonnull)error { - (BOOL)shouldFinishProcessingLoad:(nonnull MVMCoreLoadObject *)loadObject error:(MVMCoreErrorObject *_Nonnull *_Nonnull)error {
[self updateRequiredModulesForPageJSON:loadObject.pageJSON];
self.loadObject = loadObject; self.loadObject = loadObject;
self.pageType = loadObject.pageType; self.pageType = loadObject.pageType;
return YES; return YES;
@ -208,6 +209,20 @@
return nil; return nil;
} }
- (nullable NSArray *)requiredModulesFromPageJSON:(nullable NSDictionary *)page {
return nil;
}
- (nullable NSArray *)updateRequiredModulesForPageJSON:(nullable NSDictionary *)page {
// get new needed modules and add them to the mapping for future loads.
NSArray *newRequiredModules = [self requiredModulesFromPageJSON:page];
NSString *pageType = [page string:KeyPageType];
if (newRequiredModules && pageType) {
[[MVMCoreViewControllerMappingObject sharedViewControllerMappingObject] addRequiredModulesToMapping:newRequiredModules forPageType:pageType];
}
return newRequiredModules;
}
- (void)responseJSONUpdated:(nonnull NSNotification *)notification { - (void)responseJSONUpdated:(nonnull NSNotification *)notification {
// Checks for a page we are listening for. // Checks for a page we are listening for.
@ -223,11 +238,22 @@
}]; }];
// Checks for modules we are listening for. // Checks for modules we are listening for.
NSArray *modulesListeningFor = [self modulesToListenFor]; NSMutableArray *modulesListeningFor = [NSMutableArray array];
NSDictionary *modules = [notification.userInfo dict:KeyModuleMap]; NSArray *modules = [self modulesToListenFor];
if (modules) {
[modulesListeningFor addObjectsFromArray:modules];
}
// Check if the page dictates if there are any new modules to load. Would eventually like to consolidate this and modulesToListenFor somehow.
NSArray *requiredForPage = [self updateRequiredModulesForPageJSON:pageUpdated];
if (requiredForPage) {
[modulesListeningFor addObjectsFromArray:requiredForPage];
}
NSDictionary *modulesReceived = [notification.userInfo dict:KeyModuleMap];
__block NSMutableDictionary *modulesUpdated = [NSMutableDictionary dictionary]; __block NSMutableDictionary *modulesUpdated = [NSMutableDictionary dictionary];
[modulesListeningFor enumerateObjectsUsingBlock:^(NSString * _Nonnull moduleToListenFor, NSUInteger idx, BOOL * _Nonnull stop) { [modulesListeningFor enumerateObjectsUsingBlock:^(NSString * _Nonnull moduleToListenFor, NSUInteger idx, BOOL * _Nonnull stop) {
NSDictionary *module = [modules dict:moduleToListenFor]; NSDictionary *module = [modulesReceived dict:moduleToListenFor];
if (module) { if (module) {
[modulesUpdated setObject:module forKey:moduleToListenFor]; [modulesUpdated setObject:module forKey:moduleToListenFor];
} }

View File

@ -65,14 +65,17 @@ open class ModuleMolecule: ViewConstrainingView {
} }
public override static func requiredModules(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject?>?) -> [String]? { public override static func requiredModules(_ json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject?>?) -> [String]? {
guard let moduleName = json?.optionalStringForKey("moduleName"), let _ = delegateObject?.moleculeDelegate?.getModuleWithName(moduleName) else { let moduleName = json?.optionalStringForKey("moduleName")
if moduleName == nil || delegateObject?.moleculeDelegate?.getModuleWithName(moduleName) == nil {
if let errorObject = MVMCoreErrorObject(title: nil, message: MVMCoreGetterUtility.hardcodedString(withKey: HardcodedErrorUnableToProcess), code: CoreUIErrorCode.ErrorCodeModuleMolecule.rawValue, domain: ErrorDomainNative, location: String(describing: self)) { if let errorObject = MVMCoreErrorObject(title: nil, message: MVMCoreGetterUtility.hardcodedString(withKey: HardcodedErrorUnableToProcess), code: CoreUIErrorCode.ErrorCodeModuleMolecule.rawValue, domain: ErrorDomainNative, location: String(describing: self)) {
error?.pointee = errorObject error?.pointee = errorObject
MVMCoreUILoggingHandler.shared()?.addError(toLog: errorObject) MVMCoreUILoggingHandler.shared()?.addError(toLog: errorObject)
} }
return nil
} }
return [moduleName] if let moduleName = moduleName {
return [moduleName]
}
return nil
} }
// MARK: - MVMCoreUIViewConstrainingProtocol // MARK: - MVMCoreUIViewConstrainingProtocol

View File

@ -12,20 +12,6 @@ open class MoleculeListTemplate: ThreeLayerTableViewController {
var moleculesInfo: [(identifier: String, class: AnyClass, molecule: [AnyHashable: Any])]? var moleculesInfo: [(identifier: String, class: AnyClass, molecule: [AnyHashable: Any])]?
var modulesRequired: NSMutableArray? var modulesRequired: NSMutableArray?
open override func shouldFinishProcessingLoad(_ loadObject: MVMCoreLoadObject, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject>) -> Bool {
var shouldFinish = super.shouldFinishProcessingLoad(loadObject, error: error)
guard shouldFinish else {
return shouldFinish
}
if let firstError = setup()?.firstObject as? MVMCoreErrorObject {
// Don't continue if there was an error loading needed modules.
error.pointee = firstError
shouldFinish = false
}
return shouldFinish
}
open override func viewForTop() -> UIView { open override func viewForTop() -> UIView {
guard let moleculeJSON = loadObject?.pageJSON?.optionalDictionaryForKey("header"), guard let moleculeJSON = loadObject?.pageJSON?.optionalDictionaryForKey("header"),
let molecule = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(forJSON: moleculeJSON, delegateObject: delegateObject() as? MVMCoreUIDelegateObject, constrainIfNeeded: true) else { let molecule = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(forJSON: moleculeJSON, delegateObject: delegateObject() as? MVMCoreUIDelegateObject, constrainIfNeeded: true) else {
@ -44,7 +30,6 @@ open class MoleculeListTemplate: ThreeLayerTableViewController {
open override func newDataBuildScreen() { open override func newDataBuildScreen() {
super.newDataBuildScreen() super.newDataBuildScreen()
_ = setup()
registerWithTable() registerWithTable()
} }
@ -105,8 +90,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController {
} }
open override func modulesToListenFor() -> [Any]? { open override func modulesToListenFor() -> [Any]? {
// Get all of the molecules that need modules. return loadObject?.requestParameters?.modules
return modulesRequired as? [Any]
} }
// MARK: - Convenience // MARK: - Convenience
@ -134,24 +118,27 @@ open class MoleculeListTemplate: ThreeLayerTableViewController {
} }
/// Sets up the header, footer, molecule list and ensures no errors loading all content. /// Sets up the header, footer, molecule list and ensures no errors loading all content.
func setup() -> NSMutableArray? { func setup(fromPageJSON page: [AnyHashable : Any]?) {
let modules: NSMutableArray = [] let modules: NSMutableArray = []
let errors: NSMutableArray = []
let delegate = delegateObject() as? MVMCoreUIDelegateObject let delegate = delegateObject() as? MVMCoreUIDelegateObject
MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: loadObject?.pageJSON?.optionalDictionaryForKey("header"), delegateObject: delegate, moduleList: modules, errorList: errors) MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: page?.optionalDictionaryForKey("header"), delegateObject: delegate, moduleList: modules, errorList: nil)
MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: loadObject?.pageJSON?.optionalDictionaryForKey("footer"), delegateObject: delegate, moduleList: modules, errorList: errors) MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: page?.optionalDictionaryForKey("footer"), delegateObject: delegate, moduleList: modules, errorList: nil)
var moleculeList: [(identifier: String, class: AnyClass, molecule: [AnyHashable: Any])] = [] var moleculeList: [(identifier: String, class: AnyClass, molecule: [AnyHashable: Any])] = []
if let molecules = loadObject?.pageJSON?.optionalArrayForKey(KeyMolecules) as? [[AnyHashable: Any]] { if let molecules = page?.optionalArrayForKey(KeyMolecules) as? [[AnyHashable: Any]] {
for molecule in molecules { for molecule in molecules {
if let info = getMoleculeInfo(with: molecule) { if let info = getMoleculeInfo(with: molecule) {
moleculeList.append(info) moleculeList.append(info)
MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: molecule, delegateObject: delegate, moduleList: modules, errorList: errors) MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: molecule, delegateObject: delegate, moduleList: modules, errorList: nil)
} }
} }
} }
moleculesInfo = moleculeList moleculesInfo = moleculeList
modulesRequired = modules modulesRequired = modules
return errors.count > 0 ? errors : nil }
open override func requiredModules(fromPageJSON page: [AnyHashable : Any]?) -> [Any]? {
setup(fromPageJSON: page)
return modulesRequired as? [Any]
} }
} }

View File

@ -9,17 +9,6 @@
import UIKit import UIKit
public class MoleculeStackTemplate: ThreeLayerViewController { public class MoleculeStackTemplate: ThreeLayerViewController {
var modulesRequired: NSMutableArray?
public override func shouldFinishProcessingLoad(_ loadObject: MVMCoreLoadObject, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject>) -> Bool {
var shouldFinish = super.shouldFinishProcessingLoad(loadObject, error: error)
if shouldFinish, let firstError = modulesNeeded().errors?.firstObject as? MVMCoreErrorObject {
// Don't continue if there was an error loading needed modules.
error.pointee = firstError
shouldFinish = false
}
return shouldFinish
}
public override func spaceBetweenTopAndMiddle() -> CGFloat? { public override func spaceBetweenTopAndMiddle() -> CGFloat? {
return 0 return 0
@ -58,19 +47,20 @@ public class MoleculeStackTemplate: ThreeLayerViewController {
} }
public override func modulesToListenFor() -> [Any]? { public override func modulesToListenFor() -> [Any]? {
// Get all of the molecules that need modules. return loadObject?.requestParameters?.modules
return modulesNeeded().modules as? [Any] }
open override func requiredModules(fromPageJSON page: [AnyHashable : Any]?) -> [Any]? {
return modulesNeeded() as? [Any]
} }
/// Gets the modules needed. /// Gets the modules needed.
func modulesNeeded() -> (modules: NSMutableArray?, errors: NSMutableArray?) { func modulesNeeded() -> NSMutableArray? {
let modules: NSMutableArray = [] let modules: NSMutableArray = []
let errors: NSMutableArray = []
let delegate = delegateObject() as? MVMCoreUIDelegateObject let delegate = delegateObject() as? MVMCoreUIDelegateObject
MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: loadObject?.pageJSON?.optionalDictionaryForKey("header"), delegateObject: delegate, moduleList: modules, errorList: errors) MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: loadObject?.pageJSON?.optionalDictionaryForKey("header"), delegateObject: delegate, moduleList: modules, errorList: nil)
MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: loadObject?.pageJSON?.optionalDictionaryForKey("footer"), delegateObject: delegate, moduleList: modules, errorList: errors) MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: loadObject?.pageJSON?.optionalDictionaryForKey("footer"), delegateObject: delegate, moduleList: modules, errorList: nil)
MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: loadObject?.pageJSON?.optionalDictionaryForKey("moleculeStack"), delegateObject: delegate, moduleList: modules, errorList: errors) MVMCoreUIMoleculeMappingObject.addRequiredModules(forJSON: loadObject?.pageJSON?.optionalDictionaryForKey("moleculeStack"), delegateObject: delegate, moduleList: modules, errorList: nil)
modulesRequired = modules return modules.count > 0 ? modules : nil
return (modules.count > 0 ? modules : nil, errors.count > 0 ? errors : nil)
} }
} }