Browse Source

提交人:jtm
提交内容:菜品加载优化

江天明 1 year ago
parent
commit
d04dd0258d

+ 19 - 9
BusinessCommon/src/main/java/com/develop/common/widget/FoodContentView.kt

@@ -3,6 +3,7 @@ package com.develop.common.widget
 import android.content.Context
 import android.graphics.drawable.Drawable
 import android.util.AttributeSet
+import android.util.Log
 import android.view.View
 import android.widget.RelativeLayout
 import com.blankj.utilcode.util.ThreadUtils.runOnUiThread
@@ -30,6 +31,7 @@ import com.develop.common.tag.*
 import com.drake.net.utils.scope
 import com.drake.net.utils.withMain
 import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
 import org.greenrobot.eventbus.EventBus
 
@@ -150,6 +152,7 @@ class FoodContentView : RelativeLayout {
      * @param isFoodList 是否为FoodListActivity
      */
     fun setFoodContent(content: FoodContentModel, isFoodList: Boolean = false) {
+        Log.d("------", "---333---")
         mFoodContent = content
         mIsFoodList = isFoodList
         val isNetRecipes = content.isNetRecipes
@@ -168,22 +171,16 @@ class FoodContentView : RelativeLayout {
                 FoodDataProvider.getResourcePath(content.cover.toString())
             }
         }
+        Log.d("------", "---444---")
         binding.apply {
             if ("" != cover) ivCover.load(cover, binding.ivCover.width, binding.ivCover.height)
             tvFoodName.updateText(content.foodName)
-            scope(Dispatchers.IO) {
-                val result = FoodDataProvider.getUserDatabase().userInfoDao()
-                    .queryUserTag(CURRENT_USER_ID, content.foodId)
-                withMain {
-                    result?.starCount?.apply {
-                        starView.setStarCount(this)
-                    }
-                }
-            }
+            Log.d("------", "---666---")
             starView.apply {
                 setCommentNumber(content.useNum.toString())
                 setCommentTextColorGray()
             }
+            Log.d("------", "---777---")
             tvTimeAndLevel.updateText("${content.time}・${content.level}")
             if (isNetRecipes) {
                 ivNet.visibility = View.VISIBLE
@@ -201,6 +198,19 @@ class FoodContentView : RelativeLayout {
             }
 
         }
+
+        com.develop.base.scop.safeGlobalScope.launch(Dispatchers.IO){
+            val result = FoodDataProvider.getUserDatabase().userInfoDao()
+                .queryUserTag(CURRENT_USER_ID, content.foodId)
+            withMain {
+                result?.starCount?.apply {
+                    Log.d("------", "---888---")
+                    binding.starView.setStarCount(this)
+                }
+            }
+        }
+
+        Log.d("------", "---999---")
     }
 
     companion object {

+ 32 - 35
BusinessMain/src/main/java/com/develop/main/ui/RecipesFragment.kt

@@ -3,6 +3,7 @@ package com.develop.main.ui
 
 import android.graphics.Color
 import android.os.Bundle
+import android.util.Log
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
@@ -161,36 +162,6 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
                 addType<CommonSearchModel>(HOME_SEARCH_PROVIDER)
                 addType<CategoryModel>(HOME_CATEGORY_PROVIDER)
                 addType<FoodListModel>(HOME_FOOD_LIST_PROVIDER)
-                onCreate {
-                    if (it == HOME_FOOD_LIST_PROVIDER) {
-                        foodRecycle = findView(R.id.food_recycle)
-                        (foodRecycle?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations =
-                            false
-                        foodRecycle?.setHasFixedSize(true)
-                        foodRecycle?.isNestedScrollingEnabled = false
-                        foodRecycle?.setRecycledViewPool(recycledViewPool)
-                        foodRecycle?.grid(4)
-                        foodRecycle?.setup {
-                            if (isNightTheme()) {
-                                addType<FoodContentModel>(com.develop.common.R.layout.food_content_night_provider)
-                                onBind {
-                                    findView<FoodContentView>(com.develop.common.R.id.food_content_view).setFoodContent(
-                                        getModel()
-                                    )
-                                }
-                            } else {
-                                addType<FoodContentModel>(com.develop.common.R.layout.food_content_provider)
-                                onBind {
-                                    findView<FoodContentView>(com.develop.common.R.id.food_content_view).setFoodContent(
-                                        getModel()
-                                    )
-                                }
-                            }
-
-
-                        }
-                    }
-                }
                 onBind {
                     when (itemViewType) {
                         HOME_TITLE_PROVIDER -> {
@@ -202,8 +173,33 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
                         }
 
                         HOME_FOOD_LIST_PROVIDER -> {
-                            val foodListModel = getModel<FoodListModel>()
-                            foodRecycle?.models = foodListModel.foodList
+                            val models = getModel<FoodListModel>()
+                            foodRecycle = findView(R.id.food_recycle)
+                            (foodRecycle?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations =
+                                false
+                            foodRecycle?.setHasFixedSize(true)
+                            foodRecycle?.isNestedScrollingEnabled = false
+                            foodRecycle?.setRecycledViewPool(recycledViewPool)
+                            foodRecycle?.grid(4)
+                            foodRecycle?.setup {
+                                if (isNightTheme()) {
+                                    addType<FoodContentModel>(com.develop.common.R.layout.food_content_night_provider)
+                                    onBind {
+                                        findView<FoodContentView>(com.develop.common.R.id.food_content_view).setFoodContent(
+                                            getModel()
+                                        )
+                                    }
+                                } else {
+                                    addType<FoodContentModel>(com.develop.common.R.layout.food_content_provider)
+                                    onBind {
+                                        findView<FoodContentView>(com.develop.common.R.id.food_content_view).setFoodContent(
+                                            getModel()
+                                        )
+                                    }
+                                }
+
+
+                            }?.models = models.foodList
                         }
 
                         HOME_FUNCTION_PROVIDER -> {
@@ -485,13 +481,14 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
             }
 
             moreLocalRecipeLiveData.observe(viewLifecycleOwner) {
+                Log.d("------", "---222---")
                 binding.page.finishLoadMore(0)
-                foodRecycle?.addModels(it, false)
+                foodRecycle?.bindingAdapter?.addModels(it, true)
             }
 
             moreSearchLocalRecipeLiveData.observe(viewLifecycleOwner) {
                 binding.page.finishLoadMore(0)
-                foodRecycle?.addModels(it, false)
+                foodRecycle?.bindingAdapter?.addModels(it, true)
             }
 
             //获取线上分类回调
@@ -525,7 +522,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
                 }
                 loadingDialog.removeSelf()
             }
-            errorCallback.observe(viewLifecycleOwner){
+            errorCallback.observe(viewLifecycleOwner) {
                 GlobalToast.showToast(getString(com.develop.common.R.string.network_error))
                 loadingDialog.removeSelf()
             }

+ 6 - 4
BusinessMain/src/main/java/com/develop/main/viewmodel/HomeViewModel.kt

@@ -182,10 +182,11 @@ class HomeViewModel : BaseViewModel() {
      */
     fun loadMoreLocalRecipes(): Boolean {
         if (hasMoreLocal) {
+            Log.d("------","------")
             lastLocalRecipeList.clear()
             scope(Dispatchers.IO) {
                 startSize = endSize
-                endSize = startSize + 12
+                endSize = startSize + 8
                 if (endSize > totalLocalSize) {
                     endSize = totalLocalSize
                     hasMoreLocal = false
@@ -215,6 +216,7 @@ class HomeViewModel : BaseViewModel() {
                         )
                     )
                 }
+                Log.d("------","---111---")
                 moreLocalRecipeLiveData.postValue(foodList)
             }
         } else {
@@ -343,12 +345,12 @@ class HomeViewModel : BaseViewModel() {
     fun loadMoreForSearch(): Boolean {
         if (hasMoreTotalSearchLocal) {
             startSearchSize = endSearchSize
-            endSearchSize = startSearchSize + 12
+            endSearchSize = startSearchSize + 8
             startRecipesSearchSize = endRecipesSearchSize
-            endRecipesSearchSize = startRecipesSearchSize + 12
+            endRecipesSearchSize = startRecipesSearchSize + 8
 
             startFoodSearchSize = endFoodSearchSize
-            endFoodSearchSize = startFoodSearchSize + 12
+            endFoodSearchSize = startFoodSearchSize + 8
 
             if (endSearchSize > totalSearchLocalSize) {
                 endSearchSize = totalSearchLocalSize

+ 1 - 3
libBase/src/main/java/com/develop/base/ext/GlobaExt.kt

@@ -1,15 +1,12 @@
 package com.develop.base.ext
 
 import android.Manifest
-import android.annotation.SuppressLint
 import android.app.Application
 import android.content.Context
 import android.content.pm.PackageManager
 import android.content.res.Resources
 import android.graphics.Bitmap
 import android.graphics.BitmapFactory
-import android.graphics.Color
-import android.graphics.drawable.ColorDrawable
 import android.graphics.drawable.Drawable
 import android.net.wifi.WifiInfo
 import android.net.wifi.WifiManager
@@ -23,6 +20,7 @@ import androidx.core.content.ContextCompat
 import com.bumptech.glide.Glide
 import com.bumptech.glide.load.engine.DiskCacheStrategy
 import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
+import com.bumptech.glide.request.target.BitmapImageViewTarget
 import com.develop.base.app.BaseApp
 import com.develop.base.util.MMkvUtils
 import kotlinx.serialization.decodeFromString