-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
122 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
routerlib/src/main/java/com/lzh/nonview/router/CreatorRouter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.lzh.nonview.router; | ||
|
||
import android.net.Uri; | ||
import android.os.Bundle; | ||
|
||
import com.lzh.compiler.parceler.Parceler; | ||
import com.lzh.nonview.router.module.CreatorRouteRule; | ||
import com.lzh.nonview.router.parser.URIParser; | ||
import com.lzh.nonview.router.tools.Cache; | ||
import com.lzh.nonview.router.tools.RouterLog; | ||
import com.lzh.nonview.router.tools.Utils; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author haoge on 2018/5/25 | ||
*/ | ||
public class CreatorRouter { | ||
|
||
Uri uri; | ||
Bundle extra = new Bundle(); | ||
|
||
private CreatorRouter(Uri uri) { | ||
this.uri = uri; | ||
} | ||
|
||
public static CreatorRouter create(String url) { | ||
return new CreatorRouter(Uri.parse(url)); | ||
} | ||
|
||
public static CreatorRouter create(Uri uri) { | ||
return new CreatorRouter(uri); | ||
} | ||
|
||
public CreatorRouter addExtras(Bundle extra){ | ||
if (extra != null) { | ||
this.extra.putAll(extra); | ||
} | ||
return this; | ||
} | ||
|
||
public <T> T createTarget() { | ||
try { | ||
Map<String, CreatorRouteRule> rules = Cache.getCreatorRules(); | ||
URIParser parser = new URIParser(uri); | ||
String route = parser.getRoute(); | ||
CreatorRouteRule rule = rules.get(route); | ||
if (rule == null) { | ||
RouterLog.d("Could not match rule for this uri"); | ||
return null; | ||
} | ||
|
||
Object instance = rule.getTarget().newInstance(); | ||
|
||
Bundle bundle = Utils.parseRouteMapToBundle(parser, rule); | ||
if (Utils.PARCELER_SUPPORT) { | ||
Parceler.toEntity(instance, bundle); | ||
} | ||
|
||
return (T) instance; | ||
} catch (Throwable e) { | ||
RouterLog.e("Create target class from CreatorRouter failed. cause by:" + e.getMessage(), e); | ||
return null; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
routerlib/src/main/java/com/lzh/nonview/router/module/CreatorRouteRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.lzh.nonview.router.module; | ||
|
||
import com.lzh.nonview.router.launcher.Launcher; | ||
|
||
/** | ||
* @author haoge on 2018/5/25 | ||
*/ | ||
public class CreatorRouteRule extends RouteRule<CreatorRouteRule, Launcher> { | ||
|
||
private Class target; | ||
|
||
public CreatorRouteRule(Class clz) { | ||
super(clz.getCanonicalName()); | ||
this.target = clz; | ||
} | ||
|
||
public Class getTarget() { | ||
return target; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters