Skip to content

Commit c13995d

Browse files
committed
拆分核心代码,方便代码阅读,LogisticsCenter拆分成RouteCenter,ServiceCenter,InterceptorCenter,EventCenter
1 parent 02e5492 commit c13995d

File tree

9 files changed

+294
-260
lines changed

9 files changed

+294
-260
lines changed

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
import com.wyjson.router.callback.GoCallback;
2525
import com.wyjson.router.callback.InterceptorCallback;
2626
import com.wyjson.router.core.ApplicationModuleCenter;
27+
import com.wyjson.router.core.EventCenter;
28+
import com.wyjson.router.core.InterceptorCenter;
2729
import com.wyjson.router.core.InterceptorServiceImpl;
28-
import com.wyjson.router.core.LogisticsCenter;
30+
import com.wyjson.router.core.RouteCenter;
2931
import com.wyjson.router.core.RouteModuleCenter;
32+
import com.wyjson.router.core.ServiceCenter;
3033
import com.wyjson.router.core.interfaces.IInterceptorService;
3134
import com.wyjson.router.exception.NoFoundRouteException;
3235
import com.wyjson.router.exception.RouterException;
@@ -52,8 +55,8 @@ public final class GoRouter {
5255
private volatile static boolean isDebug = false;
5356

5457
private GoRouter() {
55-
LogisticsCenter.clearInterceptors();
56-
LogisticsCenter.addService(InterceptorServiceImpl.class);
58+
InterceptorCenter.clearInterceptors();
59+
ServiceCenter.addService(InterceptorServiceImpl.class);
5760
}
5861

5962
private static class InstanceHolder {
@@ -156,7 +159,7 @@ public static void registerAM(Class<? extends IApplicationModule> am) {
156159
* @param service 实现类.class
157160
*/
158161
public void addService(Class<? extends IService> service) {
159-
LogisticsCenter.addService(service);
162+
ServiceCenter.addService(service);
160163
}
161164

162165
/**
@@ -168,7 +171,7 @@ public void addService(Class<? extends IService> service) {
168171
*/
169172
@Nullable
170173
public <T> T getService(Class<? extends T> service) {
171-
return LogisticsCenter.getService(service);
174+
return ServiceCenter.getService(service);
172175
}
173176

174177
/**
@@ -178,7 +181,7 @@ public <T> T getService(Class<? extends T> service) {
178181
* @param interceptor
179182
*/
180183
public void addInterceptor(int ordinal, Class<? extends IInterceptor> interceptor) {
181-
LogisticsCenter.addInterceptor(ordinal, interceptor, false);
184+
InterceptorCenter.addInterceptor(ordinal, interceptor, false);
182185
}
183186

184187
/**
@@ -188,14 +191,14 @@ public void addInterceptor(int ordinal, Class<? extends IInterceptor> intercepto
188191
* @param interceptor
189192
*/
190193
public void setInterceptor(int ordinal, Class<? extends IInterceptor> interceptor) {
191-
LogisticsCenter.setInterceptor(ordinal, interceptor);
194+
InterceptorCenter.setInterceptor(ordinal, interceptor);
192195
}
193196

194197
/**
195198
* 动态添加路由分组,按需加载路由
196199
*/
197200
public void addRouterGroup(String group, IRouteModuleGroup routeModuleGroup) {
198-
LogisticsCenter.addRouterGroup(group, routeModuleGroup);
201+
RouteCenter.addRouterGroup(group, routeModuleGroup);
199202
}
200203

201204
private void runInMainThread(Runnable runnable) {
@@ -224,7 +227,7 @@ public Card build(Uri uri) {
224227
* @param activity
225228
*/
226229
public String getRawURI(Activity activity) {
227-
return LogisticsCenter.getRawURI(activity);
230+
return RouteCenter.getRawURI(activity);
228231
}
229232

230233
/**
@@ -233,7 +236,7 @@ public String getRawURI(Activity activity) {
233236
* @param fragment
234237
*/
235238
public String getRawURI(Fragment fragment) {
236-
return LogisticsCenter.getRawURI(fragment);
239+
return RouteCenter.getRawURI(fragment);
237240
}
238241

239242
/**
@@ -242,7 +245,7 @@ public String getRawURI(Fragment fragment) {
242245
* @param activity
243246
*/
244247
public String getCurrentPath(Activity activity) {
245-
return LogisticsCenter.getCurrentPath(activity);
248+
return RouteCenter.getCurrentPath(activity);
246249
}
247250

248251
/**
@@ -251,31 +254,31 @@ public String getCurrentPath(Activity activity) {
251254
* @param fragment
252255
*/
253256
public String getCurrentPath(Fragment fragment) {
254-
return LogisticsCenter.getCurrentPath(fragment);
257+
return RouteCenter.getCurrentPath(fragment);
255258
}
256259

257260
public void inject(Activity activity) {
258-
LogisticsCenter.inject(activity, null, null);
261+
RouteCenter.inject(activity, null, null);
259262
}
260263

261264
public void inject(Activity activity, Intent intent) {
262-
LogisticsCenter.inject(activity, intent, null);
265+
RouteCenter.inject(activity, intent, null);
263266
}
264267

265268
public void inject(Activity activity, Bundle bundle) {
266-
LogisticsCenter.inject(activity, null, bundle);
269+
RouteCenter.inject(activity, null, bundle);
267270
}
268271

269272
public void inject(Fragment fragment) {
270-
LogisticsCenter.inject(fragment, null, null);
273+
RouteCenter.inject(fragment, null, null);
271274
}
272275

273276
public void inject(Fragment fragment, Intent intent) {
274-
LogisticsCenter.inject(fragment, intent, null);
277+
RouteCenter.inject(fragment, intent, null);
275278
}
276279

277280
public void inject(Fragment fragment, Bundle bundle) {
278-
LogisticsCenter.inject(fragment, null, bundle);
281+
RouteCenter.inject(fragment, null, bundle);
279282
}
280283

281284
@Nullable
@@ -296,7 +299,7 @@ public Object go(Context context, Card card, int requestCode, ActivityResultLaun
296299
card.setInterceptorException(null);
297300

298301
try {
299-
LogisticsCenter.assembleRouteCard(card);
302+
RouteCenter.assembleRouteCard(card);
300303
} catch (NoFoundRouteException e) {
301304
logger.warning(null, e.getMessage());
302305

@@ -428,39 +431,39 @@ private Object goFragment(Card card, GoCallback callback) {
428431
}
429432

430433
public <T> void registerEvent(FragmentActivity activity, @NonNull Class<T> type, @NonNull Observer<T> observer) {
431-
LogisticsCenter.registerEvent(activity, type, false, observer);
434+
EventCenter.registerEvent(activity, type, false, observer);
432435
}
433436

434437
public <T> void registerEvent(Fragment fragment, @NonNull Class<T> type, @NonNull Observer<T> observer) {
435-
LogisticsCenter.registerEvent(fragment, type, false, observer);
438+
EventCenter.registerEvent(fragment, type, false, observer);
436439
}
437440

438441
public <T> void registerEventForever(FragmentActivity activity, @NonNull Class<T> type, @NonNull Observer<T> observer) {
439-
LogisticsCenter.registerEvent(activity, type, true, observer);
442+
EventCenter.registerEvent(activity, type, true, observer);
440443
}
441444

442445
public <T> void registerEventForever(Fragment fragment, @NonNull Class<T> type, @NonNull Observer<T> observer) {
443-
LogisticsCenter.registerEvent(fragment, type, true, observer);
446+
EventCenter.registerEvent(fragment, type, true, observer);
444447
}
445448

446449
public <T> void unRegisterEvent(FragmentActivity activity, @NonNull Class<T> type) {
447-
LogisticsCenter.unRegisterEvent(activity, type, null);
450+
EventCenter.unRegisterEvent(activity, type, null);
448451
}
449452

450453
public <T> void unRegisterEvent(Fragment fragment, @NonNull Class<T> type) {
451-
LogisticsCenter.unRegisterEvent(fragment, type, null);
454+
EventCenter.unRegisterEvent(fragment, type, null);
452455
}
453456

454457
public <T> void unRegisterEvent(FragmentActivity activity, @NonNull Class<T> type, Observer<T> observer) {
455-
LogisticsCenter.unRegisterEvent(activity, type, observer);
458+
EventCenter.unRegisterEvent(activity, type, observer);
456459
}
457460

458461
public <T> void unRegisterEvent(Fragment fragment, @NonNull Class<T> type, Observer<T> observer) {
459-
LogisticsCenter.unRegisterEvent(fragment, type, observer);
462+
EventCenter.unRegisterEvent(fragment, type, observer);
460463
}
461464

462465
public <T> void postEvent(@NonNull String path, @NonNull T value) {
463-
LogisticsCenter.postEvent(path, value);
466+
EventCenter.postEvent(path, value);
464467
}
465468

466469

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.wyjson.router.core;
2+
3+
import static com.wyjson.router.core.Constants.ROUTER_CURRENT_PATH;
4+
5+
import android.app.Activity;
6+
import android.os.Handler;
7+
import android.os.Looper;
8+
9+
import androidx.annotation.NonNull;
10+
import androidx.fragment.app.Fragment;
11+
import androidx.lifecycle.Lifecycle;
12+
import androidx.lifecycle.LifecycleEventObserver;
13+
import androidx.lifecycle.LifecycleObserver;
14+
import androidx.lifecycle.LifecycleOwner;
15+
import androidx.lifecycle.MutableLiveData;
16+
import androidx.lifecycle.Observer;
17+
18+
import com.wyjson.router.GoRouter;
19+
import com.wyjson.router.exception.RouterException;
20+
import com.wyjson.router.utils.TextUtils;
21+
22+
public class EventCenter {
23+
24+
public static <T> void registerEvent(LifecycleOwner owner, Class<T> type, boolean isForever, @NonNull Observer<T> observer) {
25+
if (!(owner instanceof Activity) && !(owner instanceof Fragment)) {
26+
/**
27+
* 正常通过api调用是不会走到这里,除非直接调用了此方法才有可能出现这种情况
28+
* 可能是FragmentViewLifecycleOwner类型,需要导包才能直接判断,为了少导入包,就这么写判断吧.
29+
*/
30+
throw new RouterException("The owner can only be an Activity or Fragment");
31+
}
32+
if (type == null) {
33+
throw new RouterException("type cannot be empty!");
34+
}
35+
36+
String path = RouteCenter.getCurrentPath(owner);
37+
if (TextUtils.isEmpty(path)) {
38+
GoRouter.logger.error(null, "[registerEvent] The " + ROUTER_CURRENT_PATH + " parameter was not found in the intent");
39+
return;
40+
}
41+
42+
String key = path + "$" + type.getCanonicalName();
43+
MutableLiveData<T> liveData;
44+
if (Warehouse.events.containsKey(key)) {
45+
liveData = Warehouse.events.get(key);
46+
} else {
47+
liveData = new MutableLiveData<>();
48+
Warehouse.events.put(key, liveData);
49+
addLifecycleObserver(owner, getLifecycleObserver(key));
50+
}
51+
if (liveData != null) {
52+
if (isForever) {
53+
liveData.observeForever(observer);
54+
} else {
55+
liveData.observe(owner, observer);
56+
}
57+
} else {
58+
GoRouter.logger.error(null, "[registerEvent] LiveData is empty??");
59+
}
60+
}
61+
62+
public static <T> void unRegisterEvent(LifecycleOwner owner, Class<T> type, Observer<T> observer) {
63+
if (type == null) {
64+
throw new RouterException("type cannot be empty!");
65+
}
66+
67+
String path = RouteCenter.getCurrentPath(owner);
68+
if (TextUtils.isEmpty(path)) {
69+
GoRouter.logger.error(null, "[registerEvent] The " + ROUTER_CURRENT_PATH + " parameter was not found in the intent");
70+
return;
71+
}
72+
73+
String key = path + "$" + type.getCanonicalName();
74+
if (Warehouse.events.containsKey(key)) {
75+
MutableLiveData<T> liveData = Warehouse.events.get(key);
76+
if (liveData != null) {
77+
if (observer != null) {
78+
liveData.removeObserver(observer);
79+
} else {
80+
liveData.removeObservers(owner);
81+
}
82+
if (!liveData.hasObservers()) {
83+
Warehouse.events.remove(key);
84+
}
85+
}
86+
} else {
87+
GoRouter.logger.warning(null, "[unRegisterEvent] No observer was found for this event");
88+
}
89+
}
90+
91+
/**
92+
* 页面销毁时删除当前页面注册的事件
93+
*
94+
* @param key
95+
* @param <T>
96+
* @return
97+
*/
98+
@NonNull
99+
private static <T> LifecycleEventObserver getLifecycleObserver(String key) {
100+
return new LifecycleEventObserver() {
101+
@Override
102+
public void onStateChanged(@NonNull LifecycleOwner owner, @NonNull Lifecycle.Event event) {
103+
if (event == Lifecycle.Event.ON_DESTROY) {
104+
owner.getLifecycle().removeObserver(this);
105+
if (Warehouse.events.containsKey(key)) {
106+
MutableLiveData<T> liveData = Warehouse.events.get(key);
107+
if (liveData != null && liveData.hasObservers()) {
108+
liveData.removeObservers(owner);
109+
}
110+
Warehouse.events.remove(key);
111+
}
112+
}
113+
}
114+
};
115+
}
116+
117+
private static void addLifecycleObserver(final LifecycleOwner lifecycleOwner, @NonNull LifecycleObserver lifecycleObserver) {
118+
if (lifecycleOwner == null)
119+
return;
120+
Runnable runnable = () -> lifecycleOwner.getLifecycle().addObserver(lifecycleObserver);
121+
if (Looper.myLooper() == Looper.getMainLooper()) {
122+
runnable.run();
123+
} else {
124+
new Handler(Looper.getMainLooper()).post(runnable);
125+
}
126+
}
127+
128+
public static <T> void postEvent(String path, T value) {
129+
if (TextUtils.isEmpty(path)) {
130+
throw new RouterException("path Parameter is invalid!");
131+
}
132+
if (value == null) {
133+
throw new RouterException("value cannot be empty!");
134+
}
135+
String key = path + "$" + value.getClass().getCanonicalName();
136+
if (Warehouse.events.containsKey(key)) {
137+
MutableLiveData<T> liveData = Warehouse.events.get(key);
138+
if (liveData != null) {
139+
if (Looper.myLooper() == Looper.getMainLooper()) {
140+
liveData.setValue(value);
141+
} else {
142+
liveData.postValue(value);
143+
}
144+
} else {
145+
GoRouter.logger.error(null, "[postEvent] LiveData is empty??");
146+
}
147+
} else {
148+
GoRouter.logger.warning(null, "[postEvent] No observer was found for this event");
149+
}
150+
}
151+
152+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.wyjson.router.core;
2+
3+
import com.wyjson.router.GoRouter;
4+
import com.wyjson.router.exception.RouterException;
5+
import com.wyjson.router.interfaces.IInterceptor;
6+
7+
public class InterceptorCenter {
8+
9+
/**
10+
* 重复添加相同序号会catch
11+
*
12+
* @param ordinal
13+
* @param interceptor
14+
* @param isForce
15+
*/
16+
public static void addInterceptor(int ordinal, Class<? extends IInterceptor> interceptor, boolean isForce) {
17+
String title = isForce ? "[setInterceptor]" : "[addInterceptor]";
18+
try {
19+
if (isForce) {
20+
Warehouse.interceptors.remove(ordinal);
21+
}
22+
IInterceptor instance = interceptor.getConstructor().newInstance();
23+
instance.init();
24+
Warehouse.interceptors.put(ordinal, instance);
25+
GoRouter.logger.debug(null, title + " size:" + Warehouse.interceptors.size() + ", ordinal:" + ordinal + " -> " + interceptor.getSimpleName());
26+
} catch (Exception e) {
27+
throw new RouterException(title + " " + e.getMessage());
28+
}
29+
}
30+
31+
/**
32+
* 重复添加相同序号会覆盖(更新)
33+
*
34+
* @param ordinal
35+
* @param interceptor
36+
*/
37+
public static void setInterceptor(int ordinal, Class<? extends IInterceptor> interceptor) {
38+
addInterceptor(ordinal, interceptor, true);
39+
}
40+
41+
public static void clearInterceptors() {
42+
Warehouse.interceptors.clear();
43+
}
44+
45+
}

0 commit comments

Comments
 (0)