13

I have been reading some articles about the clean architecture, and how it can be implemented in android. I saw the sample app which shows its Android Implementation. Also, I went through a nice talk on Clean architecture on Android

So, I kind of understand most of the concepts, but there is some clarity that I would like to get on certain things.

As per my Understanding,

  • The View layer is the outer layer which deals with the UI, and framework related stuff
  • The presenter is the direct communicator for the view, which accepts user inputs, and executes certain use cases based on this by passing it to the use case layer or the interactor layer.
  • Interactor executes the use-case, give it back to the callback sent by presenter,
  • Presenter again converts this result into a view understandable data structure (a ViewModel) and just pass it back to the view.

I am not listing more details about the inner layers like repository since my question is related to the above-mentioned steps

Here, does the presenter have the only job of acting as a mediator between UseCases and UI, as a data dispatcher?

Does it only do the view model to use case model conversion and vice-versa?

The input validation logics rely on which layer? Can it be inside the presenter? For example, if we take a small use case of a sign-up process,

Once the user entered the details and clicked sign-up button, and data sent to the presenter, is it like

  • Presenter validates the input values if any error is there notify the view
  • If values are proper, convert it to a use case model, and execute certain use case, and once the result is given by the interactor, again convert to view model, send it to view.

And the second question is, who controls the navigation? The View or the Presenter or the UseCase?

who decides where to go next?

For example - Consider a use case of a login process, Where user will enter the credentials and click OK.

On successful login,

  • If users e-mail is not verified, go to email verify screen
  • If users profile is not completed, set-up the profile then only go to home screen
  • If user is new, show new offers screen, else directly go to home screen

So, who is responsible for making these decisions on which screen to go next? Is it the presenter, which decides and navigate the view accordingly? Or is it the use case handlers responsibility to inform the presenter what is the next State?

Sorry for making the question too long, but I just wanted to elaborate my current understandings. Thanks in advance

1 Answers1

15

Here, does the presenter have the only job of acting as a mediator between UseCases and UI, as a data dispatcher?

Yes

The input validation logics rely on which layer? Can it be inside the presenter?

validation should rely on business layer not the presentation, can it be inside the presenter? sure it could, but what if you have multiple screens that take similar inputs, do you have to repeat your validation logic inside each presenter! you can argue that you can make a base presenter, but it's not the perfect solution, because presenter should have one purpose.

And the second question is, who controls the navigation? The View or the Presenter or the UseCase?

do you consider the navigation is part of the Domain or the presentation or the data layer, it's likely related to the presentation layer, but you could make a small component inside the presentation layer which is control the whole navigation generically, So you can use this component the moment you decide that you do need other platform and throw your activities away. you can find this approach in the sample you've mentioned.

EDIT:0

How you pass data between modules where they have different models?

you simply use mappers, and the reason is to make each module has its own models or entities, so it would be easy to test each module separately.

About presenter and view, let's say you want show error message, presenter decides why it would be shown, view decides how it would be shown.

I think the problem in understanding the clean code presentation layer with Android, that Activities and fragments aren't only view, they're also The process and the lifecycle which your code would be live in, clean code came along to separate spaghetti code on those Activities and fragments.

EDIT:1
adding to the last point, it's clear now that Google and the support team there made a great effort to make Activities and Fragments dummy views as possible via introducing the great set of libraries "Architecture components". I encourage anyone to check them out, even if you're using MVP, you'll find a great benefits.

Mohamed Ibrahim
  • 3,714
  • 2
  • 22
  • 44
  • Regarding the navigation, from the sample what I can see is the `Navigator` is a part of the BaseActivity, which means it is in the view layer and the presenter doesn't know about it. Now Take my second example in the question, where after login user has to be navigated to any of the many possible screens. –  Oct 01 '17 at 10:34
  • Would it be like, presenter just informs the view something like `onLoginSuccess` and view then performs the if..else statements to make a decision of where to go, call the appropriate routing method in the `Navigator`? Or presenter itself would perform this `if else`, and call respective view methods like `gotoHome` or `gotoEmailVerify`? –  Oct 01 '17 at 10:34
  • Also, how the data passing between the modules are handled? Does the navigator also hold the extra data to be supplied to start a new screen (or new feature or module)? For example, a payment module might be launching by holding the purchase information from the previous screen, So does the navigator transport it from the purchase module to the payment module? –  Oct 01 '17 at 11:35
  • Let's discuss your first comment, first what makes you think that navigation could be change, and if it changes depending on the platform isn't be easier to separate the navigation logic, we here talking about principles, as we agree on it we can then excute in the shape of implementations, the principle here is to separate what changes .. this is the single responsibility from clean code – Mohamed Ibrahim Oct 01 '17 at 11:42
  • Your second comment, each module has its own models or entities, so it would be easy to test each module separately. – Mohamed Ibrahim Oct 01 '17 at 11:44
  • How you pass data between modules where they have different models, you simply use mappers – Mohamed Ibrahim Oct 01 '17 at 11:45
  • 1
    About presenter and view, let's say you want show error message, presenter decides why it would be shown, view decides how it would be shown – Mohamed Ibrahim Oct 01 '17 at 11:48
  • And here is the problem in understanding the clean code presentation layer with Android, Activities and fragments aren't only view, they're also The process and the lifecycle which your code would be live in, clean code come along to separate spaghetti code on those Activities and fragments. – Mohamed Ibrahim Oct 01 '17 at 11:52
  • 1
    [This one](https://youtu.be/bXRI7YAha1M?t=706) gives some more clear understanding –  Oct 10 '17 at 10:22
  • If you are looking for a more detailed example on the role of the presenter please have a look at my post: https://plainionist.github.io/Implementing-Clean-Architecture-Controller-Presenter/ – plainionist Feb 19 '18 at 22:21