模塊化解耦框架RxFluxArchitecture2-基本功能實現

相關係列文章git

模塊化解耦框架RxFluxArchitecture1-框架簡介github

模塊化解耦框架RxFluxArchitecture2-基本功能實現bash

模塊化解耦框架RxFluxArchitecture3-訂閱管理綁定生命週期服務器

模塊化解耦框架RxFluxArchitecture4-依賴庫與依賴注入網絡

模塊化解耦框架RxFluxArchitecture5-Application多模塊共存架構

架構圖

架構圖.jpg

一、操做結果通知RxAction和UI響應通知RxChange

1.一、View 調用

LoginFragment點擊登陸按鈕,調用LoginActionCreator中的方法login(String,String)併發

@OnClick(R2.id.btn_login)
    public void login() {
        mActionCreator.login(username, password);
    }
複製代碼

1.二、ActionCreator 操做併發送

LoginActionCreator的方法login(String,String)postHttpAction(RxAction, Observable<T>)方法會調用WanApi接口方法進行登陸操做,登陸完成後發送封裝接口返回結果的RxAction(包含TagLoginAction.LOGIN)。框架

@Override
    public void login(String username, String password) {
        RxAction rxAction = newRxAction(LoginAction.LOGIN);
        postHttpAction(rxAction, mWanApi.login(username, password).flatMap(verifyResponse()));
    }
複製代碼

1.三、Store 接收併發送

LoginStore中接收 Tag 爲LoginAction.LOGIN,數據類型爲RxAction的通知。取出RxAction中封裝的接口返回數據,而後使用方法postChange(RxChange)通知 View 進行 UI 響應操做。ide

@Subscribe(tags = {LoginAction.LOGIN})
    public void onLogin(RxAction rxAction) {
        mUser = rxAction.getResponse();
        postChange(RxChange.newInstance(rxAction.getTag()));
    }
複製代碼

1.四、View 接收

LoginActivity中接收Tag爲LoginAction.LOGIN,數據類型爲RxChange的通知,進行 UI 更新操做。模塊化

@Subscribe(tags = {LoginAction.LOGIN}, sticky = true)
    public void onLogin(RxChange rxChange) {
        startActivity(new Intent(this, ArticleActivity.class));
        finish();
    }
複製代碼

二、操做進度通知RxLoading

2.一、ActionCreator 操做併發送

LoginActionCreator中使用postHttpLoadingAction(RxAction, Observable<T>)方法。 操做開始時,發送進度開始通知RxLoading; 操做完成,發送封裝接口返回結果的RxAction(包含TagLoginAction.LOGIN); 操做結束後,發送進度結束通知RxLoading

@Override
    public void login(String username, String password) {
        RxAction rxAction = newRxAction(LOGIN);
        postHttpLoadingAction(rxAction, mWanApi.login(username, password).flatMap(verifyResponse()));
    }
複製代碼

2.二、View 接收

2.2.一、全局接收

BaseActivity中全局響應RxLoading,全部子類 Activity 都會在接收到RxLoading通知後,調用onRxLoading(RxLoading)(方法名可變)。

@Subscribe(sticky = true)
    public void onRxLoading(@NonNull RxLoading rxLoading) {
        if (rxLoading.isLoading()) {
            //顯示進度框
        } else {
            //隱藏進度框
        }
    }
複製代碼

2.2.二、單獨接收

BaseActivity子類LoginActivity中重寫onRxLoading(RxLoading)方法,單獨響應Tag爲LoginAction.LOGINRxLoading

@Override
    @Subscribe(sticky = true)
    public void onRxLoading(@NonNull RxLoading rxLoading) {
        if (TextUtils.equals(rxLoading.getTag(), LoginAction.LOGIN)) {
            if (rxLoading.isLoading()) {
                //顯示進度框
            } else {
                //隱藏進度框
            }
        } else {
            super.onRxLoading(rxLoading);
        }
    }
複製代碼

三、 操做異常通知RxError

3.一、ActionCreator 操做併發送

RxActionCretorpostHttpAction(RxAction, Observable<T>)postHttpLoadingAction(RxAction, Observable<T>)方法,若是有異常,會發送操做異常通知RxError

3.二、View 接收

3.2.一、全局接收

BaseActivity中全局響應RxError,全部子類 Activity 都會在接收到RxError通知後,調用onRxError(RxError)(方法名可變)。

@Subscribe(sticky = true)
    public void onRxError(@NonNull RxError rxError) {
        Throwable throwable = rxError.getThrowable();
        if (throwable instanceof CommonException) {
            Toast.makeText(this, ((CommonException) throwable).message(), Toast.LENGTH_SHORT).show();
        } else if (throwable instanceof retrofit2.HttpException) {
            Toast.makeText(this, ((retrofit2.HttpException) throwable).code() + ":服務器問題", Toast.LENGTH_SHORT).show();
        } else if (throwable instanceof SocketException) {
            Toast.makeText(this, "網絡異常!", Toast.LENGTH_SHORT).show();
        } else if (throwable instanceof UnknownHostException) {
            Toast.makeText(this, "網絡異常!", Toast.LENGTH_SHORT).show();
        } else if (throwable instanceof SocketTimeoutException) {
            Toast.makeText(this, "鏈接超時!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, throwable.toString(), Toast.LENGTH_SHORT).show();
        }
    }
複製代碼

3.2.二、單獨接收

BaseActivity子類LoginActivity中重寫onRxError(RxError)方法,單獨響應Tag爲LoginAction.LOGINRxError

@Override
    @Subscribe(sticky = true)
    public void onRxError(@NonNull RxError rxError) {
        if (TextUtils.equals(rxError.getTag(), LoginAction.LOGIN)) {
           //單獨處理操做異常...
        } else {
            super.onRxError(rxError);
        }
    }
複製代碼

四、操做異常重試通知RxRtry

4.一、ActionCreator 操做併發送

FriendActionCreator中使用postHttpRetryAction(RxAction, Observable<T>)方法,若是操做有異常,會發送異常重試通知RxRetry

@Override
    public void getFriendList() {
        RxAction rxAction = newRxAction(FriendAction.GET_FRIEND_LIST);
        postHttpRetryAction(rxAction, mWanApi.getFriendList());
    }
複製代碼

4.二、View 接收

4.2.一、全局接收

BaseActivity中全局響應RxRetry,全部子類 Activity 都會在接收到RxRetry通知後,調用onRxRetry(RxRetry)(方法名可變)。在該方法中,能夠使用RxActionCreator中的postRetryAction(RxRetry)方法重試。

@Subscribe(sticky = true)
    public void onRxRetry(@NonNull RxRetry rxRetry) {
        CoordinatorLayout coordinatorLayout = findViewById(R.id.cdl_content);
        if (coordinatorLayout == null) {
            return;
        }
        Snackbar snackbar = Snackbar.make(coordinatorLayout, rxRetry.getTag(), Snackbar.LENGTH_INDEFINITE);
        snackbar.setAction("Retry", v -> mCommonActionCreatorLazy.get().postRetryAction(rxRetry)).show();
    }
複製代碼

4.2.二、單獨接收

BaseActivity子類中重寫onRxRetry(RxRetry)方法,單獨響應特定 Tag 的RxRetry

@Override
    @Subscribe(sticky = true)
    public void onRxRetry (@NonNull RxRetry rxRetry) {
        if (TextUtils.equals(rxRetry.getTag(), FriendAction.GET_FRIEND_LIST)) {
            //單獨處理異常重試...
        } else {
            super.onRxRetry(rxRetry);
        }
    }
複製代碼

源碼

開源模塊化解耦框架RxFluxArchitecture,歡迎你們點贊Fork,更歡迎點評指導。

相關文章
相關標籤/搜索