ekazaev/route-composer

How get data from dismiss?

ArtSumin opened this issue · 5 comments

My secondVC have StepAssembly with

.adding(DismissalMethodProvidingContextTask(dismissalBlock: { (context, animated, completion) in
            UIViewController.router.commitNavigation(to: GeneralStep.custom(using: PresentingFinder()), with: contextNew, animated: animated, completion: completion)
        }))

how get this context in firstVC?
I am using this as(but i dont like this way):

static func secondDestination(with context: SecondContext, dismissContext: @escaping(CustomDismissContext) -> Void) -> Destination<SecondViewController, SecondContext> {}

into method call dismissContext:

            dismissContext(context) // here call closure
            UIViewController.router.commitNavigation(to: GeneralStep.custom(using: PresentingFinder()), with: contextt, animated: animated, completion: completions)
        }))

Please show true way for that case.

Hi @ArtSumin

I have the feeling that I do not understand the issue here. Can you give some more details? And what is contextNew there

Sorry, newContext misprint context is true. I want to know the correct way to get data from the presented view controller. An example: firstVC present modaly secondVC, secondVC have DismissalTargetContext, how i can read that context in firstVC aftersecondVCis complete? Sorry, my english is bad )) 🇷🇺

Ah do not worry about your English. It is an eternal process to improve it:)
So I hope I got you:

Basically you can try it yourself in the example app: Please replace ColorViewController with the next implementation:

class ColorViewController: UIViewController, DismissibleWithRuntimeStorage, ExampleAnalyticsSupport {

    typealias DismissalTargetContext = ProductContext

    let screenType = ExampleScreenTypes.color

    typealias ColorDisplayModel = String

    var colorHex: ColorDisplayModel? {
        didSet {
            if let colorHex = colorHex, isViewLoaded {
                self.view.backgroundColor = UIColor(hexString: colorHex)
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.accessibilityIdentifier = "colorViewController"
        title = "Color"
        if let colorHex = colorHex {
            view.backgroundColor = UIColor(hexString: colorHex)
        } else {
            view.backgroundColor = UIColor.white
        }

        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneTapped))
    }

    @objc func doneTapped() {
        dismissViewController(with: .ProductContext(productId: "006"), animated: true)
    }

}

and then in ExampleScreenConfiguration change this line:

            // Demonstrates ability to provide a dismissal method in the configuration using `DismissalMethodProvidingContextTask`
            UIViewController.router.commitNavigation(to: ProductConfiguration.productScreen, with: context, animated: animated, completion: completion)

A context here will have the value that you provided in @objc func doneTapped() { dismissViewController(with: .ProductContext(productId: "006"), animated: true) } so it will be passed to the product screen.

But this whole implementation is a shortcut. I would strongly advice to use it for some temporary purposes as it:

  1. Brings this implementation to the configuration and hides it there. You can break the logic just by changing the configuration.
  2. Relies on RouteComposer and It can not be properly decoupled for example if you will change your mind and decide to use another routing system.
  3. It is not very SOLID because in this situation The tail wags the dog :) But UIKit and SOLID are 2 different planets so it is the least of my concerns :)

I would recommend to implement some delegation/multiple delegation/NSNotification whatever as a long term solution if you want to pass the information to the presenting view controller

@ArtSumin Let me know if you have any other questions. Hope I understand your question correctly.

Thank you