|
@@ -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
|
|
|
}
|