Skip to content

Commit 7aa1adb

Browse files
authored
Merge pull request #2 from wyjsonGo/develop
1.URI自动组装path、type 2.开启日志可以检查路由是否有重复提交的情况 3.优化路由元数据冗余 4.生成JSON格式文档功能
2 parents 82793ec + 6ad395b commit 7aa1adb

File tree

21 files changed

+518
-100
lines changed

21 files changed

+518
-100
lines changed

GoRouter/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ android {
2929

3030
dependencies {
3131
implementation 'androidx.appcompat:appcompat:1.6.1'
32+
implementation 'com.google.code.gson:gson:2.10.1'
3233
}

GoRouter/src/main/java/com/wyjson/router/core/Card.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,25 @@
1818

1919
public final class Card extends CardMeta {
2020

21-
private final Uri uri;
21+
private Uri uri;
2222
private Bundle mBundle;
2323
private int flags = 0;
24-
private boolean greenChannel;
24+
private boolean greenChannel;// 绿色通道(跳过所有的拦截器)
2525
private String action;
2626
private Context context;
2727

28-
private Bundle optionsCompat;
29-
private int enterAnim = -1;
28+
private int enterAnim = -1;// 转场动画
3029
private int exitAnim = -1;
30+
private Bundle optionsCompat;// 转场动画(API16+)
3131

3232
private Throwable interceptorException;// 拦截执行中断异常信息
3333
private int timeout = 300;// go() timeout, TimeUnit.Second
3434

35+
private void setUri(Uri uri) {
36+
this.uri = uri;
37+
setPath(uri.getPath());
38+
}
39+
3540
public Uri getUri() {
3641
return uri;
3742
}
@@ -49,16 +54,12 @@ public int getExitAnim() {
4954
}
5055

5156
public Card(Uri uri) {
52-
this(uri.getPath(), uri, null);
57+
setUri(uri);
58+
this.mBundle = new Bundle();
5359
}
5460

5561
public Card(String path, Bundle bundle) {
56-
this(path, null, bundle);
57-
}
58-
59-
public Card(String path, Uri uri, Bundle bundle) {
6062
setPath(path);
61-
this.uri = uri;
6263
this.mBundle = (null == bundle ? new Bundle() : bundle);
6364
}
6465

GoRouter/src/main/java/com/wyjson/router/core/CardMeta.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ public class CardMeta {
1313
private String path;
1414
private RouteType type;
1515
private Class<?> pathClass;
16-
private int tag;
17-
private Map<String, ParamType> paramsType;
16+
private int tag;// 额外的标记
17+
private Map<String, ParamType> paramsType;// <参数名, 参数类型>
1818

1919
protected CardMeta() {
2020
}
2121

22+
public CardMeta(String path, Class<?> pathClass, int tag, Map<String, ParamType> paramsType) {
23+
setPath(path);
24+
this.pathClass = pathClass;
25+
this.tag = tag;
26+
this.paramsType = paramsType;
27+
}
28+
2229
public String getPath() {
2330
return path;
2431
}
@@ -73,8 +80,7 @@ private RouteType extractType(String path) {
7380

7481

7582
public void commit(Class<?> cls) {
76-
setPathClass(cls);
77-
GoRouter.getInstance().addCardMeta(this);
83+
GoRouter.getInstance().addCardMeta(new CardMeta(this.path, cls, this.tag, this.paramsType));
7884
}
7985

8086
public CardMeta putTag(int tag) {
@@ -138,7 +144,7 @@ public CardMeta putParcelable(String key) {
138144
}
139145

140146
@NonNull
141-
public String toSuperString() {
147+
public String toString() {
142148
if (!GoRouter.logger.isShowLog()) {
143149
return "";
144150
}

GoRouter/src/main/java/com/wyjson/router/core/GoRouter.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
import androidx.annotation.NonNull;
1313
import androidx.annotation.Nullable;
14+
import androidx.arch.core.util.Function;
1415
import androidx.core.app.ActivityCompat;
16+
import androidx.core.util.Supplier;
1517
import androidx.fragment.app.Fragment;
1618

1719
import com.wyjson.router.callback.GoCallback;
20+
import com.wyjson.router.document.DocumentModel;
21+
import com.wyjson.router.document.DocumentUtils;
1822
import com.wyjson.router.enums.ParamType;
1923
import com.wyjson.router.enums.RouteType;
2024
import com.wyjson.router.exception.RouterException;
@@ -26,15 +30,14 @@
2630
import com.wyjson.router.interfaces.IInterceptor;
2731
import com.wyjson.router.interfaces.IService;
2832
import com.wyjson.router.interfaces.PretreatmentService;
33+
import com.wyjson.router.logger.DefaultLogger;
34+
import com.wyjson.router.logger.ILogger;
2935
import com.wyjson.router.service.ServiceHelper;
3036
import com.wyjson.router.thread.DefaultPoolExecutor;
31-
import com.wyjson.router.utils.DefaultLogger;
32-
import com.wyjson.router.utils.ILogger;
3337
import com.wyjson.router.utils.MapUtils;
3438
import com.wyjson.router.utils.TextUtils;
3539

3640
import java.lang.reflect.Field;
37-
import java.util.HashMap;
3841
import java.util.Map;
3942
import java.util.concurrent.ThreadPoolExecutor;
4043

@@ -44,7 +47,7 @@ public final class GoRouter {
4447
public static final String ROUTER_PARAM_INJECT = "router_param_inject";
4548

4649
private final Handler mHandler = new Handler(Looper.getMainLooper());
47-
private static final Map<String, CardMeta> routes = new HashMap<>();
50+
private static final Map<String, CardMeta> routes = new RouteHashMap<>();
4851
private volatile static ThreadPoolExecutor executor = DefaultPoolExecutor.getInstance();
4952
public static ILogger logger = new DefaultLogger("GoRouter");
5053

@@ -86,21 +89,47 @@ public static synchronized void printStackTrace() {
8689
logger.info(null, "[printStackTrace]");
8790
}
8891

92+
public static String generateDocument() {
93+
return generateDocument(null);
94+
}
95+
96+
/**
97+
* 生成JSON格式文档
98+
*
99+
* @param tagFunction 不处理返回默认int类型tag,实现方法可自定义返回tag,示例[LOGIN, AUTHENTICATION]
100+
* @return JSON格式文档
101+
*/
102+
public static String generateDocument(Function<Integer, String> tagFunction) {
103+
return DocumentUtils.generate(new DocumentModel(routes, ServiceHelper.getInstance().getServices(), InterceptorUtils.getInterceptors()), tagFunction);
104+
}
105+
89106
@Nullable
90107
CardMeta getCardMeta(Card card) {
91108
CardMeta cardMeta = routes.get(card.getPath());
92109
if (cardMeta != null) {
93-
GoRouter.logger.info(null, "[getCardMeta] " + cardMeta.toSuperString());
110+
logger.info(null, "[getCardMeta] " + cardMeta.toString());
94111
} else {
95-
GoRouter.logger.warning(null, "[getCardMeta] null");
112+
logger.warning(null, "[getCardMeta] null");
96113
}
97114
return cardMeta;
98115
}
99116

100117
void addCardMeta(CardMeta cardMeta) {
101118
if (cardMeta.getType() != null) {
119+
// 检查路由是否有重复提交的情况
120+
if (logger.isShowLog()) {
121+
for (Map.Entry<String, CardMeta> cardMetaEntry : routes.entrySet()) {
122+
if (TextUtils.equals(cardMetaEntry.getKey(), cardMeta.getPath())) {
123+
logger.error(null, "[addCardMeta] Path duplicate commit!!! path[" + cardMetaEntry.getValue().getPath() + "]");
124+
break;
125+
} else if (cardMetaEntry.getValue().getPathClass() == cardMeta.getPathClass()) {
126+
logger.error(null, "[addCardMeta] PathClass duplicate commit!!! pathClass[" + cardMetaEntry.getValue().getPathClass() + "]");
127+
break;
128+
}
129+
}
130+
}
102131
routes.put(cardMeta.getPath(), cardMeta);
103-
GoRouter.logger.debug(null, "[addCardMeta] size:" + routes.size() + ", commit:" + cardMeta.toSuperString());
132+
logger.debug(null, "[addCardMeta] size:" + routes.size() + ", commit:" + cardMeta.toString());
104133
} else {
105134
throw new RouterException("The route type is incorrect! The path[" + cardMeta.getPath() + "] type can only end with " + RouteType.toStringByValues());
106135
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.wyjson.router.core;
2+
3+
import java.util.HashMap;
4+
5+
public class RouteHashMap<K, V> extends HashMap<K, V> {
6+
7+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.wyjson.router.document;
2+
3+
import com.wyjson.router.core.CardMeta;
4+
import com.wyjson.router.interfaces.IInterceptor;
5+
import com.wyjson.router.interfaces.IService;
6+
import com.wyjson.router.service.ServiceMeta;
7+
8+
import java.io.Serializable;
9+
import java.util.Map;
10+
11+
public class DocumentModel implements Serializable {
12+
13+
private final Map<String, CardMeta> routes;
14+
private final Map<Class<? extends IService>, ServiceMeta> services;
15+
private final Map<Integer, IInterceptor> interceptors;
16+
17+
public DocumentModel(Map<String, CardMeta> routes, Map<Class<? extends IService>, ServiceMeta> services, Map<Integer, IInterceptor> interceptors) {
18+
this.routes = routes;
19+
this.services = services;
20+
this.interceptors = interceptors;
21+
}
22+
23+
public Map<String, CardMeta> getRoutes() {
24+
return routes;
25+
}
26+
27+
public Map<Class<? extends IService>, ServiceMeta> getServices() {
28+
return services;
29+
}
30+
31+
public Map<Integer, IInterceptor> getInterceptors() {
32+
return interceptors;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)