I was implementing MVP in my new application, then I came across a problem. I needed to call a method of View
, inside View
(Activity
) itself. It is by definition of MVP code separation, wrong thing to do.
By definition:
The Presenter is in charge of the the orchestration between the Model and the View. It basically receive events from both and act consequently. The Presenter is the only component that knows others. It has a reference to the View and a another to the Model. (source)
In the same article it was mentioned that View
does not reacts to user interactions, it passes control to Presenter
to do the job. I have also read this SOF post about dependency rules.
In my case, I am using custom AppTheme
. AppTheme
needs to be set before setContent()
call, what I am doing is create a method in View
interface called setAppTheme()
which my Activity
implements, and there is code to apply theme. Now the problem is, this is called within the app, which makes calling of a View
method inside its implementation.
To sum up, what my understanding of MVP either one of the following should be true:
Do call
View
method insideActivity
, becausesetTheme()
wont work aftersetContent()
and ourpresenter.setView()
is inonResume()
, but will this satisfy MVP separation of M-V-P ?Do not make interface method for
setAppTheme()
, instead create a private method inActivity
which sets theme. This method will have nothing to do with any layer of MVP. But question is, if project is using 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 do point out mistakes where I missed them.
From what I know, same case can be argued in the light of
setActionBar()
andsetOnClickListener()
methods, although these may require their separate post, but they are more relevant here and new post for either of them will be duplicate.
Please note that my Activity implements View interface.
EDIT: More explanation
My View is actually Activity class. This is View of MVP, not Android API's View
class. The thing is, there is a method setAppTheme()
which only related to View of MVP, (Activity
of Android). This call is not in the Contract (AboutUsMVP.java
) which by Google convention should be AboutUsContract.java
, this setAppTheme()
is not in Contract, and it cant be, so does this violate MVP principle?
There is no possible alternative, one can say make an interface of setAppTheme()
, if I do so, it will not work because:
setAppTheme()
is called just after super()
method, if not it is useless. And MVP's presenter starts working in onResume
. If an interface is made, and setAppTheme()
is brought into MVP's jurisdiction, it will have no effect.
setView()
) in onCreate, then the Presenter callssetAppTheme
. Just afterpresenter.setView()
you can callsetContentView
as normal. – Eselfar