4

I'm currently trying to implement VIPER-architecture in my project, and there is some questions I encountered with.

I have two modules in my app, that have some similar functionality (they both have imagePicker and ability to upload media to server, that implemented absolutely the same for both screens).

My question is how could I reuse this similar functionality in both modules? Trouble is that my imagePicker has many methods declared in Interactor that handle different events while selecting and uploading image (such as didUploadMediaFile(), didFailToUploadMediaFile(), uploadMediaFile() and so on).

Should I create third module with this functionality and then somehow subclass my other modules from it? Or maybe there is a better way of doing it?

A. Buksha
  • 830
  • 9
  • 14

3 Answers3

2

The only similar components/methods I'd use are Data Managers, which can be shared between as many Interactors are you want, and yet being 100% compliant with VIPER architecture.

So, a DataManager called, for example, MediaApiDataManager() would be responsible for the implementation of the core code to UploadMediaFile() etc

I suggest you read this post for more great tips on VIPER: https://www.ckl.io/blog/best-practices-viper-architecture/

Marcelo Gracietti
  • 3,121
  • 1
  • 16
  • 24
1

I think you need create abstract class and implement inside imagePicker logic. Declare interface (protocol) for it class with didUploadMediaFile(), didFailToUploadMediaFile(), uploadMediaFile() methods, implement this methods in class and inject to your VIPER modules

iSashok
  • 2,326
  • 13
  • 21
0

For the two modules try to abstract the similarities and try to build a Class of it. If both classes differs on the data type use Generics, also you can use Protocols, so declare the common methods of the two modules in one protocol and implement each one of them as an extension. Maybe this tutorial helps. https://medium.com/@richiemon/protocol-extension-dispatching-6d5229f1338e