Introduction to the

This article is a reference to Google official MVP architecture demo and some of the previous summary of the MVP implementation of a simple demo, here to record a little experience, I hope to give a little help to those who want to use MVP.

The overall framework

Project directory structure

First, let’s take a look at the directory structure of the whole project:The code organization of a directory is organized according to its functions, which are divided into four types of files: XActivity, XContract, XFragment and xPresenter (x stands for business name). The base folder holds some common base class files, the data folder holds the code related to the business logic, and the utils folder holds some common utility classes. The demo realizes the following functions: Click the button on the interface to obtain the relevant information of the mobile phone, add delay and wait prompts (simulated network) in the process of obtaining, and finally display the information on the interface (simple demonstration, only shows the system time).

The base class

Take a look at the BasePresenter and BaseView interface classes, which are the base classes for all Presenters and Views, respectively.

public interface BasePresenter {
    void start(a);
}Copy the code

BasePresenter contains the start() method, which is used by the Presenter to retrieve data and change the display by calling the view method in the Fragment class’s onResume method.

public interface BaseView<T> {
    void setPresenter(T presenter);
}Copy the code

BaseView contains the setPresenter method, which passes an instance of Presenter into the view and is called in the constructor of the Activity’s Presenter implementation class.

Contract class

public interface GetPhoneInfoContract {

    interface View extends BaseView<Presenter> {

        void setTime(String time);

        void showLoading(a);

        void hideLoading(a);
    }

    interface Presenter extends BasePresenter {

        void getTime(a); }}Copy the code

Unlike the most common MVP implementation, the official implementation includes a contract class to manage all the interfaces of the view and Presenter. This way, the functions of the view and Presenter can be easily maintained. In this example, the presenter interface gets the system time. View interface to achieve time display and prompt dialog box display and hide.

How MVPS are organized

The effect of the activity

The activity, as the global controller, is responsible for creating view and Presenter instances and linking them together. The view is implemented by a fragment.

@EActivity(R.layout.get_phone_info_act)
public class GetPhoneInfoActivity extends ActionBarActivity {

    private FragmentManager fm;
    private GetPhoneInfoFragment mGetPhoneInfoFragment = new GetPhoneInfoFragment_();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setDefaultFragment();
        new GetPhoneInfoPresenter(mGetPhoneInfoFragment);
    }

    private void setDefaultFragment(a) { fm = getFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); transaction.add(R.id.fragcontent, mGetPhoneInfoFragment); transaction.commit(); }}Copy the code

In this example, the fragment is set in the activity using setDefaultFragment(), then instantiate GetPhoneInfoPresenter and pass the frament in. Implement in Presenter through the fragment interface to view operations display.

The realization of the presenter

public class GetPhoneInfoPresenter implements GetPhoneInfoContract.Presenter{

    private final GetPhoneInfoContract.View mGetPhoneInfoView;
    private PhoneInfoBiz phoneInfoBiz;

    public GetPhoneInfoPresenter(GetPhoneInfoContract.View getPhoneInfoView) {
        mGetPhoneInfoView = getPhoneInfoView;
        mGetPhoneInfoView.setPresenter(this);
        phoneInfoBiz = new PhoneInfoBizIml();
    }

    @Override
    public void start(a) {
        getTime();
    }

    @Override
    public void getTime(a) {
        mGetPhoneInfoView.showLoading();
        phoneInfoBiz.getPhoneInfo(new PhoneInfoBiz.GetPhoneInfoCallback() {
            @Override
            public void onGetPhoneInfo(PhoneInfo phoneInfo) { mGetPhoneInfoView.setTime(phoneInfo.getTime()); mGetPhoneInfoView.hideLoading(); }}); }}Copy the code

The Presenter constructor calls the view’s setPresenter method to pass in an instance of itself, and the start method handles data loading and presentation. If you need to change the interface, you can call the View layer method directly, so that the View layer and presenter layer can be well separated.

The implementation of the view

@EFragment(R.layout.get_phone_info_frag)
public class GetPhoneInfoFragment extends Fragment implements GetPhoneInfoContract.View {

    private GetPhoneInfoContract.Presenter mPresenter;
    ProgressDialog dialog;

    @ViewById
    TextView tv_time;

    @ViewById
    Button btn_get_time;

    @Click
    void btn_get_time(a) {
        mPresenter.getTime();
    }

    @AfterViews
    void initView(a) {
        dialog = new ProgressDialog(getActivity());
    }

    @Override
    public void onResume(a) {
        super.onResume();
        mPresenter.start();
    }

    @Override
    public void setPresenter(GetPhoneInfoContract.Presenter presenter) {
        if(presenter ! =null)
            mPresenter = presenter;
    }

    @Override
    @UiThread
    public void setTime(String time) {
        tv_time.setText(time);
    }

    @Override
    public void showLoading(a) {
        dialog.setTitle("One moment, please.");
        dialog.setMessage("loading!");
        dialog.show();
    }

    @Override
    public void hideLoading(a) { dialog.dismiss(); }}Copy the code

The setPresenter method inherits from the parent class. With this method, the view gets an instance of presenter and can call presenter code to handle business logic. The Presenter start method is also called in onResume to handle data loading and presentation.

Simple induction

  • An activity creates a fragment and instantiates a Presenter. When instantiated, the fragment is passed as a parameter, so that the presenter can call the View interface to update and display the interface
  • When a Presenter is instantiated, it also calls the view’s setPresenter method and passes itself in. Then the Fragment gets an instance of presenter and can call presenter in the view to perform business logic operations
  • View and Presenter have an instance of each other, which enables a view to call presenter in a view to handle a business, and then update the view in a Presenter.

Model layer

A brief introduction to the Model layer, PhoneInfo object stores mobile phone information, PhoneInfoBiz is the excuse class to realize the interface and callback interface required by the service, and PhoneInfoBizIml is the implementation class of the interface. Post code directly:

public interface PhoneInfoBiz {

    interface GetPhoneInfoCallback {

        void onGetPhoneInfo(PhoneInfo phoneInfo);
    }

    void getPhoneInfo(GetPhoneInfoCallback getPhoneInfoCallback);
}Copy the code
public class PhoneInfoBizIml implements PhoneInfoBiz{

    @Override
    public void getPhoneInfo(final GetPhoneInfoCallback getPhoneInfoCallback) {
        new Thread(new Runnable() {
            @Override
            public void run(a) {
                try {
                    PhoneInfo phoneInfo = new PhoneInfo();
                    phoneInfo.setTime(System.currentTimeMillis() + "");
                    phoneInfo.setMobileType(Build.MODEL);
                    phoneInfo.setMobileVer(Build.VERSION.RELEASE);
                    Thread.sleep(1000);
                    getPhoneInfoCallback.onGetPhoneInfo(phoneInfo);
                }catch(Exception e){ e.printStackTrace(); } } }).start(); }}Copy the code

conclusion

At this point, a simple MVP framework to this end, for the use of MVP is still in the exploration, the above example is combined with the official release of the demo to do a simplified project, there are shortcomings welcome to discuss exchanges!

Finally, attach the address of the demo and the official demo:

Demo link Official Demo link

This article starting address link: www.codeceo.com/article/and…