HapeLee пре 1 година
родитељ
комит
96eb57d28c

+ 48 - 17
app/src/main/java/io/legato/kazusa/ui/dict/DictDialog.kt

@@ -4,6 +4,7 @@ import android.os.Bundle
 import android.text.method.LinkMovementMethod
 import android.text.method.LinkMovementMethod
 import android.view.View
 import android.view.View
 import androidx.fragment.app.viewModels
 import androidx.fragment.app.viewModels
+import androidx.transition.TransitionManager
 import com.google.android.material.tabs.TabLayout
 import com.google.android.material.tabs.TabLayout
 import io.legato.kazusa.R
 import io.legato.kazusa.R
 import io.legato.kazusa.base.BaseBottomSheetDialogFragment
 import io.legato.kazusa.base.BaseBottomSheetDialogFragment
@@ -40,36 +41,60 @@ class DictDialog() : BaseBottomSheetDialogFragment(R.layout.dialog_dict) {
             return
             return
         }
         }
 
 
-        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
-            override fun onTabReselected(tab: TabLayout.Tab) {
-
-            }
-
-            override fun onTabUnselected(tab: TabLayout.Tab) {
+        setupTabs()
+        observeDicts()
+    }
 
 
-            }
+    private fun setupTabs() {
+        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
+            override fun onTabReselected(tab: TabLayout.Tab) = Unit
+            override fun onTabUnselected(tab: TabLayout.Tab) = Unit
 
 
             override fun onTabSelected(tab: TabLayout.Tab) {
             override fun onTabSelected(tab: TabLayout.Tab) {
                 val dictRule = tab.tag as DictRule
                 val dictRule = tab.tag as DictRule
-                binding.rotateLoading.visible()
-                viewModel.dict(dictRule, word!!) {
-                    binding.rotateLoading.gone()
-                    binding.tvDict.setHtml(it)
-                }
+                loadDict(dictRule)
             }
             }
         })
         })
-        viewModel.initData {
-            it.forEach {
+    }
+
+    private fun observeDicts() {
+        viewModel.initData { dictRules ->
+            if (dictRules.isEmpty()) {
+                showEmptyView("暂无可用词典")
+                return@initData
+            }
+
+            binding.emptyMessageView.gone()
+            dictRules.forEach {
                 binding.tabLayout.addTab(binding.tabLayout.newTab().apply {
                 binding.tabLayout.addTab(binding.tabLayout.newTab().apply {
                     text = it.name
                     text = it.name
                     tag = it
                     tag = it
                 })
                 })
             }
             }
-            setupTabLayoutMode(it.size)
+            setupTabLayoutMode(dictRules.size)
+
+
+            binding.tabLayout.getTabAt(0)?.select()
+        }
+    }
+
+    private fun loadDict(dictRule: DictRule) {
+
+        binding.tvDict.gone()
+        binding.emptyMessageView.gone()
+        binding.rotateLoading.visible()
+
+        viewModel.dict(dictRule, word!!) { result ->
+            binding.rotateLoading.gone()
+            if (result.isBlank()) {
+                showEmptyView("没有查询到结果")
+            } else {
+                binding.tvDict.visible()
+                binding.tvDict.setHtml(result)
+            }
         }
         }
     }
     }
 
 
-    //根据已启用词典数动态选取布局
     private fun setupTabLayoutMode(dictCount: Int) {
     private fun setupTabLayoutMode(dictCount: Int) {
         if (dictCount <= 4) {
         if (dictCount <= 4) {
             binding.tabLayout.tabMode = TabLayout.MODE_FIXED
             binding.tabLayout.tabMode = TabLayout.MODE_FIXED
@@ -80,4 +105,10 @@ class DictDialog() : BaseBottomSheetDialogFragment(R.layout.dialog_dict) {
         }
         }
     }
     }
 
 
-}
+    private fun showEmptyView(message: String) {
+        binding.tvDict.gone()
+        binding.rotateLoading.gone()
+        binding.emptyMessageView.visible()
+        binding.emptyMessageView.setMessage(message)
+    }
+}

+ 14 - 6
app/src/main/res/layout/dialog_dict.xml

@@ -1,14 +1,15 @@
 <?xml version="1.0" encoding="utf-8"?>
 <?xml version="1.0" encoding="utf-8"?>
-<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_height="wrap_content"
+    android:orientation="vertical"
     android:paddingHorizontal="16dp"
     android:paddingHorizontal="16dp"
     android:paddingBottom="16dp">
     android:paddingBottom="16dp">
 
 
     <LinearLayout
     <LinearLayout
         android:id="@+id/header_layout"
         android:id="@+id/header_layout"
-        android:layout_width="0dp"
+        android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_height="wrap_content"
         android:orientation="vertical"
         android:orientation="vertical"
         android:gravity="center"
         android:gravity="center"
@@ -46,6 +47,7 @@
         style="@style/Widget.Material3.LoadingIndicator.Contained"
         style="@style/Widget.Material3.LoadingIndicator.Contained"
         android:layout_width="wrap_content"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_height="wrap_content"
+        android:layout_gravity="center"
         android:layout_margin="32dp"
         android:layout_margin="32dp"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
@@ -56,17 +58,23 @@
         android:id="@+id/scroll_container"
         android:id="@+id/scroll_container"
         android:layout_width="match_parent"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_height="wrap_content"
-        android:minHeight="64dp"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@id/tab_layout">
         app:layout_constraintTop_toBottomOf="@id/tab_layout">
 
 
-        <io.legato.kazusa.ui.widget.text.ScrollTextView
+        <TextView
             android:id="@+id/tv_dict"
             android:id="@+id/tv_dict"
             android:layout_width="match_parent"
             android:layout_width="match_parent"
-            android:layout_height="match_parent" />
+            android:layout_height="wrap_content" />
 
 
     </androidx.core.widget.NestedScrollView>
     </androidx.core.widget.NestedScrollView>
 
 
-</androidx.constraintlayout.widget.ConstraintLayout>
+    <io.legato.kazusa.ui.widget.EmptyMessageView
+        android:id="@+id/empty_message_view"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        app:messageText="词典内容为空!"/>
+
+</LinearLayout>