Skip to content

Commit

Permalink
Merge pull request #36 from wyjsonGo/develop
Browse files Browse the repository at this point in the history
publish v2.4.4
添加路由Tag工具,支持更多对Tag的操作
  • Loading branch information
wyjsonGo authored Dec 26, 2023
2 parents f4dea68 + 5a1a1a2 commit f49b608
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 122 deletions.
26 changes: 26 additions & 0 deletions GoRouter-Api/src/main/java/com/wyjson/router/model/CardMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import com.wyjson.router.enums.ParamType;
import com.wyjson.router.enums.RouteType;
import com.wyjson.router.exception.RouterException;
import com.wyjson.router.utils.RouteTagUtils;
import com.wyjson.router.utils.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -238,6 +240,30 @@ private CardMeta put(String key, String name, ParamType type, boolean required)
return this;
}

public boolean isTagExist(int item) {
return RouteTagUtils.isExist(tag, item);
}

public int getTagExistCount() {
return RouteTagUtils.getExistCount(tag);
}

public int addTag(int item) {
return tag = RouteTagUtils.addItem(tag, item);
}

public int deleteTag(int item) {
return tag = RouteTagUtils.deleteItem(tag, item);
}

public int getTagNegation() {
return RouteTagUtils.getNegation(tag);
}

public ArrayList<Integer> getTagExistList(int... itemList) {
return RouteTagUtils.getExistList(tag, itemList);
}

@NonNull
public String toString() {
if (!GoRouter.isDebug()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.wyjson.router.utils;

import java.util.ArrayList;

/**
* 单个int有4字节,可以配置31个开关,范围从1到1 << 30
*/
public class RouteTagUtils {

/**
* 是否存在
*
* @param v
* @param item
* @return
*/
public static boolean isExist(int v, int item) {
return (v & item) > 0;
}

/**
* 存在多少个
*
* @param v
* @return
*/
public static int getExistCount(int v) {
return Integer.bitCount(v);
}

/**
* 添加
*
* @param v
* @param item
* @return
*/
public static int addItem(int v, int item) {
return v | item;
}

/**
* 删除
*
* @param v
* @param item
* @return
*/
public static int deleteItem(int v, int item) {
return v & ~item;
}

/**
* 取反
*
* @param v
* @return
*/
public static int getNegation(int v) {
return ~v;
}

/**
* 获取所有存在的
*
* @param v
* @param itemList
* @return
*/
public static ArrayList<Integer> getExistList(int v, int... itemList) {
ArrayList<Integer> list = new ArrayList<>();
for (int item : itemList) {
if (isExist(v, item))
list.add(item);
}
return list;
}

}
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dependencyResolutionManagement {
}
dependencies {
api 'com.github.wyjsonGo.GoRouter:GoRouter-Api:2.4.3'
api 'com.github.wyjsonGo.GoRouter:GoRouter-Api:2.4.4'
}
// Kotlin配置参见8-1
```
Expand All @@ -89,7 +89,7 @@ android {
}
dependencies {
annotationProcessor 'com.github.wyjsonGo.GoRouter:GoRouter-Compiler:2.4.3'
annotationProcessor 'com.github.wyjsonGo.GoRouter:GoRouter-Compiler:2.4.4'
}
```

Expand Down Expand Up @@ -157,7 +157,7 @@ pluginManagement {
// 项目根目录下的build.gradle
buildscript {
dependencies {
classpath 'com.github.wyjsonGo.GoRouter:GoRouter-Gradle-Plugin:2.4.3'
classpath 'com.github.wyjsonGo.GoRouter:GoRouter-Gradle-Plugin:2.4.4'
}
}
```
Expand Down Expand Up @@ -370,9 +370,16 @@ public class DegradeServiceImpl implements IDegradeService {

```java
// 我们经常需要在目标页面中配置一些属性,比方说"是否需要登陆"之类的
// 可以通过@Route注解的tag属性进行扩展,这个属性是一个 int值,换句话说,单个int有4字节,可以配置31个开关
// 剩下的可以自行发挥,通过字节操作可以标识31个开关,通过开关标记目标页面的一些属性,在拦截器中可以拿到这个标记进行业务逻辑判断
// 可以注通过@Route解的tag属性进行扩展,这个属性是一个int值,换句话说,单个int有4字节,可以配置31个开关
@Route(path = "/user/info/activity", tag = LOGIN | AUTHENTICATION)

// 在拦截器里通过Card对象拿到这个标记进行业务逻辑判断或处理
card.isTagExist(LOGIN); // 判断是否存在登录标识
card.getTagExistCount(); // tag存在多少个开关
card.addTag(LOGIN); // 追加登录标识
card.deleteTag(LOGIN); // 删除登录标识
card.getTagNegation(); // 取反
ArrayList<Integer> tagList = card.getTagExistList(LOGIN, AUTHENTICATION, SAFETY); // 获取全部存在的标识
```

tag使用示例[UserInfoActivity.java](https://github.com/wyjsonGo/GoRouter/blob/master/module_user/src/main/java/com/wyjson/module_user/activity/UserInfoActivity.java)
Expand Down Expand Up @@ -890,7 +897,7 @@ kapt {
}

dependencies {
kapt 'com.github.wyjsonGo.GoRouter:GoRouter-Compiler:2.4.3'
kapt 'com.github.wyjsonGo.GoRouter:GoRouter-Compiler:2.4.4'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
dependencies {
classpath "com.github.wyjsonGo.GoRouter:GoRouter-Gradle-Plugin:${VERSION}"
// classpath "com.github.wyjsonGo.GoRouter:GoRouter-Gradle-Plugin:${VERSION}"
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ android.nonTransitiveRClass=true
# org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

GROUP_ID=com.github.wyjsonGo.GoRouter
VERSION=2.4.3
VERSION=2.4.4
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wyjson.module_common.route;

/**
* 单个int有4字节,可以配置31个开关,范围从1到1 << 30
*/
public interface RouteTag {

int LOGIN = 1;
int AUTHENTICATION = 1 << 1;
int SAFETY = 1 << 2;

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.wyjson.module_user.activity;

import static com.wyjson.module_common.route.utils.RouteTagUtils.AUTHENTICATION;
import static com.wyjson.module_common.route.utils.RouteTagUtils.LOGIN;
import static com.wyjson.module_common.route.RouteTag.AUTHENTICATION;
import static com.wyjson.module_common.route.RouteTag.LOGIN;

import android.os.Bundle;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.wyjson.module_user.route.interceptor;

import static com.wyjson.module_common.route.utils.RouteTagUtils.TagEnum.AUTHENTICATION;
import static com.wyjson.module_common.route.RouteTag.AUTHENTICATION;

import com.wyjson.module_common.route.utils.RouteTagUtils;
import com.wyjson.router.annotation.Interceptor;
import com.wyjson.router.model.Card;
import com.wyjson.router.exception.RouterException;
import com.wyjson.router.callback.InterceptorCallback;
import com.wyjson.router.exception.RouterException;
import com.wyjson.router.interfaces.IInterceptor;
import com.wyjson.router.model.Card;

@Interceptor(ordinal = 100, remark = "身份验证拦截器")
public class AuthenticationInterceptor implements IInterceptor {
Expand All @@ -19,7 +18,7 @@ public void init() {

@Override
public void process(Card card, InterceptorCallback callback) {
if (RouteTagUtils.TagEnum.isExist(card.getTag(), AUTHENTICATION)) {
if (card.isTagExist(AUTHENTICATION)) {
if (false) {// 判断用户是否身份验证了
callback.onInterrupt(card, new RouterException("未身份认证,拦截!"));
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.wyjson.module_user.route.interceptor;

import static com.wyjson.module_common.route.RouteTag.LOGIN;

import android.os.Handler;
import android.os.Looper;

import androidx.appcompat.app.AlertDialog;

import com.wyjson.module_common.route.utils.RouteTagUtils;
import com.wyjson.router.annotation.Interceptor;
import com.wyjson.router.callback.InterceptorCallback;
import com.wyjson.router.exception.RouterException;
Expand All @@ -16,15 +17,16 @@
@Interceptor(ordinal = 1, remark = "登录拦截器")
public class SignInInterceptor implements IInterceptor {

private final Handler handler = new Handler(Looper.getMainLooper());

@Override
public void init() {

}

@Override
public void process(Card card, InterceptorCallback callback) {
if (RouteTagUtils.TagEnum.isExist(card.getTag(), RouteTagUtils.TagEnum.LOGIN)) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
if (card.isTagExist(LOGIN)) {
handler.post(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(card.getContext())
Expand Down
10 changes: 5 additions & 5 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ dependencyResolutionManagement {

rootProject.name = "GoRouter"

include ':app'
include ':module_main'
include ':module_user'
include ':module_kotlin'
include ':module_common'
//include ':app'
//include ':module_main'
//include ':module_user'
//include ':module_kotlin'
//include ':module_common'

include ':GoRouter-Api'
include ':GoRouter-Annotation'
Expand Down

0 comments on commit f49b608

Please sign in to comment.