-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into androidsupport
- Loading branch information
Showing
17 changed files
with
225 additions
and
101 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
8 changes: 8 additions & 0 deletions
8
GaiaXAndroidDemo/app/src/main/assets/remote_data_source/api_response.json
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,8 @@ | ||
{ | ||
"templates": [ | ||
{ | ||
"templateBiz": "remote", | ||
"templateId": "gx-vertical-item" | ||
} | ||
] | ||
} |
Binary file added
BIN
+990 Bytes
GaiaXAndroidDemo/app/src/main/assets/remote_data_source/templates/gx-vertical-item
Binary file not shown.
2 changes: 1 addition & 1 deletion
2
GaiaXAndroidDemo/app/src/main/assets/templates/gx-vertical-item/index.databinding
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"data":{ | ||
"cover-img":{ | ||
"value":"$img", | ||
"value":"$img" | ||
}, | ||
"sub-title":{ | ||
"value":"$desc" | ||
|
80 changes: 0 additions & 80 deletions
80
GaiaXAndroidDemo/app/src/main/kotlin/com/alibaba/gaiax/demo/ApiTemplateActivity.kt
This file was deleted.
Oops, something went wrong.
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
164 changes: 164 additions & 0 deletions
164
...ndroidDemo/app/src/main/kotlin/com/alibaba/gaiax/demo/RemoteDataSourceTemplateActivity.kt
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,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) | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
GaiaXAndroidDemo/app/src/main/kotlin/com/alibaba/gaiax/demo/utils/UiExecutor.kt
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,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) | ||
} | ||
} |
9 changes: 0 additions & 9 deletions
9
GaiaXAndroidDemo/app/src/main/res/layout/activity_api_template.xml
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
GaiaXAndroidDemo/app/src/main/res/layout/activity_remote_data_source_template.xml
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,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> |
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
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
Oops, something went wrong.