I'm building my first app using VIPER. I have two modules: ObserverModule and CurrenciesModule. I modally present CurrenciesViewController from ObserverViewController, and when CurrenciesViewController is being dismissed, I need ObserverViewController to know about it. I've found that I need to use delegates for that purpose. So I create a protocol:
protocol CurrenciesDelegate {
func onCurrenciesScreenDismissed()
}
Then I create a property of this protocol inside my CurrenciesPresenter:
var delegate: CurrenciesDelegate?
Then, when my CurrenciesViewController is being dismissed, and notifies my presenter about it, I start calling my delegate from presenter in such way:
delegate?.onCurrenciesScreenDismissed()
Then, I sign my ObserverPresenter to CurrenciesDelegate and realize its method. Inside of it I tell my view to update:
extension ObserverPresenter: CurrenciesDelegate {
func onCurrenciesScreenDismissed() {
view.updateView()
}
}
I realize that now it's a time to make dependencies, and it's the most confusing part. I've tried doing it inside of CurrenciesConfigurator, but it doesn't work:
protocol CurrenciesConfiguratorProtocol {
func configure(with viewController: CurrenciesViewController)
}
class CurrenciesConfigurator: CurrenciesConfiguratorProtocol {
func configure(with viewController: CurrenciesViewController) {
let presenter = CurrenciesPresenter(view: viewController)
let interactor = CurrenciesInteractor(presenter: presenter)
let router = CurrenciesRouter(view: viewController)
viewController.presenter = presenter
presenter.interactor = interactor
presenter.router = router
var currenciesDelegate: CurrenciesDelegate!
presenter.delegate = currenciesDelegate
}
}
That's how I call my configurator from my CurrenciesViewController:
let configurator = CurrenciesConfigurator()
override func viewDidLoad() {
configurator.configure(with: self)
presenter.viewLoaded()
}
I have no idea of how to configure dependencies make it work as expected. Any help is appreciated!