diff --git a/MVMCore/MVMCore.xcodeproj/project.pbxproj b/MVMCore/MVMCore.xcodeproj/project.pbxproj index fc17f77..e8ce53e 100644 --- a/MVMCore/MVMCore.xcodeproj/project.pbxproj +++ b/MVMCore/MVMCore.xcodeproj/project.pbxproj @@ -135,6 +135,9 @@ AFFCFA671FCCC0D700FD0730 /* MVMCoreLoadingOverlayHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = AFFCFA631FCCC0D600FD0730 /* MVMCoreLoadingOverlayHandler.m */; }; AFFCFA681FCCC0D700FD0730 /* MVMCoreLoadingViewControllerProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = AFFCFA641FCCC0D600FD0730 /* MVMCoreLoadingViewControllerProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; BB780ADF250F8C890030BD2F /* ActionNoopModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB780ADE250F8C890030BD2F /* ActionNoopModel.swift */; }; + D27073B725BB45C4001C7246 /* ActionActionsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D27073B625BB45C4001C7246 /* ActionActionsModel.swift */; }; + D27073CD25BB4CEF001C7246 /* MVMCoreActionHandler+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D27073CC25BB4CEF001C7246 /* MVMCoreActionHandler+Extension.swift */; }; + D27073D125BB844B001C7246 /* MVMCoreActionDelegateProtocol+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D27073D025BB844B001C7246 /* MVMCoreActionDelegateProtocol+Extension.swift */; }; D282AAB62240085300C46919 /* MVMCoreGetterUtility+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D282AAB52240085300C46919 /* MVMCoreGetterUtility+Extension.swift */; }; D282AAB82240342D00C46919 /* NSNumber+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D282AAB72240342D00C46919 /* NSNumber+Extension.swift */; }; D2DEDCB723C63F3B00C44CC4 /* Clamping.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2DEDCB623C63F3B00C44CC4 /* Clamping.swift */; }; @@ -266,6 +269,9 @@ AFFCFA631FCCC0D600FD0730 /* MVMCoreLoadingOverlayHandler.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MVMCoreLoadingOverlayHandler.m; sourceTree = ""; }; AFFCFA641FCCC0D600FD0730 /* MVMCoreLoadingViewControllerProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MVMCoreLoadingViewControllerProtocol.h; sourceTree = ""; }; BB780ADE250F8C890030BD2F /* ActionNoopModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionNoopModel.swift; sourceTree = ""; }; + D27073B625BB45C4001C7246 /* ActionActionsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionActionsModel.swift; sourceTree = ""; }; + D27073CC25BB4CEF001C7246 /* MVMCoreActionHandler+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MVMCoreActionHandler+Extension.swift"; sourceTree = ""; }; + D27073D025BB844B001C7246 /* MVMCoreActionDelegateProtocol+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MVMCoreActionDelegateProtocol+Extension.swift"; sourceTree = ""; }; D282AAB52240085300C46919 /* MVMCoreGetterUtility+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MVMCoreGetterUtility+Extension.swift"; sourceTree = ""; }; D282AAB72240342D00C46919 /* NSNumber+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSNumber+Extension.swift"; sourceTree = ""; }; D2DEDCB623C63F3B00C44CC4 /* Clamping.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Clamping.swift; sourceTree = ""; }; @@ -416,6 +422,7 @@ 94C014D424211AF0005811A9 /* ActionCancelModel.swift */, 94C014D824212360005811A9 /* ActionSettingModel.swift */, BB780ADE250F8C890030BD2F /* ActionNoopModel.swift */, + D27073B625BB45C4001C7246 /* ActionActionsModel.swift */, ); path = ActionType; sourceTree = ""; @@ -558,8 +565,10 @@ isa = PBXGroup; children = ( AFBB96B51FBA3CEC0008D868 /* MVMCoreActionDelegateProtocol.h */, + D27073D025BB844B001C7246 /* MVMCoreActionDelegateProtocol+Extension.swift */, AFBB96B61FBA3CEC0008D868 /* MVMCoreActionHandler.h */, AFBB96B71FBA3CEC0008D868 /* MVMCoreActionHandler.m */, + D27073CC25BB4CEF001C7246 /* MVMCoreActionHandler+Extension.swift */, ); path = ActionHandling; sourceTree = ""; @@ -813,6 +822,7 @@ D2DEDCB923C6400600C44CC4 /* UnitInterval.swift in Sources */, 94C014D3242119E6005811A9 /* ActionPreviousSubmitModel.swift in Sources */, 8876D5E91FB50AB000EB2E3D /* NSArray+MFConvenience.m in Sources */, + D27073B725BB45C4001C7246 /* ActionActionsModel.swift in Sources */, 946EE1B2237B5F260036751F /* JSONValue.swift in Sources */, AFBB96971FBA3A9A0008D868 /* MVMCorePresentViewControllerOperation.m in Sources */, AF43A5781FBA5B7C008E9347 /* MVMCoreJSONConstants.m in Sources */, @@ -826,6 +836,7 @@ AF43A57C1FBA5E6A008E9347 /* MVMCoreHardcodedStringsConstants.m in Sources */, 0AFF597A23FC6E60005C24E8 /* ActionShareModel.swift in Sources */, AFEEE81F1FCDF3CA00B5EDD0 /* MVMCoreLoggingHandler.m in Sources */, + D27073CD25BB4CEF001C7246 /* MVMCoreActionHandler+Extension.swift in Sources */, 01F2A05223A8325100D954D8 /* ModelMapping.swift in Sources */, 8876D5F51FB50AB000EB2E3D /* UILabel+MFCustom.m in Sources */, AFBB96B31FBA3B590008D868 /* MVMCoreGetterUtility.m in Sources */, @@ -839,6 +850,7 @@ AF43A70A1FC4F415008E9347 /* MVMCoreCache.m in Sources */, AF43A6FF1FBE3252008E9347 /* Reachability.m in Sources */, 01C851D123CF97FE0021F976 /* ActionBackModel.swift in Sources */, + D27073D125BB844B001C7246 /* MVMCoreActionDelegateProtocol+Extension.swift in Sources */, AFBB96921FBA3A9A0008D868 /* MVMCoreNavigationOperation.m in Sources */, AFBB96611FBA3A570008D868 /* MVMCoreLoadObject.m in Sources */, 946EE1B4237B619D0036751F /* Encoder.swift in Sources */, diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionDelegateProtocol+Extension.swift b/MVMCore/MVMCore/ActionHandling/MVMCoreActionDelegateProtocol+Extension.swift new file mode 100644 index 0000000..5a53a0b --- /dev/null +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionDelegateProtocol+Extension.swift @@ -0,0 +1,16 @@ +// +// MVMCoreActionDelegateProtocolExtension.swift +// MVMCore +// +// Created by Scott Pfeil on 1/22/21. +// Copyright © 2021 myverizon. All rights reserved. +// + +import Foundation + +public extension MVMCoreActionDelegateProtocol { + // Handles any action errors. + func handleAction(error: MVMCoreErrorObject, action: ActionModelProtocol, additionalData: [AnyHashable: Any]?) { + MVMCoreActionHandler.shared()?.defaultHandleActionError(error, additionalData: additionalData) + } +} diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift new file mode 100644 index 0000000..1df7687 --- /dev/null +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler+Extension.swift @@ -0,0 +1,39 @@ +// +// MVMCoreActionHandler+Extension.swift +// MVMCore +// +// Created by Scott Pfeil on 1/22/21. +// Copyright © 2021 myverizon. All rights reserved. +// + +import Foundation + +public extension MVMCoreActionHandler { + + /// Converts the action to json for old action handler to handle. + func convertActionToJSON(_ model: ActionModelProtocol, delegateObject: DelegateObject?) -> [AnyHashable: Any]? { + do { + let data = try model.encode() + guard let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [AnyHashable: Any] else { + throw ModelRegistry.Error.decoderError + } + return json + } catch { + let errorObject = MVMCoreErrorObject.createErrorObject(for: error, location: "")! + delegateObject?.actionDelegate?.handleAction(error: errorObject, action: model, additionalData: model.extraParameters) ?? MVMCoreActionHandler.shared()?.defaultHandleActionError(errorObject, additionalData: model.extraParameters) + return nil + } + } + + /// Start action on current thread. + func syncHandleAction(with model: ActionModelProtocol, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) { + guard let json = convertActionToJSON(model, delegateObject: delegateObject) else { return } + synchronouslyHandleAction(model.actionType, actionInformation: json, additionalData: additionalData, delegateObject: delegateObject) + } + + /// Start action on dispatched background thread. + func asyncHandleAction(with model: ActionModelProtocol, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) { + guard let json = convertActionToJSON(model, delegateObject: delegateObject) else { return } + handleAction(model.actionType, actionInformation: json, additionalData: additionalData, delegateObject: delegateObject) + } +} diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h index c677e53..26103f4 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h @@ -25,9 +25,12 @@ extern NSString * _Nonnull const KeyActionTypeOpen; // Convenience function for handling actions. This will pull action and pageInfo out of the dictionary and call handleAction: actionInformation: with those values - (void)handleActionWithDictionary:(nullable NSDictionary *)dictionary additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject; -// Handles actions. Used by server driven user actions.. +// Asynchronously handles action (dispatches and calls below function). Used by server driven user actions.. - (void)handleAction:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject; +// Synchronously handles action. Used by server driven user actions.. +- (void)synchronouslyHandleAction:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject; + #pragma mark - Actions // by default, returns the original RequestParameter that passed in. Can be overriden for some generic updates to the RequestParameter before handle open page action gets called. - (void)updateRequestParametersBeforeHandleOpenPageAction:(nonnull MVMCoreRequestParameters *)requestParameters callBack:(void (^_Nonnull)(MVMCoreRequestParameters * _Nonnull requestParameters))callback; @@ -59,6 +62,9 @@ extern NSString * _Nonnull const KeyActionTypeOpen; // Goes to settings app - (void)settingsAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject; +/// Performs multiple actions +- (void)actions:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject; + // Subclass this to handle other custom actions. Return YES if handled, and NO if not. - (BOOL)handleOtherActions:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject; @@ -101,61 +107,4 @@ extern NSString * _Nonnull const KeyActionTypeOpen; // Convenience function for handling actions. This will pull action and pageInfo out of the dictionary and call handleAction: actionInformation: with those values - (void)handleActionWithDictionary:(nullable NSDictionary *)dictionary additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; -// Handles actions. Used by server driven user actions.. -- (void)handleAction:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Logs the action. Currently is not action information driven... depends on delegate. -- (void)logAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Tries to open a page -- (void)openPageAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// restarts the app -- (void)restartAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Goes back -- (void)backAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Makes a phone call -- (void)callAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Makes the previous request, needs the delegate for this -- (void)previousSubmitAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Redirects to another experience -- (void)redirectAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Cancels (like in a popup) -- (void)cancelAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Goes to settings app -- (void)settingsAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Subclass this to handle other custom actions. Return YES if handled, and NO if not. -- (BOOL)handleOtherActions:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Last chance to handle unknown actions before throwing an error -- (void)unknownAction:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Handles action errors. -- (void)handleActionError:(nullable MVMCoreErrorObject *)error actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Links away to app or browser -- (void)linkAwayAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Can subclass to add to urls if needed at global level (delegate is also called) -- (void)prepareLinkAwayWithURL:(nullable NSURL *)url appURL:(nullable NSURL *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Opens the url -- (void)openURL:(nullable NSURL *)url appURL:(nullable NSURL *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// opens the url in a webview. -- (void)openURLInWebView:(nullable NSURL *)url actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - -// Sends the request to the load handler. -+ (void)defaultHandleOpenPageForRequestParameters:(nonnull MVMCoreRequestParameters *)requestParameters additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject*)delegate __deprecated; - -// By default, throws an error, calling defaultHandleActionError. -+ (void)defaultHandleUnknownActionType:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate __deprecated; - @end diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m index ee539d0..4c72157 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m @@ -44,50 +44,52 @@ NSString * const KeyActionTypeOpen = @"openPage"; } - (void)handleAction:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - - // Logs the action. - [self logAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; - - if ([actionType isEqualToString:KeyActionTypeOpen]) { - [self openPageAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; - - } else if ([actionType isEqualToString:KeyActionTypeLinkAway]) { - [self linkAwayAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; - - } else if ([actionType isEqualToString:KeyActionTypeRestart]) { - [self restartAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; - - } else if ([actionType isEqualToString:KeyActionTypeBack]) { - [self backAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; - - } else if ([actionType isEqualToString:KeyActionTypeCall]) { - [self callAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; - - } else if ([actionType isEqualToString:KeyActionTypeShare]) { - [self shareAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; - - } else if ([actionType isEqualToString:KeyActionTypePreviousSubmit]) { - [self previousSubmitAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; - - } else if ([actionType isEqualToString:KeyActionTypeRedirect]) { - [self redirectAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; - - } else if ([actionType isEqualToString:KeyActionTypeCancel]) { - [self cancelAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; - - } else if ([actionType isEqualToString:KeyActionTypeSettings]) { - [self settingsAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; - - } else if ([actionType isEqualToString:KeyActionTypeNoop]){ - } else if (![self handleOtherActions:actionType actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject]) { - // not a known action type. - [self unknownAction:actionType actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject]; - } + [self synchronouslyHandleAction:actionType actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject]; }); } +- (void)synchronouslyHandleAction:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { + // Logs the action. + [self logAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + if ([actionType isEqualToString:KeyActionTypeOpen]) { + [self openPageAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + } else if ([actionType isEqualToString:KeyActionTypeLinkAway]) { + [self linkAwayAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + } else if ([actionType isEqualToString:KeyActionTypeRestart]) { + [self restartAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + } else if ([actionType isEqualToString:KeyActionTypeBack]) { + [self backAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + } else if ([actionType isEqualToString:KeyActionTypeCall]) { + [self callAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + } else if ([actionType isEqualToString:KeyActionTypeShare]) { + [self shareAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + } else if ([actionType isEqualToString:KeyActionTypePreviousSubmit]) { + [self previousSubmitAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + } else if ([actionType isEqualToString:KeyActionTypeRedirect]) { + [self redirectAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + } else if ([actionType isEqualToString:KeyActionTypeCancel]) { + [self cancelAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + } else if ([actionType isEqualToString:KeyActionTypeSettings]) { + [self settingsAction:actionInformation additionalData:additionalData delegateObject:delegateObject]; + + } else if ([actionType isEqualToString:KeyActionTypeNoop]){ + } else if (![self handleOtherActions:actionType actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject]) { + // not a known action type. + [self unknownAction:actionType actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject]; + } +} + #pragma mark - Actions - (void)updateRequestParametersBeforeHandleOpenPageAction:(nonnull MVMCoreRequestParameters *)requestParameters callBack:(void (^_Nonnull)(MVMCoreRequestParameters * _Nonnull requestParameters))callback { @@ -241,6 +243,19 @@ NSString * const KeyActionTypeOpen = @"openPage"; [MVMCoreActionUtility linkAway:UIApplicationOpenSettingsURLString appURLString:nil]; } +- (void)actions:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { + NSArray *actions = [actionInformation array:@"actions"]; + BOOL concurrent = [actionInformation boolForKey:@"concurrent"]; + for (NSDictionary *action in actions) { + NSString *actionType = [action string:KeyActionType]; + if (concurrent) { + [self handleAction:actionType actionInformation:action additionalData:additionalData delegateObject:delegateObject]; + } else { + [self synchronouslyHandleAction:actionType actionInformation:action additionalData:additionalData delegateObject:delegateObject]; + } + } +} + - (BOOL)handleOtherActions:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { return NO; } @@ -363,248 +378,11 @@ NSString * const KeyActionTypeOpen = @"openPage"; - (void)handleActionWithDictionary:(nullable NSDictionary *)dictionary additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - NSString *action = [dictionary stringForKey:KeyActionType]; - [self handleAction:action actionInformation:dictionary additionalData:additionalData delegate:delegate]; -} - -- (void)handleAction:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - - // Logs the action. - [self logAction:actionInformation additionalData:additionalData delegate:delegate]; - - if ([actionType isEqualToString:KeyActionTypeOpen]) { - [self openPageAction:actionInformation additionalData:additionalData delegate:delegate]; - } else if ([actionType isEqualToString:KeyActionTypeLinkAway]) { - [self linkAwayAction:actionInformation additionalData:additionalData delegate:delegate]; - } else if ([actionType isEqualToString:KeyActionTypeRestart]) { - [self restartAction:actionInformation additionalData:additionalData delegate:delegate]; - } else if ([actionType isEqualToString:KeyActionTypeBack]) { - [self backAction:actionInformation additionalData:additionalData delegate:delegate]; - } else if ([actionType isEqualToString:KeyActionTypeCall]) { - [self callAction:actionInformation additionalData:additionalData delegate:delegate]; - } else if ([actionType isEqualToString:KeyActionTypePreviousSubmit]) { - [self previousSubmitAction:actionInformation additionalData:additionalData delegate:delegate]; - } else if ([actionType isEqualToString:KeyActionTypeRedirect]) { - [self redirectAction:actionInformation additionalData:additionalData delegate:delegate]; - } else if ([actionType isEqualToString:KeyActionTypeCancel]) { - [self cancelAction:actionInformation additionalData:additionalData delegate:delegate]; - } else if ([actionType isEqualToString:KeyActionTypeSettings]) { - [self settingsAction:actionInformation additionalData:additionalData delegate:delegate]; - } else if (![self handleOtherActions:actionType actionInformation:actionInformation additionalData:additionalData delegate:delegate]) { - // not a known action type. - [self unknownAction:actionType actionInformation:actionInformation additionalData:additionalData delegate:delegate]; - } - }); -} - -- (void)logAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - if ([delegate respondsToSelector:@selector(logActionWithActionInformation:additionalData:)]) { - [delegate logActionWithActionInformation:actionInformation additionalData:additionalData]; - } else { - [MVMCoreActionHandler defaultLogAction:actionInformation additionalData:additionalData delegate:delegate]; - } -} - -- (void)openPageAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - - // Loads the given page type. - NSString *pageType = [actionInformation stringForKey:KeyPageType]; - if (pageType.length > 0) { - MVMCoreRequestParameters *requestParameters = [[MVMCoreRequestParameters alloc] initWithActionMap:actionInformation]; - - [self updateRequestParametersBeforeHandleOpenPageAction:requestParameters callBack:^(MVMCoreRequestParameters * _Nonnull requestParameters) { - if ([delegate respondsToSelector:@selector(handleOpenPageForRequestParameters:actionInformation:additionalData:)]) { - [delegate handleOpenPageForRequestParameters:requestParameters actionInformation:actionInformation additionalData:additionalData]; - } else { - [MVMCoreActionHandler defaultHandleOpenPageForRequestParameters:requestParameters additionalData:additionalData delegate:delegate]; - } - }]; - } else { - - // No page type to load, show error. - MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeNoPageType domain:ErrorDomainNative location:[NSString stringWithFormat:@"%@_%@",NSStringFromClass([delegate class]),KeyActionTypeOpen]]; - [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegate:delegate]; - } -} - -- (void)restartAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - - // Invalidates the session before restarting. - [[MVMCoreSessionTimeHandler sharedSessionHandler] invalidateSession:^(MVMCoreErrorObject * _Nullable error) { - - // Restarts the app (forcing any passed in page types). - if (error.code != NSURLErrorCancelled) { - - if (error) { - - // Error invalidating. - [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegate:delegate]; - } else { - - // Restart the application with the page type. - NSString *pageType = [actionInformation string:KeyPageType]; - NSDictionary *parameters = [actionInformation dict:KeyExtraParameters]; - [[MVMCoreSessionObject sharedGlobal] restartSessionWithPageType:pageType parameters:parameters clearAllVariables:YES]; - } - } - }]; -} - -- (void)backAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - // Go back. - if ([delegate respondsToSelector:@selector(handleBackAction:additionalData:)]) { - [delegate handleBackAction:actionInformation additionalData:additionalData]; - } else { - [[MVMCoreNavigationHandler sharedNavigationHandler] removeCurrentViewController]; - } -} - -- (void)callAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - // Call - NSString *callNumber = [actionInformation stringForKey:KeyCallNumber]; - [MVMCoreActionUtility linkAway:[@"tel://" stringByAppendingString:callNumber] appURLString:nil]; -} - -- (void)previousSubmitAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - // Perform the previous submission. - __weak typeof(self) weakSelf = self; - if ([delegate respondsToSelector:@selector(prepareRequestForPreviousSubmission:additionalData:submit:)]) { - [delegate prepareRequestForPreviousSubmission:actionInformation additionalData:additionalData submit:^(MVMCoreRequestParameters * _Nonnull requestParameters, NSDictionary * _Nullable dataForPage) { - - [weakSelf updateRequestParametersBeforeHandleOpenPageAction:requestParameters callBack:^(MVMCoreRequestParameters * _Nonnull requestParameters) { - // Give the delegate a chance to alter the request parameters - if ([delegate respondsToSelector:@selector(handleOpenPageForRequestParameters:actionInformation:additionalData:)]) { - [delegate handleOpenPageForRequestParameters:requestParameters actionInformation:actionInformation additionalData:dataForPage]; - } else { - [MVMCoreActionHandler defaultHandleOpenPageForRequestParameters:requestParameters additionalData:additionalData delegate:delegate]; - } - }]; - }]; - } -} - -- (void)redirectAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - // Have delegate redirect. - [[MVMCoreSessionObject sharedGlobal] redirectWithInfo:actionInformation]; -} - -- (void)cancelAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - if ([delegate respondsToSelector:@selector(handleCancel:additionalData:)]) { - [delegate handleCancel:actionInformation additionalData:additionalData]; - } -} - -- (void)settingsAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - // Opens the settings. - [MVMCoreActionUtility linkAway:UIApplicationOpenSettingsURLString appURLString:nil]; -} - -- (BOOL)handleOtherActions:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - return NO; -} - -- (void)unknownAction:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - if ([delegate respondsToSelector:@selector(handleUnknownActionType:actionInformation:additionalData:)]) { - [delegate handleUnknownActionType:actionType actionInformation:actionInformation additionalData:additionalData]; - } else { - [MVMCoreActionHandler defaultHandleUnknownActionType:actionType actionInformation:actionInformation additionalData:additionalData delegate:delegate]; - } -} - -- (void)handleActionError:(nullable MVMCoreErrorObject *)error actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - if (error) { - if ([delegate respondsToSelector:@selector(handleActionError:additionalData:)]) { - [delegate handleActionError:error additionalData:additionalData]; - } else { - [self defaultHandleActionError:error additionalData:additionalData]; - } - } -} - -- (void)linkAwayAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - - // Gets the app url - NSURL *appURL = nil; - NSString *appURLString = [actionInformation string:KeyLinkAwayAppURL]; - if (appURLString.length > 0) { - appURL = [NSURL URLWithString:appURLString]; - } - - // Gets the browser url - NSURL *otherURL = nil; - NSString *otherURLString = [actionInformation string:KeyLinkAwayURL]; - if (otherURLString.length > 0) { - otherURL = [NSURL URLWithString:otherURLString]; - } - - // Provide the URL and App URL to be modified if needed by a subclass or delegate. - [self prepareLinkAwayWithURL:otherURL appURL:appURL actionInformation:actionInformation additionalData:additionalData delegate:delegate]; -} - -- (void)prepareLinkAwayWithURL:(nullable NSURL *)url appURL:(nullable NSURL *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - - void(^openURL)(NSURL *, NSURL *, NSDictionary *, NSDictionary *) = ^(NSURL *appURL, NSURL *URL, NSDictionary *actionInformation, NSDictionary *additionalData) { - [self openURL:url appURL:appURL actionInformation:actionInformation additionalData:additionalData delegate:delegate]; - }; - - // Allow delegate to modify before opening the url. - if ([delegate respondsToSelector:@selector(shouldLinkAwayWithURL:appURL:actionInformation:additionalData:linkAwayBlock:)]) { - [delegate shouldLinkAwayWithURL:url appURL:appURL actionInformation:actionInformation additionalData:additionalData linkAwayBlock:openURL]; - } else { - openURL(appURL,url,actionInformation,additionalData); - } -} - -- (void)openURL:(nullable NSURL *)url appURL:(nullable NSURL *)appURL actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - - [MVMCoreDispatchUtility performBlockOnMainThread:^{ - - // First try to open the application. - if (appURL && [[UIApplication sharedApplication] canOpenURL:appURL]) { - [[UIApplication sharedApplication] openURL:appURL options:@{} completionHandler:NULL]; - } else if (url && [[UIApplication sharedApplication] canOpenURL:url]) { - - // Check if we should load in webview - BOOL openInWebview = [actionInformation boolForKey:@"openInWebview"]; - if (openInWebview) { - [self openURLInWebView:url actionInformation:actionInformation additionalData:additionalData delegate:delegate]; - } else { - [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:NULL]; - } - } else { - [MVMCoreDispatchUtility performBlockInBackground:^{ - // Cannot linkaway, show error. - MVMCoreErrorObject *error = error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeLinkawayFailed domain:ErrorDomainNative location:[NSString stringWithFormat:@"%@_%@",NSStringFromClass([delegate class]),KeyActionTypeLinkAway]]; - [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegate:delegate]; - }]; - } - }]; -} - -- (void)openURLInWebView:(nullable NSURL *)url actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - - // Presents standard webview. - SFSafariViewController *safariViewController = [[SFSafariViewController alloc] initWithURL:url]; - //safariViewController.delegate = self; - safariViewController.preferredBarTintColor = [UIColor whiteColor]; - safariViewController.preferredControlTintColor = [UIColor blackColor]; - [[MVMCoreNavigationHandler sharedNavigationHandler] presentViewController:safariViewController animated:YES]; -} - -+ (void)defaultLogAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate{ - // Currently no default log action but this will eventually be server driven. -} - -+ (void)defaultHandleOpenPageForRequestParameters:(nonnull MVMCoreRequestParameters *)requestParameters additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject*)delegate { - [[MVMCoreLoadHandler sharedGlobal] loadRequest:requestParameters dataForPage:additionalData delegate:delegate]; -} - -+ (void)defaultHandleUnknownActionType:(nullable NSString *)actionType actionInformation:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { - - MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeUnknownActionType domain:ErrorDomainNative location:[NSString stringWithFormat:@"%@Requests_%@",NSStringFromClass([delegate class]),actionType]]; - [[self sharedActionHandler] defaultHandleActionError:error additionalData:additionalData]; + DelegateObject *delegateObject = [[DelegateObject alloc] init]; + delegateObject.actionDelegate = delegate; + delegateObject.presentationDelegate = delegate; + delegateObject.loadDelegate = delegate; + [self handleActionWithDictionary:dictionary additionalData:additionalData delegateObject:delegateObject]; } @end diff --git a/MVMCore/MVMCore/Models/ActionType/ActionActionsModel.swift b/MVMCore/MVMCore/Models/ActionType/ActionActionsModel.swift new file mode 100644 index 0000000..0638f3e --- /dev/null +++ b/MVMCore/MVMCore/Models/ActionType/ActionActionsModel.swift @@ -0,0 +1,67 @@ +// +// ActionActionsModel.swift +// MVMCore +// +// Created by Scott Pfeil on 1/22/21. +// Copyright © 2021 myverizon. All rights reserved. +// + +import Foundation + +@objcMembers open class ActionActionsModel: ActionModelProtocol { + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + + public static var identifier: String = "actions" + public var actionType: String = ActionActionsModel.identifier + public var actions: [ActionModelProtocol] + public var concurrent = true + public var extraParameters: JSONValueDictionary? + public var analyticsData: JSONValueDictionary? + + //-------------------------------------------------- + // MARK: - Initialzier + //-------------------------------------------------- + + public init(actions: [ActionModelProtocol], extraParameters: JSONValueDictionary? = nil, analyticsData: JSONValueDictionary? = nil) { + self.actions = actions + self.extraParameters = extraParameters + self.analyticsData = analyticsData + } + + //-------------------------------------------------- + // MARK: - Keys + //-------------------------------------------------- + + private enum CodingKeys: String, CodingKey { + case actionType + case actions + case concurrent + case extraParameters + case analyticsData + } + + //-------------------------------------------------- + // MARK: - Codec + //-------------------------------------------------- + + required public init(from decoder: Decoder) throws { + let typeContainer = try decoder.container(keyedBy: CodingKeys.self) + actions = try typeContainer.decodeModels(codingKey: .actions) + if let concurrent = try typeContainer.decodeIfPresent(Bool.self, forKey: .concurrent) { + self.concurrent = concurrent + } + extraParameters = try typeContainer.decodeIfPresent(JSONValueDictionary.self, forKey: .extraParameters) + analyticsData = try typeContainer.decodeIfPresent(JSONValueDictionary.self, forKey: .analyticsData) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(actionType, forKey: .actionType) + try container.encodeModels(actions, forKey: .actions) + try container.encode(concurrent, forKey: .concurrent) + try container.encodeIfPresent(extraParameters, forKey: .extraParameters) + try container.encodeIfPresent(analyticsData, forKey: .analyticsData) + } +}