放棄MVP-Android Flux 框架 RxFlux2 (三)使用

首先,說放棄 MVP,肯定是夸大其詞了。MVP 很好,只是個人不習慣那么多的回調(diào),更喜歡 Flux 這種單向數(shù)據(jù)流模式。希望大家能多多點贊,多多拍磚!

demo源碼RxFlux2

下面以 com.huyingbao.simpel.main 包中的類作為講解,主要實現(xiàn) 主頁面->列表頁面->用戶信息頁面 跳轉。

一、包結構

image.png
Views
  • MainActivty:是模塊內(nèi)唯一 Acitvity,對應 MainStore,負責容納Fragment,控制 Fragment 之間跳轉。

  • MainFragment:主頁面,只有一個 button,點擊跳轉到列表頁面
    GitRepoListFragment。

  • GitRepoListFragment:列表頁面,展示網(wǎng)絡接口獲取回來的列表數(shù)據(jù),點擊其中一條數(shù)據(jù),跳轉到用戶信息頁面 GitUserFragment。

  • GitUserFragment:用戶信息頁面,展示根據(jù) userId獲取的用戶信息。

Store
  • MainStore 模塊主 store,原則上只有一個。主要用來接收發(fā)送的 RxAction(Views 通過 ActionCreator 及其封裝的 Dispatcher 發(fā)送),封裝 RxAction 成 RxStoreChange,并發(fā)送(通過封裝的Dispatcher發(fā)送)。
其他
  • GitRepoAdapter:RecyclerVIew適配器。極其推薦BaseRecyclerViewAdapterHelper。
  • GitRepro、GitUser :model類,接口返回Json數(shù)據(jù)解析類。

二、業(yè)務流程實現(xiàn)

  • MainFragment 跳轉到 GitRepoListFragment
  1. MainFragment 中通過 mActionCreator 發(fā)送包含跳轉請求的 RxAction。
    /**
     * 到用戶信息頁面
     */
    @OnClick(R.id.btn_main_to_list)
    public void toGitRepoList() {
        mActionCreator.postLocalAction(Actions.TO_GIT_REPO_LIST);
    }
  1. BaseRxActionCreator 中 postLocalAction() 方法實現(xiàn),已經(jīng)封裝好,不需要修改。
    /**
     * 發(fā)送LocalAction
     *
     * @param actionId
     * @param data
     */
    public void postLocalAction(@NonNull String actionId, @NonNull Object... data) {
        postRxAction(newRxAction(actionId, data));
    }
  1. MainStore 在 onRxAction() 方法中接收包含跳轉請求 RxAction,通過 switch 語句判斷具體執(zhí)行什么后續(xù)操作,如果是當前模塊中需要處理邏輯,執(zhí)行 postChange() 方法,發(fā)送 RxStoreChange。
    @Override
    public void onRxAction(RxAction rxAction) {
        switch (rxAction.getType()) {
            ...
            case Actions.Actions.TO_GIT_REPO_LIST:
                break;
            default://此處不能省略,不是本模塊的邏輯,直接返回,不發(fā)送RxStoreChange
                return;
        }
        postChange(new RxStoreChange(getClass().getSimpleName(), rxAction));
    }
  1. MainActivity 在onRxStoreChanged() 方法中接收 RxStoreChange,獲取 RxStoreChange 中的 RxAction,通過 switch 語句判斷業(yè)務邏輯,控制跳轉到 GitRepoListFragment。
    @Override
    public void onRxStoreChanged(@NonNull RxStoreChange rxStoreChange) {
        RxAction rxAction = rxStoreChange.getRxAction();
        switch (rxAction.getType()) {
            case Actions.TO_GIT_REPO_LIST:
                toGitRepoList();
                break;
            ...
        }
    }
    /**
     * 到列表頁面
     */
    private void toGitRepoList() {
        getFragmentTransaction(R.id.fl_content)
                .add(R.id.fl_content, GitRepoListFragment.newInstance())
                .commit();
    }

注意: MainFragment 不需要響應 RxStoreChange,只是繼承 BaseFragment,沒有繼承BaseRxFluxFragment(實現(xiàn)RxViewDispatch 接口)。

  • GitRepoListFragment 獲取列表數(shù)據(jù)

GitRepoListFragment 繼承 BaseRxFluxListFragment,一個封裝了 RecyclerView 的 BaseRxFluxFragment,大家可以看看,但是可能不符合自己項目需要。

  • GitUserFragment 獲取用戶信息
  1. GitUserFragment 中調(diào)用 mActionCreator 中的 getGitUser() 方法獲取接口數(shù)據(jù)。
    @Override
    public void afterCreate(Bundle savedInstanceState) {
        initActionBar("用戶信息");//Fragment 控制 Activity的ToolBar 展示
        mActionCreator.getGitUser(mContext, getArguments().getInt(ActionsKeys.USER_ID));
    }
  1. ActionCreator 中 getGitUser() 方法實現(xiàn)。
    @Override
    public void getGitUser(Context context, int userId) {
        RxAction action = newRxAction(GET_GIT_USER);
        postLoadingHttpAction(context,action, mHttpApi.getUser(userId));
    }
  1. MainStore 在 onRxAction() 方法中接收包含接口返回數(shù)據(jù)的 RxAction,通過 switch 語句判斷具體執(zhí)行什么后續(xù)操作,如果是當前模塊中需要處理邏輯,緩存接口返回數(shù)據(jù),執(zhí)行 postChange() 方法,發(fā)送 RxStoreChange。
    @Override
    public void onRxAction(RxAction rxAction) {
        switch (rxAction.getType()) {
            ...
            case Actions.GET_GIT_USER:
                mGitUser = rxAction.get(ActionsKeys.RESPONSE);
                break;
            ...
            default://此處不能省略,不是本模塊的邏輯,直接返回,不發(fā)送RxStoreChange
                return;
        }
        postChange(new RxStoreChange(getClass().getSimpleName(), rxAction));
    }
    public GitUser getGitUser() {//供view使用
        return mGitUser;
    }
  1. GitUserFragment 在onRxStoreChanged() 方法中接收 RxStoreChange,獲取 RxStoreChange 中的 RxAction,通過 switch 語句判斷業(yè)務邏輯,從 MainStore 獲取數(shù)據(jù)并顯示。
    @Override
    public void onRxStoreChanged(@NonNull RxStoreChange rxStoreChange) {
        switch (rxStoreChange.getRxAction().getType()) {
            case Actions.GET_GIT_USER:
                showGitUserInfo(mStore.getGitUser());
                break;
        }
    }
    /**
     * 顯示用戶信息
     *
     * @param gitUser
     */
    private void showGitUserInfo(GitUser gitUser) {
        mTvGitUserLogin.setText(gitUser.getLogin());
        mTvGitUserName.setText(gitUser.getName());
        mTvGitUserStatistics.setText(gitUser.getName());
    }

三、總結

  • MainStore 通過 Dagger2 注入到模塊中需要用的 View 中。
  • ActionCreator 通過 Dagger2 注入到 Base View中。
  • 按流程用就好,僅僅是部分業(yè)務邏輯需要自定義方法實現(xiàn),代碼清晰明確。

全局用法請等待,等不及的直接看源碼...

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,917評論 25 709
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,172評論 4 61
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評論 19 139
  • ----------------------------喵小姐 手機鈴聲吵醒了在睡夢中的鏡其,臉埋在枕頭里,手在柜...
    喵小姐閱讀 185評論 0 1

友情鏈接更多精彩內(nèi)容