Android开发经验谈Android开发Android知识

放弃MVP-Android Flux 框架 RxFlux2 (三

2017-11-10  本文已影响409人  coolfireApy

首先,说放弃 MVP,肯定是夸大其词了。MVP 很好,只是个人不习惯那么多的回调,更喜欢 Flux 这种单向数据流模式。希望大家能多多点赞,多多拍砖!

demo源码RxFlux2

下面以 com.huyingbao.simpel.main 包中的类作为讲解,主要实现 主页面->列表页面->用户信息页面 跳转。

一、包结构

image.png
Views
Store
其他

二、业务流程实现

  1. MainFragment 中通过 mActionCreator 发送包含跳转请求的 RxAction。
    /**
     * 到用户信息页面
     */
    @OnClick(R.id.btn_main_to_list)
    public void toGitRepoList() {
        mActionCreator.postLocalAction(Actions.TO_GIT_REPO_LIST);
    }
  1. BaseRxActionCreator 中 postLocalAction() 方法实现,已经封装好,不需要修改。
    /**
     * 发送LocalAction
     *
     * @param actionId
     * @param data
     */
    public void postLocalAction(@NonNull String actionId, @NonNull Object... data) {
        postRxAction(newRxAction(actionId, data));
    }
  1. MainStore 在 onRxAction() 方法中接收包含跳转请求 RxAction,通过 switch 语句判断具体执行什么后续操作,如果是当前模块中需要处理逻辑,执行 postChange() 方法,发送 RxStoreChange。
    @Override
    public void onRxAction(RxAction rxAction) {
        switch (rxAction.getType()) {
            ...
            case Actions.Actions.TO_GIT_REPO_LIST:
                break;
            default://此处不能省略,不是本模块的逻辑,直接返回,不发送RxStoreChange
                return;
        }
        postChange(new RxStoreChange(getClass().getSimpleName(), rxAction));
    }
  1. MainActivity 在onRxStoreChanged() 方法中接收 RxStoreChange,获取 RxStoreChange 中的 RxAction,通过 switch 语句判断业务逻辑,控制跳转到 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(实现RxViewDispatch 接口)。

GitRepoListFragment 继承 BaseRxFluxListFragment,一个封装了 RecyclerView 的 BaseRxFluxFragment,大家可以看看,但是可能不符合自己项目需要。

  1. GitUserFragment 中调用 mActionCreator 中的 getGitUser() 方法获取接口数据。
    @Override
    public void afterCreate(Bundle savedInstanceState) {
        initActionBar("用户信息");//Fragment 控制 Activity的ToolBar 展示
        mActionCreator.getGitUser(mContext, getArguments().getInt(ActionsKeys.USER_ID));
    }
  1. ActionCreator 中 getGitUser() 方法实现。
    @Override
    public void getGitUser(Context context, int userId) {
        RxAction action = newRxAction(GET_GIT_USER);
        postLoadingHttpAction(context,action, mHttpApi.getUser(userId));
    }
  1. MainStore 在 onRxAction() 方法中接收包含接口返回数据的 RxAction,通过 switch 语句判断具体执行什么后续操作,如果是当前模块中需要处理逻辑,缓存接口返回数据,执行 postChange() 方法,发送 RxStoreChange。
    @Override
    public void onRxAction(RxAction rxAction) {
        switch (rxAction.getType()) {
            ...
            case Actions.GET_GIT_USER:
                mGitUser = rxAction.get(ActionsKeys.RESPONSE);
                break;
            ...
            default://此处不能省略,不是本模块的逻辑,直接返回,不发送RxStoreChange
                return;
        }
        postChange(new RxStoreChange(getClass().getSimpleName(), rxAction));
    }
    public GitUser getGitUser() {//供view使用
        return mGitUser;
    }
  1. GitUserFragment 在onRxStoreChanged() 方法中接收 RxStoreChange,获取 RxStoreChange 中的 RxAction,通过 switch 语句判断业务逻辑,从 MainStore 获取数据并显示。
    @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());
    }

三、总结

全局用法请等待,等不及的直接看源码...

上一篇 下一篇

猜你喜欢

热点阅读