Related articles

Module decoupling Framework RxFluxArchitecture1- Introduction to the framework

Module decoupling framework RxFluxArchitecture2- Basic function realization

Module decoupling framework RxFluxArchitecture3- Subscription management binding lifecycle

Module decoupling framework RxFluxArchitecture4- Dependency libraries and dependency injection

Module decoupling framework RXFluxArchitecture5-Application co-exists with multiple modules

Architecture diagram

1. Notification of operation resultsRxActionAnd UI responds to notificationsRxChange

1.1. View Call

Click the Login button in LoginFragment and invoke the method login(String,String) in LoginActionCreator.

    @OnClick(R2.id.btn_login)
    public void login() {
        mActionCreator.login(username, password);
    }
Copy the code

1.2 ActionCreator operation and send

In LoginActionCreator’s login(String,String) method, postHttpAction(RxAction, Observable

) calls the WanApi method for login. Send the RxAction (including tagLogincomprehensible.LOGIN) with the result returned by the encapsulation interface after LOGIN.

    @Override
    public void login(String username, String password) {
        RxAction rxAction = newRxAction(LoginAction.LOGIN);
        postHttpAction(rxAction, mWanApi.login(username, password).flatMap(verifyResponse()));
    }
Copy the code

1.3 Store receives and sends

Receives a notification in LoginStore with a Tag of LoginAction.LOGIN and a data type of RxAction. Retrieve the data returned by the interface encapsulated in RxAction, and then use the method postChange(RxChange) to inform the View of the UI response action.

    @Subscribe(tags = {LoginAction.LOGIN})
    public void onLogin(RxAction rxAction) {
        mUser = rxAction.getResponse();
        postChange(RxChange.newInstance(rxAction.getTag()));
    }
Copy the code

1.4. View Reception

Receives a notification with a Tag of LoginAction.LOGIN and a data type of RxChange in the LoginActivity to update the UI.

    @Subscribe(tags = {LoginAction.LOGIN}, sticky = true)
    public void onLogin(RxChange rxChange) {
        startActivity(new Intent(this, ArticleActivity.class));
        finish();
    }
Copy the code

2. Operation progress notificationRxLoading

2.1 ActionCreator operation and send

Use the postHttpLoadingAction(RxAction, Observable

) method in LoginActionCreator. Send RxLoading notification when the operation starts. When the action is complete, send the RxAction (including the TagLoginAction.LOGIN) that the wrapper interface returns the result. After the operation is complete, RxLoading is notified.

    @Override
    public void login(String username, String password) {
        RxAction rxAction = newRxAction(LOGIN);
        postHttpLoadingAction(rxAction, mWanApi.login(username, password).flatMap(verifyResponse()));
    }
Copy the code

2.2 View reception

2.2.1 Global reception

BaseActivity responds to RxLoading globally, and all subclass activities call onRxLoading(RxLoading)(method name variable) when notified of RxLoading.

    @Subscribe(sticky = true)
    public void onRxLoading(@NonNull RxLoading rxLoading) {
        if(rxloading.isloading ()) {// Display the progress box}else{// Hide progress box}}Copy the code

2.2.2 Receive separately

Rewrite the onRxLoading(RxLoading) method in the BaseActivity subclass LoginActivity, responding separately to the RxLoading of LoginAction.LOGIN.

    @Override
    @Subscribe(sticky = true)
    public void onRxLoading(@NonNull RxLoading rxLoading) {
        if (TextUtils.equals(rxLoading.getTag(), LoginAction.LOGIN)) {
            if(rxloading.isloading ()) {// Display the progress box}else{// Hide progress box}}else{ super.onRxLoading(rxLoading); }}Copy the code

3. Notification of abnormal operationRxError

3.1 ActionCreator operation and send

RxActionCretor postHttpAction(RxAction, Observable

) and postHttpLoadingAction(RxAction, Observable

). An operation exception notification RxError is sent.

3.2 View Reception

3.2.1. Global reception

In BaseActivity, the global response to RxError occurs, and all subclasses of Activity call onRxError(RxError)(method name variable) upon receiving the RxError notification.

    @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() + ": Server problem", Toast.LENGTH_SHORT).show();
        } else if (throwable instanceof SocketException) {
            Toast.makeText(this, "Network exception!", Toast.LENGTH_SHORT).show();
        } else if (throwable instanceof UnknownHostException) {
            Toast.makeText(this, "Network exception!", Toast.LENGTH_SHORT).show();
        } else if (throwable instanceof SocketTimeoutException) {
            Toast.makeText(this, "Connection timed out!, Toast.LENGTH_SHORT).show();
        } else{ Toast.makeText(this, throwable.toString(), Toast.LENGTH_SHORT).show(); }}Copy the code

3.2.2 Receive separately

Override the onRxError(RxError) method in the BaseActivity subclass LoginActivity, responding separately to the RxError of the LoginAction.LOGIN Tag.

    @Override
    @Subscribe(sticky = true)
    public void onRxError(@NonNull RxError rxError) {
        if(textutils.equals (rxerror.getTag (), LoginAction.LOGIN)) {// Handle operation exceptions separately... }else{ super.onRxError(rxError); }}Copy the code

4. Retry notificationRxRtry

4.1 ActionCreator operation and send

Use postHttpRetryAction(RxAction, Observable

) in FriendActionCreator. If an error occurs, RxRetry is sent.

    @Override
    public void getFriendList() {
        RxAction rxAction = newRxAction(FriendAction.GET_FRIEND_LIST);
        postHttpRetryAction(rxAction, mWanApi.getFriendList());
    }
Copy the code

4.2 View Reception

4.2.1 Global reception

In BaseActivity, the global response to RxRetry is that all subclasses of Activity call onRxRetry(RxRetry)(variable method name) upon receiving the RxRetry notification. In this method, you can retry using the postRetryAction(RxRetry) method in RxActionCreator.

    @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();
    }
Copy the code

4.2.2 Receiving separately

Override the onRxRetry(RxRetry) method in the BaseActivity subclass to respond individually to the RxRetry of a particular Tag.

    @Override
    @Subscribe(sticky = true)
    public void onRxRetry (@NonNull RxRetry rxRetry) {
        if(textutils.equals (rxretry.gettag (), friendAction.get_friend_list)) {// Handle exception retry separately... }else{ super.onRxRetry(rxRetry); }}Copy the code

The source code

Open source module decoupling framework RxFluxArchitecture, welcome to like Fork, welcome to comment on guidance.