Skip to content

Commit dfc05f8

Browse files
author
mrzhang
committed
add BaseCompRouter
1 parent baadcf5 commit dfc05f8

File tree

17 files changed

+196
-527
lines changed

17 files changed

+196
-527
lines changed

URI设计.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

app/build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ android {
1515
enabled = true
1616
}
1717

18+
javaCompileOptions {
19+
annotationProcessorOptions {
20+
arguments = [host: "app"]
21+
}
22+
}
23+
1824
}
1925
buildTypes {
2026
release {
@@ -31,7 +37,7 @@ android {
3137

3238
dependencies {
3339
compile project(':componentservice')
34-
// annotationProcessor project(':router-anno-compiler')
40+
annotationProcessor project(':router-anno-compiler')
3541
}
3642

3743
combuild {

app/src/main/java/com/luojilab/componentdemo/MainActivity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.luojilab.componentdemo;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45
import android.support.v4.app.Fragment;
56
import android.support.v4.app.FragmentTransaction;
67
import android.support.v7.app.AppCompatActivity;
78
import android.view.View;
89
import android.widget.Button;
10+
import android.widget.Toast;
911

1012
import com.luojilab.component.componentlib.router.Router;
1113
import com.luojilab.componentservice.readerbook.ReadBookService;
14+
import com.luojilab.router.facade.annotation.RouteNode;
1215

16+
@RouteNode(host = "app", path = "/main")
1317
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
1418

1519
Fragment fragment;
@@ -57,4 +61,13 @@ public void onClick(View v) {
5761
break;
5862
}
5963
}
64+
65+
66+
@Override
67+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
68+
super.onActivityResult(requestCode, resultCode, data);
69+
if (data != null) {
70+
Toast.makeText(this, data.getStringExtra("result"), Toast.LENGTH_SHORT).show();
71+
}
72+
}
6073
}

app/src/main/java/com/luojilab/componentdemo/application/AppApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.app.Application;
44

5+
import com.luojilab.component.componentlib.router.ui.UIRouter;
6+
57
/**
68
* Created by mrzhang on 2017/6/15.
79
*/
@@ -12,6 +14,8 @@ public class AppApplication extends Application {
1214
public void onCreate() {
1315
super.onCreate();
1416

17+
UIRouter.getInstance().registerUI("app");
18+
1519
//如果isRegisterCompoAuto为false,则需要通过反射加载组件
1620
// Router.registerComponent("com.luojilab.reader.applike.ReaderAppLike");
1721
// Router.registerComponent("com.luojilab.share.applike.ShareApplike");

componentlib/src/main/java/com/luojilab/component/componentlib/router/UiActivityUri.java

Lines changed: 0 additions & 76 deletions
This file was deleted.

componentlib/src/main/java/com/luojilab/component/componentlib/router/ui/BaseCompRouter.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Created by mrzhang on 2017/12/19.
55
*/
66

7+
import android.app.Activity;
78
import android.content.Context;
89
import android.content.Intent;
910
import android.net.Uri;
@@ -15,11 +16,22 @@
1516
import java.util.Map;
1617

1718
public class BaseCompRouter implements IComponentRouter {
18-
protected static Map<String, Class> routeMapper = new HashMap<String, Class>();
19+
protected Map<String, Class> routeMapper = new HashMap<String, Class>();
1920

20-
protected static Map<Class, Map<String, Integer>> paramsMapper;
21+
protected Map<Class, Map<String, Integer>> paramsMapper = new HashMap<>();
2122

22-
protected static String HOST = "";
23+
protected String HOST = "";
24+
25+
protected boolean hasInitHost = false;
26+
protected boolean hasInitMap = false;
27+
28+
protected void initHost() {
29+
hasInitHost = true;
30+
}
31+
32+
protected void initMap() {
33+
hasInitMap = true;
34+
}
2335

2436
@Override
2537
public boolean openUri(Context context, String url, Bundle bundle) {
@@ -44,6 +56,9 @@ public boolean openUri(Context context, String url, Bundle bundle, Integer reque
4456

4557
@Override
4658
public boolean openUri(Context context, Uri uri, Bundle bundle, Integer requestCode) {
59+
if (!hasInitMap) {
60+
initMap();
61+
}
4762
if (uri == null || context == null) {
4863
return true;
4964
}
@@ -64,8 +79,8 @@ public boolean openUri(Context context, Uri uri, Bundle bundle, Integer requestC
6479
com.luojilab.component.componentlib.utils.UriUtils.setBundleValue(bundle, params, paramsType);
6580
Intent intent = new Intent(context, target);
6681
intent.putExtras(bundle);
67-
if (requestCode > 0 && context instanceof android.app.Activity) {
68-
((android.app.Activity) context).startActivityForResult(intent, requestCode);
82+
if (requestCode > 0 && context instanceof Activity) {
83+
((Activity) context).startActivityForResult(intent, requestCode);
6984
return true;
7085
}
7186
context.startActivity(intent);
@@ -76,10 +91,19 @@ public boolean openUri(Context context, Uri uri, Bundle bundle, Integer requestC
7691

7792
@Override
7893
public boolean verifyUri(Uri uri) {
94+
if (!hasInitHost) {
95+
initHost();
96+
}
7997
String host = uri.getHost();
98+
if (!HOST.equals(host)) {
99+
return false;
100+
}
101+
if (!hasInitMap) {
102+
initMap();
103+
}
80104
List<String> pathSegments = uri.getPathSegments();
81105
String path = "/" + TextUtils.join("/", pathSegments);
82-
return HOST.equals(host) && routeMapper.containsKey(path);
106+
return routeMapper.containsKey(path);
83107
}
84108
}
85109

componentlib/src/main/java/com/luojilab/component/componentlib/router/ui/IComponentRouter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* router behaviors for component type
99
* Created by mrzhang on 2017/6/20.
1010
*/
11-
public interface IComponentRouter {
11+
public interface IComponentRouter {
1212

1313
/**
1414
* 打开一个链接

componentlib/src/main/java/com/luojilab/component/componentlib/utils/UriUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static int parseInt(String src, int defaultValue) {
8383
}
8484

8585
public static void setBundleValue(Bundle bundle, Map<String, String> params, Map<String, Integer> paramsType) {
86-
if (!paramsType.isEmpty() && !params.isEmpty()) {
86+
if (paramsType != null && params != null && !paramsType.isEmpty() && !params.isEmpty()) {
8787
for (Map.Entry<String, Integer> param : paramsType.entrySet()) {
8888
UriUtils.setBundleValue(bundle, param.getValue(), param.getKey(), params.get(param.getKey()));
8989
}

readercomponent/build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ android {
1111

1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313

14+
javaCompileOptions {
15+
annotationProcessorOptions {
16+
arguments = [host: "reader"]
17+
}
18+
}
19+
1420
}
1521

1622
buildTypes {
@@ -32,7 +38,7 @@ android {
3238
dependencies {
3339
compile project(':componentservice')
3440
compile fileTree(include: ['*.jar'], dir: 'libs')
35-
41+
annotationProcessor project(':router-anno-compiler')
3642
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
3743
exclude group: 'com.android.support', module: 'support-annotations'
3844
})

readercomponent/src/main/java/com/luojilab/reader/ReaderFragment.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class ReaderFragment extends Fragment {
2020

2121
private View rootView;
2222

23+
private final static int REQUEST_CODE = 7777;
24+
2325
@Nullable
2426
@Override
2527
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@@ -28,14 +30,27 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
2830
rootView = inflater.inflate(R.layout.readerbook_fragment_reader, container,
2931
false);
3032

31-
rootView.findViewById(R.id.tv_content).setOnClickListener(new View.OnClickListener() {
33+
rootView.findViewById(R.id.tv_1).setOnClickListener(new View.OnClickListener() {
34+
35+
@Override
36+
public void onClick(View v) {
37+
goToShareActivityWithBundle();
38+
}
39+
});
40+
rootView.findViewById(R.id.tv_2).setOnClickListener(new View.OnClickListener() {
3241

3342
@Override
3443
public void onClick(View v) {
3544
goToShareActivityWithUri();
3645
}
3746
});
47+
rootView.findViewById(R.id.tv_3).setOnClickListener(new View.OnClickListener() {
3848

49+
@Override
50+
public void onClick(View v) {
51+
goToShareActivityForResult();
52+
}
53+
});
3954

4055
}
4156
return rootView;
@@ -49,16 +64,16 @@ private void goToShareActivityWithBundle() {
4964
Bundle bundle = new Bundle();
5065
bundle.putString("bookName", "Gone with the Wind");
5166
bundle.putString("author", JsonService.Factory.getInstance().create().toJsonString(author));
52-
UIRouter.getInstance().openUri(getActivity(), "dunb://share/shareBook", bundle);
67+
UIRouter.getInstance().openUri(getActivity(), "DDComp://share/shareBook", bundle);
5368
}
5469

5570
// UI transfer with URI
5671
private void goToShareActivityWithUri() {
5772
Author author = new Author();
58-
author.setName("Margaret Mitchell");
59-
author.setCounty("USA");
73+
author.setName("Barack Obama");
74+
author.setCounty("New York");
6075
UIRouter.getInstance().openUri(getActivity(),
61-
"dunb://share/shareBook?bookName=Gone with the Wind&author="
76+
"DDComp://share/shareMagazine?bookName=NYTIME&author="
6277
+ JsonService.Factory.getInstance().create().toJsonString(author), null);
6378
}
6479

@@ -68,8 +83,8 @@ private void goToShareActivityForResult() {
6883
author.setName("Margaret Mitchell");
6984
author.setCounty("USA");
7085
UIRouter.getInstance().openUri(getActivity(),
71-
"dunb://share/shareBook?bookName=Gone with the Wind&author="
72-
+ JsonService.Factory.getInstance().create().toJsonString(author), null);
86+
"DDComp://share/shareBook?bookName=Gone with the Wind&author="
87+
+ JsonService.Factory.getInstance().create().toJsonString(author), null, REQUEST_CODE);
7388
}
7489

7590
}

0 commit comments

Comments
 (0)