Be updated on the latest digital news

Home →
Blog →
The use and adaptation of the VIPER architecture on our own example of development, part 3

The use and adaptation of the VIPER architecture on our own example of development, part 3

This is the third part of the article series about our experience of using and adapting the VIPER architecture. In the first part, we had discussed the basics of using and adapting the architecture of the VIPER and last time we talked about structuring and creating project skeleton for VIPER. It's high time to talk about creating a module in terms of VIPER architecture

Here goes!

Probably, the most time-consuming part of using VIPER architecture is creating lots of additional files and classes. So let’s sort it out and first of all see what we should create and why we need it.

1. Factory

The factory is a class responsible for creating a module with parameters (any data needed to be transferred to module we’re opening) and completion (a module which opens current one and which has to decide what to do with it). The controller should conform to ModuleInputProtocol.

2. Storyboard

Storyboard is a very important class because all the UI elements should be placed here. Moreover, there’re a few requirements for storyboard.

Initial storyboard controller should have storyboard id, defined in Factory

Storyboard has to contain an object with custom class name acting as module initializer (e.g StartScreenModuleInitializer)

3. View

One of the VIPER module parts is the View which means not only UIView but also all the presentation layers including View, ViewController, DataSources, Delegates, Adapters etc. One more important moment is the interaction between View and other parts of the module, namely the Presenter.

And, of course, we should define input and output protocols. So input protocol (ViewInput) defines methods by which the Presenter calls the View and output protocol (ViewOutput) defines how the View calls the Presenter.

4. Presenter

This part acts as a mediator between View and the data layer. It communicates with View in two ways using ViewInput and ViewOutput protocols. And the main task is to process the data retrieved and then assign it to the View.

5. Interactor

The interactor is a class responsible for communication between VIPER module and the external “world”. In this case, Presenter asks for data by InteractorInput protocol. We don’t use InteractorOutput here to transfer data to Presenter for asynchronous operations (e.g API calls) because we usually use PromiseKit for them. We will show how we use it in the final part.

6. Configurator

This part of VIPER module is responsible for initial module setup and consists of two parts

As we can see, it creates Presenter and Interactor, assigning all required services to them and connecting them together. The presenter has a reference to Interactor while View refers to Presenter.

Now module is ready to be shown on the screen.

It looks clear enough, but in reality, it requires a lot of work to create the same files for each module. And on the other hand, too much time is spent for forming VIPER module, whereas similar screens could be easily created using MVC or MVP. Is it possible to find a solution to this problem? Maybe some script could simplify the whole process by doing all this work for us?

Luckily, there is a great solution, namely - Generamba (created by Rambler Digital Solutions), which allows defining folder structure, files, and the code they contain. So, by using easy command we receive all these files generated automatically! And the only thing we need to do is to implement business logic to the module. Quite easy, right?

Generamba in practice

The first step would be Generamba installation. You can achieve this by using “gem install generamba” command that requires Ruby 2.2 version.

The next step is to set up Generamba in the project. Run “generamba setup” command in project root to create Rambafile and add required information about project and author such as author name, company name, target name, etc. It’s necessary to properly add new files to the project. And don’t worry, this file can be modified any time you want.

Now you should add all the templates you’re going to use in the project to the Rambafile. For instance: