From 2830e044c0604861390321555da3ddef7f1e4d64 Mon Sep 17 00:00:00 2001 From: Mayur Nilwant Date: Thu, 15 Aug 2024 18:07:34 -0400 Subject: [PATCH] Added method to NavigationHandler which handles removing specific viewController object. Added previousActionCompletion block to Model. Called previousAction completion callback in ActionPreviousSubmitModel. --- .../ActionPreviousSubmitHandler.swift | 3 ++ .../ActionPreviousSubmitModel.swift | 28 ++++++++++++++++++- .../NavigationHandler.swift | 9 ++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/MVMCore/MVMCore/ActionHandling/ActionPreviousSubmitHandler.swift b/MVMCore/MVMCore/ActionHandling/ActionPreviousSubmitHandler.swift index a3bded2..c88feed 100644 --- a/MVMCore/MVMCore/ActionHandling/ActionPreviousSubmitHandler.swift +++ b/MVMCore/MVMCore/ActionHandling/ActionPreviousSubmitHandler.swift @@ -43,5 +43,8 @@ open class ActionPreviousSubmitHandler: MVMCoreJSONActionHandlerProtocol { } } } + if let _model = model as? ActionPreviousSubmitModel, let callBack = _model.completionHandler { + await callBack() + } } } diff --git a/MVMCore/MVMCore/ActionHandling/ActionPreviousSubmitModel.swift b/MVMCore/MVMCore/ActionHandling/ActionPreviousSubmitModel.swift index 489aaf7..a242986 100644 --- a/MVMCore/MVMCore/ActionHandling/ActionPreviousSubmitModel.swift +++ b/MVMCore/MVMCore/ActionHandling/ActionPreviousSubmitModel.swift @@ -11,11 +11,19 @@ public struct ActionPreviousSubmitModel: ActionModelProtocol { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- - + public typealias previousActionCompletion = (() async -> Void) public static var identifier: String = "previousSubmit" public var actionType: String = ActionPreviousSubmitModel.identifier public var extraParameters: JSONValueDictionary? public var analyticsData: JSONValueDictionary? + public var completionHandler : previousActionCompletion? + + private enum CodingKeys: String, CodingKey { + case actionType + case completionHandler + case extraParameters + case analyticsData + } //-------------------------------------------------- // MARK: - Initialzier @@ -26,6 +34,24 @@ public struct ActionPreviousSubmitModel: ActionModelProtocol { self.analyticsData = analyticsData } + public init(_ extraParameters: JSONValueDictionary? = nil, _ analyticsData: JSONValueDictionary? = nil, completionHandler handler: @escaping previousActionCompletion) { + self.init(extraParameters, analyticsData) + self.completionHandler = handler + } + + public init(from decoder: any Decoder) throws { + let typeContainer = try decoder.container(keyedBy: CodingKeys.self) + extraParameters = try typeContainer.decodeIfPresent(JSONValueDictionary.self, forKey: .extraParameters) + analyticsData = try typeContainer.decodeIfPresent(JSONValueDictionary.self, forKey: .analyticsData) + } + + public func encode(to encoder: any Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(actionType, forKey: .actionType) + try container.encodeIfPresent(extraParameters, forKey: .extraParameters) + try container.encodeIfPresent(analyticsData, forKey: .analyticsData) + } + // Default public func isEqual(to model: any ModelComparisonProtocol) -> Bool { guard let model = model as? Self else { return false } diff --git a/MVMCore/MVMCore/PresentationHandling/NavigationHandler.swift b/MVMCore/MVMCore/PresentationHandling/NavigationHandler.swift index ec27379..2d96b78 100644 --- a/MVMCore/MVMCore/PresentationHandling/NavigationHandler.swift +++ b/MVMCore/MVMCore/PresentationHandling/NavigationHandler.swift @@ -177,6 +177,10 @@ public class NavigationHandler { await navigate(with: .dismiss(viewController: presentedViewController), delegateObject: delegateObject) } + public func remove(viewController: UIViewController) async { + await self.navigationController?.removeViewController(viewController: viewController) + } + public func removeCurrentViewController(delegateObject: DelegateObject? = nil, animated: Bool = true) async { if let presentedViewController = await getTopMostPresentedViewController() { if let navigationController = (presentedViewController as? UINavigationController), @@ -217,6 +221,11 @@ public extension UINavigationController { func getViewControllers() -> [UIViewController] { NavigationHandler.shared().getViewControllers(for: self) } + + @objc @MainActor + func removeViewController(viewController: UIViewController) { + self.viewControllers.removeAll { $0 === viewController } + } } extension UIViewController {