|
|
@@ -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()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|