rockbruno / RouterService

💉Type-safe Navigation/Dependency Injection Framework for Swift
MIT License
333 stars 27 forks source link

How to implement delegate pattern? #6

Closed iRILLLL closed 4 years ago

iRILLLL commented 4 years ago

Hi there, I'm still have no idea how to implement delegate pattern the right way using this library

my MainViewController (rootViewController) present OnboardingViewController

how do I set delegate on OnboardingViewController from MainViewController

final class OnboardingViewController: UIViewController { weak var delegate: OnboardingViewControllerDelegate? }

here's my example project

if i'm using this method, that's mean I have to make OnboardingViewController public. or do you have any recommendation or workaround?

Thank you very much

rockbruno commented 4 years ago

If you're trying to delegate pushes, what you can do is implement your own presentation style that contains a delegate or enhancing this lib's default ones to do so: https://github.com/rockbruno/RouterService/blob/master/Sources/RouterServiceInterface/PresentationStyle.swift#L14

rockbruno commented 4 years ago

Delegating directly from the view controller might not a good idea because RouterService is meant to be used with backend-driven flows, so there would be no guarantee that you'd be pushing what you're expecting.

iRILLLL commented 4 years ago

If you're trying to delegate pushes, what you can do is implement your own presentation style that contains a delegate or enhancing this lib's default ones to do so: https://github.com/rockbruno/RouterService/blob/master/Sources/RouterServiceInterface/PresentationStyle.swift#L14

hi, thanks for your time to reply.

I've made changes like this. what do you think? is it okay?

public typealias ONBOARDING_SKIP_CALLBACK = (UIViewController) -> Void

public final class OnboardingPresentationStyle: PresentationStyle {

    private let callback: ONBOARDING_SKIP_CALLBACK

    public init(callback: @escaping ONBOARDING_SKIP_CALLBACK) {
        self.callback = callback
    }

    public func present(viewController: UIViewController, fromViewController: UIViewController, animated: Bool) {
        if let vc = viewController as? OnboardingViewController {
            vc.skipCallback = self.callback
        }
        fromViewController.present(
            viewController,
            animated: true)
    }
}
iRILLLL commented 4 years ago

Delegating directly from the view controller might not a good idea because RouterService is meant to be used with backend-driven flows, so there would be no guarantee that you'd be pushing what you're expecting.

I see, how do you communicate with previous vc or parent vc ? notification center? thank you

rockbruno commented 4 years ago

hi, thanks for your time to reply. I've made changes like this. what do you think? is it okay?

I assumed you're looking for the modal's completion itself, is that not correct?

    public func present(viewController: UIViewController, fromViewController: UIViewController, animated: Bool) {
        fromViewController.present(
            viewController,
            animated: true,
            completion: yourCallback
    }

If you want to delegate anything else besides the screen showing up I would re-think what you're trying to do, because that's not what this framework is built for. The screens should be completely independent from each other.

I see, how do you communicate with previous vc or parent vc ? notification center? thank you

Notifications are fine, but again, it depends on what you're trying to do. If you want to notificate that a user has logged in that should be fine, but if you just want to delegate a previous VC that a specific VC has been pushed, I would re-evaluate why you're using this framework as that's not its purpose.

iRILLLL commented 4 years ago

Hi Bruno, thank you so much for your explanation. that's clear enough.

i'm too excited to try this library because I'm facing cycling dependencies between modules a few times. need to find a solution.