Skip to content

Commit

Permalink
support query route extras with last uri.
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Aug 11, 2017
1 parent a72791d commit 39f69c6
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 51 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/com/lzh/nonview/router/demo/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.lzh.nonview.router.module.ActivityRouteRule;
import com.lzh.nonview.router.module.RouteCreator;
import com.lzh.nonview.router.module.RouteRule;
import com.lzh.nonview.router.route.InternalCallback;
import com.lzh.nonview.router.route.RouteCallback;

import java.util.HashMap;
Expand Down Expand Up @@ -54,10 +55,11 @@ public void onIntercepted(Uri uri, RouteBundleExtras extras, Context context) {
});

// 对Router设置Activity Route Callback,作辅助功能
Router.setGlobalRouteCallback(new RouteCallback() {
RouterConfiguration.get().setCallback(new RouteCallback() {

@Override
public void notFound(Uri uri, NotFoundException e) {
RouteBundleExtras extras = RouterConfiguration.get().restoreExtras(uri);
Toast.makeText(App.this, e.getNotFoundName() + " not find", Toast.LENGTH_SHORT).show();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void onAddExtrasClick (View v) {
extras.putString("username","haoge");
extras.putString("password","lzh");
extras.putString("usertype","VIP");
Router.create("jumei://main")
Router.create("jumei://main/host")
.setCallback(new SerializableCallback())
.getActivityRoute()
// .addInterceptor(new SerialInterceptor())
Expand Down
6 changes: 3 additions & 3 deletions routerlib/src/main/java/com/lzh/nonview/router/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public final class Router{

private Router(Uri uri) {
this.uri = Utils.completeUri(uri);
internalCallback = new InternalCallback(uri);
internalCallback = new InternalCallback(this.uri);
}

/**
Expand Down Expand Up @@ -127,7 +127,7 @@ public IRoute getRoute () {
return route;
}
route = HostServiceWrapper.create(uri, internalCallback);
if (!(route instanceof IRoute.EmptyRoute)) {
if (route instanceof IRoute.EmptyRoute) {
notifyNotFound(String.format("find Route by %s failed:",uri));
}
return route;
Expand Down Expand Up @@ -194,7 +194,7 @@ public IActionRoute getActionRoute() {
}

private void notifyNotFound(String msg) {
internalCallback.notFound(new NotFoundException(msg, NotFoundException.TYPE_SCHEMA, uri.toString()));
internalCallback.onOpenFailed(new NotFoundException(msg, NotFoundException.TYPE_SCHEMA, uri.toString()));
}

/** Consider to change entrance to {@link RouterConfiguration#setCallback(RouteCallback)}*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.lzh.nonview.router;

import android.net.Uri;

import com.lzh.nonview.router.extras.RouteBundleExtras;
import com.lzh.nonview.router.interceptors.RouteInterceptor;
import com.lzh.nonview.router.launcher.ActionLauncher;
import com.lzh.nonview.router.launcher.ActivityLauncher;
Expand All @@ -24,6 +27,7 @@
import com.lzh.nonview.router.protocol.IRemoteFactory;
import com.lzh.nonview.router.route.ActionRoute;
import com.lzh.nonview.router.route.ActivityRoute;
import com.lzh.nonview.router.route.InternalCallback;
import com.lzh.nonview.router.route.RouteCallback;
import com.lzh.nonview.router.tools.Cache;

Expand Down Expand Up @@ -137,6 +141,16 @@ public void registerExecutors(Class<? extends Executor> key, Executor value) {
Cache.registerExecutors(key, value);
}

/**
* Restore a {@link RouteBundleExtras} by uri. this method should only be called in lifecycle of {@link RouteCallback}.
* otherwise it will be null cause it is cleaned.
* @param uri The uri that you open
* @return The {@link RouteBundleExtras} instance that you may set before before you open the routing by uri.
*/
public RouteBundleExtras restoreExtras(Uri uri) {
return InternalCallback.findExtrasByUri(uri);
}

private static RouterConfiguration config = new RouterConfiguration();
private RouterConfiguration() {}
public static RouterConfiguration get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import android.net.Uri;
import android.text.TextUtils;

import com.lzh.nonview.router.tools.Utils;

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
Expand All @@ -38,7 +40,7 @@ public URIParser(Uri uri) {
}

private void parse() {
this.route = uri.getScheme() + "://" + uri.getHost() + uri.getPath();
this.route = Utils.format(uri.getScheme() + "://" + uri.getHost() + uri.getPath());
String query = uri.getEncodedQuery();
if (!TextUtils.isEmpty(query)) {
params = parseParams(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.content.Context;
import android.content.Intent;

import com.lzh.nonview.router.exception.NotFoundException;
import com.lzh.nonview.router.launcher.ActivityLauncher;
import com.lzh.nonview.router.launcher.DefaultActivityLauncher;
import com.lzh.nonview.router.launcher.Launcher;
Expand All @@ -35,63 +34,55 @@ public class ActivityRoute extends BaseRoute<IActivityRoute> implements IActivit
@Override
public Intent createIntent(Context context) {
ActivityLauncher activityLauncher = (ActivityLauncher) launcher;
activityLauncher.set(uri, bundle, callback.extras, (ActivityRouteRule) routeRule, remote);
activityLauncher.set(uri, bundle, callback.getExtras(), (ActivityRouteRule) routeRule, remote);
return activityLauncher.createIntent(context);
}

@Override
public IActivityRoute requestCode(int requestCode) {
this.callback.extras.setRequestCode(requestCode);
this.callback.getExtras().setRequestCode(requestCode);
return this;
}

@Override
public IActivityRoute setAnim(int enterAnim, int exitAnim) {
this.callback.extras.setInAnimation(enterAnim);
this.callback.extras.setOutAnimation(exitAnim);
this.callback.getExtras().setInAnimation(enterAnim);
this.callback.getExtras().setOutAnimation(exitAnim);
return this;
}

@Override
public IActivityRoute addFlags(int flag) {
this.callback.extras.addFlags(flag);
this.callback.getExtras().addFlags(flag);
return this;
}

@Override
public void open(Fragment fragment) {
try {
Utils.checkInterceptor(uri, callback.extras, fragment.getActivity(), getInterceptors());
Utils.checkInterceptor(uri, callback.getExtras(), fragment.getActivity(), getInterceptors());
ActivityLauncher activityLauncher = (ActivityLauncher) launcher;
activityLauncher.set(uri, bundle, callback.extras, (ActivityRouteRule) routeRule, remote);
activityLauncher.set(uri, bundle, callback.getExtras(), (ActivityRouteRule) routeRule, remote);
activityLauncher.open(fragment);
callback.onOpenSuccess(routeRule);
} catch (Throwable e) {
if (e instanceof NotFoundException) {
callback.notFound((NotFoundException) e);
} else {
callback.onOpenFailed(e);
}
callback.onOpenFailed(e);
}

callback.invoke();
}

@Override
public void open(android.support.v4.app.Fragment fragment) {
try {
Utils.checkInterceptor(uri, callback.extras, fragment.getActivity(), getInterceptors());
Utils.checkInterceptor(uri, callback.getExtras(), fragment.getActivity(), getInterceptors());
ActivityLauncher activityLauncher = (ActivityLauncher) launcher;
activityLauncher.set(uri, bundle, callback.extras, (ActivityRouteRule) routeRule, remote);
activityLauncher.set(uri, bundle, callback.getExtras(), (ActivityRouteRule) routeRule, remote);
activityLauncher.open(fragment);
callback.onOpenSuccess(routeRule);
} catch (Throwable e) {
if (e instanceof NotFoundException) {
callback.notFound((NotFoundException) e);
} else {
callback.onOpenFailed(e);
}
callback.onOpenFailed(e);
}

callback.invoke();
}

Expand Down
25 changes: 10 additions & 15 deletions routerlib/src/main/java/com/lzh/nonview/router/route/BaseRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import com.lzh.nonview.router.Router;
import com.lzh.nonview.router.RouterConfiguration;
import com.lzh.nonview.router.exception.NotFoundException;
import com.lzh.nonview.router.extras.RouteBundleExtras;
import com.lzh.nonview.router.interceptors.RouteInterceptor;
import com.lzh.nonview.router.interceptors.RouteInterceptorAction;
Expand Down Expand Up @@ -69,42 +68,38 @@ public final void open(Context context) {
// realOpen(context);
callback.onOpenSuccess(routeRule);
} catch (Throwable e) {
if (e instanceof NotFoundException) {
callback.notFound((NotFoundException) e);
} else {
callback.onOpenFailed(e);
}
callback.onOpenFailed(e);
}

callback.invoke();
}

@Override
public T addExtras(Bundle extras) {
this.callback.extras.addExtras(extras);
this.callback.getExtras().addExtras(extras);
return (T) this;
}

// =============RouteInterceptor operation===============
public T addInterceptor(RouteInterceptor interceptor) {
if (callback.extras != null) {
callback.extras.addInterceptor(interceptor);
if (callback.getExtras() != null) {
callback.getExtras().addInterceptor(interceptor);
}
return (T) this;
}

@Override
public T removeInterceptor(RouteInterceptor interceptor) {
if (callback.extras != null) {
callback.extras.removeInterceptor(interceptor);
if (callback.getExtras() != null) {
callback.getExtras().removeInterceptor(interceptor);
}
return (T) this;
}

@Override
public T removeAllInterceptors() {
if (callback.extras != null) {
callback.extras.removeAllInterceptors();
if (callback.getExtras() != null) {
callback.getExtras().removeAllInterceptors();
}
return (T) this;
}
Expand All @@ -118,8 +113,8 @@ public List<RouteInterceptor> getInterceptors() {
}

// add extra interceptors
if (callback.extras != null) {
interceptors.addAll(callback.extras.getInterceptors());
if (callback.getExtras() != null) {
interceptors.addAll(callback.getExtras().getInterceptors());
}

return interceptors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public final class InternalCallback {
// store the map to provided find extras for uri.
private static Map<Uri, RouteBundleExtras> cache = new HashMap<>();

Uri uri;
RouteBundleExtras extras = new RouteBundleExtras();
RouteRule rule;
Throwable error;
private Uri uri;
private RouteBundleExtras extras = new RouteBundleExtras();
private RouteRule rule;
private Throwable error;

public InternalCallback(Uri uri) {
this.uri = uri;
Expand All @@ -43,10 +43,6 @@ public void setExtras(RouteBundleExtras extras) {
}
}

public void notFound(NotFoundException e) {
this.error = e;
}

public void onOpenSuccess(RouteRule rule) {
this.rule = rule;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static boolean isHttp (String scheme) {
return "http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme);
}

static String format(String url) {
public static String format(String url) {
if (url.endsWith("/")){
return url.substring(0, url.length() - 1);
}
Expand Down

0 comments on commit 39f69c6

Please sign in to comment.