说到MVP就不得不提到MVC,做过J2EE的猿友们肯定知道MVC是个什么东西。MVC即 Model、View、Controller, 那MVP就Model、View、Presenter。Model用于提供数据模型,View用于显示数据,当然Presenter也就用来处理业务逻辑并将数据显示数据到View上了,它是Model和View的桥梁。
题外话,其实在15年的时候,我们公司的项目就采用mvp,当时没时间写,最近看时间不紧,就分享下
我将整个项目分为三个Module,如下图
其实这种结构我是不太喜欢的,我喜欢在app的根目录下,新建一个liabary文件,这里放第三方的库工程;
一、数据处理模块domain(包含Model),这里的domain命名只是我喜欢这样把数据处理相关的东西都放在这里。
ServiceManager用于向外提供数据的入口(其他类代码在前面博文中已提及)
- package com.micky.retrofitrxandroiddragger2.domain.service;
- import retrofit.GsonConverterFactory;
- import retrofit.Retrofit;
- import retrofit.RxJavaCallAdapterFactory;
- public class ServiceManager {
- private static final String ENDPOINT = "http://ip.taobao.com";
- private static class ServiceManagerHolder {
- private static final ServiceManager INSTANCE = new ServiceManager();
- }
- private ServiceManager() {}
- public static final ServiceManager getInstance() {
- return ServiceManagerHolder.INSTANCE;
- }
- private ApiService mApiService = null;
- public ApiService getApiService() {
- if (mApiService == null) {
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(ENDPOINT)
- .addConverterFactory(GsonConverterFactory.create())
- .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
- .build();
- mApiService = retrofit.create(ApiService.class);
- return mApiService;
- }
- return mApiService;
- }
- }
二、View和Presenter模块
为了方便管理我将View和Presenter相关类都放在Presentation模块中
View接口
-
- public interface MainView {
- void showProgress();
- void hideProgress();
- void setIpText(String text);
- }
Presenter接口
- package com.micky.retrofitrxandroiddragger2.presenter;
-
- public interface MainPresenter extends BasePresenter {
- void getIpInfo(String ip);
- }
Presenter实现类
- package com.micky.retrofitrxandroiddragger2.presenter.impl;
- import android.text.TextUtils;
- import android.util.Log;
- import android.widget.Toast;
- import com.micky.retrofitrxandroiddragger2.BaseApplication;
- import com.micky.retrofitrxandroiddragger2.R;
- import com.micky.retrofitrxandroiddragger2.domain.service.ServiceManager;
- import com.micky.retrofitrxandroiddragger2.domain.service.response.GetIpInfoResponse;
- import com.micky.retrofitrxandroiddragger2.presenter.MainPresenter;
- import com.micky.retrofitrxandroiddragger2.presenter.impl.BasePresenterImpl;
- import com.micky.retrofitrxandroiddragger2.ui.view.MainView;
- import rx.Subscriber;
- import rx.android.schedulers.AndroidSchedulers;
- import rx.schedulers.Schedulers;
- public class MainPresenterImpl extends BasePresenterImpl implements MainPresenter {
- private static final String TAG = "TAG";
- private MainView mMainView;
- public MainPresenterImpl(MainView mainView) {
- mMainView = mainView;
- }
- @Override
- public void getIpInfo(String ip) {
- if (TextUtils.isEmpty(ip)) {
- Toast.makeText(BaseApplication.getContext(), R.string.input_tip_ip, Toast.LENGTH_SHORT).show();
- return;
- }
- mMainView.setIpText("");
- mMainView.showProgress();
- ServiceManager.getInstance().getApiService().getIpInfo(ip)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Subscriber<GetIpInfoResponse>() {
- @Override
- public void onCompleted() {
- mMainView.hideProgress();
- }
- @Override
- public void onError(Throwable e) {
- Log.e(TAG, e.getMessage(), e);
- mMainView.hideProgress();
- mMainView.setIpText(BaseApplication.getContext().getString(R.string.network_error));
- }
- @Override
- public void onNext(GetIpInfoResponse getIpInfoResponse) {
- mMainView.setIpText(getIpInfoResponse.data.country + " " + getIpInfoResponse.data.area);
- }
- });
- }
- }
-
MainActivity
- package com.micky.retrofitrxandroiddragger2.ui.activity;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.EditText;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import com.micky.retrofitrxandroiddragger2.R;
- import com.micky.retrofitrxandroiddragger2.presenter.MainPresenter;
- import com.micky.retrofitrxandroiddragger2.presenter.impl.MainPresenterImpl;
- import com.micky.retrofitrxandroiddragger2.ui.view.MainView;
-
- public class MainActivity extends AppCompatActivity implements MainView {
- private EditText mEtIp;
- private TextView mTvContent;
- private ProgressBar mProgressBar;
- private MainPresenter mMainPresenter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- mEtIp = (EditText) findViewById(R.id.et_ip);
- mTvContent = (TextView) findViewById(R.id.tv_content);
- mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
- mMainPresenter = new MainPresenterImpl(this);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- mMainPresenter.getIpInfo(mEtIp.getText().toString());
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public void showProgress() {
- mProgressBar.setVisibility(View.VISIBLE);
- }
- @Override
- public void hideProgress() {
- mProgressBar.setVisibility(View.GONE);
- }
- @Override
- public void setIpText(String text) {
- mTvContent.setText(text);
- }
- }
OK,代码基本完了,看了上述代码大家也许都会说以前就在一个类里面就搞定的功能,现在怎么多出这么多接口、实现类啊。别急我刚看到这个mvp的时候也这么想,几度写着写着都把它抛之脑后,这类都多得看不过来了。仔细研究下不难发现这M、V、P三者的关系还是挺清晰的。
好了就到这里,你也赶快试试吧