Skip to content

Commit

Permalink
Merge branch 'master' into master-androidx
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojinzi123 committed Mar 13, 2020
2 parents 80a82d5 + 256cea4 commit 4f17370
Show file tree
Hide file tree
Showing 29 changed files with 237 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.xiaojinzi.component.anno.router;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 标记一个 Action 是跳转前的 Action
*/
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.CLASS)
public @interface AfterStartActionAnno {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.xiaojinzi.component.anno.router;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 标记一个 Action 是跳转前的 Action
*/
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.CLASS)
public @interface BeforStartActionAnno {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import com.xiaojinzi.component.anno.router.AfterActionAnno;
import com.xiaojinzi.component.anno.router.AfterErrorActionAnno;
import com.xiaojinzi.component.anno.router.AfterEventActionAnno;
import com.xiaojinzi.component.anno.router.AfterStartActionAnno;
import com.xiaojinzi.component.anno.router.BeforActionAnno;
import com.xiaojinzi.component.anno.router.BeforStartActionAnno;
import com.xiaojinzi.component.anno.router.CategoryAnno;
import com.xiaojinzi.component.anno.router.FlagAnno;
import com.xiaojinzi.component.anno.router.HostAndPathAnno;
Expand Down Expand Up @@ -262,6 +264,8 @@ private void implementInterfaceMethod(TypeSpec.Builder typeSpecBuilder, Executab
VariableElement activityBundleOptionsParameter = null;
VariableElement intentConsumerParameter = null;
VariableElement beforActionParameter = null;
VariableElement beforStartActionParameter = null;
VariableElement afterStartActionParameter = null;
VariableElement afterActionParameter = null;
VariableElement afterErrorActionParameter = null;
VariableElement afterEventActionParameter = null;
Expand All @@ -288,6 +292,10 @@ private void implementInterfaceMethod(TypeSpec.Builder typeSpecBuilder, Executab
intentConsumerParameter = parameter;
} else if (parameter.getAnnotation(BeforActionAnno.class) != null) { // 如果是 beforAction
beforActionParameter = parameter;
} else if (parameter.getAnnotation(BeforStartActionAnno.class) != null) { // 如果是 beforStartAction
beforStartActionParameter = parameter;
} else if (parameter.getAnnotation(AfterStartActionAnno.class) != null) { // 如果是 afterStartAction
afterStartActionParameter = parameter;
} else if (parameter.getAnnotation(AfterActionAnno.class) != null) { // 如果是 afterAction
afterActionParameter = parameter;
} else if (parameter.getAnnotation(AfterErrorActionAnno.class) != null) { // 如果是 afterErrorAction
Expand Down Expand Up @@ -616,11 +624,19 @@ private void implementInterfaceMethod(TypeSpec.Builder typeSpecBuilder, Executab

// 几个 action
if (beforActionParameter != null) {
routerStatement.append("\n.beforJumpAction($N)");
routerStatement.append("\n.beforAction($N)");
args.add(beforActionParameter.getSimpleName().toString());
}
if (beforStartActionParameter != null) {
routerStatement.append("\n.beforStartAction($N)");
args.add(beforStartActionParameter.getSimpleName().toString());
}
if (afterStartActionParameter != null) {
routerStatement.append("\n.afterStartAction($N)");
args.add(afterStartActionParameter.getSimpleName().toString());
}
if (afterActionParameter != null) {
routerStatement.append("\n.afterJumpAction($N)");
routerStatement.append("\n.afterAction($N)");
args.add(afterActionParameter.getSimpleName().toString());
}
if (afterErrorActionParameter != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,26 @@ public Navigator addIntentCategories(@Nullable String... categories) {
}

@Override
public Navigator beforJumpAction(@Nullable @MainThread Action action) {
super.beforJumpAction(action);
public Navigator beforAction(@Nullable @MainThread Action action) {
super.beforAction(action);
return this;
}

@Override
public Navigator afterJumpAction(@Nullable @MainThread Action action) {
super.afterJumpAction(action);
public Navigator beforStartAction(@Nullable Action action) {
super.beforStartAction(action);
return this;
}

@Override
public Navigator afterStartAction(@Nullable Action action) {
super.afterStartAction(action);
return this;
}

@Override
public Navigator afterAction(@Nullable @MainThread Action action) {
super.afterAction(action);
return this;
}

Expand Down Expand Up @@ -814,8 +826,10 @@ public synchronized NavigationDisposable navigate(
queryMap = null;
bundle = null;
intentConsumer = null;
beforJumpAction = null;
afterJumpAction = null;
beforAction = null;
beforStartAction = null;
afterStartAction = null;
afterAction = null;
afterErrorAction = null;
afterEventAction = null;
}
Expand Down Expand Up @@ -976,7 +990,7 @@ public List<RouterInterceptor> get() {
@Override
public void intercept(Chain chain) throws Exception {
// 执行跳转前的 Callback
RouterRequestHelp.executeBeforCallback(chain.request());
RouterRequestHelp.executeBeforAction(chain.request());
// 继续下一个拦截器
chain.proceed(chain.request());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,41 +187,51 @@ private void doStartIntent(@NonNull RouterRequest request, Intent intent) throws
);
}

if (request.beforStartAction != null) {
request.beforStartAction.run();
}

// ------------------------------- 启动界面核心代码 ------------------------------- START

// 如果是普通的启动界面
if (request.requestCode == null) { // 如果是 startActivity
if (request.isForResult) { // 如果是 startActivity
// 使用 context 跳转 startActivityForResult
if (request.context != null) {
Fragment rxFragment = findFragment(request.context);
Activity rawAct = null;
if (rxFragment != null) {
rxFragment.startActivityForResult(intent, request.requestCode, request.options);
} else if ((rawAct = Utils.getActivityFromContext(request.context)) != null) {
rawAct.startActivityForResult(intent, request.requestCode, request.options);
} else {
throw new NavigationFailException("Context is not a Activity,so can't use 'startActivityForResult' method");
}
} else if (request.fragment != null) { // 使用 Fragment 跳转
Fragment rxFragment = findFragment(request.fragment);
if (rxFragment != null) {
rxFragment.startActivityForResult(intent, request.requestCode, request.options);
} else {
request.fragment.startActivityForResult(intent, request.requestCode, request.options);
}
} else {
throw new NavigationFailException("the context or fragment both are null");
}
}else {
if (request.context != null) {
request.context.startActivity(intent, request.options);
} else if (request.fragment != null) {
request.fragment.startActivity(intent, request.options);
} else {
throw new NavigationFailException("the context or fragment both are null");
}
return;
}

// 使用 context 跳转 startActivityForResult
if (request.context != null) {
Fragment rxFragment = findFragment(request.context);
boolean isUseRxFragment = rxFragment != null && request.isForResult;
Activity rawAct = null;
if (isUseRxFragment) {
rxFragment.startActivityForResult(intent, request.requestCode, request.options);
} else if ((rawAct = Utils.getActivityFromContext(request.context)) != null) {
rawAct.startActivityForResult(intent, request.requestCode, request.options);
} else {
throw new NavigationFailException("Context is not a Activity,so can't use 'startActivityForResult' method");
}
} else if (request.fragment != null) { // 使用 Fragment 跳转
Fragment rxFragment = findFragment(request.fragment);
boolean isUseRxFragment = rxFragment != null && request.isForResult;
if (isUseRxFragment) {
rxFragment.startActivityForResult(intent, request.requestCode, request.options);
} else {
request.fragment.startActivityForResult(intent, request.requestCode, request.options);
}
} else {
throw new NavigationFailException("the context or fragment both are null");
// ------------------------------- 启动界面核心代码 ------------------------------- END

if (request.afterStartAction != null) {
request.afterStartAction.run();
}

}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,53 @@ public class RouterRequest {
@Nullable
public final Consumer<Intent> intentConsumer;

/**
* 这个 {@link Action} 是在路由开始的时候调用的.
* 和 {@link Activity#startActivity(Intent)} 不是连着执行的.
* 中间 post 到主线程的操作
*/
@Nullable
public final Action beforJumpAction;
public final Action beforAction;

/**
* 这个 {@link Action} 是在 {@link Activity#startActivity(Intent)} 之前调用的.
* 和 {@link Activity#startActivity(Intent)} 是连着执行的.
*/
@Nullable
public final Action afterJumpAction;
public final Action beforStartAction;

/**
* 这个 {@link Action} 是在 {@link Activity#startActivity(Intent)} 之后调用的.
* 和 {@link Activity#startActivity(Intent)} 是连着执行的.
*/
@Nullable
public final Action afterStartAction;

/**
* 这个 {@link Action} 是在结束之后调用的.
* 和 {@link Activity#startActivity(Intent)} 不是连着执行的.
* 是在 {@link RouterInterceptor.Callback#onSuccess(RouterResult)}
* 方法中 post 到主线程完成的
*/
@Nullable
public final Action afterAction;

/**
* 这个 {@link Action} 是在结束之后调用的.
* 和 {@link Activity#startActivity(Intent)} 不是连着执行的.
* 是在 {@link RouterInterceptor.Callback#onError(Throwable)}
* 方法中 post 到主线程完成的
*/
@Nullable
public final Action afterErrorAction;

/**
* 这个 {@link Action} 是在结束之后调用的.
* 和 {@link Activity#startActivity(Intent)} 不是连着执行的.
* 是在 {@link RouterInterceptor.Callback#onSuccess(RouterResult)} 或者
* {@link RouterInterceptor.Callback#onError(Throwable)}
* 方法中 post 到主线程完成的
*/
@Nullable
public final Action afterEventAction;

Expand Down Expand Up @@ -249,8 +287,10 @@ public Builder toBuilder() {
builder.intentFlags = new ArrayList<>(intentFlags);

builder.intentConsumer = intentConsumer;
builder.beforJumpAction = beforJumpAction;
builder.afterJumpAction = afterJumpAction;
builder.beforAction = beforAction;
builder.beforStartAction = beforStartAction;
builder.afterStartAction = afterStartAction;
builder.afterAction = afterAction;
builder.afterErrorAction = afterErrorAction;
builder.afterEventAction = afterEventAction;
return builder;
Expand All @@ -270,8 +310,10 @@ private RouterRequest(@NonNull Builder builder) {
this.bundle.putAll(builder.bundle);
}
intentConsumer = builder.intentConsumer;
beforJumpAction = builder.beforJumpAction;
afterJumpAction = builder.afterJumpAction;
beforAction = builder.beforAction;
beforStartAction = builder.beforStartAction;
afterStartAction = builder.afterStartAction;
afterAction = builder.afterAction;
afterErrorAction = builder.afterErrorAction;
afterEventAction = builder.afterEventAction;
}
Expand Down Expand Up @@ -322,7 +364,13 @@ public static class Builder extends URIBuilder {
* 跳转前的 Callback
*/
@Nullable
protected Action beforJumpAction;
protected Action beforAction;

@Nullable
protected Action beforStartAction;

@Nullable
protected Action afterStartAction;

/**
* 跳转成功之后的 Callback
Expand All @@ -331,7 +379,7 @@ public static class Builder extends URIBuilder {
* 当你拿到 Intent 的回调了, 和此回调已经没关系了
*/
@Nullable
protected Action afterJumpAction;
protected Action afterAction;

/**
* 跳转失败之后的 Callback
Expand All @@ -355,13 +403,23 @@ public Builder fragment(@Nullable Fragment fragment) {
return this;
}

public Builder beforJumpAction(@Nullable @MainThread Action action) {
this.beforJumpAction = action;
public Builder beforAction(@Nullable @MainThread Action action) {
this.beforAction = action;
return this;
}

public Builder beforStartAction(@Nullable @MainThread Action action) {
this.beforStartAction = action;
return this;
}

public Builder afterStartAction(@Nullable @MainThread Action action) {
this.afterStartAction = action;
return this;
}

public Builder afterJumpAction(@Nullable @MainThread Action action) {
this.afterJumpAction = action;
public Builder afterAction(@Nullable @MainThread Action action) {
this.afterAction = action;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static void errorCallbackOnMainThread(@Nullable final Callback callback,
// 执行 Request 中 的 errorCallback
if (errorResult.getOriginalRequest() != null) {
try {
RouterRequestHelp.executeAfterErrorCallback(errorResult.getOriginalRequest());
RouterRequestHelp.executeAfterErrorAction(errorResult.getOriginalRequest());
} catch (Exception e) {
throw new RouterRuntimeException("afterErrorCallback or afterEventCallback can't throw any exception!", e);
}
Expand Down Expand Up @@ -122,7 +122,7 @@ private static void successCallbackOnMainThread(@Nullable final Callback callbac
}
// 执行 Request 中 的 afterCallback
try {
RouterRequestHelp.executeAfterJumpCallback(result.getOriginalRequest());
RouterRequestHelp.executeAfterAction(result.getOriginalRequest());
} catch (Exception e) {
throw new RouterRuntimeException("afterJumpCallback or afterEventCallback can't throw any exception!", e);
}
Expand Down
Loading

0 comments on commit 4f17370

Please sign in to comment.