Search code examples
androidarchitecturemvpclean-architectureviper-architecture

Clean Architecture - Should simple view logic be on Presenter or on View?


I have a simple example:

A user selects a Date from a Calendar, then that date is set in a label. When there is no date or an invalid date is selected, a placeholder must be placed instead.

My question is, which of these better approaches Clean Architecture?

1) Send a Date object from Presenter -> View, object can be null. View decides if date is null, to place placeholder text, and if not, to format it.
View completely decides how interface is drawn based on received Date

2) Presenter sends the formatted Date string to View, but if it is null, View sets the placeholder. Presenter defines value format as it is a business rule, but View decides how data are graphically displayed, so View decides placeholder text and components that will be altered. Supposing that Interface may change to f.e. placeholder is placed in another label and date field is left empty

3) Presenter instructs View both to set Date text, and to set placeholder text (only known by View, as it is an static text). View receives specifically what things to do. Presenter knows exactly what components do View has and controls them. But static texts are an interface component so Views decides which placeholder text to set.

4) Presenter completely defines what will be drawn in UI, View is merely a layer to connect to UI components, Presenter knows exactly or mostly how View is drawn

For me, Presenter should be only managing overall input and output events from View, leaving whole responsibility to View on how things are drawn, which formats are used and even how input is retrieved, so that if UI changes, main screen i/o use cases will remain valid in Presenter.

However I have seen code examples that give much more control to presenter, and UI is merely the framework way to draw elements and capture user input.


Solution

  • You could argue for any of those 4, what you really want is consistency across the codebase once you have chosen one.

    Personally I would choose #3 it gives the most control to the presenter, does not allow for conditional logic in the View, and like you said decouples the UI from the Presenter the most, allowing for a swapping of the UI and no change to the presenter.

    i.e. two methods

    Presenter.showDateSelected(long dateTimeSelectedInMillis)

    Presenter.showDateSelectedIsInvalid()

    never pass null when you actually know what null means (an invalid date) as you are being implicit when you always want to be explicit.

    Above I have chose to pass a primitive, but passing a Date object could be argued as well, it just ties to you a platform, or perhaps you could create your own View Domain Date object, again you have multiple choice of how Presenter interacts with View.

    All of your choices above are a flavor of CleanArchitecture and the thing that makes it most clean is consistency & clarity in your decision choices.