Skip to content

Commit 92aaa4f

Browse files
authored
Merge pull request #38 from wyjsonGo/develop
publish v2.4.5 添加go()无参方法
2 parents 63f0424 + af78d7b commit 92aaa4f

File tree

22 files changed

+120
-111
lines changed

22 files changed

+120
-111
lines changed

GoRouter-Api/src/main/java/com/wyjson/router/GoRouter.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public final class GoRouter {
5454
private volatile static ThreadPoolExecutor executor = DefaultPoolExecutor.getInstance();
5555
public static ILogger logger = new DefaultLogger();
5656
private volatile static boolean isDebug = false;
57+
private static Application mApplication;
5758

5859
private GoRouter() {
5960
InterceptorCenter.clearInterceptors();
@@ -74,6 +75,7 @@ public static GoRouter getInstance() {
7475
* @param application
7576
*/
7677
public static synchronized void autoLoadRouteModule(Application application) {
78+
setApplication(application);
7779
logger.info(null, "[GoRouter] autoLoadRouteModule!");
7880
RouteModuleCenter.load(application);
7981
}
@@ -93,6 +95,10 @@ public static synchronized void openDebug() {
9395
logger.info(null, "[openDebug]");
9496
}
9597

98+
public static void setApplication(Application application) {
99+
mApplication = application;
100+
}
101+
96102
public static boolean isDebug() {
97103
return isDebug;
98104
}
@@ -125,8 +131,9 @@ public boolean isAMRegisterMode() {
125131
return ApplicationModuleCenter.isRegisterByPlugin();
126132
}
127133

128-
public static void callAMOnCreate(Application app) {
129-
ApplicationModuleCenter.callOnCreate(app);
134+
public static void callAMOnCreate(Application application) {
135+
setApplication(application);
136+
ApplicationModuleCenter.callOnCreate(application);
130137
}
131138

132139
public static void callAMOnTerminate() {
@@ -348,10 +355,14 @@ private <T> void inject(T target, Intent intent, Bundle bundle, boolean isCheck)
348355

349356
@Nullable
350357
public Object go(Context context, Card card, int requestCode, ActivityResultLauncher<Intent> activityResultLauncher, GoCallback callback) {
358+
card.setContext(context == null ? mApplication : context);
359+
card.setInterceptorException(null);
360+
351361
logger.debug(null, "[go] " + card);
362+
352363
IPretreatmentService pretreatmentService = getService(IPretreatmentService.class);
353364
if (pretreatmentService != null) {
354-
if (!pretreatmentService.onPretreatment(context, card)) {
365+
if (!pretreatmentService.onPretreatment(card.getContext(), card)) {
355366
// 预处理失败,导航取消
356367
logger.debug(null, "[go] IPretreatmentService Failure!");
357368
return null;
@@ -360,21 +371,18 @@ public Object go(Context context, Card card, int requestCode, ActivityResultLaun
360371
logger.warning(null, "[go] This [IPretreatmentService] was not found!");
361372
}
362373

363-
card.setContext(context);
364-
card.setInterceptorException(null);
365-
366374
try {
367375
RouteCenter.assembleRouteCard(card);
368376
} catch (NoFoundRouteException e) {
369377
logger.warning(null, e.getMessage());
370378

371379
if (isDebug()) {
372-
runInMainThread(() -> Toast.makeText(context, "There's no route matched!\n" +
380+
runInMainThread(() -> Toast.makeText(card.getContext(), "There's no route matched!\n" +
373381
" Path = [" + card.getPath() + "]\n" +
374382
" Group = [" + card.getGroup() + "]", Toast.LENGTH_LONG).show());
375383
}
376384

377-
onLost(context, card, callback);
385+
onLost(card.getContext(), card, callback);
378386
return null;
379387
}
380388

@@ -387,7 +395,7 @@ public Object go(Context context, Card card, int requestCode, ActivityResultLaun
387395

388396
if (isDebug() && card.isDeprecated()) {
389397
logger.warning(null, "[go] This page has been marked as deprecated. path[" + card.getPath() + "]");
390-
runInMainThread(() -> Toast.makeText(context, "This page has been marked as deprecated!\n" +
398+
runInMainThread(() -> Toast.makeText(card.getContext(), "This page has been marked as deprecated!\n" +
391399
" Path = [" + card.getPath() + "]\n" +
392400
" Group = [" + card.getGroup() + "]", Toast.LENGTH_SHORT).show());
393401
}
@@ -399,7 +407,7 @@ public Object go(Context context, Card card, int requestCode, ActivityResultLaun
399407
interceptorService.doInterceptions(card, new InterceptorCallback() {
400408
@Override
401409
public void onContinue(Card card) {
402-
goActivity(context, card, requestCode, activityResultLauncher, callback);
410+
goActivity(card.getContext(), card, requestCode, activityResultLauncher, callback);
403411
}
404412

405413
@Override
@@ -412,7 +420,7 @@ public void onInterrupt(Card card, @NonNull Throwable exception) {
412420
}
413421
});
414422
} else {
415-
goActivity(context, card, requestCode, activityResultLauncher, callback);
423+
goActivity(card.getContext(), card, requestCode, activityResultLauncher, callback);
416424
}
417425
break;
418426
case FRAGMENT:

GoRouter-Api/src/main/java/com/wyjson/router/core/ApplicationModuleCenter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ private static void call(Consumer consumer) {
191191
}
192192
}
193193

194-
public static void callOnCreate(Application app) {
195-
load(app);
194+
public static void callOnCreate(Application application) {
195+
load(application);
196196
call(am -> {
197-
am.onCreate(app);
197+
am.onCreate(application);
198198
new Thread(() -> {
199199
// 设置线程的优先级,不与主线程抢资源
200200
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
201-
am.onLoadAsync(app);
201+
am.onLoadAsync(application);
202202
}).start();
203203
});
204204
}

GoRouter-Api/src/main/java/com/wyjson/router/model/Card.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public Card(String path, Bundle bundle) {
7676
this.mBundle = (null == bundle ? new Bundle() : bundle);
7777
}
7878

79+
@Nullable
80+
public Object go() {
81+
return go(null, this, -1, null, null);
82+
}
83+
7984
@Nullable
8085
public Object go(Context context) {
8186
return go(context, this, -1, null, null);
@@ -342,6 +347,9 @@ public Context getContext() {
342347
}
343348

344349
public void setContext(Context context) {
350+
if (context == null) {
351+
throw new RouterException("Context cannot be empty, Call 'GoRouter.setApplication(...)' in application.");
352+
}
345353
this.context = context;
346354
}
347355

GoRouter-Gradle-Plugin/src/main/kotlin/com/wyjson/router/gradle_plugin/helper/AssembleGoRouteHelperCode.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ class AssembleGoRouteHelperCode(private val model: RouteHelperModel) {
150150

151151
val goMethod = MethodSpec.methodBuilder("go")
152152
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
153-
.addParameter(Context, "context")
154153

155154
methods.add(getPathMethod.build())
156155
methods.add(getCardMetaMethod.build())
@@ -369,13 +368,14 @@ class AssembleGoRouteHelperCode(private val model: RouteHelperModel) {
369368
)
370369
methods.add(newBuildMethod.build())
371370

372-
val newGoMethod = goMethod.build().toBuilder()
373371
val goParamCodeString = handleGoParamCodeString(goParamCode)
372+
373+
val newGoMethod = goMethod.build().toBuilder()
374374
if (routeModel.type == "Activity") {
375-
newGoMethod.addStatement("\$N(\$L).go(context)", buildMethod.build().name, goParamCodeString)
375+
newGoMethod.addStatement("\$N(\$L).go()", buildMethod.build().name, goParamCodeString)
376376
} else {
377377
newGoMethod.returns(Fragment)
378-
newGoMethod.addStatement("return (Fragment) \$N(\$L).go(context)", buildMethod.build().name, goParamCodeString)
378+
newGoMethod.addStatement("return (Fragment) \$N(\$L).go()", buildMethod.build().name, goParamCodeString)
379379
}
380380
methods.add(newGoMethod.build())
381381
}
@@ -400,13 +400,14 @@ class AssembleGoRouteHelperCode(private val model: RouteHelperModel) {
400400

401401
methods.add(newBuildMethod.build())
402402

403-
val newGoMethod = goMethod.build().toBuilder()
404403
val goParamCodeString = handleGoParamCodeString(goParamCode)
404+
405+
val newGoMethod = goMethod.build().toBuilder()
405406
if (routeModel.type == "Activity") {
406-
newGoMethod.addStatement("\$N(\$L).go(context)", buildMethod.build().name, goParamCodeString)
407+
newGoMethod.addStatement("\$N(\$L).go()", buildMethod.build().name, goParamCodeString)
407408
} else {
408409
newGoMethod.returns(Fragment)
409-
newGoMethod.addStatement("return (Fragment) \$N(\$L).go(context)", buildMethod.build().name, goParamCodeString)
410+
newGoMethod.addStatement("return (Fragment) \$N(\$L).go()", buildMethod.build().name, goParamCodeString)
410411
}
411412
methods.add(newGoMethod.build())
412413
}

0 commit comments

Comments
 (0)