MVPArms使用

 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>

Api的baseurl

 String APP_DOMAIN = "http://api.channel.petope.com";

CommonService设置service

public interface CommonService {
    //登录
    String HEADER_API_VERSION = "ua: 1.1;2;API1;Android";
    @Headers({HEADER_API_VERSION})
    @FormUrlEncoded
    @POST("code/index")
    Observable<LoginBean> login(@Field("type") String type, @Field("phone") String phone);
}

LoginContracts设置接口

public interface LoginContract {
    //对于经常使用的关于UI的方法可以定义到IView中,如显示隐藏进度条,和显示文字消息
    interface View extends IView {
        void LoginSuccess();
        void LoginFail();
    }

    //Model层定义接口,外部只需关心Model返回的数据,无需关心内部细节,即是否使用缓存
    interface Model extends IModel {
//            Observable<BaseResponseBean> getLogin();
        io.reactivex.Observable<LoginBean> getLogin(String type, String phone);

    }
}

LoginModel

@ActivityScope
public class LoginModel extends BaseModel implements  LoginContract.Model {
    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    @Inject
    public LoginModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<LoginBean> getLogin(String type, String phone) {
        return mRepositoryManager.obtainRetrofitService(CommonService.class).login(type,phone);

    }

}
@ActivityScope
public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public LoginPresenter(LoginContract.Model model, LoginContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }


    public void getLogin(String type,String phone) {

        mModel.getLogin(type,phone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(AndroidSchedulers.mainThread())
//                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<LoginBean>(mErrorHandler) {
                    @Override
                    public void onNext(LoginBean loginBean) {
                        Log.i("aseResponseBean",loginBean.getMsg());
                        if (loginBean.isSuccess()){
                            mRootView.LoginSuccess();
                        }else {
                            mRootView.LoginFail();
                        }

                    }
                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);

                        Log.i("onError", "=====" + t.getMessage());
                    }

                });
    }
}

LoginActivity写视图

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一个是甜的发慌,一个是疼在心上。 无论写得什么,当时的每一个字都叫深情。 徐徐徐 ...
    爱酒的猫九阅读 112评论 0 0
  • 岁月在恬静中走过 悠长而舒缓 心无挂碍的日子 聆听着自己内心的呼唤 没有谁会是谁的宿命 没有烦恼可以让自己愁眉不展...
    泉水叮咚波斯猫阅读 923评论 12 52
  • 1、爱一个人,无论何时都希望他有可以选择的机会,一件事到了非做不可的时候也是把人禁锢住了。 红包可发可不发的时候,...
    桃乐心理阅读 260评论 3 2
  • 枝闹火红群鸟蹓,风荡落英鸦咯笑。 藏花怜玉见悲殇,地泯杰豪多寡少。 重蹈涅槃薪新燎,霜冻雹...
    蝉鸣莺语阅读 440评论 8 4
  • 一女人在池塘里洗着衣服,她是我在村里走着看到的比较年轻的一位。 古朴的房子,美丽的飞檐,还有大姐在池塘边捶打着衣服...
    曲悦秋华阅读 315评论 0 0