|
@@ -1,5 +1,6 @@
|
|
|
package com.develop.step.viewmodel
|
|
|
|
|
|
+import android.text.TextUtils
|
|
|
import android.util.Log
|
|
|
import androidx.lifecycle.LiveData
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
@@ -8,9 +9,7 @@ import com.blankj.utilcode.util.FileUtils
|
|
|
import com.blankj.utilcode.util.GsonUtils
|
|
|
import com.blankj.utilcode.util.ToastUtils
|
|
|
import com.blankj.utilcode.util.ZipUtils
|
|
|
-import com.develop.base.ext.fromJson
|
|
|
import com.develop.base.ext.globalApp
|
|
|
-import com.develop.base.ext.toJson
|
|
|
import com.develop.base.mvvm.BaseViewModel
|
|
|
import com.develop.base.util.FileKit
|
|
|
import com.develop.base.util.MMkvUtils
|
|
@@ -19,24 +18,21 @@ import com.develop.common.bean.TranslateBean
|
|
|
import com.develop.common.data_repo.FoodDataProvider
|
|
|
import com.develop.common.data_repo.db.entity.DevAccessory
|
|
|
import com.develop.common.data_repo.db.entity.DevRecipeCategory
|
|
|
+import com.develop.common.data_repo.db.entity.DevRecipeCookingStep
|
|
|
import com.develop.common.data_repo.db.entity.DevRecipePortionSize
|
|
|
import com.develop.common.data_repo.db.entity.UserOnLineRecipes
|
|
|
import com.develop.common.data_repo.net.Api
|
|
|
-import com.develop.common.data_repo.net.converter.SerializationConverter
|
|
|
import com.develop.common.data_repo.net.model.response.RecipeDataConfig
|
|
|
import com.develop.common.data_repo.net.model.response.RecipeDetailResult
|
|
|
import com.develop.common.tag.CURRENT_LANGUAGE
|
|
|
import com.develop.common.tag.CURRENT_USER_ID
|
|
|
import com.develop.common.utils.Resource
|
|
|
import com.develop.step.ui.recipes_detail.model.CookDetailInfo
|
|
|
-import com.develop.step.ui.recipes_detail.model.CookDetailInfoBean
|
|
|
+import com.develop.step.ui.recipes_detail.model.CookDetailTranslateBean
|
|
|
import com.drake.net.Get
|
|
|
import com.drake.net.Post
|
|
|
import com.drake.net.component.Progress
|
|
|
import com.drake.net.interfaces.ProgressListener
|
|
|
-import kotlinx.serialization.decodeFromString
|
|
|
-import kotlinx.serialization.json.Json
|
|
|
-import org.json.JSONArray
|
|
|
import org.json.JSONObject
|
|
|
import java.io.File
|
|
|
|
|
@@ -50,8 +46,13 @@ class CookDetailViewModel : BaseViewModel() {
|
|
|
private var mRecipeUrl: String? = null
|
|
|
var cookDetailInfo :CookDetailInfo? = null
|
|
|
|
|
|
-// //原始数据,用于做翻译功能
|
|
|
-// var cookDetailInfoInit :CookDetailInfoBean? = null
|
|
|
+ //原始数据,用于做翻译功能
|
|
|
+ var translateInfoInit : CookDetailTranslateBean? = null
|
|
|
+
|
|
|
+
|
|
|
+ var cookingStep = mutableListOf<DevRecipeCookingStep>()
|
|
|
+ var translateCookingStep = mutableListOf<DevRecipeCookingStep>()
|
|
|
+
|
|
|
//是否线上菜谱
|
|
|
var isRemote = false
|
|
|
|
|
@@ -85,13 +86,9 @@ class CookDetailViewModel : BaseViewModel() {
|
|
|
devRecipeNutritions,
|
|
|
devRecipePortionSizes
|
|
|
)
|
|
|
-// cookDetailInfoInit = CookDetailInfoBean(
|
|
|
-// devRecipe,
|
|
|
-// devAccessorys,
|
|
|
-// devRecipeFoods,
|
|
|
-// devRecipeNutritions,
|
|
|
-// devRecipePortionSizes
|
|
|
-// )
|
|
|
+ //由于对象赋值问题,对象内存地址共用,估现在先转json,在重新赋值给初始实体
|
|
|
+ var json = GsonUtils.toJson(cookDetailInfo)
|
|
|
+ translateInfoInit = GsonUtils.fromJson(json,CookDetailTranslateBean::class.java)
|
|
|
|
|
|
portionSizeLiveData.postValue(cookDetailInfo?.portionSize?.firstOrNull())
|
|
|
recipeLiveData.postValue(cookDetailInfo)
|
|
@@ -105,24 +102,24 @@ class CookDetailViewModel : BaseViewModel() {
|
|
|
}
|
|
|
} else {
|
|
|
FoodDataProvider.getDatabase().runInTransaction {
|
|
|
- val recipeDao = FoodDataProvider.getDatabase().recipeDao()
|
|
|
- val recipeBean = recipeDao.queryRecipe(number)
|
|
|
+ var recipeDao = FoodDataProvider.getDatabase().recipeDao()
|
|
|
+ var recipeBean = recipeDao.queryRecipe(number)
|
|
|
if (recipeBean == null) {
|
|
|
// show toast
|
|
|
errorLiveData.postValue(ERR_NO_RECIPE_MATCH)
|
|
|
return@runInTransaction
|
|
|
}
|
|
|
- val includeAccessory = mutableListOf<DevAccessory>()
|
|
|
- val accessoryIds = recipeDao.queryAccessoryIds(number)
|
|
|
+ var includeAccessory = mutableListOf<DevAccessory>()
|
|
|
+ var accessoryIds = recipeDao.queryAccessoryIds(number)
|
|
|
for (accessoryId in accessoryIds) {
|
|
|
val accessory = recipeDao.queryAccessory(accessoryId.accessoryNumber ?: "")
|
|
|
if (accessory != null) {
|
|
|
includeAccessory.add(accessory)
|
|
|
}
|
|
|
}
|
|
|
- val includeMaterial = recipeDao.queryFood(number)
|
|
|
- val includeNutrition = recipeDao.queryNutrition(number)
|
|
|
- val includePortionSize = recipeDao.queryPortionSize(number)
|
|
|
+ var includeMaterial = recipeDao.queryFood(number)
|
|
|
+ var includeNutrition = recipeDao.queryNutrition(number)
|
|
|
+ var includePortionSize = recipeDao.queryPortionSize(number)
|
|
|
cookDetailInfo = CookDetailInfo(
|
|
|
recipeBean,
|
|
|
includeAccessory,
|
|
@@ -130,13 +127,10 @@ class CookDetailViewModel : BaseViewModel() {
|
|
|
includeNutrition,
|
|
|
includePortionSize
|
|
|
)
|
|
|
-// cookDetailInfoInit = CookDetailInfoBean(
|
|
|
-// recipeBean,
|
|
|
-// includeAccessory,
|
|
|
-// includeMaterial,
|
|
|
-// includeNutrition,
|
|
|
-// includePortionSize
|
|
|
-// )
|
|
|
+ //由于对象赋值问题,对象内存地址共用,估现在先转json,在重新赋值给初始实体
|
|
|
+ var json = GsonUtils.toJson(cookDetailInfo)
|
|
|
+ translateInfoInit = GsonUtils.fromJson(json,CookDetailTranslateBean::class.java)
|
|
|
+
|
|
|
portionSizeLiveData.postValue(cookDetailInfo?.portionSize?.firstOrNull())
|
|
|
recipeLiveData.postValue(cookDetailInfo)
|
|
|
}
|
|
@@ -185,6 +179,14 @@ class CookDetailViewModel : BaseViewModel() {
|
|
|
cookDetailInfo?.material?.get(index)?.foodName = value
|
|
|
}
|
|
|
|
|
|
+ if (id.contains("cookstep")){
|
|
|
+ var ids = id
|
|
|
+ var list = ids.split("_")
|
|
|
+ var index = list[1].toInt()
|
|
|
+ translateCookingStep[index].description = value
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
@@ -196,14 +198,28 @@ class CookDetailViewModel : BaseViewModel() {
|
|
|
|
|
|
|
|
|
fun setTranslateDate(value :String):String{
|
|
|
+ //需要添加步骤到翻译,由于菜谱详情没有获取,估在这重新获取
|
|
|
+ //只获取一次,避免多次获取
|
|
|
+ if (cookingStep.size<=0){
|
|
|
+ cookingStep = FoodDataProvider
|
|
|
+ .getDatabase()
|
|
|
+ .recipeDao()
|
|
|
+ .queryCookingStep(recipeNumber?:"").toMutableList()
|
|
|
+ translateCookingStep = FoodDataProvider
|
|
|
+ .getDatabase()
|
|
|
+ .recipeDao()
|
|
|
+ .queryCookingStep(recipeNumber?:"").toMutableList()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ language = MMkvUtils.getString(CURRENT_LANGUAGE) ?:"en"
|
|
|
var listLanguage = mutableListOf<String>()
|
|
|
listLanguage.add(value)
|
|
|
-
|
|
|
var translateList = mutableListOf<TranslateBean>()
|
|
|
- translateList.add(TranslateBean("name",cookDetailInfo?.recipe?.name))
|
|
|
- translateList.add(TranslateBean("introduction",cookDetailInfo?.recipe?.introduction))
|
|
|
- translateList.add(TranslateBean("difficulty",cookDetailInfo?.recipe?.difficultyLevel))
|
|
|
- var size = cookDetailInfo?.material?.size?:0
|
|
|
+ translateList.add(TranslateBean("name",translateInfoInit?.recipe?.name))
|
|
|
+ translateList.add(TranslateBean("introduction",translateInfoInit?.recipe?.introduction))
|
|
|
+ translateList.add(TranslateBean("difficulty",translateInfoInit?.recipe?.difficultyLevel))
|
|
|
+ var size = translateInfoInit?.material?.size?:0
|
|
|
|
|
|
if (size>0){
|
|
|
size--
|
|
@@ -211,12 +227,27 @@ class CookDetailViewModel : BaseViewModel() {
|
|
|
|
|
|
for (i in 0..size){
|
|
|
// DevRecipeFood
|
|
|
- var material = cookDetailInfo?.material?.get(i)
|
|
|
+ var material = translateInfoInit?.material?.get(i)
|
|
|
material?.let {
|
|
|
translateList.add(TranslateBean("foodname_"+i, it.foodName ?: ""))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ var sizeStep = cookingStep.size
|
|
|
+ if (sizeStep>0){
|
|
|
+ sizeStep--
|
|
|
+ }
|
|
|
+
|
|
|
+ for (i in 0..sizeStep){
|
|
|
+ var cookingStep = cookingStep[i]
|
|
|
+ cookingStep?.let {
|
|
|
+ if (!TextUtils.isEmpty(it.description)){
|
|
|
+ translateList.add(TranslateBean("cookstep_"+i, it.description ?: ""))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
var map = HashMap<String,Any>()
|
|
|
map["from"] = language.lowercase()
|
|
|
map["tos"] = listLanguage
|