|
@@ -38,19 +38,46 @@ class HomeViewModel : BaseViewModel() {
|
|
|
var currentPage = 1
|
|
|
var currentPageSize = 12
|
|
|
var totalPageSize = 1
|
|
|
- var sortedType = SortedType.Popular
|
|
|
+ var sortedType = SortedType.A2Z
|
|
|
var homeOrBackLiveData = MutableLiveData<HomeOrBack>()
|
|
|
var isSearch = false
|
|
|
private val userId = CURRENT_USER_ID
|
|
|
|
|
|
private var totalLocalRecipes = mutableListOf<DevRecipe>()
|
|
|
+ private var totalSearchLocalRecipes = mutableListOf<DevRecipe>()
|
|
|
+ private var searchLocalRecipes = mutableListOf<DevRecipe>()
|
|
|
+ private var searchLocalFood = mutableListOf<DevRecipe>()
|
|
|
+
|
|
|
private var totalLocalSize = 0
|
|
|
private var startSize = 0
|
|
|
- private var endSize = 12
|
|
|
+ private var endSize = 11
|
|
|
+
|
|
|
+ private var totalSearchLocalSize = 0
|
|
|
+ private var searchLocalSize = 0
|
|
|
+ private var foodSize = 0
|
|
|
+ private var startSearchSize = 0
|
|
|
+ private var endSearchSize = 11
|
|
|
+
|
|
|
+ private var startRecipesSearchSize = 0
|
|
|
+ private var endRecipesSearchSize = 11
|
|
|
+
|
|
|
+ private var startFoodSearchSize = 0
|
|
|
+ private var endFoodSearchSize = 11
|
|
|
+
|
|
|
private var lastLocalRecipesShowList = mutableListOf<Any>()
|
|
|
private var hasMoreLocal = false
|
|
|
+ private var hasMoreTotalSearchLocal = false
|
|
|
+ private var hasMoreSearchLocal = false
|
|
|
+ private var hasMoreFoodLocal = false
|
|
|
private var lastLocalRecipeList = mutableListOf<DevRecipe>()
|
|
|
+ private var lastTotalLocalSearchRecipeList = mutableListOf<DevRecipe>()
|
|
|
+ private var lastLocalSearchRecipeList = mutableListOf<DevRecipe>()
|
|
|
+ private var lastLocalSearchFoodList = mutableListOf<DevRecipe>()
|
|
|
private var localFavoriteRecipesList = mutableListOf<UserFavoriteRecipes>()
|
|
|
+ private var lastSearchCategoryType = CategoryType.All
|
|
|
+
|
|
|
+ private var recipeCount = 0
|
|
|
+ private var foodCount = 0
|
|
|
|
|
|
/**查询本地菜谱分类*/
|
|
|
fun queryRecipesCategory() {
|
|
@@ -65,7 +92,7 @@ class HomeViewModel : BaseViewModel() {
|
|
|
fun queryLocalRecipes(categoryNum: String) {
|
|
|
scope(Dispatchers.IO) {
|
|
|
startSize = 0
|
|
|
- endSize = 12
|
|
|
+ endSize = 11
|
|
|
localFavoriteRecipesList =
|
|
|
FoodDataProvider.getUserDatabase().userInfoDao().queryFavoriteRecipes(userId)
|
|
|
.toMutableList()
|
|
@@ -77,6 +104,54 @@ class HomeViewModel : BaseViewModel() {
|
|
|
FoodDataProvider.getDatabase().recipeDao().queryRecipesByCategory(categoryNum)
|
|
|
.toMutableList()
|
|
|
}
|
|
|
+ totalLocalRecipes.sortWith(Comparator { t, t2 ->
|
|
|
+ when (sortedType) {
|
|
|
+ SortedType.Popular -> {
|
|
|
+ if ((t.useNum ?: 0) > (t2.useNum ?: 0)) {
|
|
|
+ return@Comparator -1
|
|
|
+ } else if ((t.useNum ?: 0) == (t2.useNum ?: 0)) {
|
|
|
+ return@Comparator 0
|
|
|
+ } else {
|
|
|
+ return@Comparator 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ SortedType.Newest -> {
|
|
|
+ if ((t.updateTime ?: 0L) > (t2.updateTime ?: 0L)) {
|
|
|
+ return@Comparator 1
|
|
|
+ } else if ((t.updateTime ?: 0L) == (t2.updateTime
|
|
|
+ ?: 0L)
|
|
|
+ ) return@Comparator 0
|
|
|
+ else {
|
|
|
+ return@Comparator -1
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ SortedType.Score -> {
|
|
|
+ val score1 = t.score?.toInt() ?: 0
|
|
|
+ val score2 = t2.score?.toInt() ?: 0
|
|
|
+ if (score1 > score2) {
|
|
|
+ return@Comparator 1
|
|
|
+ } else if (score1 == score2) {
|
|
|
+ return@Comparator 0
|
|
|
+ } else {
|
|
|
+ return@Comparator -1
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ SortedType.A2Z -> {
|
|
|
+ val score1 = t.name?.compareTo(t2.name ?: "") ?: 0
|
|
|
+ if (score1 > 0) {
|
|
|
+ return@Comparator 1
|
|
|
+ } else if (score1 == 0) {
|
|
|
+ return@Comparator 0
|
|
|
+ } else {
|
|
|
+ return@Comparator -1
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
totalLocalSize = totalLocalRecipes.size
|
|
|
val localRecipeList = if (totalLocalSize < 12) {
|
|
|
hasMoreLocal = false
|
|
@@ -124,11 +199,20 @@ class HomeViewModel : BaseViewModel() {
|
|
|
fun queryRecipesByHotWord(hotWord: String, categoryType: CategoryType) {
|
|
|
val result = mutableListOf<Any>()
|
|
|
result.add(FilterSortModel(""))
|
|
|
- FoodDataProvider.getDatabase().runInTransaction {
|
|
|
+ startSearchSize = 0
|
|
|
+ endSearchSize = 11
|
|
|
+ startFoodSearchSize = 0
|
|
|
+ endFoodSearchSize = 11
|
|
|
+ startRecipesSearchSize = 0
|
|
|
+ endRecipesSearchSize = 11
|
|
|
+ totalSearchLocalRecipes.clear()
|
|
|
+ searchLocalRecipes.clear()
|
|
|
+ searchLocalFood.clear()
|
|
|
+ lastSearchCategoryType = categoryType
|
|
|
+ scope(Dispatchers.IO) {
|
|
|
val recipeDao = FoodDataProvider.getDatabase().recipeDao()
|
|
|
val recipesList = recipeDao.queryRecipeByName(hotWord)
|
|
|
val foodList = mutableListOf<DevRecipe>()
|
|
|
- val allList = mutableListOf<DevRecipe>()
|
|
|
recipeDao.queryFoodByFoodName(hotWord).forEach {
|
|
|
it.recipeNumber?.apply {
|
|
|
recipeDao.queryRecipe(this)?.let { devRecipe ->
|
|
@@ -136,10 +220,41 @@ class HomeViewModel : BaseViewModel() {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- val recipeCount = recipesList.size
|
|
|
- val foodCount = foodList.size
|
|
|
- allList.addAll(recipesList)
|
|
|
- allList.addAll(foodList)
|
|
|
+ recipeCount = recipesList.size
|
|
|
+ foodCount = foodList.size
|
|
|
+ totalSearchLocalRecipes.addAll(recipesList)
|
|
|
+ totalSearchLocalRecipes.addAll(foodList)
|
|
|
+ totalSearchLocalSize = totalSearchLocalRecipes.size
|
|
|
+ searchLocalSize = recipeCount
|
|
|
+ foodSize = foodCount
|
|
|
+ searchLocalFood.addAll(foodList)
|
|
|
+
|
|
|
+ searchLocalRecipes.addAll(recipesList)
|
|
|
+ val allList: MutableList<DevRecipe> = if (totalSearchLocalSize < 12) {
|
|
|
+ hasMoreTotalSearchLocal = false
|
|
|
+ totalSearchLocalRecipes
|
|
|
+ } else {
|
|
|
+ hasMoreTotalSearchLocal = true
|
|
|
+ totalSearchLocalRecipes.subList(0, 12)
|
|
|
+ }
|
|
|
+ val localRecipesList = if (recipeCount < 12) {
|
|
|
+ hasMoreSearchLocal = false
|
|
|
+ searchLocalRecipes
|
|
|
+ } else {
|
|
|
+ hasMoreSearchLocal = true
|
|
|
+ searchLocalRecipes.subList(0, 12)
|
|
|
+ }
|
|
|
+
|
|
|
+ val fooShowList = if (foodSize < 12) {
|
|
|
+ hasMoreFoodLocal = false
|
|
|
+ foodList
|
|
|
+ } else {
|
|
|
+ hasMoreFoodLocal = true
|
|
|
+ foodList.subList(0, 12)
|
|
|
+ }
|
|
|
+
|
|
|
+ lastTotalLocalSearchRecipeList.clear()
|
|
|
+ lastTotalLocalSearchRecipeList.addAll(allList)
|
|
|
result.add(CategoryModel(recipeCount + foodCount, recipeCount, foodCount))
|
|
|
val foodContentList = mutableListOf<FoodContentModel>()
|
|
|
when (categoryType) {
|
|
@@ -157,8 +272,9 @@ class HomeViewModel : BaseViewModel() {
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
CategoryType.Recipes -> {
|
|
|
- recipesList.forEach {
|
|
|
+ localRecipesList.forEach {
|
|
|
foodContentList.add(
|
|
|
FoodContentModel(
|
|
|
it.photoPath ?: "",
|
|
@@ -171,8 +287,9 @@ class HomeViewModel : BaseViewModel() {
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
CategoryType.Ingredients -> {
|
|
|
- foodList.forEach {
|
|
|
+ fooShowList.forEach {
|
|
|
foodContentList.add(
|
|
|
FoodContentModel(
|
|
|
it.photoPath ?: "",
|
|
@@ -186,11 +303,106 @@ class HomeViewModel : BaseViewModel() {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- result.add(FoodListModel(foodContentList,""))
|
|
|
+ result.add(FoodListModel(foodContentList, ""))
|
|
|
hotWordSearchRecipesLiveData.postValue(result)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ fun loadMoreForSearch(): Boolean {
|
|
|
+ if (hasMoreTotalSearchLocal) {
|
|
|
+ startSearchSize = endSearchSize
|
|
|
+ endSearchSize = startSearchSize + 12
|
|
|
+
|
|
|
+ startRecipesSearchSize = endRecipesSearchSize
|
|
|
+ endRecipesSearchSize = startRecipesSearchSize + 12
|
|
|
+
|
|
|
+ startFoodSearchSize = endFoodSearchSize
|
|
|
+ endFoodSearchSize = startFoodSearchSize + 12
|
|
|
+
|
|
|
+ if (endSearchSize > totalSearchLocalSize) {
|
|
|
+ endSearchSize = totalSearchLocalSize
|
|
|
+ hasMoreTotalSearchLocal = false
|
|
|
+ }
|
|
|
+ if (endRecipesSearchSize > searchLocalSize) {
|
|
|
+ endRecipesSearchSize = searchLocalSize
|
|
|
+ hasMoreSearchLocal = false
|
|
|
+ }
|
|
|
+ if (endFoodSearchSize > foodSize) {
|
|
|
+ endFoodSearchSize = foodSize
|
|
|
+ hasMoreFoodLocal = false
|
|
|
+ }
|
|
|
+ val newTotal = totalSearchLocalRecipes.subList(
|
|
|
+ startSearchSize, endSearchSize
|
|
|
+ )
|
|
|
+ lastTotalLocalSearchRecipeList.addAll(newTotal)
|
|
|
+ if (hasMoreSearchLocal) {
|
|
|
+ val newRecipes = searchLocalRecipes.subList(
|
|
|
+ startRecipesSearchSize, endRecipesSearchSize
|
|
|
+ )
|
|
|
+ lastLocalSearchRecipeList.addAll(newRecipes)
|
|
|
+ }
|
|
|
+ if (hasMoreFoodLocal) {
|
|
|
+ val newFoods = searchLocalFood.subList(startFoodSearchSize, endFoodSearchSize)
|
|
|
+ lastLocalSearchFoodList.addAll(newFoods)
|
|
|
+ }
|
|
|
+ val result = mutableListOf<Any>()
|
|
|
+ result.add(FilterSortModel(""))
|
|
|
+ result.add(CategoryModel(recipeCount + foodCount, recipeCount, foodCount))
|
|
|
+ val foodContentList = mutableListOf<FoodContentModel>()
|
|
|
+ when (lastSearchCategoryType) {
|
|
|
+ CategoryType.All -> {
|
|
|
+ lastTotalLocalSearchRecipeList.forEach {
|
|
|
+ foodContentList.add(
|
|
|
+ FoodContentModel(
|
|
|
+ it.photoPath ?: "",
|
|
|
+ it.name ?: "",
|
|
|
+ 3,
|
|
|
+ getTime(it.makeHours ?: 0, it.makeMinutes ?: 0),
|
|
|
+ it.difficultyLevel ?: "",
|
|
|
+ foodId = it.number ?: ""
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ CategoryType.Recipes -> {
|
|
|
+ lastLocalRecipeList.forEach {
|
|
|
+ foodContentList.add(
|
|
|
+ FoodContentModel(
|
|
|
+ it.photoPath ?: "",
|
|
|
+ it.name ?: "",
|
|
|
+ 3,
|
|
|
+ getTime(it.makeHours ?: 0, it.makeMinutes ?: 0),
|
|
|
+ it.difficultyLevel ?: "",
|
|
|
+ foodId = it.number ?: ""
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ CategoryType.Ingredients -> {
|
|
|
+ lastLocalSearchFoodList.forEach {
|
|
|
+ foodContentList.add(
|
|
|
+ FoodContentModel(
|
|
|
+ it.photoPath ?: "",
|
|
|
+ it.name ?: "",
|
|
|
+ 3,
|
|
|
+ getTime(it.makeHours ?: 0, it.makeMinutes ?: 0),
|
|
|
+ it.difficultyLevel ?: "",
|
|
|
+ foodId = it.number ?: ""
|
|
|
+ )
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ result.add(FoodListModel(foodContentList, ""))
|
|
|
+ hotWordSearchRecipesLiveData.postValue(result)
|
|
|
+ } else {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
/**获取线上的菜谱分类*/
|
|
|
fun getOnLineCategoryList() {
|
|
|
scopeNetLife {
|