Android MVP - Call Method in Implementation View (Fragements or Activity)

I have implemented MVP in my new application, then I ran into a problem. I needed to call a method View

, inside View

( Activity

). This is by the definition of MVP code splitting, something is wrong.

A-priory:

The Moderator is responsible for the orchestration between the Model and the View. He basically receives events from both and acts accordingly. The leader is the only ingredient that knows others. It refers to the view and the other refers to the Model. ( source )

In the same article, it was mentioned that it View

does not respond to user interaction, it passes control Presenter

to complete the task. I also read this SOF report on dependency rules.

In my case, I am using a custom AppTheme

. AppTheme

needs to be set before calling setContent()

, what I do is create a method in the interface View

called setAppTheme()

which mine implements Activity

and there is code to apply the theme. Now the problem is that it is being called inside the application, which is calling the method call View

inside its implementation.

To summarize, which is my understanding of MVP or one of the following should be true:

  • The method call is View

    inside Activity

    , because it setTheme()

    doesn't work after setContent()

    , and ours presenter.setView()

    is in onResume()

    , but does this satisfy the MVP separation of MVP?

  • Don't use an interface method for setAppTheme()

    , instead create a private method in Activity

    that sets the theme. This method will have nothing to do with any MVP layer. But the question is, if the project is using the MVP pattern, is this practice valid?

Here is my MVP:

public class AboutUsMVP
{

    public interface Model
    {


        String getFbLink();
        String getTwitterLink();
        String getEmailLink();
        String getCompanyLink();

    }

    public interface Presenter
    {
        void setView( View view );

        void fbButtonClicked();

        void twitterButtonClicked();

        void emailButtonClicked();

        void imageButtonClicked();

    }

    public interface View
    {

        void showFacebookPage();
        void showTwitterPage();
        void showEmailIntent();
        void showCompanyWebsite();
        void setAppTheme();
        void setCustomActionBar();

    }

}

      

Please point out the errors when I missed them.

From what I know, the same case can be argued in light setActionBar()

and setOnClickListener()

, although these may require their separate position, but they are more relevant here and the new message for any of them will be duplicated.

Please note that my activity implements the View interface.

EDIT: More explanation

My View is an Activity class. This is a kind of M V P class , not for Android API View

. The point is that there is a method setAppTheme()

that is only related to the MVP view ( Activity

for Android). This call is not in the Contract ( AboutUsMVP.java

), which Google agreed to AboutUsContract.java

, this setAppTheme()

one is not in the Contract, and it cannot be, so this violates the MVP principle?

There is no alternative, one might say, to make an interface setAppTheme()

, if I do it it won't work because:

setAppTheme()

called immediately after the method super()

, unless it is useless. And the lead MVP starts working for onResume

. If the interface is made, but setAppTheme()

introduced into the MVP jurisdiction, it will have no effect.

+3


source to share


2 answers


Indeed, View

the MVP should be dumb. This: they do not contain any logic. Just get the user generated event and submit your work to the presenter immediately . View

can also inform the presenter that some events have occurred (view was created, screen rotation, etc.)

This can lead to some confusion. Who is responsible for calling some of the methods? As you said, View

has some action to take, such as setOnClickListener

, but the presenter is responsible for handling the event. Just keep this in mind:



View

is just an interface . This means that you can use anything that implements this interface

You are now creating a mobile application. But if you want to code a console or desktop application, the presentation logic will not change . Anything specific to the "Viewing Technology" (Android, desktop, etc.) should be executed inside the technology specific code. This way your code will be loosely coupled to your tech stack

+2


source


I believe @ Pelocho's answer is correct and you should mark it as correct. But I would like to present my answer (which agree with it), but from an alternative point of view. For this other POV, I'd like to discuss yours as per speaker definition:

The presenter is responsible for the orchestration between the Model and the View

So the question I am proposing here is, "What is this model that the facilitator is interacting with?" And my answer to that is to take a look at the classic "notes" app and argue that the model is text and associated metadata usually stored in some DB that the presenter will read, parse, and click on the view.

Nowhere in this "model" is the application theme relevant. Theme is purely a function of the look. The topic is only related to the final display on the screen. Just like the opinion is the one who is responsible for the layout of the content on the screen, or to know which font size to use.



And that means that even though these layouts, sizes and colors can be changed using the Custom Preferences option, it is still outside the responsibility of the presenter and the model, as they are only interested in the content.

TL; DR:

Just read the thread on Activity.onCreate

(before super.onCreate) directly from SharedPreferences and don't include Presenter in it.

+1


source







All Articles