内容简介:在在在
- 该框架使用 Flux 架构,数据朝单一方向流动。
- 上一级不需要持有下一级对象,不需要知道下一级如何实现响应。
- ActionCreator 类似 MVP 架构中的 Presenter,只是其中不持有 View 对象,无需操作结束回调 View 的响应方法。
-
Store 继承自
androidx.lifecycle.ViewModel
,可以通过androidx.lifecycle.LiveData<T>
实现 MVVM 架构。 - Store 自动关联 View 生命周期,维持设备横竖屏切换时数据。
- View,Store 自动注册订阅、解除订阅。
- 使用 Dagger.Android 实现 View (Activity/Fragment)的依赖注入。
- 使用实现 Tag 功能的 EventBus 实现数据总线功能。
功能实现1:操作响应 RxAction
和 RxChange
,以登录功能为例
- View
在 LoginFragment
点击登录按钮,调用 LoginActionCreator
中的方法 login(String,String)
。
@OnClick(R2.id.btn_login) public void login() { mActionCreator.login(username, password); } 复制代码
- ActionCreator
在 LoginActionCreator
的方法 login(String,String)
中 postHttpAction(RxAction, Observable<T>)
方法会调用 WanApi
接口方法进行登录操作,登录完成后发送封装接口返回结果的 RxAction
(包含Tag LoginAction.LOGIN
)。
@Override public void login(String username, String password) { RxAction rxAction = newRxAction(LoginAction.LOGIN); postHttpAction(rxAction, mWanApi.login(username, password).flatMap(verifyResponse())); } 复制代码
- 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())); } 复制代码
- View
在 LoginActivity
中接收Tag为 LoginAction.LOGIN
,数据类型为 RxChange
的通知,跳转其他页面。
@Subscribe(tags = {LoginAction.LOGIN}, sticky = true) public void onLogin(RxChange rxChange) { startActivity(new Intent(this, ArticleActivity.class)); finish(); } 复制代码
功能实现2:进度通知 RxLoading
- ActionCreator
在 LoginActionCreator
中使用 postHttpLoadingAction(RxAction, Observable<T>)
方法。
操作开始时,发送进度开始通知 RxLoading
;
操作完成,发送封装接口返回结果的 RxAction
(包含Tag LoginAction.LOGIN
);
操作结束后,发送进度结束通知 RxLoading
。
@Override public void login(String username, String password) { RxAction rxAction = newRxAction(LOGIN); postHttpLoadingAction(rxAction, mWanApi.login(username, password).flatMap(verifyResponse())); } 复制代码
- View
在 BaseActivity
中全局响应 RxLoading
@Subscribe(sticky = true) public void onRxLoading(@NonNull RxLoading rxLoading) { if (rxLoading.isLoading()) { //显示进度框 } else { //隐藏进度框 } } 复制代码
或者在特定 View LoginActivity
中重写 onRxLoading(RxLoading)
方法,单独响应Tag为 LoginAction.LOGIN
的 RxLoading
。
@Override @Subscribe(sticky = true) public void onRxLoading(@NonNull RxLoading rxLoading) { if (TextUtils.equals(rxLoading.getTag(), LoginAction.LOGIN)) { if (rxLoading.isLoading()) { //显示进度框 } else { //隐藏进度框 } } } 复制代码
功能实现3:操作异常 RxError
RxActionCretor
中 postHttpAction(RxAction, Observable<T>)
和 postHttpLoadingAction(RxAction, Observable<T>)
方法,如果有异常,会发送操作异常通知 RxError
。
可以在 BaseActivity
中全局响应 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(); } } 复制代码
或者在特定 View LoginActivity
中重写 onRxError(RxError)
方法,单独响应Tag为 LoginAction.LOGIN
的 RxError
。
@Override @Subscribe(sticky = true) public void onRxError(@NonNull RxError rxError) { if (TextUtils.equals(rxError.getTag(), LoginAction.LOGIN)) { //单独处理操作异常... } } 复制代码
功能实现4:异常重试 RxRtry
- ActionCreator
在 FriendActionCreator
中使用 postHttpRetryAction(RxAction, Observable<T>)
方法,如果操作有异常,会发送异常重试通知 RxRetry
@Override public void getFriendList() { RxAction rxAction = newRxAction(FriendAction.GET_FRIEND_LIST); postHttpRetryAction(rxAction, mWanApi.getFriendList()); } 复制代码
- View
在 BaseActivity
中全局响应 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(); } 复制代码
或者在特定 View 中重写 onRxRetry(RxRetry)
方法,单独响应特定Tag的 RxRetry
。
@Override @Subscribe(sticky = true) public void onRxRetry (@NonNull RxRetry rxRetry) { if (TextUtils.equals(rxRetry.getTag(), FriendAction.GET_FRIEND_LIST)) { //单独处理异常重试... } } 复制代码
源码
在开源模块化框架 RxFluxArchitecture 中,欢迎大家指正点赞。可以提取有用的代码,单独编写!
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 自己实现集合框架(十):顺序栈的实现
- Golang实现简单爬虫框架(4)——队列实现并发任务调度
- 简易RPC框架实现
- 优秀开源框架的扩展机制实现
- Go 实现简易 RPC 框架
- 如何实现一个Python爬虫框架
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web 2.0 Architectures
Duane Nickull、Dion Hinchcliffe、James Governor / O'Reilly / 2009 / USD 34.99
The "Web 2.0" phenomena has become more pervasive than ever before. It is impacting the very fabric of our society and presents opportunities for those with knowledge. The individuals who understand t......一起来看看 《Web 2.0 Architectures》 这本书的介绍吧!