Przeglądaj źródła

[feat]键盘插件,Ai键盘,键盘列表和Ai生成页,使用嵌套路由切换

hezihao 8 miesięcy temu
rodzic
commit
6094f457a6

+ 2 - 2
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/base/AnimationComponent.kt

@@ -3,16 +3,16 @@ package com.atmob.keyboard_android.component.base
 import android.content.Context
 import android.util.AttributeSet
 import android.view.ViewGroup
-import android.widget.FrameLayout
 import com.atmob.keyboard_android.component.base.animator.ComponentAnimator
 import com.atmob.keyboard_android.component.base.animator.impl.NoAnimationAnimator
+import com.atmob.keyboard_android.component.widget.CustomOrderFrameLayout
 
 /**
  * 带有动画功能的组件基类
  */
 abstract class AnimationComponent @JvmOverloads constructor(
     context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
-) : FrameLayout(context, attrs, defStyleAttr), IUIComponent {
+) : CustomOrderFrameLayout(context, attrs, defStyleAttr), IUIComponent {
     /**
      * 动画执行器
      */

+ 13 - 0
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/base/FakeComponent.kt

@@ -0,0 +1,13 @@
+package com.atmob.keyboard_android.component.base
+
+import android.content.Context
+import android.util.AttributeSet
+
+/**
+ * 虚假的组件,用于组件本身不处理业务,而是用于处理一个控件在组件树中,但又不想将这个控件抽成一个组件
+ * 所以通过该方式,将控件做为组件的子控件,让路由组件控制它
+ */
+class FakeComponent @JvmOverloads constructor(
+    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
+) : GroupComponent(context, attrs, defStyleAttr) {
+}

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

@@ -8,9 +8,11 @@ import androidx.recyclerview.widget.LinearLayoutManager
 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.IAiChatComponent
 import com.atmob.keyboard_android.component.item.AiChatViewBinder
 import com.atmob.keyboard_android.component.item.EmptyPlaceholderViewBinder
+import com.atmob.keyboard_android.constant.Constants
 import com.atmob.keyboard_android.ext.click
 import com.atmob.keyboard_android.model.AiChatModel
 import com.atmob.keyboard_android.model.EmptyPlaceholderModel
@@ -93,6 +95,14 @@ class AiChatComponent @JvmOverloads constructor(
         return IAiChatComponent::class.java
     }
 
+    override fun getComponentAnimator(): Class<*> {
+        return if (Constants.COMPONENT_ANIMATOR_ENABLE) {
+            TransitionAnimator::class.java
+        } else {
+            super.getComponentAnimator()
+        }
+    }
+
     /**
      * 配置ViewModel
      */

+ 8 - 5
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/child/impl/AiKeyboardComponent.kt

@@ -7,8 +7,9 @@ import android.view.View
 import androidx.recyclerview.widget.GridLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 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.base.FakeComponent
+import com.atmob.keyboard_android.component.base.RouteComponent
 import com.atmob.keyboard_android.component.child.IAiChatComponent
 import com.atmob.keyboard_android.component.child.IAiKeyboardComponent
 import com.atmob.keyboard_android.component.item.AiKeyboardKeyViewBinder
@@ -45,6 +46,7 @@ class AiKeyboardComponent @JvmOverloads constructor(
     private lateinit var vTabBarLayout: View
     private lateinit var vMagicIndicator: MagicIndicator
     private lateinit var vKeyboardKeyContent: View
+    private lateinit var vRouteComponent: RouteComponent
     private lateinit var vPasteBarLayout: View
     private lateinit var vKeyList: RecyclerView
     private lateinit var vPasteBtn: View
@@ -68,6 +70,7 @@ class AiKeyboardComponent @JvmOverloads constructor(
         vTabBarLayout = view.findViewById(R.id.tar_bar_layout)
         vMagicIndicator = view.findViewById(R.id.magic_indicator)
         vKeyboardKeyContent = view.findViewById(R.id.keyboard_key_content)
+        vRouteComponent = view.findViewById(R.id.route_component)
         vPasteBarLayout = view.findViewById(R.id.paste_bar_layout)
         vKeyList = view.findViewById(R.id.key_list)
         vPasteBtn = view.findViewById(R.id.paste_btn)
@@ -317,12 +320,12 @@ class AiKeyboardComponent @JvmOverloads constructor(
      * 控制Ai生成内容面板,是否显示
      */
     private fun controlAiChatPageShowing(isShow: Boolean) {
-        val aiChatComponent =
-            ComponentMediator.findComponent<IAiChatComponent>(IAiChatComponent::class.java)
+        // 切换到Ai生成内容页
         if (isShow) {
-            aiChatComponent?.show()
+            vRouteComponent.routeChildComponent(IAiChatComponent::class.java)
         } else {
-            aiChatComponent?.hide()
+            // 切换回键盘页
+            vRouteComponent.routeChildComponent(FakeComponent::class.java, true)
         }
     }
 }

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

@@ -128,24 +128,6 @@ class KeyboardSelectComponent @JvmOverloads constructor(
      * 设置ViewModel
      */
     private fun setupViewModel() {
-        KeyboardHolder.getKeyboardService()?.run {
-            getKeyboardViewModel().run {
-                // 监听帮助模式切换时,关闭掉选择页
-//                helpMode.observe(getLifecycleOwner()) {
-//                    if (keyboardSelectPageShowing.value == true) {
-//                        hide()
-//                    }
-//                }
-                // 如果设置页打开,则关闭掉选择页
-//                settingPageShowing.observe(getLifecycleOwner()) { isShowing ->
-//                    if (isShowing) {
-//                        if (keyboardSelectPageShowing.value == true) {
-//                            updateKeyboardSelectPageShowing(false)
-//                        }
-//                    }
-//                }
-            }
-        }
     }
 
     @SuppressLint("NotifyDataSetChanged")

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

@@ -56,16 +56,6 @@ class SettingComponent @JvmOverloads constructor(
      * 配置ViewModel
      */
     private fun setupViewModel() {
-        KeyboardHolder.getKeyboardService()?.run {
-            // 如果键盘选择页打开,则关闭设置页
-//            getKeyboardViewModel().keyboardSelectPageShowing.observe(getLifecycleOwner()) { isShowing ->
-//                if (isShowing) {
-//                    if (getKeyboardViewModel().settingPageShowing.value == true) {
-//                        hide()
-//                    }
-//                }
-//            }
-        }
     }
 
     override fun onComponentShow() {

+ 97 - 0
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/component/widget/CustomOrderFrameLayout.java

@@ -0,0 +1,97 @@
+package com.atmob.keyboard_android.component.widget;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.View;
+import android.widget.FrameLayout;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+/**
+ * 可以自定义绘制顺序的FrameLayout
+ */
+public class CustomOrderFrameLayout extends FrameLayout {
+    // 置顶的View索引
+    private int frontIndex = -1;
+    // 置底的View索引
+    private int backIndex = -1;
+
+    public CustomOrderFrameLayout(@NonNull Context context) {
+        this(context, null);
+    }
+
+    public CustomOrderFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public CustomOrderFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+        init();
+    }
+
+    public CustomOrderFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
+        super(context, attrs, defStyleAttr, defStyleRes);
+        init();
+    }
+
+    private void init() {
+        // 启用自定义绘制顺序
+        setChildrenDrawingOrderEnabled(true);
+    }
+
+    /**
+     * 将指定View置顶
+     */
+    @Override
+    public void bringChildToFront(View child) {
+        int index = indexOfChild(child);
+        if (index != -1) {
+            frontIndex = index;
+            // 清除置底标记
+            backIndex = -1;
+            // 触发重绘
+            invalidate();
+        }
+    }
+
+    /**
+     * 将指定View置底
+     */
+    public void sendChildToBack(View child) {
+        int index = indexOfChild(child);
+        if (index != -1) {
+            backIndex = index;
+            // 清除置顶标记
+            frontIndex = -1;
+            invalidate();
+        }
+    }
+
+    @Override
+    protected int getChildDrawingOrder(int childCount, int i) {
+        // 处理置顶逻辑
+        if (frontIndex != -1) {
+            if (i == childCount - 1) {
+                // 最后绘制置顶View
+                return frontIndex;
+            } else if (i >= frontIndex) {
+                // 调整其他View顺序
+                return i + 1;
+            }
+        }
+
+        // 处理置底逻辑
+        if (backIndex != -1) {
+            // 最先绘制置底View
+            if (i == 0) {
+                return backIndex;
+            } else if (i <= backIndex) {
+                // 调整其他View顺序
+                return i - 1;
+            }
+        }
+
+        return super.getChildDrawingOrder(childCount, i);
+    }
+}

+ 12 - 7
plugins/keyboard_android/android/src/main/res/layout/component_ai_keyboard.xml

@@ -76,26 +76,31 @@
             android:layout_height="wrap_content"
             android:orientation="horizontal">
 
-            <FrameLayout
+            <com.atmob.keyboard_android.component.base.RouteComponent
+                android:id="@+id/route_component"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_marginStart="10dp"
                 android:layout_marginEnd="6dp"
                 android:layout_weight="1">
 
-                <androidx.recyclerview.widget.RecyclerView
-                    android:id="@+id/key_list"
+                <com.atmob.keyboard_android.component.base.FakeComponent
                     android:layout_width="match_parent"
-                    android:layout_height="wrap_content"
-                    tools:layout_height="150dp" />
+                    android:layout_height="wrap_content">
+
+                    <androidx.recyclerview.widget.RecyclerView
+                        android:id="@+id/key_list"
+                        android:layout_width="match_parent"
+                        android:layout_height="wrap_content"
+                        tools:layout_height="150dp" />
+                </com.atmob.keyboard_android.component.base.FakeComponent>
 
                 <com.atmob.keyboard_android.component.child.impl.AiChatComponent
-                    android:id="@+id/ai_chat_component"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     android:visibility="gone"
                     tools:visibility="visible" />
-            </FrameLayout>
+            </com.atmob.keyboard_android.component.base.RouteComponent>
 
             <LinearLayout
                 android:layout_width="wrap_content"