前面寫了一篇從實(shí)現(xiàn)到封裝一個MVP,但是每次新建一個Act都要重建許多文件详恼,因此封裝一個通用的IBaseView呵哨,BaseModule腺怯,BasePresenter能省很多時間屿愚。
通用的module
我的module是在act執(zhí)行oncreate方法中初始化好的BasePresenter中持有retrofit的ApiManager。
public class BasePresenter<M extends BaseModule, V extends BaseContract.IBaseView> {
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調(diào)用的時候只需要這樣
module.getData(getApiManager().test2(), new IPresenterI<LoginBean>() {
@Override
public void onResult(LoginBean loginBean) {
}
@Override
public void onError(String s) {
}
});
protected abstract class IPresenterI<T extends BaseBean> implements DefaultContract.IDefaultPresenter<T> {
@Override
public void onStart() {
view.showLoading();
}
@Override
public void onComplete() {
view.cancelLoading();
}
}
public interface IDefaultModule extends IBaseModule {
<T extends BaseBean> void getData(Observable<T> observable, IDefaultPresenter<T> normalPresenter);
}
public interface IDefaultPresenter<T extends BaseBean> 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<BaseBean> test1();
@GET("index.php?s=/appi/news/getNewsCat")
Observable<LoginBean> test2();
}
重寫實(shí)現(xiàn)了IPresent的2個方法喇喉,因?yàn)閟tart的時候一般都會啟動loading框動畫祖今,結(jié)束的時候關(guān)掉。拣技。presenter只需要關(guān)心拿到結(jié)果和出錯兩種情況千诬。
module中的getData方法實(shí)現(xiàn),getData方法持有一個Bean對象他繼承自BaseBean,同時在initObservable(observable,normalPresenter)
中封裝了io線程和主線程的切換膏斤,還有在doOnSubscribe()
的時候開啟loading
public class DefaultModule extends BaseModule implements DefaultContract.IDefaultModule {
@Override
public <T extends BaseBean> void getData(Observable<T> observable, final DefaultContract.IDefaultPresenter<T> normalPresenter) {
initObservable(observable, normalPresenter).subscribe(new Observer<T>() {
@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 <T extends BaseBean> Observable<T> initObservable(Observable<T> observable, final DefaultContract.IDefaultPresenter presenter){
return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(@NonNull Disposable disposable) throws Exception {
presenter.onStart();
}
});
}
}
整體代碼如下
module.getData(getApiManager().test2(), new IPresenterI<LoginBean>() {
@Override
public void onResult(LoginBean baseBean) {
}
@Override
public void onError(String s) {
}
});
public interface ApiManager {
@GET("index.php?s=/appi/news/getNewsCat")
Observable<BaseBean> test1();
@GET("index.php?s=/appi/news/getNewsCat")
Observable<LoginBean> test2();
}
protected abstract class IPresenterI<T extends BaseBean> implements DefaultContract.IDefaultPresenter<T> {
@Override
public void onStart() {
view.showLoading();
}
@Override
public void onComplete() {
view.cancelLoading();
}
}
public class DefaultModule extends BaseModule implements DefaultContract.IDefaultModule {
@Override
public <T extends BaseBean> void getData(Observable<T> observable, final DefaultContract.IDefaultPresenter<T> normalPresenter) {
initObservable(observable, normalPresenter).subscribe(new Observer<T>() {
@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 {
<T extends BaseBean> void getData(Observable<T> observable, IDefaultPresenter<T> normalPresenter);
}
public interface IDefaultPresenter<T extends BaseBean> extends IBasePresenter {
void onResult(T t);
void onStart();
void onComplete();
void onError(String s);
}
public class BaseModule implements BaseContract.IBaseModule {
protected <T extends BaseBean> Observable<T> initObservable(Observable<T> observable, final DefaultContract.IDefaultPresenter presenter){
return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(@NonNull Disposable disposable) throws Exception {
presenter.onStart();
}
});
}
}