Browse Source

提交人:jtm
提交内容:优化

江天明 2 years ago
parent
commit
f6d3984780

+ 2 - 1
BusinessMain/src/main/java/com/develop/main/ui/HomeActivity.kt

@@ -39,7 +39,8 @@ class HomeActivity : CommonBVMActivity<ActivityHomeBinding, HomeViewModel>() {
 
     private val newsPopupWindow by lazy {
         CommonPopupWindow.ViewBuilder<NewsLayout>().width(dp750)
-            .height(LinearLayout.LayoutParams.MATCH_PARENT).outsideTouchable(true).focusable(true).alpha(0.5f)
+            .height(LinearLayout.LayoutParams.MATCH_PARENT).outsideTouchable(true).focusable(true)
+            .alpha(0.5f)
             .clippingEnabled(false).view(NewsLayout(this)).onShowBefore { popupWindow, view ->
                 view.apply {
                     updateNewsData(DataFactory.genTestNewsList())

+ 14 - 11
BusinessMain/src/main/java/com/develop/main/ui/RecipesFragment.kt

@@ -43,6 +43,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
     private var currentHotWord = ""
     private var categoryCode: String = ""
 
+    private var isRefresh = false
 
     private val filterPopupWindow by lazy {
         CommonPopupWindow.ViewBuilder<FilterSortViewLayout>().width(dp417).height(dp549)
@@ -100,10 +101,6 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
         super.onPostCreateView()
         initView()
         initLiveDataCallback()
-    }
-
-    override fun onResume() {
-        super.onResume()
         refreshOnLineOrLocalRecipes()
     }
 
@@ -177,17 +174,18 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
                 refreshOnLineOrLocalRecipes()
             }
             onLoadMore {
+                isRefresh = false
                 if (viewModel.recipesType == RecipesType.ONLINE) {
                     if (viewModel.currentPage < viewModel.totalPageSize) {
                         viewModel.getOnLineRecipeList(
                             categoryCode, viewModel.currCategoryName, isLoadMore = true
                         )
                     } else {
-                        finish(hasMore = false)
+                        finishLoadMore()
                     }
                 } else {
-                    if (!viewModel.loadMoreLocalRecipes(viewModel.currCategoryName)) {
-                        finish(hasMore = false)
+                    if (!viewModel.loadMoreLocalRecipes()) {
+                        finishLoadMore()
                     }
                 }
             }
@@ -357,22 +355,26 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
             //获取对应分类菜谱回调
             localRecipesLiveData.observe(viewLifecycleOwner) {
                 binding.page.apply {
-                    finishRefresh()
+                    finishLoadMore()
                 }
                 binding.rv.apply {
                     models = it
-                    scrollToPosition(0)
+                    if (isRefresh) {
+                        scrollToPosition(0)
+                    }
                 }
             }
 
             //获取线上菜谱列表回调
             onLineRecipesLiveData.observe(viewLifecycleOwner) {
                 binding.page.apply {
-                    finishRefresh()
+                    finishLoadMore()
                 }
                 binding.rv.apply {
                     models = it
-                    scrollToPosition(0)
+                    if (isRefresh) {
+                        scrollToPosition(0)
+                    }
                 }
             }
 
@@ -449,6 +451,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
     }
 
     private fun refreshOnLineOrLocalRecipes() {
+        isRefresh = true
         if (viewModel.recipesType == RecipesType.ONLINE) {
             viewModel.apply {
                 getOnLineCategoryList()

+ 38 - 53
BusinessMain/src/main/java/com/develop/main/viewmodel/HomeViewModel.kt

@@ -9,6 +9,7 @@ import com.develop.common.data_repo.FoodDataProvider
 import com.develop.common.data_repo.db.*
 import com.develop.common.data_repo.db.entity.DevRecipe
 import com.develop.common.data_repo.db.entity.DevRecipeCategory
+import com.develop.common.data_repo.db.entity.UserFavoriteRecipes
 import com.develop.common.data_repo.net.Api
 import com.develop.common.data_repo.net.model.response.*
 import com.develop.common.tag.CURRENT_LANGUAGE
@@ -48,6 +49,8 @@ class HomeViewModel : BaseViewModel() {
     private var endSize = 10
     private var lastLocalRecipesShowList = mutableListOf<Any>()
     private var hasMoreLocal = false
+    private var lastLocalRecipeList = mutableListOf<DevRecipe>()
+    private var localFavoriteRecipesList = mutableListOf<UserFavoriteRecipes>()
 
     /**查询本地菜谱分类*/
     fun queryRecipesCategory() {
@@ -61,73 +64,55 @@ class HomeViewModel : BaseViewModel() {
     /**查询本地对应菜谱分类下的菜谱列表信息*/
     fun queryLocalRecipes(categoryNum: String) {
         scope(Dispatchers.IO) {
-            if (categoryNum.isEmpty()) {
-                //获取所有的分类
-                val favoriteRecipesList =
-                    FoodDataProvider.getUserDatabase().userInfoDao().queryFavoriteRecipes(userId)
-                        .toMutableList()
-                totalLocalRecipes =
-                    FoodDataProvider.getDatabase().recipeDao().queryAllRecipe().toMutableList()
-                totalLocalSize = totalLocalRecipes.size
-                val localRecipeList = if (totalLocalSize < 10) {
-                    hasMoreLocal = false
-                    totalLocalRecipes
-                } else {
-                    hasMoreLocal = true
-                    totalLocalRecipes.subList(0, 10)
-                }
-                val starList = mutableListOf<Int>()
-                localRecipeList.forEach {
-                    val userTag = FoodDataProvider.getUserDatabase().userInfoDao().queryUserTag(
-                        CURRENT_USER_ID, it.number ?: ""
-                    )
-                    starList.add(userTag?.starCount ?: 0)
-                }
-                lastLocalRecipesShowList = DataFactory.genLocalRecipes(
-                    currCategoryName, localRecipeList, favoriteRecipesList, sortedType
-                )
-                localRecipesLiveData.postValue(
-                    lastLocalRecipesShowList
-                )
-            } else {
-                //获取当前分类
-                val favoriteRecipesList =
-                    FoodDataProvider.getUserDatabase().userInfoDao().queryFavoriteRecipes(userId)
-                        .toMutableList()
-                val localRecipeList =
+            startSize = 0
+            endSize = 10
+            localFavoriteRecipesList =
+                FoodDataProvider.getUserDatabase().userInfoDao().queryFavoriteRecipes(userId)
+                    .toMutableList()
+            //获取所有的分类
+            totalLocalRecipes =
+                if (categoryNum.isEmpty()) FoodDataProvider.getDatabase().recipeDao()
+                    .queryAllRecipe().toMutableList() else {
+                    //获取当前分类
                     FoodDataProvider.getDatabase().recipeDao().queryRecipesByCategory(categoryNum)
                         .toMutableList()
-                val starList = mutableListOf<Int>()
-                localRecipeList.forEach {
-                    val userTag = FoodDataProvider.getUserDatabase().userInfoDao().queryUserTag(
-                        CURRENT_USER_ID, it.number ?: ""
-                    )
-                    starList.add(userTag?.starCount ?: 0)
                 }
-                localRecipesLiveData.postValue(
-                    DataFactory.genLocalRecipes(
-                        currCategoryName, localRecipeList, favoriteRecipesList, sortedType
-                    )
-                )
+            totalLocalSize = totalLocalRecipes.size
+            val localRecipeList = if (totalLocalSize < 10) {
+                hasMoreLocal = false
+                totalLocalRecipes
+            } else {
+                hasMoreLocal = true
+                totalLocalRecipes.subList(0, 10)
             }
+            lastLocalRecipeList.clear()
+            lastLocalRecipeList.addAll(localRecipeList)
+            lastLocalRecipesShowList = DataFactory.genLocalRecipes(
+                currCategoryName, localRecipeList, localFavoriteRecipesList, sortedType
+            )
+            localRecipesLiveData.postValue(
+                lastLocalRecipesShowList
+            )
         }
-
     }
 
     /**
      * 获取更多本地数据,没有返回false
      */
-    fun loadMoreLocalRecipes(categoryNum: String): Boolean {
+    fun loadMoreLocalRecipes(): Boolean {
         if (hasMoreLocal) {
             startSize = endSize
             endSize = startSize + 10
-            if (endSize < totalLocalRecipes.size) {
-                val moreData = totalLocalRecipes.subList(startSize, endSize)
-                lastLocalRecipesShowList.addAll(moreData)
-                localRecipesLiveData.postValue(lastLocalRecipesShowList)
-            } else {
-                return false
+            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)
         } else {
             return false
         }