Skip to content

Commit

Permalink
Merge branch 'main' into androidsupport
Browse files Browse the repository at this point in the history
  • Loading branch information
biezhihua committed Aug 1, 2022
2 parents 152930f + 3c6595c commit b98cbe5
Show file tree
Hide file tree
Showing 17 changed files with 225 additions and 101 deletions.
2 changes: 1 addition & 1 deletion GaiaXAndroidDemo/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".ApiTemplateActivity"
android:name=".RemoteDataSourceTemplateActivity"
android:exported="false" />
<activity
android:name=".EventTemplateActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"templates": [
{
"templateBiz": "remote",
"templateId": "gx-vertical-item"
}
]
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"data":{
"cover-img":{
"value":"$img",
"value":"$img"
},
"sub-title":{
"value":"$desc"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class MainActivity : AppCompatActivity() {
startActivity(intent)
}

findViewById<AppCompatButton>(R.id.api)?.setOnClickListener {
val intent = Intent(MainActivity@ this, ApiTemplateActivity::class.java)
findViewById<AppCompatButton>(R.id.remote)?.setOnClickListener {
val intent = Intent(MainActivity@ this, RemoteDataSourceTemplateActivity::class.java)
startActivity(intent)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package com.alibaba.gaiax.demo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.LinearLayoutCompat
import com.alibaba.fastjson.JSONObject
import com.alibaba.gaiax.GXRegisterCenter
import com.alibaba.gaiax.GXTemplateEngine
import com.alibaba.gaiax.data.assets.GXBinParser
import com.alibaba.gaiax.demo.utils.AssetsUtils
import com.alibaba.gaiax.demo.utils.UiExecutor
import com.alibaba.gaiax.template.GXSize.Companion.dpToPx
import com.alibaba.gaiax.template.GXTemplate
import com.alibaba.gaiax.template.GXTemplateKey
import java.util.concurrent.Executors

class RemoteDataSourceTemplateActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_remote_data_source_template)

// Init
GXTemplateEngine.instance.init(this)

// Register remote data template source to GaiaXSDK
val netTemplateSource = RemoteDataSourceTemplateSource.instance
GXRegisterCenter.instance.registerExtensionTemplateSource(netTemplateSource, 20)

// Request Net Data
RemoteDataSourceNetRequest.instance.requestAsync(
this,
JSONObject(),
object : RemoteDataSourceNetRequest.IDataCallback {
override fun template(bizId: String, templateId: String, data: ByteArray?) {
// add template to net template source
RemoteDataSourceTemplateSource.instance.addTemplate(bizId, templateId, data)
}

override fun finish() {
// Net finish
toBindTemplate()
}
})

// Other
}

private fun toBindTemplate() {

// 模板参数
// 去加载远程模板业务下的gx-vertical-item模板
val params = GXTemplateEngine.GXTemplateItem(this, "remote", "gx-vertical-item")

// 模板绘制尺寸
val size = GXTemplateEngine.GXMeasureSize(100F.dpToPx(), null)

// 模板数据
val templateData = GXTemplateEngine.GXTemplateData(
AssetsUtils.parseAssets(
this,
"data/vertical-item.json"
)
)

// 创建模板View
val view = GXTemplateEngine.instance.createView(params, size)

// 绑定数据
GXTemplateEngine.instance.bindData(view, templateData)

// 插入模板View
findViewById<LinearLayoutCompat>(R.id.root).addView(view, 0)
}

class RemoteDataSourceNetRequest {

interface IDataCallback {
fun template(bizId: String, templateId: String, data: ByteArray?)
fun finish()
}

fun requestAsync(
activity: RemoteDataSourceTemplateActivity,
netParams: JSONObject,
callback: IDataCallback
) {
// Thread
Executors.newSingleThreadExecutor().execute {
// request server
val response = AssetsUtils.parseAssets(
activity,
"remote_data_source/api_response.json"
)

// parse data
response.getJSONArray("templates")?.forEach {
val template = (it as JSONObject)
val templateBiz = template.getString("templateBiz")
val templateId = template.getString("templateId")
val templateBytes = getTemplateContents(activity, templateId)

// callback result
callback.template(templateBiz, templateId, templateBytes)
}

UiExecutor.action {
callback.finish()
}
}
}

// mock net
private fun getTemplateContents(
activity: RemoteDataSourceTemplateActivity,
templateId: String
): ByteArray? {
return try {
activity.resources?.assets?.open("remote_data_source/templates/${templateId}")
?.use { it.readBytes() }
} catch (e: Exception) {
e.printStackTrace()
null
}
}

companion object {

val instance by lazy {
RemoteDataSourceNetRequest()
}
}
}

/**
* 远程模板的数据源
*/
class RemoteDataSourceTemplateSource : GXRegisterCenter.GXIExtensionTemplateSource {

companion object {
val instance by lazy {
RemoteDataSourceTemplateSource()
}
}

private val templateCache: MutableList<GXTemplate> = mutableListOf()

override fun getTemplate(gxTemplateItem: GXTemplateEngine.GXTemplateItem): GXTemplate? {
return templateCache.firstOrNull { it.biz == gxTemplateItem.bizId && it.id == gxTemplateItem.templateId }
}

fun addTemplate(templateBiz: String, templateId: String, bytes: ByteArray?) {
if (bytes != null) {
val binaryData = GXBinParser.parser(bytes)
val layer = binaryData.getString(GXTemplateKey.GAIAX_LAYER)
?: throw IllegalArgumentException("Layer mustn't empty, templateBiz = $templateBiz, templateId = $templateId")
val css = binaryData.getString(GXTemplateKey.GAIAX_CSS) ?: ""
val dataBind = binaryData.getString(GXTemplateKey.GAIAX_DATABINDING) ?: ""
val js = binaryData.getString(GXTemplateKey.GAIAX_JS) ?: ""
val template = GXTemplate(templateId, templateBiz, -1, layer, css, dataBind, js)
templateCache.add(template)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.alibaba.gaiax.demo.utils

import android.os.Handler
import android.os.Looper

object UiExecutor {

val ui: Handler = Handler(Looper.getMainLooper())

fun isMainThread(): Boolean {
// 在单元测试环境下 myLooper为null
return Looper.myLooper() == null || Looper.myLooper() == Looper.getMainLooper()
}

fun action(runnable: Runnable) {
ui.post(runnable)
}

fun action(function: () -> Unit) {
ui.post { function.invoke() }
}

fun removeAction(runnable: Runnable) {
ui.removeCallbacks(runnable)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".RemoteDataSourceTemplateActivity">

</androidx.appcompat.widget.LinearLayoutCompat>
4 changes: 2 additions & 2 deletions GaiaXAndroidDemo/app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
android:textColor="@color/black" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/api"
android:id="@+id/remote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/api"
android:text="@string/remote_data_source"
android:textColor="@color/black" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<string name="container_template">容器模板</string>
<string name="data">数据绑定</string>
<string name="event">事件绑定</string>
<string name="api">Api</string>
<string name="remote_data_source">远程数据源</string>
</resources>
2 changes: 1 addition & 1 deletion GaiaXAndroidDemo/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
<string name="container_template">Container Template</string>
<string name="data">Data Binding</string>
<string name="event">Event Binding</string>
<string name="api">Api</string>
<string name="remote_data_source">Remote Data Source</string>
</resources>
2 changes: 1 addition & 1 deletion GaiaXiOS/GaiaXiOS.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

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

Expand Down
7 changes: 6 additions & 1 deletion GaiaXiOS/GaiaXiOS/Impl/Interface/GXTemplateSourceProtocal.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ NS_ASSUME_NONNULL_BEGIN
@protocol GXTemplateSourceProtocal <NSObject>

@required

// priority
- (NSInteger)priority;

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

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

0 comments on commit b98cbe5

Please sign in to comment.