diff --git a/MVMCoreUI/Atomic/Actions/ActionTopAlertModel.swift b/MVMCoreUI/Atomic/Actions/ActionTopAlertModel.swift index 62e496c2..1e93d98f 100644 --- a/MVMCoreUI/Atomic/Actions/ActionTopAlertModel.swift +++ b/MVMCoreUI/Atomic/Actions/ActionTopAlertModel.swift @@ -12,7 +12,8 @@ import Foundation public static var identifier: String = "topAlert" public var actionType: String = ActionTopAlertModel.identifier - public var pageType: String + public var pageType: String? + public var topNotification: TopNotificationModel? public var extraParameters: JSONValueDictionary? public var analyticsData: JSONValueDictionary? @@ -21,4 +22,10 @@ import Foundation self.extraParameters = extraParameters self.analyticsData = analyticsData } + + public init(topNotification: TopNotificationModel, _ extraParameters: JSONValueDictionary? = nil, _ analyticsData: JSONValueDictionary? = nil) { + self.topNotification = topNotification + self.extraParameters = extraParameters + self.analyticsData = analyticsData + } } diff --git a/MVMCoreUI/OtherHandlers/MVMCoreUIActionHandler.m b/MVMCoreUI/OtherHandlers/MVMCoreUIActionHandler.m index 14888ed1..5ca372cd 100644 --- a/MVMCoreUI/OtherHandlers/MVMCoreUIActionHandler.m +++ b/MVMCoreUI/OtherHandlers/MVMCoreUIActionHandler.m @@ -81,18 +81,28 @@ - (void)topAlertAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { // Perform a top alert. - NSString *pageTypeForTopAlert = [actionInformation stringForKey:KeyPageType]; - [[MVMCoreCache sharedCache] fetchJSONForPageType:pageTypeForTopAlert queue:nil waitUntilFinished:YES completionHandler:^(NSDictionary * _Nullable jsonDictionary) { - - NSDictionary *responseInfo = [jsonDictionary dict:KeyResponseInfo]; - if (responseInfo) { - MVMCoreAlertObject *alertObject = [MVMCoreAlertObject alertObjectForPageType:pageTypeForTopAlert responseInfo:responseInfo additionalData:additionalData delegateObject:delegateObject]; - if ([delegateObject.actionDelegate respondsToSelector:@selector(willShowTopAlertWithAlertObject:alertJson:)]) { - [((id )delegateObject.actionDelegate) willShowTopAlertWithAlertObject:alertObject alertJson:actionInformation]; + NSString *pageTypeForTopAlert = [actionInformation string:KeyPageType]; + if (pageTypeForTopAlert) { + [[MVMCoreCache sharedCache] fetchJSONForPageType:pageTypeForTopAlert queue:nil waitUntilFinished:YES completionHandler:^(NSDictionary * _Nullable jsonDictionary) { + + NSDictionary *responseInfo = [jsonDictionary dict:KeyResponseInfo]; + if (responseInfo) { + MVMCoreAlertObject *alertObject = [MVMCoreAlertObject alertObjectForPageType:pageTypeForTopAlert responseInfo:responseInfo additionalData:additionalData delegateObject:delegateObject]; + if ([delegateObject.actionDelegate respondsToSelector:@selector(willShowTopAlertWithAlertObject:alertJson:)]) { + [((id )delegateObject.actionDelegate) willShowTopAlertWithAlertObject:alertObject alertJson:actionInformation]; + } + [alertObject showAlert]; } - [alertObject showAlert]; + }]; + } else { + NSDictionary *topNotification = [actionInformation dict:@"topNotification"]; + if (topNotification) { + [[MVMCoreUITopAlertView sharedGlobal] showTopAlertWith:topNotification]; + } else { + MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"%@_%@",NSStringFromClass([delegateObject.actionDelegate class]),KeyActionTypeTopAlert]]; + [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegateObject:delegateObject]; } - }]; + } } - (void)collapseNotificationAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { @@ -152,17 +162,27 @@ - (void)topAlertAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { // Perform a top alert. NSString *pageTypeForTopAlert = [actionInformation stringForKey:KeyPageType]; - [[MVMCoreCache sharedCache] fetchJSONForPageType:pageTypeForTopAlert queue:nil waitUntilFinished:YES completionHandler:^(NSDictionary * _Nullable jsonDictionary) { - - NSDictionary *responseInfo = [jsonDictionary dict:KeyResponseInfo]; - if (responseInfo) { - MVMCoreAlertObject *alertObject = [MVMCoreAlertObject alertObjectForPageType:pageTypeForTopAlert responseInfo:responseInfo additionalData:additionalData actionDelegate:delegate]; - if ([delegate respondsToSelector:@selector(willShowTopAlertWithAlertObject:alertJson:)]) { - alertObject = [((id )delegate) willShowTopAlertWithAlertObject:alertObject alertJson:jsonDictionary]; + if (pageTypeForTopAlert) { + [[MVMCoreCache sharedCache] fetchJSONForPageType:pageTypeForTopAlert queue:nil waitUntilFinished:YES completionHandler:^(NSDictionary * _Nullable jsonDictionary) { + + NSDictionary *responseInfo = [jsonDictionary dict:KeyResponseInfo]; + if (responseInfo) { + MVMCoreAlertObject *alertObject = [MVMCoreAlertObject alertObjectForPageType:pageTypeForTopAlert responseInfo:responseInfo additionalData:additionalData actionDelegate:delegate]; + if ([delegate respondsToSelector:@selector(willShowTopAlertWithAlertObject:alertJson:)]) { + alertObject = [((id )delegate) willShowTopAlertWithAlertObject:alertObject alertJson:jsonDictionary]; + } + [alertObject showAlert]; } - [alertObject showAlert]; + }]; + } else { + NSDictionary *topNotification = [actionInformation dict:@"topNotification"]; + if (topNotification) { + [[MVMCoreUITopAlertView sharedGlobal] showTopAlertWith:topNotification]; + } else { + MVMCoreErrorObject *error = [[MVMCoreErrorObject alloc] initWithTitle:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorTitle] message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"%@_%@",NSStringFromClass([delegate class]),KeyActionTypeTopAlert]]; + [self handleActionError:error actionInformation:actionInformation additionalData:additionalData delegate:delegate]; } - }]; + } } - (void)collapseNotificationAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegate:(nullable NSObject *)delegate { diff --git a/MVMCoreUI/TopAlert/MVMCoreUITopAlertView+Extension.swift b/MVMCoreUI/TopAlert/MVMCoreUITopAlertView+Extension.swift index f179fb11..107a3596 100644 --- a/MVMCoreUI/TopAlert/MVMCoreUITopAlertView+Extension.swift +++ b/MVMCoreUI/TopAlert/MVMCoreUITopAlertView+Extension.swift @@ -61,6 +61,12 @@ public extension MVMCoreUITopAlertView { MVMCoreAlertHandler.shared()?.add(operation) } + /// Shows the top alert with the json. + @objc func showTopAlert(with json: [AnyHashable: Any]) { + guard let model = decodeTopNotification(with: json, delegateObject: getDelegateObject()) else { return } + showTopAlert(with: model) + } + /// Checks for existing top alert object of same type and updates it. Only happens for molecular top alerts. Returns true if we updated. private func checkAndUpdateExisting(with topAlertObject: MVMCoreTopAlertObject) -> Bool { guard let queue = MVMCoreAlertHandler.shared()?.topAlertQueue.operations else { return false }