Bläddra i källkod

[feat]键盘插件,实现选择键盘页的切换

hezihao 8 månader sedan
förälder
incheckning
34401ccd29

+ 17 - 0
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/base/animator/impl/AlphaAnimator.kt

@@ -14,7 +14,15 @@ class AlphaAnimator : ComponentAnimator() {
     private var mShowAnimator: ValueAnimator? = null
     private var mHideAnimator: ValueAnimator? = null
 
+    /**
+     * 是否显示中
+     */
+    private var isShow = false
+
     override fun show() {
+        if (isShow) {
+            return
+        }
         if (mShowAnimator != null && mShowAnimator!!.isRunning) {
             return
         }
@@ -32,6 +40,11 @@ class AlphaAnimator : ComponentAnimator() {
                         super.onAnimationStart(animation)
                         rootView.visibility = View.VISIBLE
                     }
+
+                    override fun onAnimationEnd(animation: Animator) {
+                        super.onAnimationEnd(animation)
+                        isShow = true
+                    }
                 })
             }
             mShowAnimator!!.start()
@@ -39,6 +52,9 @@ class AlphaAnimator : ComponentAnimator() {
     }
 
     override fun hide() {
+        if (!isShow) {
+            return
+        }
         if (mHideAnimator != null && mHideAnimator!!.isRunning) {
             return
         }
@@ -55,6 +71,7 @@ class AlphaAnimator : ComponentAnimator() {
                     override fun onAnimationEnd(animation: Animator) {
                         super.onAnimationEnd(animation)
                         rootView.visibility = View.GONE
+                        isShow = false
                     }
                 })
             }

+ 86 - 0
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/base/animator/impl/TransitionAnimator.kt

@@ -0,0 +1,86 @@
+package com.atmob.keyboard_android.component.base.animator.impl
+
+import android.animation.Animator
+import android.animation.AnimatorListenerAdapter
+import android.animation.ValueAnimator
+import android.view.View
+import android.view.animation.AccelerateInterpolator
+import com.atmob.keyboard_android.component.base.animator.ComponentAnimator
+import com.atmob.keyboard_android.ext.measureView
+
+/**
+ * 移动效果的动画执行器
+ */
+class TransitionAnimator : ComponentAnimator() {
+    private var mShowAnimator: ValueAnimator? = null
+    private var mHideAnimator: ValueAnimator? = null
+
+    /**
+     * 是否显示中
+     */
+    private var isShow = false
+
+    override fun show() {
+        if (isShow) {
+            return
+        }
+        if (mShowAnimator != null && mShowAnimator!!.isRunning) {
+            return
+        }
+        val uiComponent = getUIComponent()
+        uiComponent?.asView()?.let { rootView ->
+            // 先测量view后,才能获取到尺寸
+            measureView(rootView)
+            mShowAnimator = ValueAnimator.ofFloat(rootView.measuredWidth.toFloat(), 0f).apply {
+                duration = 300
+                interpolator = AccelerateInterpolator()
+                addUpdateListener { animation ->
+                    val translationX = animation.animatedValue as Float
+                    rootView.translationX = translationX
+                }
+                addListener(object : AnimatorListenerAdapter() {
+                    override fun onAnimationStart(animation: Animator) {
+                        super.onAnimationStart(animation)
+                        rootView.visibility = View.VISIBLE
+                    }
+
+                    override fun onAnimationEnd(animation: Animator) {
+                        super.onAnimationEnd(animation)
+                        isShow = true
+                    }
+                })
+            }
+            mShowAnimator!!.start()
+        }
+    }
+
+    override fun hide() {
+        if (!isShow) {
+            return
+        }
+        if (mHideAnimator != null && mHideAnimator!!.isRunning) {
+            return
+        }
+        val uiComponent = getUIComponent()
+        uiComponent?.asView()?.let { rootView ->
+            // 先测量view后,才能获取到尺寸
+            measureView(rootView)
+            mHideAnimator = ValueAnimator.ofFloat(0f, rootView.measuredWidth.toFloat()).apply {
+                duration = 200
+                interpolator = AccelerateInterpolator()
+                addUpdateListener { animation ->
+                    val translationX = animation.animatedValue as Float
+                    rootView.translationX = translationX
+                }
+                addListener(object : AnimatorListenerAdapter() {
+                    override fun onAnimationEnd(animation: Animator) {
+                        super.onAnimationEnd(animation)
+                        rootView.visibility = View.GONE
+                        isShow = false
+                    }
+                })
+            }
+            mHideAnimator!!.start()
+        }
+    }
+}

+ 10 - 1
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/child/impl/KeyboardSelectComponent.kt

@@ -8,6 +8,7 @@ import androidx.recyclerview.widget.GridLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import com.atmob.keyboard_android.R
 import com.atmob.keyboard_android.component.base.BaseUIComponent
+import com.atmob.keyboard_android.component.base.animator.impl.TransitionAnimator
 import com.atmob.keyboard_android.component.child.IKeyboardSelectComponent
 import com.atmob.keyboard_android.component.item.EmptyPlaceholderViewBinder
 import com.atmob.keyboard_android.component.item.KeyboardSelectViewBinder
@@ -17,6 +18,7 @@ import com.atmob.keyboard_android.model.EmptyPlaceholderModel
 import com.atmob.keyboard_android.model.KeyboardSelectModel
 import com.atmob.keyboard_android.util.recyclerview.GridDivider
 import com.blankj.utilcode.util.ConvertUtils
+import com.blankj.utilcode.util.ToastUtils
 import me.drakeet.multitype.Items
 import me.drakeet.multitype.MultiTypeAdapter
 
@@ -48,9 +50,12 @@ class KeyboardSelectComponent @JvmOverloads constructor(
     override fun bindView(view: View) {
         vBackBtn.click {
             // 关闭选择页
+            hide()
         }
         vSaveBtn.click {
             // 保存设置
+            ToastUtils.showShort("保存设置")
+            hide()
         }
 
         // 配置列表
@@ -91,10 +96,14 @@ class KeyboardSelectComponent @JvmOverloads constructor(
         return IKeyboardSelectComponent::class.java
     }
 
+    override fun getComponentAnimator(): Class<*> {
+        return TransitionAnimator::class.java
+    }
+
     @SuppressLint("NotifyDataSetChanged")
     private fun setData() {
         // TODO: 加载键盘列表
-        val emptyPlaceholderItemHeight = ConvertUtils.dp2px(60f)
+        val emptyPlaceholderItemHeight = ConvertUtils.dp2px(60f - 16f)
         mListItems.apply {
             // 添加空占位条目
             add(EmptyPlaceholderModel(emptyPlaceholderItemHeight))

+ 23 - 3
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/child/impl/QuickSwitchComponent.kt

@@ -3,9 +3,14 @@ package com.atmob.keyboard_android.component.child.impl
 import android.content.Context
 import android.util.AttributeSet
 import android.view.View
+import android.widget.TextView
 import com.atmob.keyboard_android.R
+import com.atmob.keyboard_android.component.ComponentMediator
 import com.atmob.keyboard_android.component.base.BaseUIComponent
+import com.atmob.keyboard_android.component.child.IKeyboardSelectComponent
 import com.atmob.keyboard_android.component.child.IQuickSwitchComponent
+import com.atmob.keyboard_android.enums.Mode
+import com.atmob.keyboard_android.ext.click
 import com.atmob.keyboard_android.ui.popup.ModeSwitchPopupWindow
 
 /**
@@ -15,11 +20,13 @@ class QuickSwitchComponent @JvmOverloads constructor(
     context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
 ) : BaseUIComponent<IQuickSwitchComponent>(context, attrs, defStyleAttr), IQuickSwitchComponent {
     private lateinit var vModeSwitchLayout: View
+    private lateinit var vModeText: TextView
+    private lateinit var vKeyboardSelectLayout: View
 
     /**
      * 模式切换的弹窗
      */
-    private val mModeSwitchPopupWindow = ModeSwitchPopupWindow(context)
+    private val mModeSwitchPopupWindow = ModeSwitchPopupWindow()
 
     override fun onInflateViewId(): Int {
         return R.layout.quick_switch_component
@@ -27,17 +34,30 @@ class QuickSwitchComponent @JvmOverloads constructor(
 
     override fun findView(view: View) {
         vModeSwitchLayout = view.findViewById(R.id.mode_switch_layout)
+        vModeText = view.findViewById(R.id.mode_text)
+        vKeyboardSelectLayout = view.findViewById(R.id.keyboard_select_layout)
     }
 
     override fun bindView(view: View) {
-        vModeSwitchLayout.setOnClickListener {
+        vModeSwitchLayout.click {
             // 弹出模式切换弹窗
             if (mModeSwitchPopupWindow.isShowing()) {
                 mModeSwitchPopupWindow.dismiss()
             } else {
-                mModeSwitchPopupWindow.show(vModeSwitchLayout)
+                mModeSwitchPopupWindow.show(vModeSwitchLayout, Mode.HELP_CHAT, object :
+                    ModeSwitchPopupWindow.OnModeSelectedListener {
+                    override fun onModeSelected(mode: Mode) {
+                        vModeText.text = mode.modeName
+                    }
+                })
             }
         }
+        vKeyboardSelectLayout.click {
+            // 显示键盘选择页
+            val keyboardSelectComponent =
+                ComponentMediator.findComponent(IKeyboardSelectComponent::class.java)
+            keyboardSelectComponent?.show()
+        }
     }
 
     override fun getComponentInterfaceClazz(): Class<IQuickSwitchComponent> {

+ 13 - 5
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/ui/popup/ModeSwitchPopupWindow.kt

@@ -1,7 +1,6 @@
 package com.atmob.keyboard_android.ui.popup
 
 import android.annotation.SuppressLint
-import android.content.Context
 import android.view.Gravity
 import android.view.LayoutInflater
 import android.view.View
@@ -21,16 +20,24 @@ import me.drakeet.multitype.MultiTypeAdapter
 /**
  * 模式切换PopupWindow弹窗
  */
-class ModeSwitchPopupWindow(private val context: Context) {
+class ModeSwitchPopupWindow() {
     private var popupWindow: PopupWindow? = null
 
     /**
+     * 模式切换监听器
+     */
+    interface OnModeSelectedListener {
+        fun onModeSelected(mode: Mode)
+    }
+
+    /**
      * 显示弹窗
      *
      * @param anchorView 锚点View
+     * @param currentMode 当前选中的模式
      */
     @SuppressLint("InflateParams", "NotifyDataSetChanged")
-    fun show(anchorView: View) {
+    fun show(anchorView: View, currentMode: Mode, listener: OnModeSelectedListener) {
         if (isShowing()) {
             return
         }
@@ -52,8 +59,7 @@ class ModeSwitchPopupWindow(private val context: Context) {
 
             val listItems = Items().apply {
                 addAll(Mode.getAll().mapIndexed { index, item ->
-                    // TODO: 先写死选中第一个,后续根据flutter端传过来的模式,来决定选中哪个
-                    ModeItem(item, selected = index == 0)
+                    ModeItem(item, selected = item.modeName == currentMode.modeName)
                 })
             }
 
@@ -76,6 +82,8 @@ class ModeSwitchPopupWindow(private val context: Context) {
 
                         // 更新当前选择的模式
                         currentSelectMode.text = targetItem.mode.modeName
+                        // 通知外部更新模式
+                        listener.onModeSelected(targetItem.mode)
 
                         // 关闭弹窗
                         dismiss()

+ 1 - 0
plugins/keyboard_android/android/src/main/res/layout/component_key_board_container.xml

@@ -14,6 +14,7 @@
     <FrameLayout
         android:layout_width="match_parent"
         android:layout_height="0dp"
+        android:layout_marginTop="16dp"
         android:layout_weight="1">
 
         <!-- 拼音键盘 -->

+ 2 - 2
plugins/keyboard_android/android/src/main/res/layout/component_keyboard_select.xml

@@ -4,7 +4,8 @@
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="@dimen/keyboard_content_height"
-    tools:background="@mipmap/bg_keyboard">
+    android:background="@mipmap/bg_keyboard"
+    android:clickable="true">
 
     <androidx.recyclerview.widget.RecyclerView
         android:id="@+id/list"
@@ -20,7 +21,6 @@
         android:layout_width="32dp"
         android:layout_height="32dp"
         android:layout_marginStart="12dp"
-        android:layout_marginTop="14dp"
         android:src="@mipmap/ic_back_btn"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />

+ 22 - 14
plugins/keyboard_android/android/src/main/res/layout/quick_switch_component.xml

@@ -23,6 +23,7 @@
             android:gravity="center_vertical">
 
             <TextView
+                android:id="@+id/mode_text"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_gravity="center_vertical"
@@ -43,21 +44,28 @@
                 android:src="@mipmap/ic_arrow_btn" />
         </LinearLayout>
 
-        <TextView
+        <LinearLayout
+            android:id="@+id/keyboard_select_layout"
             android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_gravity="center_vertical"
-            android:layout_marginStart="9dp"
-            android:text="小蕊"
-            android:textColor="@color/text_color_primary"
-            android:textSize="12sp" />
+            android:layout_height="match_parent"
+            android:orientation="horizontal">
 
-        <ImageView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_gravity="center_vertical"
-            android:layout_marginStart="4dp"
-            android:layout_marginEnd="8dp"
-            android:src="@mipmap/ic_switch" />
+            <TextView
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_gravity="center_vertical"
+                android:layout_marginStart="9dp"
+                android:text="小蕊"
+                android:textColor="@color/text_color_primary"
+                android:textSize="12sp" />
+
+            <ImageView
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_gravity="center_vertical"
+                android:layout_marginStart="4dp"
+                android:layout_marginEnd="8dp"
+                android:src="@mipmap/ic_switch" />
+        </LinearLayout>
     </LinearLayout>
 </FrameLayout>

+ 0 - 4
plugins/keyboard_android/android/src/main/res/layout/view_ai_keyboard_content.xml

@@ -5,10 +5,6 @@
     android:layout_height="match_parent"
     android:orientation="vertical">
 
-    <Space
-        android:layout_width="match_parent"
-        android:layout_height="10dp" />
-
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"

+ 1 - 1
plugins/keyboard_android/android/src/main/res/values/dimens.xml

@@ -3,7 +3,7 @@
     <!-- 键盘高度 -->
     <dimen name="keyboard_height">292dp</dimen>
     <!-- 内容高度,不包含工具栏 -->
-    <dimen name="keyboard_content_height">242dp</dimen>
+    <dimen name="keyboard_content_height">226dp</dimen>
     <!-- ViewPager指示器的高度 -->
     <dimen name="common_navigator_height">32dp</dimen>
 </resources>