Skip to content

Commit

Permalink
supplement
Browse files Browse the repository at this point in the history
supplement
  • Loading branch information
goldze committed Dec 6, 2018
1 parent a72d7b9 commit 5bddc1e
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 30 deletions.
66 changes: 58 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ dependencies {
```

### 2.4、完成
运行效果如下:
<img src="./img/img8.jpg" width="240" hegiht="240" align=center />

到此为止,一个最基本的组件化工程搭建完毕。

## 3、可行性方案
Expand Down Expand Up @@ -394,29 +397,76 @@ public void onCreate() {
### 3.2、组件间通信
> 组件间是完全无耦合的存在,但是在实际开发中肯定会存在业务交叉的情况,该如何实现无联系的组件间通信呢?
#### 3.2.1、ARouter
ARouter之所以作为整个组件化的核心,是因为它拥有强大的路由机制。ARouter在library-base中依赖,所有组件又依赖于library-base,所以它可以看作为组件间通信的桥梁。
**[ARouter](https://github.com/alibaba/ARouter)**之所以作为整个组件化的核心,是因为它拥有强大的路由机制。ARouter在library-base中依赖,所有组件又依赖于library-base,所以它可以看作为组件间通信的桥梁。

<img src="./img/img7.png" width="320" hegiht="320" align=center />

在组件A中跳转到组件B页面:

```java
ARouter.getInstance()
.build(router_url)
.withString(key, value)
.navigation();
.build(router_url)
.withString(key, value)
.navigation();
```
在组件B页面中接收传过来的参数:

```java
@Autowired(name = key)
String value;
```

更多ARouter用法:**[https://github.com/alibaba/ARouter/blob/master/README_CN.md](https://github.com/alibaba/ARouter/blob/master/README_CN.md)**
#### 3.2.2、事件总线(RxBus)
**[MVVMHabit](https://github.com/goldze/MVVMHabit)**中提供了RxBus,可作为全局事件的通信工具。

### 3.3、base规则
当组件B页面需要回传数据给组件A时,可以调用:

#### 3.3.1、config
```java
_Login _login = new _Login();
RxBus.getDefault().post(_login);
```

#### 3.3.2、contract
在组件A中注册接收(注册在调用之前完成):

#### 3.3.3、global
```java
subscribe = RxBus.getDefault().toObservable(_Login.class)
.subscribe(new Consumer<_Login>() {
@Override
public void accept(_Login l) throws Exception {
//登录成功后重新刷新数据
initData();
//解除注册
RxSubscriptions.remove(subscribe);
}
});
RxSubscriptions.add(subscribe);
```

### 3.3、base规范
**library-base** 有两个主要作用:一是依赖通用基础jar或第三方框架,二是存放一些公共的静态属性和方法。下面列举一些基础通用类的约定规范。
#### 3.3.1、config
在base的config包下面,统一存放全局的配置文件,比如组件生命周期初始化类:**ModuleLifecycleConfig、ModuleLifecycleReflexs**,网络ROOT_URL,SD卡文件读写目录等。
#### 3.3.2、contract
RxBus组件通信,需要经过base层,统一规范。那么可以在contract包下面定义RxBus的契约类,写好注释,便于其他组件开发人员使用。
#### 3.3.3、global
主要存放全局的Key,比如 **IntentKeyGlobal:** 存放组件间页面跳转传参的Key名称; **SPKeyGlobal:** 全局SharedPreferences Key 统一存放在这里。单个组件中内部的key可以另外在单独组件中定义。
#### 3.3.4、router
**ARouter** 路由@Route注解中Path可以单独抽取一个或者两个RouterPath类出来,比如定义一个RouterActivityPath:

```java
public class RouterActivityPath {
/**
* 主业务组件
*/
public static class Main {
private static final String MAIN = "/main";
/*主业务界面*/
public static final String PAGER_MAIN = MAIN +"/Main";
}
```

Activity的路由路径统一在此类中定义,并使用静态内部类分块定义各个组件中的路径路由。
## 4、总结

<img src="./img/img9.png" width="320" hegiht="320" align=center />
Binary file modified axure.rp
Binary file not shown.
Binary file modified img/img2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/img8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/img9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@

public class RouterActivityPath {
/**
* 宿主组件
* 主业务组件
*/
public static class Main {
/*主界面*/
public static final String MAIN_MAIN = "/main/Main";
private static final String MAIN = "/main";
/*主业务界面*/
public static final String PAGER_MAIN = MAIN +"/Main";
}

/**
* 身份验证组件
*/
public static class Sign {
private static final String SIGN = "/sign";
/*登录界面*/
public static final String SIGN_LOGIN = "/sign/Login";
public static final String PAGER_LOGIN = SIGN + "/Login";
}

/**
* 用户组件
*/
public static class User {
private static final String USER = "/user";
/*用户详情*/
public static final String USER_USERDETAIL = "/user/UserDetail";
public static final String PAGER_USERDETAIL = USER + "/UserDetail";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,35 @@ public class RouterFragmentPath {
* 首页组件
*/
public static class Home {
private static final String HOME = "/home";
/*首页*/
public static final String HOME_HOME = "/home/Home";
public static final String PAGER_HOME = HOME + "/Home";
}

/**
* 工作组件
*/
public static class Work {
private static final String WORK = "/work";
/*工作*/
public static final String WORK_WORK = "/work/Work";
public static final String PAGER_WORK = WORK + "/Work";
}

/**
* 消息组件
*/
public static class Msg {
private static final String MSG = "/msg";
/*消息*/
public static final String MSG_MSG = "/msg/Msg";
public static final String PAGER_MSG = MSG + "/msg/Msg";
}

/**
* 用户组件
*/
public static class User {
private static final String USER = "/user";
/*我的*/
public static final String USER_ME = "/user/Me";
public static final String PAGER_ME = USER + "/Me";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Created by goldze on 2018/6/21
*/
@Route(path = RouterFragmentPath.Home.HOME_HOME)
@Route(path = RouterFragmentPath.Home.PAGER_HOME)
public class HomeFragment extends BaseFragment<FragmentHomeBinding, HomeViewModel> {
@Override
public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand Down
10 changes: 5 additions & 5 deletions module-main/src/main/java/com/goldze/main/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Created by goldze on 2018/6/21
*/
@Route(path = RouterActivityPath.Main.MAIN_MAIN)
@Route(path = RouterActivityPath.Main.PAGER_MAIN)
public class MainActivity extends BaseActivity<ActivityMainBinding, BaseViewModel> {
private List<Fragment> mFragments;

Expand All @@ -48,10 +48,10 @@ public void initData() {

private void initFragment() {
//ARouter拿到多Fragment(这里需要通过ARouter获取,不能直接new,因为在组件独立运行时,宿主app是没有依赖其他组件,所以new不到其他组件的Fragment)
Fragment homeFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.Home.HOME_HOME).navigation();
Fragment workFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.Work.WORK_WORK).navigation();
Fragment msgFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.Msg.MSG_MSG).navigation();
Fragment meFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.User.USER_ME).navigation();
Fragment homeFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.Home.PAGER_HOME).navigation();
Fragment workFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.Work.PAGER_WORK).navigation();
Fragment msgFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.Msg.PAGER_MSG).navigation();
Fragment meFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.User.PAGER_ME).navigation();
mFragments = new ArrayList<>();
mFragments.add(homeFragment);
mFragments.add(workFragment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Created by goldze on 2018/6/21
*/
@Route(path = RouterFragmentPath.Msg.MSG_MSG)
@Route(path = RouterFragmentPath.Msg.PAGER_MSG)
public class MsgFragment extends BaseFragment<FragmentMsgBinding, MsgViewModel> {
@Override
public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* 一个MVVM模式的登陆界面
* 作为登录验证模块的路由页
*/
@Route(path = RouterActivityPath.Sign.SIGN_LOGIN)
@Route(path = RouterActivityPath.Sign.PAGER_LOGIN)
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginViewModel> {
//ActivityLoginBinding类是databinding框架自定生成的,对应activity_login.xml
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Created by goldze on 2018/6/21.
*/
@Route(path = RouterActivityPath.User.USER_USERDETAIL)
@Route(path = RouterActivityPath.User.PAGER_USERDETAIL)
public class UserDetailActivity extends BaseActivity<ActivityUserDetailBinding, UserDetailViewModel> {
//拿到路由过来的参数
@Autowired()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Created by goldze on 2018/6/21
*/
@Route(path = RouterFragmentPath.User.USER_ME)
@Route(path = RouterFragmentPath.User.PAGER_ME)
public class MeFragment extends BaseFragment<FragmentMeBinding, MeViewModel> {
@Override
public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void initData() {
@Override
public void call() {
//采用ARouter+RxBus实现组件间通信
ARouter.getInstance().build(RouterActivityPath.Sign.SIGN_LOGIN).navigation();
ARouter.getInstance().build(RouterActivityPath.Sign.PAGER_LOGIN).navigation();
subscribe = RxBus.getDefault().toObservable(_Login.class)
.subscribe(new Consumer<_Login>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* Created by goldze on 2018/6/21
*/
@Route(path = RouterFragmentPath.Work.WORK_WORK)
@Route(path = RouterFragmentPath.Work.PAGER_WORK)
public class WorkFragment extends BaseFragment<FragmentWorkBinding, WorkViewModel> {
@Override
public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand Down
1 change: 0 additions & 1 deletion module-work/src/main/res/layout/fragment_work.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/appBackColor"
android:gravity="center"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
Expand Down

0 comments on commit 5bddc1e

Please sign in to comment.