Skip to content

Commit

Permalink
add page tempalte extension
Browse files Browse the repository at this point in the history
  • Loading branch information
biezhihua committed May 17, 2024
1 parent b31d89a commit b2e645f
Show file tree
Hide file tree
Showing 19 changed files with 267 additions and 49 deletions.
22 changes: 22 additions & 0 deletions GaiaXAndroid/src/main/kotlin/com/alibaba/gaiax/GXRegisterCenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ class GXRegisterCenter {
fun getTemplate(gxTemplateItem: GXTemplateEngine.GXTemplateItem): GXTemplate?
}

/**
* 页面模板数据源,需要配合 GXTemplateItem.isPageMode=true使用
*/
interface GXIExtensionPageTemplateSource {

/**
* To get GXTemplate from data source
*
* @see GXTemplate
*/
fun getTemplate(gxTemplateItem: GXTemplateEngine.GXTemplateItem): GXTemplate?
}

interface GXIExtensionContainerItemBind {
fun bindViewHolder(
tag: Any?,
Expand Down Expand Up @@ -280,6 +293,11 @@ class GXRegisterCenter {
internal var extensionLottieAnimation: GXIExtensionLottieAnimation? = null
internal var gxItemViewLifecycleListener: GXIItemViewLifecycleListener? = null

/**
* 页面数据源
*/
internal var pageSource: GXRegisterCenter.GXIExtensionPageTemplateSource? = null

fun registerExtensionBizMapRelation(extensionBizMap: GXIExtensionBizMap): GXRegisterCenter {
this.extensionBizMap = extensionBizMap
return this
Expand All @@ -290,6 +308,10 @@ class GXRegisterCenter {
return this
}

fun setExtensionPageTemplateSource(source: GXIExtensionPageTemplateSource) {
pageSource = source
}

/**
* @param source
* @param priority [0,99]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ class GXTemplateEngine {
*/
var isLocal: Boolean = false

/**
* 是否是页面模式,如果是页面模式,那么仅从指定的数据源读取模板数据
*/
var isPageMode: Boolean = false

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
39 changes: 17 additions & 22 deletions GaiaXAndroid/src/main/kotlin/com/alibaba/gaiax/data/GXDataImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ class GXDataImpl(val context: Context) {
*/
class GXTemplateSource(val context: Context) : GXRegisterCenter.GXIExtensionTemplateSource {

data class Value(
val priority: Int,
val source: GXRegisterCenter.GXIExtensionTemplateSource
) {
data class Value(val priority: Int, val source: GXRegisterCenter.GXIExtensionTemplateSource) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand All @@ -68,14 +65,21 @@ class GXDataImpl(val context: Context) {
}
}

private val dataSource: PriorityQueue<Value> =
PriorityQueue<Value>(11) { o1, o2 -> (o2?.priority ?: 0) - (o1?.priority ?: 0) }
private val dataSource: PriorityQueue<Value> = PriorityQueue<Value>(11) { o1, o2 -> (o2?.priority ?: 0) - (o1?.priority ?: 0) }
private val dataSourceSorted: MutableList<Value> = mutableListOf<Value>()

override fun getTemplate(gxTemplateItem: GXTemplateEngine.GXTemplateItem): GXTemplate {
dataSourceSorted.forEach { it ->
it.source.getTemplate(gxTemplateItem)?.let { return it }

// 如果是页面数据源,那么走独立的逻辑
if (gxTemplateItem.isPageMode) {
GXRegisterCenter.instance.pageSource?.getTemplate(gxTemplateItem)?.let { return it }
} else {
dataSourceSorted.forEach { it ->
it.source.getTemplate(gxTemplateItem)?.let { return it }
}
}

// 未倒找数据源则直接抛出异常,默认会被最外层捕获。
throw IllegalArgumentException("Not found target gxTemplate, templateItem = $gxTemplateItem")
}

Expand All @@ -89,8 +93,7 @@ class GXDataImpl(val context: Context) {
this.dataSource.remove(needRemove)
this.dataSource.add(Value(priority, source))

val dataSource: PriorityQueue<Value> =
PriorityQueue(11) { o1, o2 -> (o2?.priority ?: 0) - (o1?.priority ?: 0) }
val dataSource: PriorityQueue<Value> = PriorityQueue(11) { o1, o2 -> (o2?.priority ?: 0) - (o1?.priority ?: 0) }
dataSource.addAll(this.dataSource)
dataSourceSorted.clear()
while (dataSource.isNotEmpty()) {
Expand All @@ -107,13 +110,9 @@ class GXDataImpl(val context: Context) {
/**
* @suppress
*/
class GXTemplateInfoSource(val context: Context) :
GXRegisterCenter.GXIExtensionTemplateInfoSource {
class GXTemplateInfoSource(val context: Context) : GXRegisterCenter.GXIExtensionTemplateInfoSource {

data class Value(
val priority: Int,
val source: GXRegisterCenter.GXIExtensionTemplateInfoSource
) {
data class Value(val priority: Int, val source: GXRegisterCenter.GXIExtensionTemplateInfoSource) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand All @@ -130,8 +129,7 @@ class GXDataImpl(val context: Context) {
}
}

private val dataSource: PriorityQueue<Value> =
PriorityQueue<Value>(11) { o1, o2 -> (o2?.priority ?: 0) - (o1?.priority ?: 0) }
private val dataSource: PriorityQueue<Value> = PriorityQueue<Value>(11) { o1, o2 -> (o2?.priority ?: 0) - (o1?.priority ?: 0) }

private val dataSourceSorted: MutableList<Value> = mutableListOf<Value>()

Expand All @@ -142,10 +140,7 @@ class GXDataImpl(val context: Context) {
throw IllegalArgumentException("Not found target gxTemplateInfo, templateItem = $gxTemplateItem")
}

fun registerByPriority(
source: GXRegisterCenter.GXIExtensionTemplateInfoSource,
priority: Int
) {
fun registerByPriority(source: GXRegisterCenter.GXIExtensionTemplateInfoSource, priority: Int) {
var needRemove: Value? = null
this.dataSource.forEach {
if (it.priority == priority) {
Expand Down
1 change: 1 addition & 0 deletions GaiaXAndroidDemo/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion GaiaXAndroidDemo/.idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion GaiaXAndroidDemo/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ dependencies {

implementation("com.github.princekin-f:EasyFloat:2.0.4")

implementation 'com.airbnb.android:lottie:4.1.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'com.airbnb.android:lottie:4.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
implementation 'com.google.android.material:material:1.8.0'
Expand Down
11 changes: 8 additions & 3 deletions GaiaXAndroidDemo/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".PageTemplateActivity"
android:exported="false" />

<profileable android:shell="true" android:enabled="true"/>
<profileable
android:enabled="true"
android:shell="true" />

<activity
android:name="com.alibaba.gaiax.demo.fastpreview.GXQRCodeActivity"
android:name=".fastpreview.GXQRCodeActivity"
android:screenOrientation="portrait"
android:theme="@style/CaptureTheme" />
<activity
android:name="com.alibaba.gaiax.demo.fastpreview.GXFastPreviewActivity"
android:name=".fastpreview.GXFastPreviewActivity"
android:screenOrientation="portrait" />
<activity
android:name=".StyleTemplateActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"img": "https://t7.baidu.com/it/u=376303577,3502948048&fm=193&f=GIF",
"title": "我是标题",
"desc": "我是副标题"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#gx-vertical-item {
flex-direction: column;
width: 100px;
overflow: flex;
}
#cover-img {
background-color: #f7f7f7;
background-size: cover;
aspect-ratio: 0.67;
border-radius: 7px;
width: 100%;
}
#title_group {
width: 100%;
flex-direction: column;
}
#title {
color: #000000;
font-size: 14px;
margin-top: 9px;
height: 21px;
width: 100%;
}
#sub-title {
color: #999999;
font-size: 12px;
height: 17px;
width: 100%;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"data":{
"cover-img":{
"value":"$img"
},
"sub-title":{
"value":"$desc"
},
"title":{
"value":"$title"
},
"title_group":{
"accessibilityDesc": "'无障碍文案'",
"accessibilityEnable": true,
"accessibilityTraits": "'header'"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"id": "gx-vertical-item",
"type": "gaia-template",
"package": {
"id": "gx-vertical-item",
"version": "0.0.1",
"modify-timestamp": "Mon Mar 18 2024 10:52:40 GMT+0800 (China Standard Time)",
"constraint-size": { "width": 100, "height": 667, "zoom": 1 },
"dependencies": {}
},
"layers": [
{ "id": "cover-img", "class": "cover-img", "type": "image" },
{
"id": "title_group",
"type": "view",
"layers": [
{ "id": "title", "class": "title", "type": "text" },
{ "id": "sub-title", "class": "sub-title", "type": "text" }
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ class ContainerTemplateActivity : AppCompatActivity() {
GXTemplateEngine.instance.init(activity)

// 模板参数
val params =
GXTemplateEngine.GXTemplateItem(activity, "assets_data_source/templates", "gx-content-uper-scroll")
val params = GXTemplateEngine.GXTemplateItem(activity, "assets_data_source/templates", "gx-content-uper-scroll")

// 模板绘制尺寸
val size = GXTemplateEngine.GXMeasureSize(GXScreenUtils.getScreenWidthPx(this), null)

// 模板数据
val templateData =
GXTemplateEngine.GXTemplateData(AssetsUtils.parseAssets(activity, "assets_data_source/data/uper.json"))
val templateData = GXTemplateEngine.GXTemplateData(AssetsUtils.parseAssets(activity, "assets_data_source/data/uper.json"))

// 创建模板View
val view = GXTemplateEngine.instance.createView(params, size)!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class JSTemplateActivity : AppCompatActivity() {

private fun sendNativeMessage() {
findViewById<Button>(R.id.template_btn).setOnClickListener {
val nativeData: JSONObject = JSONObject()
val nativeData = JSONObject()
nativeData["key1"] = "Native Message Value1"
val nativeMessageProtocol: JSONObject = JSONObject()
val nativeMessageProtocol = JSONObject()
nativeMessageProtocol["userData"] = nativeData
nativeMessageProtocol["type"] = "CustomNotificationNameForNative"
GXJSEngineProxy.instance.dispatchNativeEvent(nativeMessageProtocol)
Expand All @@ -57,9 +57,7 @@ class JSTemplateActivity : AppCompatActivity() {
var customGXView: View? = null;
var apiGXView: View? = null;

private fun renderJSCustomModuleDemoTemplate(
activity: JSTemplateActivity, templateId: String
) {
private fun renderJSCustomModuleDemoTemplate(activity: JSTemplateActivity, templateId: String) {
// 初始化
GXTemplateEngine.instance.init(activity)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ import com.lzf.easyfloat.permission.PermissionUtils

class MainActivity : AppCompatActivity() {

private val launcher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
val scanResult =
it.data?.getStringExtra("SCAN_RESULT") ?: return@registerForActivityResult
Log.d("MainActivity", "StartActivityForResult() called scanResult = $scanResult")
val intent = Intent(MainActivity@ this, GXFastPreviewActivity::class.java)
intent.putExtra(GXFastPreviewActivity.GAIA_STUDIO_URL, scanResult)
startActivity(intent)
}
private val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
val scanResult = it.data?.getStringExtra("SCAN_RESULT") ?: return@registerForActivityResult
Log.d("MainActivity", "StartActivityForResult() called scanResult = $scanResult")
val intent = Intent(MainActivity@ this, GXFastPreviewActivity::class.java)
intent.putExtra(GXFastPreviewActivity.GAIA_STUDIO_URL, scanResult)
startActivity(intent)
}


override fun onCreateOptionsMenu(menu: Menu): Boolean {
Expand Down Expand Up @@ -132,6 +130,11 @@ class MainActivity : AppCompatActivity() {
val intent = Intent(MainActivity@ this, JSTemplateActivity::class.java)
startActivity(intent)
}

findViewById<AppCompatButton>(R.id.page_template)?.setOnClickListener {
val intent = Intent(MainActivity@ this, PageTemplateActivity::class.java)
startActivity(intent)
}
}

private fun initGXDevTools() {
Expand Down Expand Up @@ -190,8 +193,7 @@ class MainActivity : AppCompatActivity() {

GXRegisterCenter.instance.registerExtensionExpression(GXExtensionMultiVersionExpression())

GXRegisterCenter.instance.registerExtensionException(object :
GXRegisterCenter.GXIExtensionException {
GXRegisterCenter.instance.registerExtensionException(object : GXRegisterCenter.GXIExtensionException {
override fun exception(exception: Exception) {
exception.printStackTrace()
}
Expand All @@ -200,8 +202,7 @@ class MainActivity : AppCompatActivity() {
GXRegisterCenter.instance.registerExtensionTemplateSource(GXManualPushSource.instance, 101)
.registerExtensionTemplateSource(GXFastPreviewSource.instance, 102)

GXRegisterCenter.instance.registerExtensionFontFamily(object :
GXRegisterCenter.GXIExtensionFontFamily {
GXRegisterCenter.instance.registerExtensionFontFamily(object : GXRegisterCenter.GXIExtensionFontFamily {
override fun fontFamily(fontFamilyName: String): Typeface? {
if (fontFamilyName == "iconfont") {
return Typeface.createFromAsset(assets, "$fontFamilyName.ttf")
Expand Down
Loading

0 comments on commit b2e645f

Please sign in to comment.