Browse Source

[feat]键盘插件,组件支持用名称注册,用于实现组件多实例的情况

hezihao 9 months ago
parent
commit
1d7065d2de

+ 42 - 4
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/ComponentMediator.kt

@@ -9,29 +9,67 @@ object ComponentMediator {
     /**
      * 组件Map,保存组件的实例
      */
-    private val mComponentMap: MutableMap<Class<*>, IUIComponent> = mutableMapOf()
+    private val mComponentMap: MutableMap<String, IUIComponent> = mutableMapOf()
 
     /**
      * 注册组件
+     *
+     * @param componentInterfaceClazz 组件的接口Class类
+     * @param component 组件实例
      */
     fun <T : IUIComponent> registerComponent(
         componentInterfaceClazz: Class<T>,
         component: IUIComponent
     ) {
-        mComponentMap[componentInterfaceClazz] = component
+        mComponentMap[componentInterfaceClazz.name] = component
+    }
+
+    /**
+     * 注册组件
+     *
+     * @param componentName 组件名称
+     * @param component 组件实例
+     */
+    fun registerComponent(
+        componentName: String,
+        component: IUIComponent
+    ) {
+        mComponentMap[componentName] = component
     }
 
     /**
      * 取消注册组件
+     *
+     * @param componentInterfaceClazz 组件的接口Class类
      */
     fun <T : IUIComponent> unRegisterComponent(componentInterfaceClazz: Class<T>) {
-        mComponentMap.remove(componentInterfaceClazz)
+        mComponentMap.remove(componentInterfaceClazz.name)
+    }
+
+    /**
+     * 取消注册组件
+     *
+     * @param componentName 组件名称
+     */
+    fun unRegisterComponent(componentName: String) {
+        mComponentMap.remove(componentName)
     }
 
     /**
      * 查找组件
+     *
+     * @param componentInterfaceClazz 组件的接口Class类
      */
     fun <T : IUIComponent> findComponent(componentInterfaceClazz: Class<T>): T? {
-        return mComponentMap[componentInterfaceClazz] as T?
+        return mComponentMap[componentInterfaceClazz.name] as T?
+    }
+
+    /**
+     * 查找组件
+     *
+     * @param componentName 组件名称
+     */
+    fun <T : IUIComponent> findComponent(componentName: String): T? {
+        return mComponentMap[componentName] as T?
     }
 }

+ 50 - 3
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/base/BaseUIComponent.kt

@@ -1,9 +1,11 @@
 package com.atmob.keyboard_android.component.base
 
 import android.content.Context
+import android.content.res.TypedArray
 import android.util.AttributeSet
 import android.view.LayoutInflater
 import android.view.View
+import com.atmob.keyboard_android.R
 import com.atmob.keyboard_android.component.ComponentMediator
 
 /**
@@ -12,9 +14,16 @@ import com.atmob.keyboard_android.component.ComponentMediator
 abstract class BaseUIComponent<T : IUIComponent> @JvmOverloads constructor(
     context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
 ) : GroupComponent(context, attrs, defStyleAttr), IBusinessComponent {
+    /**
+     * 组件名称,不设置则为null
+     */
+    private var mComponentName: String? = null
+
     init {
-        // 组件创建,注册组件
-        ComponentMediator.registerComponent(getComponentInterfaceClazz(), this)
+        // 初始化自定义属性
+        initAttr(context, attrs)
+        // 组件创建,注册组件,如果有自定义组件名称,则使用自定义的,否则使用接口的类名来注册组件
+        doRegisterComponent()
         val view = LayoutInflater.from(context).inflate(onInflateViewId(), this, true)
         findView(view)
         bindView(view)
@@ -23,7 +32,45 @@ abstract class BaseUIComponent<T : IUIComponent> @JvmOverloads constructor(
     override fun onDetachedFromWindow() {
         super.onDetachedFromWindow()
         // 组件销毁,解开注册
-        ComponentMediator.unRegisterComponent(getComponentInterfaceClazz())
+        doUnRegisterComponent()
+    }
+
+    /**
+     * 注册组件
+     */
+    private fun doRegisterComponent() {
+        val componentName = mComponentName ?: ""
+        if (componentName.isNotBlank()) {
+            ComponentMediator.registerComponent(componentName, this)
+        } else {
+            ComponentMediator.registerComponent(getComponentInterfaceClazz(), this)
+        }
+    }
+
+    /**
+     * 解注册组件
+     */
+    private fun doUnRegisterComponent() {
+        val componentName = mComponentName ?: ""
+        if (componentName.isNotBlank()) {
+            ComponentMediator.unRegisterComponent(componentName)
+        } else {
+            ComponentMediator.unRegisterComponent(getComponentInterfaceClazz())
+        }
+    }
+
+    /**
+     * 初始化自定义属性
+     */
+    private fun initAttr(context: Context, attrs: AttributeSet? = null) {
+        var typeArray: TypedArray? = null
+        try {
+            typeArray =
+                context.obtainStyledAttributes(attrs, R.styleable.BaseUIComponent)
+            mComponentName = typeArray.getString(R.styleable.BaseUIComponent_uic_component_name)
+        } finally {
+            typeArray?.recycle()
+        }
     }
 
     /**

+ 8 - 0
plugins/keyboard_android/android/src/main/res/values/attrs_component.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <!-- UI组件自定义属性 -->
+    <declare-styleable name="BaseUIComponent">
+        <!-- 组件的名称,如果不设置,默认名称为组件接口的className全路径 -->
+        <attr name="uic_component_name" format="string" />
+    </declare-styleable>
+</resources>