Skip to content

Commit

Permalink
Merge pull request #2 from wyjsonGo/develop
Browse files Browse the repository at this point in the history
1.URI自动组装path、type 
2.开启日志可以检查路由是否有重复提交的情况 
3.优化路由元数据冗余 
4.生成JSON格式文档功能
  • Loading branch information
wyjsonGo authored Oct 21, 2023
2 parents 82793ec + 6ad395b commit 7aa1adb
Show file tree
Hide file tree
Showing 21 changed files with 518 additions and 100 deletions.
1 change: 1 addition & 0 deletions GoRouter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ android {

dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.code.gson:gson:2.10.1'
}
21 changes: 11 additions & 10 deletions GoRouter/src/main/java/com/wyjson/router/core/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@

public final class Card extends CardMeta {

private final Uri uri;
private Uri uri;
private Bundle mBundle;
private int flags = 0;
private boolean greenChannel;
private boolean greenChannel;// 绿色通道(跳过所有的拦截器)
private String action;
private Context context;

private Bundle optionsCompat;
private int enterAnim = -1;
private int enterAnim = -1;// 转场动画
private int exitAnim = -1;
private Bundle optionsCompat;// 转场动画(API16+)

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

private void setUri(Uri uri) {
this.uri = uri;
setPath(uri.getPath());
}

public Uri getUri() {
return uri;
}
Expand All @@ -49,16 +54,12 @@ public int getExitAnim() {
}

public Card(Uri uri) {
this(uri.getPath(), uri, null);
setUri(uri);
this.mBundle = new Bundle();
}

public Card(String path, Bundle bundle) {
this(path, null, bundle);
}

public Card(String path, Uri uri, Bundle bundle) {
setPath(path);
this.uri = uri;
this.mBundle = (null == bundle ? new Bundle() : bundle);
}

Expand Down
16 changes: 11 additions & 5 deletions GoRouter/src/main/java/com/wyjson/router/core/CardMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ public class CardMeta {
private String path;
private RouteType type;
private Class<?> pathClass;
private int tag;
private Map<String, ParamType> paramsType;
private int tag;// 额外的标记
private Map<String, ParamType> paramsType;// <参数名, 参数类型>

protected CardMeta() {
}

public CardMeta(String path, Class<?> pathClass, int tag, Map<String, ParamType> paramsType) {
setPath(path);
this.pathClass = pathClass;
this.tag = tag;
this.paramsType = paramsType;
}

public String getPath() {
return path;
}
Expand Down Expand Up @@ -73,8 +80,7 @@ private RouteType extractType(String path) {


public void commit(Class<?> cls) {
setPathClass(cls);
GoRouter.getInstance().addCardMeta(this);
GoRouter.getInstance().addCardMeta(new CardMeta(this.path, cls, this.tag, this.paramsType));
}

public CardMeta putTag(int tag) {
Expand Down Expand Up @@ -138,7 +144,7 @@ public CardMeta putParcelable(String key) {
}

@NonNull
public String toSuperString() {
public String toString() {
if (!GoRouter.logger.isShowLog()) {
return "";
}
Expand Down
43 changes: 36 additions & 7 deletions GoRouter/src/main/java/com/wyjson/router/core/GoRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.arch.core.util.Function;
import androidx.core.app.ActivityCompat;
import androidx.core.util.Supplier;
import androidx.fragment.app.Fragment;

import com.wyjson.router.callback.GoCallback;
import com.wyjson.router.document.DocumentModel;
import com.wyjson.router.document.DocumentUtils;
import com.wyjson.router.enums.ParamType;
import com.wyjson.router.enums.RouteType;
import com.wyjson.router.exception.RouterException;
Expand All @@ -26,15 +30,14 @@
import com.wyjson.router.interfaces.IInterceptor;
import com.wyjson.router.interfaces.IService;
import com.wyjson.router.interfaces.PretreatmentService;
import com.wyjson.router.logger.DefaultLogger;
import com.wyjson.router.logger.ILogger;
import com.wyjson.router.service.ServiceHelper;
import com.wyjson.router.thread.DefaultPoolExecutor;
import com.wyjson.router.utils.DefaultLogger;
import com.wyjson.router.utils.ILogger;
import com.wyjson.router.utils.MapUtils;
import com.wyjson.router.utils.TextUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

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

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

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

public static String generateDocument() {
return generateDocument(null);
}

/**
* 生成JSON格式文档
*
* @param tagFunction 不处理返回默认int类型tag,实现方法可自定义返回tag,示例[LOGIN, AUTHENTICATION]
* @return JSON格式文档
*/
public static String generateDocument(Function<Integer, String> tagFunction) {
return DocumentUtils.generate(new DocumentModel(routes, ServiceHelper.getInstance().getServices(), InterceptorUtils.getInterceptors()), tagFunction);
}

@Nullable
CardMeta getCardMeta(Card card) {
CardMeta cardMeta = routes.get(card.getPath());
if (cardMeta != null) {
GoRouter.logger.info(null, "[getCardMeta] " + cardMeta.toSuperString());
logger.info(null, "[getCardMeta] " + cardMeta.toString());
} else {
GoRouter.logger.warning(null, "[getCardMeta] null");
logger.warning(null, "[getCardMeta] null");
}
return cardMeta;
}

void addCardMeta(CardMeta cardMeta) {
if (cardMeta.getType() != null) {
// 检查路由是否有重复提交的情况
if (logger.isShowLog()) {
for (Map.Entry<String, CardMeta> cardMetaEntry : routes.entrySet()) {
if (TextUtils.equals(cardMetaEntry.getKey(), cardMeta.getPath())) {
logger.error(null, "[addCardMeta] Path duplicate commit!!! path[" + cardMetaEntry.getValue().getPath() + "]");
break;
} else if (cardMetaEntry.getValue().getPathClass() == cardMeta.getPathClass()) {
logger.error(null, "[addCardMeta] PathClass duplicate commit!!! pathClass[" + cardMetaEntry.getValue().getPathClass() + "]");
break;
}
}
}
routes.put(cardMeta.getPath(), cardMeta);
GoRouter.logger.debug(null, "[addCardMeta] size:" + routes.size() + ", commit:" + cardMeta.toSuperString());
logger.debug(null, "[addCardMeta] size:" + routes.size() + ", commit:" + cardMeta.toString());
} else {
throw new RouterException("The route type is incorrect! The path[" + cardMeta.getPath() + "] type can only end with " + RouteType.toStringByValues());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wyjson.router.core;

import java.util.HashMap;

public class RouteHashMap<K, V> extends HashMap<K, V> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.wyjson.router.document;

import com.wyjson.router.core.CardMeta;
import com.wyjson.router.interfaces.IInterceptor;
import com.wyjson.router.interfaces.IService;
import com.wyjson.router.service.ServiceMeta;

import java.io.Serializable;
import java.util.Map;

public class DocumentModel implements Serializable {

private final Map<String, CardMeta> routes;
private final Map<Class<? extends IService>, ServiceMeta> services;
private final Map<Integer, IInterceptor> interceptors;

public DocumentModel(Map<String, CardMeta> routes, Map<Class<? extends IService>, ServiceMeta> services, Map<Integer, IInterceptor> interceptors) {
this.routes = routes;
this.services = services;
this.interceptors = interceptors;
}

public Map<String, CardMeta> getRoutes() {
return routes;
}

public Map<Class<? extends IService>, ServiceMeta> getServices() {
return services;
}

public Map<Integer, IInterceptor> getInterceptors() {
return interceptors;
}

}
Loading

0 comments on commit 7aa1adb

Please sign in to comment.