Browse Source

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

江天明 1 year ago
parent
commit
245e15e65f

+ 13 - 2
BusinessCommon/src/main/java/com/develop/common/widget/FoodContentView.kt

@@ -1,10 +1,17 @@
 package com.develop.common.widget
 
 import android.content.Context
+import android.graphics.drawable.Drawable
 import android.util.AttributeSet
 import android.view.View
 import android.widget.RelativeLayout
 import com.blankj.utilcode.util.ThreadUtils.runOnUiThread
+import com.bumptech.glide.Glide
+import com.bumptech.glide.load.DataSource
+import com.bumptech.glide.load.engine.DiskCacheStrategy
+import com.bumptech.glide.load.engine.GlideException
+import com.bumptech.glide.request.RequestListener
+import com.bumptech.glide.request.target.Target
 import com.develop.base.ext.load
 import com.develop.base.ext.navigateTo
 import com.develop.base.ext.resId2Dimension
@@ -19,6 +26,10 @@ import com.develop.common.dialog.CancelConfirmDialog
 import com.develop.common.event.RefreshDataEvent
 import com.develop.common.router.Screens
 import com.develop.common.tag.*
+import com.drake.net.utils.scope
+import com.drake.net.utils.withMain
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
 import org.greenrobot.eventbus.EventBus
 
 class FoodContentView : RelativeLayout {
@@ -90,10 +101,10 @@ class FoodContentView : RelativeLayout {
         binding.apply {
             if("" != cover) ivCover.load(cover)
             tvFoodName.updateText(content.foodName)
-            FoodDataProvider.getUserDatabase().runInTransaction {
+            scope(Dispatchers.IO) {
                 val result = FoodDataProvider.getUserDatabase().userInfoDao()
                     .queryUserTag(CURRENT_USER_ID, content.foodId)
-                runOnUiThread {
+                withMain {
                     result?.starCount?.apply {
                         starView.setStarCount(this)
                     }

+ 46 - 20
BusinessMain/src/main/java/com/develop/main/ui/RecipesFragment.kt

@@ -29,6 +29,7 @@ import com.develop.common.tag.*
 import com.develop.common.ui.CommonBVMFragment
 import com.develop.common.widget.*
 import com.develop.main.R
+import com.develop.main.databinding.FoodGridViewBinding
 import com.develop.main.databinding.ItemFilterAndSortViewBinding
 import com.develop.main.viewmodel.HomeViewModel
 import com.drake.brv.BindingAdapter
@@ -140,6 +141,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
 
         binding.rv.apply {
             setHasFixedSize(true)
+            isNestedScrollingEnabled = false
             linear()
             setup {
                 addType<String>(HOME_TITLE_PROVIDER)
@@ -148,7 +150,23 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
                 addType<CommonSearchModel>(HOME_SEARCH_PROVIDER)
                 addType<CategoryModel>(HOME_CATEGORY_PROVIDER)
                 addType<FoodListModel>(HOME_FOOD_LIST_PROVIDER)
-                addType<FoodContentModel>(com.develop.common.R.layout.food_content_provider)
+                onCreate {
+                    if (it == HOME_FOOD_LIST_PROVIDER) {
+                        foodRecycle = findView(R.id.food_recycle)
+                        foodRecycle?.setHasFixedSize(true)
+                        foodRecycle?.isNestedScrollingEnabled = false
+                        foodRecycle?.setRecycledViewPool(recycledViewPool)
+                        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()
+                                )
+                            }
+                        }
+                    }
+                }
                 onBind {
                     when (itemViewType) {
                         HOME_TITLE_PROVIDER -> {
@@ -161,24 +179,7 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
 
                         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()
-                                        )
-                                    }
-                                }
-                            }
-
-
-                            foodRecycle!!.models = foodListModel.foodList
-
-
+                            foodRecycle?.models = foodListModel.foodList
                         }
 
                         HOME_FUNCTION_PROVIDER -> {
@@ -212,10 +213,25 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
                     }
                 }
             }
+            addOnScrollListener(object : RecyclerView.OnScrollListener() {
+                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
+                    super.onScrollStateChanged(recyclerView, newState)
+                    when (newState) {
+
+                        RecyclerView.SCROLL_STATE_IDLE -> {
+                            Glide.with(hostActivity).resumeRequests()
+                        }
+
+                        RecyclerView.SCROLL_STATE_DRAGGING, RecyclerView.SCROLL_STATE_SETTLING -> {
+                            Glide.with(hostActivity).pauseRequests()
+                        }
+                    }
+                }
+            })
         }
         binding.page.apply {
             setEnableRefresh(false)
-            setEnableNestedScroll(true)
+            setEnableNestedScroll(false)
             setOnLoadMoreListener {
                 isRefresh = false
                 if (viewModel.isSearch) {
@@ -449,6 +465,16 @@ class RecipesFragment : CommonBVMFragment<FragmentCommeListBinding, HomeViewMode
                 loadingDialog.removeSelf()
             }
 
+            moreLocalRecipeLiveData.observe(viewLifecycleOwner) {
+                foodRecycle?.addModels(it)
+                binding.page.finishLoadMore(true)
+            }
+
+            moreSearchLocalRecipeLiveData.observe(viewLifecycleOwner) {
+                foodRecycle?.addModels(it)
+                binding.page.finishLoadMore(true)
+            }
+
             //获取线上分类回调
             onlineCategoryLiveData.observe(viewLifecycleOwner) {
                 categoryDataList = it

+ 32 - 8
BusinessMain/src/main/java/com/develop/main/viewmodel/HomeViewModel.kt

@@ -1,5 +1,6 @@
 package com.develop.main.viewmodel
 
+import android.util.Log
 import androidx.annotation.RestrictTo.Scope
 import androidx.lifecycle.MutableLiveData
 import androidx.lifecycle.scopeNetLife
@@ -32,6 +33,8 @@ class HomeViewModel : BaseViewModel() {
     val onLineHotTagsLiveData = MutableLiveData<MutableList<String>>()
     var noticesLiveData = MutableLiveData<MutableList<Inform>>()
     var tempOnLineRecipes = mutableListOf<DevRecipe>()
+    var moreLocalRecipeLiveData = MutableLiveData<MutableList<FoodContentModel>>()
+    var moreSearchLocalRecipeLiveData = MutableLiveData<MutableList<FoodContentModel>>()
 
     var currCategoryName = ""
     var recipesType = RecipesType.LOCAL
@@ -76,7 +79,6 @@ 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
@@ -179,6 +181,7 @@ class HomeViewModel : BaseViewModel() {
      */
     fun loadMoreLocalRecipes(): Boolean {
         if (hasMoreLocal) {
+            lastLocalRecipeList.clear()
             scope(Dispatchers.IO) {
                 startSize = endSize
                 endSize = startSize + 12
@@ -188,10 +191,30 @@ class HomeViewModel : BaseViewModel() {
                 }
                 val moreData = totalLocalRecipes.subList(startSize, endSize)
                 lastLocalRecipeList.addAll(moreData)
-                lastLocalRecipesShowList = DataFactory.genLocalRecipes(
-                    currCategoryName, lastLocalRecipeList, localFavoriteRecipesList, sortedType
-                )
-                localRecipesLiveData.postValue(lastLocalRecipesShowList)
+                val favoriteRecipesIds = mutableListOf<String>()
+                localFavoriteRecipesList.forEach {
+                    favoriteRecipesIds.add(it.favoriteRecipesId)
+                }
+                val foodList = mutableListOf<FoodContentModel>()
+                for (i in lastLocalRecipeList.indices) {
+                    val recipes = lastLocalRecipeList[i]
+                    foodList.add(
+                        FoodContentModel(
+                            recipes.photoPath ?: "",
+                            recipes.name ?: "",
+                            recipes.score?.toInt() ?: 0,
+                            getTime(recipes.makeHours ?: 0, recipes.makeMinutes ?: 0),
+                            recipes.difficultyLevel ?: "",
+                            isLike = favoriteRecipesIds.contains(recipes.number),
+                            foodId = recipes.number ?: "",
+                            isNetRecipes = false,
+                            hasDownloaded = false,
+                            recipesEdition = recipes.edition,
+                            useNum = recipes.useNum?.toInt() ?: 0
+                        )
+                    )
+                }
+                moreLocalRecipeLiveData.postValue(foodList)
             }
         } else {
             return false
@@ -317,7 +340,6 @@ class HomeViewModel : BaseViewModel() {
         if (hasMoreTotalSearchLocal) {
             startSearchSize = endSearchSize
             endSearchSize = startSearchSize + 12
-
             startRecipesSearchSize = endRecipesSearchSize
             endRecipesSearchSize = startRecipesSearchSize + 12
 
@@ -339,6 +361,9 @@ class HomeViewModel : BaseViewModel() {
             val newTotal = totalSearchLocalRecipes.subList(
                 startSearchSize, endSearchSize
             )
+            lastTotalLocalSearchRecipeList.clear()
+            lastLocalSearchRecipeList.clear()
+            lastLocalSearchFoodList.clear()
             lastTotalLocalSearchRecipeList.addAll(newTotal)
             if (hasMoreSearchLocal) {
                 val newRecipes = searchLocalRecipes.subList(
@@ -400,8 +425,7 @@ class HomeViewModel : BaseViewModel() {
                     }
                 }
             }
-            result.add(FoodListModel(foodContentList, ""))
-            hotWordSearchRecipesLiveData.postValue(result)
+            moreSearchLocalRecipeLiveData.postValue(foodContentList)
         } else {
             return false
         }

+ 2 - 3
BusinessStep/src/main/res/layout/activity_cook_step_base.xml

@@ -134,7 +134,7 @@
             android:layout_marginBottom="@dimen/convert_42px"
             android:includeFontPadding="false"
             android:layout_gravity="center_horizontal"
-            app:layout_constraintStart_toStartOf="parent"
+            android:layout_marginEnd="@dimen/convert_310px"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintBottom_toTopOf="@+id/weight_view"/>
 
@@ -143,7 +143,6 @@
             android:layout_width="@dimen/convert_740px"
             android:layout_height="wrap_content"
             app:layout_constraintBottom_toBottomOf="parent"
-            app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintEnd_toEndOf="parent"/>
 
         <TextView
@@ -153,11 +152,11 @@
             android:textColor="#ffffff"
             android:textSize="@dimen/convert_36px"
             android:text="@string/tare"
+            android:layout_marginEnd="@dimen/convert_270px"
             android:gravity="center"
             android:includeFontPadding="false"
             android:layout_marginBottom="@dimen/convert_58px"
             android:background="@drawable/bg_tare_button"
-            app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintBottom_toBottomOf="parent"/>
 

+ 2 - 0
libBase/src/main/java/com/develop/base/ext/GlobaExt.kt

@@ -16,6 +16,7 @@ import android.widget.ImageView
 import androidx.annotation.DimenRes
 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.develop.base.app.BaseApp
 import kotlinx.serialization.decodeFromString
@@ -184,6 +185,7 @@ fun getSN(): String {
 
 fun ImageView.load(data: Any?) {
     Glide.with(this.context).load(data)
+        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
         .into(this)
 }
 

+ 3 - 1
libBase/src/main/java/com/develop/base/util/GlobalToast.kt

@@ -8,7 +8,9 @@ object GlobalToast {
 
     fun showToast(msg: String, duration: Int = Toast.LENGTH_SHORT) {
         ThreadUtils.runOnMainThread {
-            Toast.makeText(globalApp(), msg, duration).show()
+            if (!msg.contains("错误")) {
+                Toast.makeText(globalApp(), msg, duration).show()
+            }
         }
     }