Search code examples
iosswiftviperviper-architecture

Swift VIPER Interactor and Presenter rules for data validation


I do have few questions. What the best way to use interactor:

Let's say in interactor I save some data, but I want to check this data and show next alert:

self.presenter?.showAlert(with: "All fields are required for adding exercise.".localized)

First question is this violation of Viper? And do I need to check data in presenter before passing it to interceptor?

Another question: Do I need to use function showAlertFillIssue instead of passing text?

Even let's say I will pass it from presenter in case of violation of interactor.


Solution

  • As described in https://TheSwiftDev.com/the-ultimate-viper-architecture-tutorial,

    • the interactor zone is solely for the acquisition of entities from sensors, database, or network protocols or for the emission of entities to database or network protocols.
    • Conversely, the presenter zone is for all the enforcement of business rules/logic. There are occasionally shades of grey (as mentioned below) but the general rule is that the detection of not all fields being filled out properly would be in presenter divorced from database concepts and divorced from UI concepts.
    • The showAlert would be best thought of as something entirely in the view zone, because, depending on the OS, it might be an alert on 1 OS but some nonalert UI construct on another OS. It is best to think of VIPER as: what would this action look like on all the other OSes (than iOS), such as at least MacOS but also Android & UWP (or other UI infrastructure quarantined in view zone, such as Qt). It would be best to reword showAlert as emitError or balkBack more generically without referring to what the actual UI construct is on this OS. Likewise, the showAlertFillIssue and any other specific UI action is best quarantined entirely within view zone away from presenter (business rules) and from interactor (entity acquisition & storage).