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!
@MaryLitv21, the good news is that you are focused on the correct goal: managing dependencies in VIPER, as visualizing the correct dependencies is the path to true understanding of VIPER. The bad news is that your current mode of thinking is skew & divergent enough to the path that you need to travel that old habits are too comforting.
This diagram below should be posted on the wall of every person on your team. Indeed, it would be best to redraw it with each façade shown in the v, i, & r zones, so that it is obvious what the p zone is actually evoking Entity-expressed responses from by Entity-expressed stimuli via the thin veneer of the façade, not by invoking anything in the guts of within the v, i, or r zone. Also, each of the bilateral pairs of arrows should be labeled in your(-team's) redraw of this diagram to overtly state which communication technology that it is (e.g., asynchronous operation with callback upon completion, subroutine invocation that pends/suspends-invoker's-execution until completion, message queue posting & pending). You(r team) must have a published API for each façade between zones; perhaps developers can add to it themselves, which makes the software architect a police-esque veto function when some developer destroys the quarantine; perhaps the software architect owns the published façade's API, where a developer bypassing the façade to destroy each zone's quarantine lowers the developer's performance review harshly. The interzone quarantine is inviolable in VIPER. That inviolability said, the usage of Entities as the lingua franca across zone boundaries for interzone evocations of APIs in the façades is implied but not overtly shown in the diagram below.