Skip to content

Commit d56477d

Browse files
committed
Add sample codes for bundle injector
1 parent 2083b28 commit d56477d

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

app/src/main/java/com/haoge/sample/easyandroid/activities/EasyBundleActivity.kt

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Parcel
55
import android.os.Parcelable
66
import butterknife.OnClick
77
import com.google.gson.Gson
8+
import com.haoge.easyandroid.easy.BundleField
89
import com.haoge.easyandroid.easy.EasyBundle
910
import com.haoge.easyandroid.easy.EasyLog
1011
import com.haoge.sample.easyandroid.BaseActivity
@@ -84,13 +85,38 @@ class EasyBundleActivity:BaseActivity() {
8485
EasyLog.DEFAULT.e("读取空Boolean数据:${easyBundle.get("key", Boolean::class.java)}")
8586
}
8687

87-
@OnClick(R.id.restoreWithJSON)
88-
fun restoreWithJSON() {
89-
val json = Gson().toJson(Info("不可序列化的类"))
90-
val easyBundle = EasyBundle.create(null).put("jsonOfInfo", json)// 先将json保存进去
91-
val info = easyBundle.get<Info>("jsonOfInfo")
92-
EasyLog.DEFAULT.e("从json反序列化回来后的info数据:$info")
88+
@OnClick(R.id.testForConverter)
89+
fun testForConverter() {
90+
val easyBundle = EasyBundle.create(null)
91+
.put("int", 10)// 测试自动基本数据类型转换
92+
// 测试json自动转换
93+
.put("jsonOfInfo", Gson().toJson(Info("不可序列化的类, 将转换为json存储")))
94+
95+
EasyLog.DEFAULT.e("从json反序列化回来后的info数据:${easyBundle.get<Info>("jsonOfInfo")} \n bundle中保存的info原始数据为:${easyBundle.bundle["jsonOfInfo"]}")
96+
EasyLog.DEFAULT.e("从json反序列化回来后的int数据:${easyBundle.get<Int>("int")}")
97+
}
98+
99+
@OnClick(R.id.testForInjector)
100+
fun testForInjector() {
101+
val entity = TestInjector("this is name of Injector",
102+
100,
103+
ParcelableSubclass("Hello parcelable"),
104+
SerializableSubclass("Hello Serializable!"),
105+
Info())
106+
107+
val bundle = EasyBundle.toBundle(entity, null)
108+
EasyLog.DEFAULT.e("注入后的bundle数据:\n$bundle")
109+
110+
// 对bundle数据部分数据进行重置:
111+
EasyBundle.create(bundle).put("name", "重置的name属性")
112+
.put("age", 18)
113+
114+
// 再次将数据反注入回实体类中:
115+
EasyBundle.toEntity(entity, bundle)
116+
EasyLog.DEFAULT.e("反注入回后的entity实例:\n$entity")
93117
}
118+
119+
94120
}
95121

96122
data class ParcelableSubclass(val name: String = "this is a subclass of Parcelable"):Parcelable {
@@ -124,4 +150,14 @@ class Info(val name:String?) {
124150
return "Info(name=$name)"
125151
}
126152

153+
}
154+
155+
class TestInjector(@BundleField var name:String,
156+
@BundleField var age:Int,
157+
@BundleField var parcelable:ParcelableSubclass,
158+
@BundleField var serializable: SerializableSubclass,
159+
@BundleField var unserial:Info) {
160+
override fun toString(): String {
161+
return "TestInjector(name='$name', age=$age, parcelable=$parcelable, serializable=$serializable, unserial=$unserial)"
162+
}
127163
}

app/src/main/res/layout/activity_bundle.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,16 @@
4545
android:layout_height="wrap_content" />
4646

4747
<Button
48-
android:id="@+id/restoreWithJSON"
49-
android:text="读取json数据并自动转换测试"
48+
android:id="@+id/testForConverter"
49+
android:text="自动转换功能测试"
5050
android:layout_width="match_parent"
5151
android:layout_height="wrap_content" />
5252

53-
53+
<Button
54+
android:id="@+id/testForInjector"
55+
android:text="自动注入功能测试"
56+
android:layout_width="match_parent"
57+
android:layout_height="wrap_content" />
5458

5559
</LinearLayout>
5660

utils/src/main/java/com/haoge/easyandroid/easy/EasyBundle.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,14 @@ class EasyBundle private constructor(val bundle: Bundle){
142142
return EasyBundle(source?: Bundle())
143143
}
144144

145-
fun toEntity(entity:Any?, bundle: Bundle?) {
146-
if (entity == null || bundle == null) return
147-
injector.toEntity(entity, bundle)
145+
fun toEntity(entity:Any?, bundle: Bundle?):Any? {
146+
if (entity == null) return null
147+
return injector.toEntity(entity, bundle?: Bundle())
148148
}
149149

150-
fun toBundle(entity:Any?, bundle: Bundle?) {
151-
if (entity == null || bundle == null) return
152-
injector.toBundle(entity, bundle)
150+
fun toBundle(entity:Any?, bundle: Bundle?):Bundle {
151+
if (entity == null) return bundle?:Bundle()
152+
return injector.toBundle(entity, bundle?: Bundle())
153153
}
154154

155155
@JvmStatic
@@ -162,7 +162,9 @@ class EasyBundle private constructor(val bundle: Bundle){
162162
}
163163
}
164164

165-
annotation class BundleField(val value:String, val throwable:Boolean = true)
165+
@Retention(AnnotationRetention.RUNTIME)
166+
@Target(AnnotationTarget.FIELD)
167+
annotation class BundleField(val value:String = "", val throwable:Boolean = true)
166168

167169
private class BundleInjector {
168170
// 缓存注解与字段的匹配信息。进行加速
@@ -203,7 +205,7 @@ private class BundleInjector {
203205
}
204206

205207
// 将bundle中的数据注入到entity的对应字段中去。
206-
fun toEntity(entity:Any, bundle: Bundle) {
208+
fun toEntity(entity:Any, bundle: Bundle):Any {
207209
val map = parseFields(entity.javaClass)
208210
val easyBundle = EasyBundle.create(bundle)
209211
for ((name, pair) in map) {
@@ -219,10 +221,11 @@ private class BundleInjector {
219221
}
220222
}
221223
}
224+
return entity
222225
}
223226

224227
// 将entity中的指定数据注入到bundle中去
225-
fun toBundle(entity:Any, bundle: Bundle) {
228+
fun toBundle(entity:Any, bundle: Bundle):Bundle {
226229
val map = parseFields(entity.javaClass)
227230
val easyBundle = EasyBundle.create(bundle)
228231
for ((name, pair) in map) {
@@ -235,5 +238,6 @@ private class BundleInjector {
235238
}
236239
}
237240
}
241+
return bundle
238242
}
239243
}

0 commit comments

Comments
 (0)