标签:etc art input cte common res util nts create
前两篇已经将Retrofit和RxAndroid应用到了项目中,这篇本打算直接将Dagger2引进项目,但是考虑到整个项目结构,就来个结构整理吧,一起来看看网上炒得火热MVP模式。
说到MVP就不得不提到MVC,做过J2EE的猿友们肯定知道MVC是个什么东西。MVC即 Model、View、Controller, 那MVP就Model、View、Presenter。Model用于提供数据模型,View用于显示数据,当然Presenter也就用来处理业务逻辑并将数据显示数据到View上了,它是Model和View的桥梁。
想更清晰的理解MPV如何工作,那就直接上代码吧。
我将整个项目分为三个Module,如下图
一、数据处理模块domain(包含Model),这里的domain命名只是我喜欢这样把数据处理相关的东西都放在这里。
ServiceManager用于向外提供数据的入口(其他类代码在前面博文中已提及)
package com.micky.retrofitrxandroiddragger2.domain.service;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
/**
* @Project RetrofitRxAndroidDragger2
* @Packate com.micky.retrofitrxandroiddragger2.domain.service
* @Description
* @Author Micky Liu
* @Email mickyliu@126.com
* @Date 2015-12-22 14:43
* @Version 1.0
*/
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接口
/**
* @Project RetrofitRxAndroidDragger2
* @Packate com.micky.presentation
* @Description
* @Author Micky Liu
* @Email mickyliu@126.com
* @Date 2015-12-22 13:55
* @Version 1.0
*/
public interface MainView {
void showProgress();
void hideProgress();
void setIpText(String text);
}
Presenter接口
package com.micky.retrofitrxandroiddragger2.presenter;
/**
* @Project RetrofitRxAndroidDragger2
* @Packate com.micky.presentation
* @Description
* @Author Micky Liu
* @Email mickyliu@126.com
* @Date 2015-12-22 13:46
* @Version 1.0
*/
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;
/**
* @Project RetrofitRxAndroidDragger2
* @Packate com.micky.retrofitrxandroiddragger2.presenter
* @Description
* @Author Micky Liu
* @Email mickyliu@126.com
* @Date 2015-12-22 14:33
* @Version 1.0
*/
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;
/**
* @Project RetrofitRxAndroidDragger2
* @Packate com.micky.presentation
* @Description
* @Author Micky Liu
* @Email mickyliu@126.com
* @Date 2015-12-22 12:22
* @Version 1.0
*/
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三者的关系还是挺清晰的。
为了让大家更清晰的理解,请看如下时序图(MainView只是为了更好的展示调用与数据流程而特意画出来的)
看到这里,是不是觉得so simple! 呢。
详细代码在老地方: https://github.com/mickyliu945/CommonProj
本文转载自:http://blog.csdn.net/liuhongwei123888/article/details/50380368。
标签:etc art input cte common res util nts create
原文地址:http://www.cnblogs.com/czwew/p/6182205.html