相關係列文章git
模塊化解耦框架RxFluxArchitecture1-框架簡介github
模塊化解耦框架RxFluxArchitecture2-基本功能實現bash
模塊化解耦框架RxFluxArchitecture3-訂閱管理綁定生命週期服務器
模塊化解耦框架RxFluxArchitecture4-依賴庫與依賴注入網絡
模塊化解耦框架RxFluxArchitecture5-Application多模塊共存架構
RxAction
和UI響應通知RxChange
在LoginFragment
點擊登陸按鈕,調用LoginActionCreator
中的方法login(String,String)
。併發
@OnClick(R2.id.btn_login)
public void login() {
mActionCreator.login(username, password);
}
複製代碼
在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()));
}
複製代碼
在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()));
}
複製代碼
在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
在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()));
}
複製代碼
在BaseActivity
中全局響應RxLoading
,全部子類 Activity 都會在接收到RxLoading
通知後,調用onRxLoading(RxLoading)
(方法名可變)。
@Subscribe(sticky = true)
public void onRxLoading(@NonNull RxLoading rxLoading) {
if (rxLoading.isLoading()) {
//顯示進度框
} else {
//隱藏進度框
}
}
複製代碼
在BaseActivity
子類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 {
//隱藏進度框
}
} else {
super.onRxLoading(rxLoading);
}
}
複製代碼
RxError
在RxActionCretor
中postHttpAction(RxAction, Observable<T>)
和postHttpLoadingAction(RxAction, Observable<T>)
方法,若是有異常,會發送操做異常通知RxError
。
在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();
}
}
複製代碼
在BaseActivity
子類LoginActivity
中重寫onRxError(RxError)
方法,單獨響應Tag爲LoginAction.LOGIN
的RxError
。
@Override
@Subscribe(sticky = true)
public void onRxError(@NonNull RxError rxError) {
if (TextUtils.equals(rxError.getTag(), LoginAction.LOGIN)) {
//單獨處理操做異常...
} else {
super.onRxError(rxError);
}
}
複製代碼
RxRtry
在FriendActionCreator
中使用postHttpRetryAction(RxAction, Observable<T>)
方法,若是操做有異常,會發送異常重試通知RxRetry
。
@Override
public void getFriendList() {
RxAction rxAction = newRxAction(FriendAction.GET_FRIEND_LIST);
postHttpRetryAction(rxAction, mWanApi.getFriendList());
}
複製代碼
在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();
}
複製代碼
在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,更歡迎點評指導。