模块化解耦框架RxFluxArchitecture2-基本功能实现

相关系列文章

模块化解耦框架RxFluxArchitecture1-框架简介

模块化解耦框架RxFluxArchitecture2-基本功能实现

模块化解耦框架RxFluxArchitecture3-订阅管理绑定生命周期

模块化解耦框架RxFluxArchitecture4-依赖库与依赖注入

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

架构图

架构图.jpg

1、操作结果通知RxAction和UI响应通知RxChange

1.1、View 调用

LoginFragment点击登录按钮,调用LoginActionCreator中的方法login(String,String)

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

1.2、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.3、Store 接收并发送

LoginStore中接收 Tag 为LoginAction.LOGIN,数据类型为RxAction的通知。取出RxAction中封装的接口返回数据,然后使用方法postChange(RxChange)通知 View 进行 UI 响应操作。

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

1.4、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();
    }

2、操作进度通知RxLoading

2.1、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.2、View 接收

2.2.1、全局接收

BaseActivity中全局响应RxLoading,所有子类 Activity 都会在接收到RxLoading通知后,调用onRxLoading(RxLoading)(方法名可变)。

    @Subscribe(sticky = true)
    public void onRxLoading(@NonNull RxLoading rxLoading) {
        if (rxLoading.isLoading()) {
            //显示进度框
        } else {
            //隐藏进度框
        }
    }

2.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);
        }
    }

3、 操作异常通知RxError

3.1、ActionCreator 操作并发送

RxActionCretorpostHttpAction(RxAction, Observable<T>)postHttpLoadingAction(RxAction, Observable<T>)方法,如果有异常,会发送操作异常通知RxError

3.2、View 接收

3.2.1、全局接收

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.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);
        }
    }

4、操作异常重试通知RxRtry

4.1、ActionCreator 操作并发送

FriendActionCreator中使用postHttpRetryAction(RxAction, Observable<T>)方法,如果操作有异常,会发送异常重试通知RxRetry

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

4.2、View 接收

4.2.1、全局接收

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.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,更欢迎点评指导。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,688评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,559评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,749评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,581评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,741评论 3 271
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,684评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,122评论 2 292
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,847评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,441评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,939评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,333评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,783评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,275评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,830评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,444评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,553评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,618评论 2 249

推荐阅读更多精彩内容