android的日常看点Android知识Android开发经验谈

Android——MVP——封装一个通用的Module

2017-06-20  本文已影响330人  超神的菠萝

前面写了一篇从实现到封装一个MVP,但是每次新建一个Act都要重建许多文件,因此封装一个通用的IBaseView,BaseModule,BasePresenter能省很多时间。

通用的module

我的module是在act执行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调用的时候只需要这样

        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();


}

重写实现了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 <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();
            }
        });
    }

}

上一篇下一篇

猜你喜欢

热点阅读