瀏覽代碼

提交人:jtm
提交内容:bug fixed

江天明 1 年之前
父節點
當前提交
8e947282b2

+ 55 - 30
BusinessMain/src/main/java/com/develop/main/ui/RecipesFragment.kt

@@ -32,7 +32,10 @@ import com.develop.main.R
 import com.develop.main.databinding.ItemFilterAndSortViewBinding
 import com.develop.main.viewmodel.HomeViewModel
 import com.drake.brv.BindingAdapter
+import com.drake.brv.listener.OnMultiStateListener
 import com.drake.brv.utils.*
+import com.scwang.smart.refresh.layout.api.RefreshLayout
+import com.scwang.smart.refresh.layout.listener.OnMultiListener
 import org.greenrobot.eventbus.EventBus
 import org.greenrobot.eventbus.Subscribe
 
@@ -49,7 +52,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
     private var currentHotWord = ""
     private var categoryCode: String = ""
 
-    private val loadingDialog  by lazy {
+    private val loadingDialog by lazy {
         LoadingDialog()
     }
     private var isRefresh = false
@@ -129,7 +132,6 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
 
         binding.rv.apply {
             setHasFixedSize(true)
-            isNestedScrollingEnabled = false
             linear()
             setup {
                 addType<String>(HOME_TITLE_PROVIDER)
@@ -142,23 +144,27 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
                 onBind {
                     when (itemViewType) {
                         HOME_TITLE_PROVIDER -> {
-                            findView<AppCompatTextView>(R.id.tv_food_category_name).updateText(
-                                getModel()
-                            )
+                            if (!viewModel.isSearch) {
+                                findView<AppCompatTextView>(R.id.tv_food_category_name).updateText(
+                                    getModel()
+                                )
+                            }
                         }
+
                         HOME_FOOD_LIST_PROVIDER -> {
                             val foodListModel = getModel<FoodListModel>()
 
-                            if(foodRecycle == null){
-                                 foodRecycle = findView<RecyclerView>(R.id.food_recycle)
-                                 foodRecycle!!.grid(4)
-                                 foodRecycle!!.setup {
-                                     addType<FoodContentModel>(com.develop.common.R.layout.food_content_provider)
-                                     onBind {
-                                         findView<FoodContentView>(com.develop.common.R.id.food_content_view).setFoodContent(
-                                             getModel()
-                                         )
-                                     } }
+                            if (foodRecycle == null) {
+                                foodRecycle = findView<RecyclerView>(R.id.food_recycle)
+                                foodRecycle!!.grid(4)
+                                foodRecycle!!.setup {
+                                    addType<FoodContentModel>(com.develop.common.R.layout.food_content_provider)
+                                    onBind {
+                                        findView<FoodContentView>(com.develop.common.R.id.food_content_view).setFoodContent(
+                                            getModel()
+                                        )
+                                    }
+                                }
                             }
 
 
@@ -166,6 +172,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
 
 
                         }
+
                         HOME_FUNCTION_PROVIDER -> {
                             filterSortSearchView =
                                 findView<HomeFilterSortSearchView>(R.id.function_view)
@@ -177,12 +184,14 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
                                 onFunctionCallback = functionCallback
                             }
                         }
+
                         HOME_SEARCH_PROVIDER -> {
                             findView<CommonSearchLayout>(R.id.common_search_view).apply {
                                 updateData(getModel())
                                 onHotWordsSelectedListener = hotWordsSelectedCallback
                             }
                         }
+
                         HOME_CATEGORY_PROVIDER -> {
                             findView<CategoryTabViewLayout>(R.id.category_tab_view).apply {
                                 val model = getModel<CategoryModel>()
@@ -253,7 +262,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
 
         override fun onInputSearch(searchContent: String) {
             currentHotWord = searchContent
-            loadingDialog.showDialog(childFragmentManager,"loadingDialog")
+            loadingDialog.showDialog(childFragmentManager, "loadingDialog")
             if (viewModel.recipesType == RecipesType.ONLINE) {
                 viewModel.getOnLineRecipeList("", currentHotWord)
             } else {
@@ -274,7 +283,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
 
         override fun onOnlineRecipesClick() {
             viewModel.apply {
-                loadingDialog.showDialog(childFragmentManager,"loadingDialog")
+                loadingDialog.showDialog(childFragmentManager, "loadingDialog")
                 getOnLineCategoryList()
                 recipesType = RecipesType.ONLINE
                 netFoodLiveData.postValue(true)
@@ -288,7 +297,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
     private var hotWordsSelectedCallback = object : CommonSearchLayout.OnHotWordsSelectedListener {
         override fun onHotWordsSelected(hotWord: String) {
             currentHotWord = hotWord
-            loadingDialog.showDialog(childFragmentManager,"loadingDialog")
+            loadingDialog.showDialog(childFragmentManager, "loadingDialog")
             if (viewModel.recipesType == RecipesType.ONLINE) {
                 viewModel.getOnLineRecipeList("", currentHotWord)
             } else {
@@ -304,7 +313,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
     private var categoryTabSelectedCallback =
         object : CategoryTabViewLayout.OnCategoryTabSelectedListener {
             override fun onCurrentSelected(categoryType: CategoryType) {
-                loadingDialog.showDialog(childFragmentManager,"loadingDialog")
+                loadingDialog.showDialog(childFragmentManager, "loadingDialog")
                 if (viewModel.recipesType == RecipesType.ONLINE) {
                     viewModel.getOnLineRecipeList("", currentHotWord)
                 } else {
@@ -389,9 +398,12 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
 
             //获取对应分类菜谱回调
             localRecipesLiveData.observe(viewLifecycleOwner) {
-                loadingDialog.removeSelf()
                 binding.page.apply {
-                    finishLoadMore(true)
+                    if (isRefresh){
+                        finishRefresh()
+                    } else {
+                        finishLoadMore(true)
+                    }
                 }
                 binding.rv.apply {
                     models = it
@@ -399,13 +411,17 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
                         scrollToPosition(0)
                     }
                 }
+                loadingDialog.removeSelf()
             }
 
             //获取线上菜谱列表回调
             onLineRecipesLiveData.observe(viewLifecycleOwner) {
-                loadingDialog.removeSelf()
                 binding.page.apply {
-                    finishLoadMore(true)
+                    if (isRefresh){
+                        finishRefresh()
+                    } else {
+                        finishLoadMore(true)
+                    }
                 }
                 binding.rv.apply {
                     models = it
@@ -413,6 +429,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
                         scrollToPosition(0)
                     }
                 }
+                loadingDialog.removeSelf()
             }
 
             //获取线上分类回调
@@ -436,11 +453,15 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
 
             //搜索回调
             hotWordSearchRecipesLiveData.observe(viewLifecycleOwner) {
-                loadingDialog.removeSelf()
                 binding.page.apply {
-                    finishLoadMore(0)
+                    if (isRefresh){
+                        finishRefresh()
+                    } else {
+                        finishLoadMore(true)
+                    }
                 }
                 binding.rv.models = it
+                loadingDialog.removeSelf()
             }
         }
     }
@@ -448,7 +469,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
 
     /**更新分类名称并获取对应数据*/
     private fun updateCategoryName(devRecipeCategory: DevRecipeCategory) {
-        loadingDialog.showDialog(childFragmentManager,"loadingDialog")
+        loadingDialog.showDialog(childFragmentManager, "loadingDialog")
         viewModel.currCategoryName = devRecipeCategory.name ?: ""
         filterSortSearchView?.updateFilterName(
             viewModel.currCategoryName
@@ -466,23 +487,27 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
 
     /**更新分类排序并获取对应数据*/
     private fun updateSortName(pos: Int, needRefresh: Boolean = true) {
-        loadingDialog.showDialog(childFragmentManager,"loadingDialog")
+        loadingDialog.showDialog(childFragmentManager, "loadingDialog")
         filterSortSearchView?.updateSortName(
             sortDataList[pos].name
         )
         viewModel.sortedType = when (pos) {
-            3 -> {
+            1 -> {
                 SortedType.Popular
             }
-            1 -> {
+
+            2 -> {
                 SortedType.Newest
             }
-            2 -> {
+
+            3 -> {
                 SortedType.Score
             }
+
             0 -> {
                 SortedType.A2Z
             }
+
             else -> {
                 SortedType.A2Z
             }

+ 16 - 11
BusinessMain/src/main/java/com/develop/main/viewmodel/HomeViewModel.kt

@@ -1,5 +1,6 @@
 package com.develop.main.viewmodel
 
+import androidx.annotation.RestrictTo.Scope
 import androidx.lifecycle.MutableLiveData
 import androidx.lifecycle.scopeNetLife
 import com.develop.base.ext.getSN
@@ -75,9 +76,11 @@ class HomeViewModel : BaseViewModel() {
     private var lastLocalSearchFoodList = mutableListOf<DevRecipe>()
     private var localFavoriteRecipesList = mutableListOf<UserFavoriteRecipes>()
     private var lastSearchCategoryType = CategoryType.All
+    var moreLocalRecipesCallBack = MutableLiveData<MutableList<FoodContentModel>>()
 
     private var recipeCount = 0
     private var foodCount = 0
+    private var recipeDao = FoodDataProvider.getDatabase().recipeDao()
 
     /**查询本地菜谱分类*/
     fun queryRecipesCategory() {
@@ -176,18 +179,20 @@ class HomeViewModel : BaseViewModel() {
      */
     fun loadMoreLocalRecipes(): Boolean {
         if (hasMoreLocal) {
-            startSize = endSize
-            endSize = startSize + 24
-            if (endSize > totalLocalSize) {
-                endSize = totalLocalSize
-                hasMoreLocal = false
+            scope(Dispatchers.IO){
+                startSize = endSize
+                endSize = startSize + 12
+                if (endSize > totalLocalSize) {
+                    endSize = totalLocalSize
+                    hasMoreLocal = false
+                }
+                val moreData = totalLocalRecipes.subList(startSize, endSize)
+                lastLocalRecipeList.addAll(moreData)
+                lastLocalRecipesShowList = DataFactory.genLocalRecipes(
+                    currCategoryName, lastLocalRecipeList, localFavoriteRecipesList, sortedType
+                )
+                localRecipesLiveData.postValue(lastLocalRecipesShowList)
             }
-            val moreData = totalLocalRecipes.subList(startSize, endSize)
-            lastLocalRecipeList.addAll(moreData)
-            lastLocalRecipesShowList = DataFactory.genLocalRecipes(
-                currCategoryName, lastLocalRecipeList, localFavoriteRecipesList, sortedType
-            )
-            localRecipesLiveData.postValue(lastLocalRecipesShowList)
         } else {
             return false
         }