Skip to content

Commit b98cbe5

Browse files
committed
Merge branch 'main' into androidsupport
2 parents 152930f + 3c6595c commit b98cbe5

File tree

17 files changed

+225
-101
lines changed

17 files changed

+225
-101
lines changed

GaiaXAndroidDemo/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
android:supportsRtl="true"
1313
android:theme="@style/Theme.MyApplication">
1414
<activity
15-
android:name=".ApiTemplateActivity"
15+
android:name=".RemoteDataSourceTemplateActivity"
1616
android:exported="false" />
1717
<activity
1818
android:name=".EventTemplateActivity"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"templates": [
3+
{
4+
"templateBiz": "remote",
5+
"templateId": "gx-vertical-item"
6+
}
7+
]
8+
}
Binary file not shown.

GaiaXAndroidDemo/app/src/main/assets/templates/gx-vertical-item/index.databinding

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"data":{
33
"cover-img":{
4-
"value":"$img",
4+
"value":"$img"
55
},
66
"sub-title":{
77
"value":"$desc"

GaiaXAndroidDemo/app/src/main/kotlin/com/alibaba/gaiax/demo/ApiTemplateActivity.kt

Lines changed: 0 additions & 80 deletions
This file was deleted.

GaiaXAndroidDemo/app/src/main/kotlin/com/alibaba/gaiax/demo/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ class MainActivity : AppCompatActivity() {
7272
startActivity(intent)
7373
}
7474

75-
findViewById<AppCompatButton>(R.id.api)?.setOnClickListener {
76-
val intent = Intent(MainActivity@ this, ApiTemplateActivity::class.java)
75+
findViewById<AppCompatButton>(R.id.remote)?.setOnClickListener {
76+
val intent = Intent(MainActivity@ this, RemoteDataSourceTemplateActivity::class.java)
7777
startActivity(intent)
7878
}
7979
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package com.alibaba.gaiax.demo
2+
3+
import android.os.Bundle
4+
import androidx.appcompat.app.AppCompatActivity
5+
import androidx.appcompat.widget.LinearLayoutCompat
6+
import com.alibaba.fastjson.JSONObject
7+
import com.alibaba.gaiax.GXRegisterCenter
8+
import com.alibaba.gaiax.GXTemplateEngine
9+
import com.alibaba.gaiax.data.assets.GXBinParser
10+
import com.alibaba.gaiax.demo.utils.AssetsUtils
11+
import com.alibaba.gaiax.demo.utils.UiExecutor
12+
import com.alibaba.gaiax.template.GXSize.Companion.dpToPx
13+
import com.alibaba.gaiax.template.GXTemplate
14+
import com.alibaba.gaiax.template.GXTemplateKey
15+
import java.util.concurrent.Executors
16+
17+
class RemoteDataSourceTemplateActivity : AppCompatActivity() {
18+
override fun onCreate(savedInstanceState: Bundle?) {
19+
super.onCreate(savedInstanceState)
20+
setContentView(R.layout.activity_remote_data_source_template)
21+
22+
// Init
23+
GXTemplateEngine.instance.init(this)
24+
25+
// Register remote data template source to GaiaXSDK
26+
val netTemplateSource = RemoteDataSourceTemplateSource.instance
27+
GXRegisterCenter.instance.registerExtensionTemplateSource(netTemplateSource, 20)
28+
29+
// Request Net Data
30+
RemoteDataSourceNetRequest.instance.requestAsync(
31+
this,
32+
JSONObject(),
33+
object : RemoteDataSourceNetRequest.IDataCallback {
34+
override fun template(bizId: String, templateId: String, data: ByteArray?) {
35+
// add template to net template source
36+
RemoteDataSourceTemplateSource.instance.addTemplate(bizId, templateId, data)
37+
}
38+
39+
override fun finish() {
40+
// Net finish
41+
toBindTemplate()
42+
}
43+
})
44+
45+
// Other
46+
}
47+
48+
private fun toBindTemplate() {
49+
50+
// 模板参数
51+
// 去加载远程模板业务下的gx-vertical-item模板
52+
val params = GXTemplateEngine.GXTemplateItem(this, "remote", "gx-vertical-item")
53+
54+
// 模板绘制尺寸
55+
val size = GXTemplateEngine.GXMeasureSize(100F.dpToPx(), null)
56+
57+
// 模板数据
58+
val templateData = GXTemplateEngine.GXTemplateData(
59+
AssetsUtils.parseAssets(
60+
this,
61+
"data/vertical-item.json"
62+
)
63+
)
64+
65+
// 创建模板View
66+
val view = GXTemplateEngine.instance.createView(params, size)
67+
68+
// 绑定数据
69+
GXTemplateEngine.instance.bindData(view, templateData)
70+
71+
// 插入模板View
72+
findViewById<LinearLayoutCompat>(R.id.root).addView(view, 0)
73+
}
74+
75+
class RemoteDataSourceNetRequest {
76+
77+
interface IDataCallback {
78+
fun template(bizId: String, templateId: String, data: ByteArray?)
79+
fun finish()
80+
}
81+
82+
fun requestAsync(
83+
activity: RemoteDataSourceTemplateActivity,
84+
netParams: JSONObject,
85+
callback: IDataCallback
86+
) {
87+
// Thread
88+
Executors.newSingleThreadExecutor().execute {
89+
// request server
90+
val response = AssetsUtils.parseAssets(
91+
activity,
92+
"remote_data_source/api_response.json"
93+
)
94+
95+
// parse data
96+
response.getJSONArray("templates")?.forEach {
97+
val template = (it as JSONObject)
98+
val templateBiz = template.getString("templateBiz")
99+
val templateId = template.getString("templateId")
100+
val templateBytes = getTemplateContents(activity, templateId)
101+
102+
// callback result
103+
callback.template(templateBiz, templateId, templateBytes)
104+
}
105+
106+
UiExecutor.action {
107+
callback.finish()
108+
}
109+
}
110+
}
111+
112+
// mock net
113+
private fun getTemplateContents(
114+
activity: RemoteDataSourceTemplateActivity,
115+
templateId: String
116+
): ByteArray? {
117+
return try {
118+
activity.resources?.assets?.open("remote_data_source/templates/${templateId}")
119+
?.use { it.readBytes() }
120+
} catch (e: Exception) {
121+
e.printStackTrace()
122+
null
123+
}
124+
}
125+
126+
companion object {
127+
128+
val instance by lazy {
129+
RemoteDataSourceNetRequest()
130+
}
131+
}
132+
}
133+
134+
/**
135+
* 远程模板的数据源
136+
*/
137+
class RemoteDataSourceTemplateSource : GXRegisterCenter.GXIExtensionTemplateSource {
138+
139+
companion object {
140+
val instance by lazy {
141+
RemoteDataSourceTemplateSource()
142+
}
143+
}
144+
145+
private val templateCache: MutableList<GXTemplate> = mutableListOf()
146+
147+
override fun getTemplate(gxTemplateItem: GXTemplateEngine.GXTemplateItem): GXTemplate? {
148+
return templateCache.firstOrNull { it.biz == gxTemplateItem.bizId && it.id == gxTemplateItem.templateId }
149+
}
150+
151+
fun addTemplate(templateBiz: String, templateId: String, bytes: ByteArray?) {
152+
if (bytes != null) {
153+
val binaryData = GXBinParser.parser(bytes)
154+
val layer = binaryData.getString(GXTemplateKey.GAIAX_LAYER)
155+
?: throw IllegalArgumentException("Layer mustn't empty, templateBiz = $templateBiz, templateId = $templateId")
156+
val css = binaryData.getString(GXTemplateKey.GAIAX_CSS) ?: ""
157+
val dataBind = binaryData.getString(GXTemplateKey.GAIAX_DATABINDING) ?: ""
158+
val js = binaryData.getString(GXTemplateKey.GAIAX_JS) ?: ""
159+
val template = GXTemplate(templateId, templateBiz, -1, layer, css, dataBind, js)
160+
templateCache.add(template)
161+
}
162+
}
163+
}
164+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.alibaba.gaiax.demo.utils
2+
3+
import android.os.Handler
4+
import android.os.Looper
5+
6+
object UiExecutor {
7+
8+
val ui: Handler = Handler(Looper.getMainLooper())
9+
10+
fun isMainThread(): Boolean {
11+
// 在单元测试环境下 myLooper为null
12+
return Looper.myLooper() == null || Looper.myLooper() == Looper.getMainLooper()
13+
}
14+
15+
fun action(runnable: Runnable) {
16+
ui.post(runnable)
17+
}
18+
19+
fun action(function: () -> Unit) {
20+
ui.post { function.invoke() }
21+
}
22+
23+
fun removeAction(runnable: Runnable) {
24+
ui.removeCallbacks(runnable)
25+
}
26+
}

GaiaXAndroidDemo/app/src/main/res/layout/activity_api_template.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/root"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:gravity="center"
8+
tools:context=".RemoteDataSourceTemplateActivity">
9+
10+
</androidx.appcompat.widget.LinearLayoutCompat>

GaiaXAndroidDemo/app/src/main/res/layout/content_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@
5454
android:textColor="@color/black" />
5555

5656
<androidx.appcompat.widget.AppCompatButton
57-
android:id="@+id/api"
57+
android:id="@+id/remote"
5858
android:layout_width="match_parent"
5959
android:layout_height="wrap_content"
60-
android:text="@string/api"
60+
android:text="@string/remote_data_source"
6161
android:textColor="@color/black" />
6262
</LinearLayout>
6363
</androidx.constraintlayout.widget.ConstraintLayout>

GaiaXAndroidDemo/app/src/main/res/values-zh-rCN/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
<string name="container_template">容器模板</string>
77
<string name="data">数据绑定</string>
88
<string name="event">事件绑定</string>
9-
<string name="api">Api</string>
9+
<string name="remote_data_source">远程数据源</string>
1010
</resources>

GaiaXAndroidDemo/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
<string name="container_template">Container Template</string>
77
<string name="data">Data Binding</string>
88
<string name="event">Event Binding</string>
9-
<string name="api">Api</string>
9+
<string name="remote_data_source">Remote Data Source</string>
1010
</resources>

GaiaXiOS/GaiaXiOS.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "GaiaXiOS"
4-
s.version = "0.2.5"
4+
s.version = "0.2.7"
55
s.platform = :ios, "9.0"
66
s.summary = "dynamic template engine is a lightweight cross-end solution of pure native dynamic card"
77

GaiaXiOS/GaiaXiOS/Impl/Interface/GXTemplateSourceProtocal.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ NS_ASSUME_NONNULL_BEGIN
1313
@protocol GXTemplateSourceProtocal <NSObject>
1414

1515
@required
16+
1617
// priority
1718
- (NSInteger)priority;
1819

19-
//get template
20+
/// get template
21+
/// @param templateItem 模板相关信息
22+
/// 注意:
23+
/// 1. 如需缓存逻辑,需要在协议方法内自行添加
24+
/// 2. 配合 GXUtils (Template) 中的方法,可以解析文件夹和二进制模板
2025
- (NSDictionary *)getTemplateInfoWithTemplateItem:(GXTemplateItem *)templateItem;
2126

2227
@optional

README-ZH.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<h1 align="center">
2-
<img src="https://img.alicdn.com/imgextra/i2/O1CN01DvZYVD1hLaOVqNlkK_!!6000000004261-2-tps-1024-1024.png" width="250" alt="GaiaX-logo">
2+
<img src="https://gw.alicdn.com/imgextra/i2/O1CN0140Ny0n1kYMIeGmeyp_!!6000000004695-2-tps-1024-1024.png" width="250" alt="GaiaX-logo">
33
</h1>
44
<p align="center">
55
GaiaX动态模板引擎是阿里巴巴优酷技术团队研发的一套轻量级的纯原生动态化卡片跨端解决方案

0 commit comments

Comments
 (0)