Skip to content

Commit

Permalink
Add object autowired support.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhi1ong committed Mar 16, 2017
1 parent c9760cb commit b8fc9c7
Show file tree
Hide file tree
Showing 22 changed files with 364 additions and 95 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
##### [Demo apk下载](http://public.cdn.zhilong.me/app-debug.apk)[Demo Gif](https://raw.githubusercontent.com/alibaba/ARouter/master/demo/arouter-demo.gif)

#### 一、功能介绍
1. **支持直接解析标准URL进行跳转,并自动解析参数注入**
1. **支持直接解析标准URL进行跳转,并自动注入参数到目标页面中**
2. **支持多模块工程使用**
3. **支持添加多个拦截器,自定义拦截顺序**
4. **支持依赖注入,可单独作为依赖注入框架使用**
Expand Down Expand Up @@ -50,8 +50,10 @@
}

dependencies {
// 替换成最新版本, 需要注意的是api
// 要与compiler匹配使用,均使用最新版可以保证兼容
compile 'com.alibaba:arouter-api:x.x.x'
annotationProcessor 'com.alibaba:arouter-compiler:x.x.x' // 替换成最新版本
annotationProcessor 'com.alibaba:arouter-compiler:x.x.x'
...
}
// 旧版本gradle插件(< 2.2),可以使用apt插件,配置方法见文末'其他#4'
Expand Down Expand Up @@ -82,6 +84,7 @@
ARouter.getInstance().build("/test/1")
.withLong("key1", 666L)
.withString("key3", "888")
.withObject("key4", new Test("Jack", "Rose"))
.navigation();

5. 添加混淆规则(如果使用了Proguard)
Expand Down Expand Up @@ -122,6 +125,7 @@
2. 解析URL中的参数

// 为每一个参数声明一个字段,并使用 @Autowired 标注
// URL中不能传递Parcelable类型数据,通过ARouter api可以传递Parcelable对象
@Route(path = "/test/activity")
public class Test1Activity extends Activity {
@Autowired
Expand All @@ -130,6 +134,8 @@
int age;
@Autowired(name = "girl") // 通过name来映射URL中的不同参数
boolean boy;
@Autowired
TestObj obj; // 支持解析自定义对象,URL中使用json传递

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -290,6 +296,11 @@
.build("/home/main")
.withFlags();
.navigation();
// 对象传递
ARouter.getInstance()
.withObject("key", new TestObj("Jack", "Rose"))
.navigation();

// 觉得接口不够多,可以直接拿出Bundle赋值
ARouter.getInstance()
Expand Down
15 changes: 13 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
##### [Demo apk下载](http://public.cdn.zhilong.me/app-debug.apk)[Demo Gif](https://raw.githubusercontent.com/alibaba/ARouter/master/demo/arouter-demo.gif)

#### 一、功能介绍
1. **支持直接解析标准URL进行跳转,并自动解析参数注入**
1. **支持直接解析标准URL进行跳转,并自动注入参数到目标页面中**
2. **支持多模块工程使用**
3. **支持添加多个拦截器,自定义拦截顺序**
4. **支持依赖注入,可单独作为依赖注入框架使用**
Expand Down Expand Up @@ -48,8 +48,10 @@
}

dependencies {
// 替换成最新版本, 需要注意的是api
// 要与compiler匹配使用,均使用最新版可以保证兼容
compile 'com.alibaba:arouter-api:x.x.x'
annotationProcessor 'com.alibaba:arouter-compiler:x.x.x' // 替换成最新版本
annotationProcessor 'com.alibaba:arouter-compiler:x.x.x'
...
}
// 旧版本gradle插件(< 2.2),可以使用apt插件,配置方法见文末'其他#4'
Expand Down Expand Up @@ -80,6 +82,7 @@
ARouter.getInstance().build("/test/1")
.withLong("key1", 666L)
.withString("key3", "888")
.withObject("key4", new Test("Jack", "Rose"))
.navigation();

5. 添加混淆规则(如果使用了Proguard)
Expand Down Expand Up @@ -120,6 +123,7 @@
2. 解析URL中的参数

// 为每一个参数声明一个字段,并使用 @Autowired 标注
// URL中不能传递Parcelable类型数据,通过ARouter api可以传递Parcelable对象
@Route(path = "/test/activity")
public class Test1Activity extends Activity {
@Autowired
Expand All @@ -128,6 +132,8 @@
int age;
@Autowired(name = "girl") // 通过name来映射URL中的不同参数
boolean boy;
@Autowired
TestObj obj; // 支持解析自定义对象,URL中使用json传递

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -288,6 +294,11 @@
.build("/home/main")
.withFlags();
.navigation();
// 对象传递
ARouter.getInstance()
.withObject("key", new TestObj("Jack", "Rose"))
.navigation();

// 觉得接口不够多,可以直接拿出Bundle赋值
ARouter.getInstance()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:icon="@drawable/ic_launcher"
android:label="ARouter demo"
android:theme="@style/Base.Theme.AppCompat">
<activity android:name=".SchameFilterActivity">
<activity android:name=".SchemeFilterActivity">

<!-- Schame -->
<intent-filter>
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/assets/schame-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

<h2>跳转测试</h2>

<h2>自定义Schame[通常来说都是这样的]</h2>
<h2>自定义Scheme[通常来说都是这样的]</h2>
<p><a href="arouter://m.aliyun.com/test/activity1">arouter://m.aliyun.com/test/activity1</a></p>
<p><a href="arouter://m.aliyun.com/test/activity1?url=https%3a%2f%2fm.abc.com%3fa%3db%26c%3dd">测试URL Encode情况</a></p>
<p><a href="arouter://m.aliyun.com/test/activity1?name=alex&age=18&boy=true&high=180">arouter://m.aliyun.com/test/activity1?name=alex&age=18&boy=true&high=180</a></p>
<p><a href="arouter://m.aliyun.com/test/activity1?name=alex&age=18&boy=true&high=180&obj=%7b%22name%22%3a%22jack%22%2c%22id%22%3a666%7d">arouter://m.aliyun.com/test/activity1?name=alex&age=18&boy=true&high=180&obj={"name":"jack","id":"666"}</a></p>
<p><a href="arouter://m.aliyun.com/test/activity2">arouter://m.aliyun.com/test/activity2</a></p>
<p><a href="arouter://m.aliyun.com/test/activity2?key1=value1">arouter://m.aliyun.com/test/activity2?key1=value1</a></p>
<p><a href="arouter://m.aliyun.com/test/activity3?name=alex&age=18&boy=true&high=180">(需要开启自动注入)arouter://m.aliyun.com/test/activity3?name=alex&age=18&boy=true&high=180</a></p>
<p><a href="arouter://m.aliyun.com/test/activity3?name=alex&age=18&boy=true&high=180">arouter://m.aliyun.com/test/activity3?name=alex&age=18&boy=true&high=180</a></p>

<h2>App Links[防止被App屏蔽]</h2>
<p><a href="http://m.aliyun.com/test/activity1">http://m.aliyun.com/test/activity1</a></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.demo.testinject.TestObj;
import com.alibaba.android.arouter.demo.testinject.TestParcelable;
import com.alibaba.android.arouter.demo.testservice.HelloService;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
Expand Down Expand Up @@ -74,12 +76,17 @@ public void onClick(View v) {
.navigation();
break;
case R.id.autoInject:
TestParcelable testParcelable = new TestParcelable("jack", 666);
TestObj testObj = new TestObj("Rose", 777);

ARouter.getInstance().build("/test/activity1")
.withString("name", "老王")
.withInt("age", 18)
.withBoolean("boy", true)
.withLong("high", 180)
.withString("url", "https://a.b.c")
.withParcelable("pac", testParcelable)
.withObject("obj", testObj)
.navigation();
break;
case R.id.navByName:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.launcher.ARouter;

public class SchameFilterActivity extends Activity {
public class SchemeFilterActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.widget.TextView;

import com.alibaba.android.arouter.demo.R;
import com.alibaba.android.arouter.demo.testinject.TestObj;
import com.alibaba.android.arouter.demo.testinject.TestParcelable;
import com.alibaba.android.arouter.demo.testservice.HelloService;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
Expand All @@ -25,6 +27,12 @@ public class Test1Activity extends AppCompatActivity {
@Autowired(name = "boy")
boolean girl;

@Autowired
TestParcelable pac;

@Autowired
TestObj obj;

private long high;

@Autowired
Expand All @@ -47,7 +55,16 @@ protected void onCreate(Bundle savedInstanceState) {
// high = getIntent().getLongExtra("high", 0);
// url = getIntent().getStringExtra("url");

String params = String.format("name=%s, age=%s, girl=%s, high=%s, url=%s", name, age, girl, high, url);
String params = String.format(
"name=%s,\n age=%s,\n girl=%s,\n high=%s,\n url=%s,\n pac=%s,\n obj=%s",
name,
age,
girl,
high,
url,
pac,
obj
);
helloService.sayHello("Hello moto.");

((TextView)findViewById(R.id.test)).setText("I am " + Test1Activity.class.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.alibaba.android.arouter.demo.testinject;

/**
* TODO:Feature
*
* @author zhilong <a href="mailto:[email protected]">Contact me.</a>
* @version 1.0
* @since 2017/3/16 下午4:42
*/
public class TestObj {
public String name;
public int id;

public TestObj() {
}

public TestObj(String name, int id) {
this.name = name;
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.alibaba.android.arouter.demo.testinject;

import android.os.Parcel;
import android.os.Parcelable;

/**
* TODO:Feature
*
* @author zhilong <a href="mailto:[email protected]">Contact me.</a>
* @version 1.0
* @since 2017/3/16 下午4:42
*/
public class TestParcelable implements Parcelable {
public String name;
public int id;

public TestParcelable() {
}

public TestParcelable(String name, int id) {
this.name = name;
this.id = id;
}

protected TestParcelable(Parcel in) {
name = in.readString();
id = in.readInt();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(id);
}

@Override
public int describeContents() {
return 0;
}

public static final Creator<TestParcelable> CREATOR = new Creator<TestParcelable>() {
@Override
public TestParcelable createFromParcel(Parcel in) {
return new TestParcelable(in);
}

@Override
public TestParcelable[] newArray(int size) {
return new TestParcelable[size];
}
};
}
2 changes: 1 addition & 1 deletion arouter-annotation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ext {
artifact = bintrayName
libraryName = 'ARouter annotation'
libraryDescription = 'The annotation used in arouter api'
libraryVersion = '1.0.2'
libraryVersion = arouter_annotation_version
}

compileJava {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.alibaba.android.arouter.facade.enums;

/**
* Kind of field type.
*
* @author Alex <a href="mailto:[email protected]">Contact me.</a>
* @version 1.0
* @since 2017-03-16 19:13:38
*/
public enum TypeKind {
// Base type
BOOLEAN,
BYTE,
SHORT,
INT,
LONG,
CHAR,
FLOAT,
DOUBLE,

// Other type
STRING,
PARCELABLE,
OBJECT;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,70 @@ public RouteMeta() {

/**
* For versions of 'compiler' less than 1.0.7, contain 1.0.7
*
* @param type type
* @param destination destination
* @param path path
* @param group group
* @param priority priority
* @param extra extra
* @return this
*/
public static RouteMeta build(RouteType type, Class<?> destination, String path, String group, int priority, int extra) {
return new RouteMeta(type, null, destination, path, group, null, priority, extra);
}

/**
* For versions of 'compiler' greater than 1.0.7
*
* @param type type
* @param destination destination
* @param path path
* @param group group
* @param paramsType paramsType
* @param priority priority
* @param extra extra
* @return this
*/
public static RouteMeta build(RouteType type, Class<?> destination, String path, String group, Map<String, Integer> paramsType, int priority, int extra) {
return new RouteMeta(type, null, destination, path, group, paramsType, priority, extra);
}

/**
* Type
*
* @param route route
* @param destination destination
* @param type type
*/
public RouteMeta(Route route, Class<?> destination, RouteType type) {
this(type, null, destination, route.path(), route.group(), null, route.priority(), route.extras());
}

/**
* Type
*
* @param route route
* @param rawType rawType
* @param type type
* @param paramsType paramsType
*/
public RouteMeta(Route route, Element rawType, RouteType type, Map<String, Integer> paramsType) {
this(type, rawType, null, route.path(), route.group(), paramsType, route.priority(), route.extras());
}

/**
* Type
*
* @param type type
* @param rawType rawType
* @param destination destination
* @param path path
* @param group group
* @param paramsType paramsType
* @param priority priority
* @param extra extra
*/
public RouteMeta(RouteType type, Element rawType, Class<?> destination, String path, String group, Map<String, Integer> paramsType, int priority, int extra) {
this.type = type;
this.destination = destination;
Expand Down
Loading

0 comments on commit b8fc9c7

Please sign in to comment.