前面写了一篇从实现到封装一个MVP,但是每次新建一个Act都要重建许多文件,因此封装一个通用的IBaseView,BaseModule,BasePresenter能省很多时间。
通用的module
我的module是在act执行oncreate方法中初始化好的BasePresenter中持有retrofit的ApiManager。
public class BasePresenter {
protected M module;
protected V view;
protected Context context;
ApiManager apiManager;
protected ApiManager getApiManager() {
if(apiManager==null){
return new Retrofit.Builder().baseUrl("http://www.eltyl.com/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create()).build().create(ApiManager.class);
}else {
return apiManager;
}
}
void attatchWindow(M m, V v) {
this.module = m;
this.view = v;
}
void detachWindow() {
this.module = null;
this.view = null;
}
在presenter调用的时候只需要这样
module.getData(getApiManager().test2(), new IPresenterI() {
@Override
public void onResult(LoginBean loginBean) {
}
@Override
public void onError(String s) {
}
});
protected abstract class IPresenterI implements DefaultContract.IDefaultPresenter {
@Override
public void onStart() {
view.showLoading();
}
@Override
public void onComplete() {
view.cancelLoading();
}
}
public interface IDefaultModule extends IBaseModule {
void getData(Observable observable, IDefaultPresenter normalPresenter);
}
public interface IDefaultPresenter extends IBasePresenter {
void onResult(T t);
void onStart();
void onComplete();
void onError(String s);
}
public interface ApiManager {
@GET("index.php?s=/appi/news/getNewsCat")
Observable test1();
@GET("index.php?s=/appi/news/getNewsCat")
Observable test2();
}
重写实现了IPresent的2个方法,因为start的时候一般都会启动loading框动画,结束的时候关掉。。presenter只需要关心拿到结果和出错两种情况。
module中的getData方法实现,getData方法持有一个Bean对象他继承自BaseBean,同时在initObservable(observable,normalPresenter)
中封装了io线程和主线程的切换,还有在doOnSubscribe()
的时候开启loading
public class DefaultModule extends BaseModule implements DefaultContract.IDefaultModule {
@Override
public void getData(Observable observable, final DefaultContract.IDefaultPresenter normalPresenter) {
initObservable(observable, normalPresenter).subscribe(new Observer() {
@Override
public void onSubscribe(@NonNull Disposable disposable) {
}
@Override
public void onNext(@NonNull T t) {
normalPresenter.onResult(t);
}
@Override
public void onError(@NonNull Throwable throwable) {
normalPresenter.onError(throwable.toString());
}
@Override
public void onComplete() {
normalPresenter.onComplete();
}
});
}
}
public class BaseModule implements BaseContract.IBaseModule {
protected Observable initObservable(Observable observable, final DefaultContract.IDefaultPresenter presenter){
return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Consumer() {
@Override
public void accept(@NonNull Disposable disposable) throws Exception {
presenter.onStart();
}
});
}
}
整体代码如下
module.getData(getApiManager().test2(), new IPresenterI() {
@Override
public void onResult(LoginBean baseBean) {
}
@Override
public void onError(String s) {
}
});
public interface ApiManager {
@GET("index.php?s=/appi/news/getNewsCat")
Observable test1();
@GET("index.php?s=/appi/news/getNewsCat")
Observable test2();
}
protected abstract class IPresenterI implements DefaultContract.IDefaultPresenter {
@Override
public void onStart() {
view.showLoading();
}
@Override
public void onComplete() {
view.cancelLoading();
}
}
public class DefaultModule extends BaseModule implements DefaultContract.IDefaultModule {
@Override
public void getData(Observable observable, final DefaultContract.IDefaultPresenter normalPresenter) {
initObservable(observable, normalPresenter).subscribe(new Observer() {
@Override
public void onSubscribe(@NonNull Disposable disposable) {
}
@Override
public void onNext(@NonNull T t) {
normalPresenter.onResult(t);
}
@Override
public void onError(@NonNull Throwable throwable) {
normalPresenter.onError(throwable.toString());
}
@Override
public void onComplete() {
normalPresenter.onComplete();
}
});
}
}
public interface IDefaultModule extends IBaseModule {
void getData(Observable observable, IDefaultPresenter normalPresenter);
}
public interface IDefaultPresenter extends IBasePresenter {
void onResult(T t);
void onStart();
void onComplete();
void onError(String s);
}
public class BaseModule implements BaseContract.IBaseModule {
protected Observable initObservable(Observable observable, final DefaultContract.IDefaultPresenter presenter){
return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Consumer() {
@Override
public void accept(@NonNull Disposable disposable) throws Exception {
presenter.onStart();
}
});
}
}