|
@@ -1,5 +1,6 @@
|
|
package com.develop.step.viewmodel
|
|
package com.develop.step.viewmodel
|
|
|
|
|
|
|
|
+import android.util.Log
|
|
import androidx.lifecycle.LiveData
|
|
import androidx.lifecycle.LiveData
|
|
import androidx.lifecycle.MutableLiveData
|
|
import androidx.lifecycle.MutableLiveData
|
|
import androidx.lifecycle.scopeNetLife
|
|
import androidx.lifecycle.scopeNetLife
|
|
@@ -9,10 +10,12 @@ import com.blankj.utilcode.util.ToastUtils
|
|
import com.blankj.utilcode.util.ZipUtils
|
|
import com.blankj.utilcode.util.ZipUtils
|
|
import com.develop.base.ext.fromJson
|
|
import com.develop.base.ext.fromJson
|
|
import com.develop.base.ext.globalApp
|
|
import com.develop.base.ext.globalApp
|
|
|
|
+import com.develop.base.ext.toJson
|
|
import com.develop.base.mvvm.BaseViewModel
|
|
import com.develop.base.mvvm.BaseViewModel
|
|
import com.develop.base.util.FileKit
|
|
import com.develop.base.util.FileKit
|
|
import com.develop.base.util.MMkvUtils
|
|
import com.develop.base.util.MMkvUtils
|
|
import com.develop.base.util.ThreadUtils
|
|
import com.develop.base.util.ThreadUtils
|
|
|
|
+import com.develop.common.bean.TranslateBean
|
|
import com.develop.common.data_repo.FoodDataProvider
|
|
import com.develop.common.data_repo.FoodDataProvider
|
|
import com.develop.common.data_repo.db.entity.DevAccessory
|
|
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.DevRecipeCategory
|
|
@@ -27,10 +30,13 @@ import com.develop.common.tag.CURRENT_USER_ID
|
|
import com.develop.common.utils.Resource
|
|
import com.develop.common.utils.Resource
|
|
import com.develop.step.ui.recipes_detail.model.CookDetailInfo
|
|
import com.develop.step.ui.recipes_detail.model.CookDetailInfo
|
|
import com.drake.net.Get
|
|
import com.drake.net.Get
|
|
|
|
+import com.drake.net.Post
|
|
import com.drake.net.component.Progress
|
|
import com.drake.net.component.Progress
|
|
import com.drake.net.interfaces.ProgressListener
|
|
import com.drake.net.interfaces.ProgressListener
|
|
import kotlinx.serialization.decodeFromString
|
|
import kotlinx.serialization.decodeFromString
|
|
import kotlinx.serialization.json.Json
|
|
import kotlinx.serialization.json.Json
|
|
|
|
+import org.json.JSONArray
|
|
|
|
+import org.json.JSONObject
|
|
import java.io.File
|
|
import java.io.File
|
|
|
|
|
|
class CookDetailViewModel : BaseViewModel() {
|
|
class CookDetailViewModel : BaseViewModel() {
|
|
@@ -41,7 +47,10 @@ class CookDetailViewModel : BaseViewModel() {
|
|
var portionSizeLiveData = MutableLiveData<DevRecipePortionSize>()
|
|
var portionSizeLiveData = MutableLiveData<DevRecipePortionSize>()
|
|
var recipeNumber: String? = null
|
|
var recipeNumber: String? = null
|
|
private var mRecipeUrl: String? = null
|
|
private var mRecipeUrl: String? = null
|
|
|
|
+ var cookDetailInfo :CookDetailInfo? = null
|
|
|
|
|
|
|
|
+ //原始数据,用于做翻译功能
|
|
|
|
+ var cookDetailInfoInit :CookDetailInfo? = null
|
|
//是否线上菜谱
|
|
//是否线上菜谱
|
|
var isRemote = false
|
|
var isRemote = false
|
|
|
|
|
|
@@ -66,15 +75,16 @@ class CookDetailViewModel : BaseViewModel() {
|
|
addQuery("lang", MMkvUtils.getString(CURRENT_LANGUAGE) ?: "EN")
|
|
addQuery("lang", MMkvUtils.getString(CURRENT_LANGUAGE) ?: "EN")
|
|
}.await().apply {
|
|
}.await().apply {
|
|
mRecipeUrl = recipeUrl
|
|
mRecipeUrl = recipeUrl
|
|
- val detailInfo = CookDetailInfo(
|
|
|
|
|
|
+ cookDetailInfo = CookDetailInfo(
|
|
devRecipe,
|
|
devRecipe,
|
|
devAccessorys,
|
|
devAccessorys,
|
|
devRecipeFoods,
|
|
devRecipeFoods,
|
|
devRecipeNutritions,
|
|
devRecipeNutritions,
|
|
devRecipePortionSizes
|
|
devRecipePortionSizes
|
|
)
|
|
)
|
|
- portionSizeLiveData.postValue(detailInfo.portionSize.firstOrNull())
|
|
|
|
- recipeLiveData.postValue(detailInfo)
|
|
|
|
|
|
+ cookDetailInfoInit = cookDetailInfo
|
|
|
|
+ portionSizeLiveData.postValue(cookDetailInfo?.portionSize?.firstOrNull())
|
|
|
|
+ recipeLiveData.postValue(cookDetailInfo)
|
|
}
|
|
}
|
|
}.catch {
|
|
}.catch {
|
|
ToastUtils.showShort(it.message)
|
|
ToastUtils.showShort(it.message)
|
|
@@ -103,19 +113,106 @@ class CookDetailViewModel : BaseViewModel() {
|
|
val includeMaterial = recipeDao.queryFood(number)
|
|
val includeMaterial = recipeDao.queryFood(number)
|
|
val includeNutrition = recipeDao.queryNutrition(number)
|
|
val includeNutrition = recipeDao.queryNutrition(number)
|
|
val includePortionSize = recipeDao.queryPortionSize(number)
|
|
val includePortionSize = recipeDao.queryPortionSize(number)
|
|
- val detailInfo = CookDetailInfo(
|
|
|
|
|
|
+ cookDetailInfo = CookDetailInfo(
|
|
recipeBean,
|
|
recipeBean,
|
|
includeAccessory,
|
|
includeAccessory,
|
|
includeMaterial,
|
|
includeMaterial,
|
|
includeNutrition,
|
|
includeNutrition,
|
|
includePortionSize
|
|
includePortionSize
|
|
)
|
|
)
|
|
- portionSizeLiveData.postValue(detailInfo.portionSize.firstOrNull())
|
|
|
|
- recipeLiveData.postValue(detailInfo)
|
|
|
|
|
|
+ cookDetailInfoInit = cookDetailInfo
|
|
|
|
+ portionSizeLiveData.postValue(cookDetailInfo?.portionSize?.firstOrNull())
|
|
|
|
+ recipeLiveData.postValue(cookDetailInfo)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fun getTranslateDetail(jsons :String,value: String){
|
|
|
|
+ scopeNetLife {
|
|
|
|
+ Post<String>(Api.TRANSLATE) {
|
|
|
|
+ json(jsons)
|
|
|
|
+ }.await().apply {
|
|
|
|
+ //由于后端返回不规则key ,需特殊处理
|
|
|
|
+ jsonTranslate( this,value)
|
|
|
|
+ }
|
|
|
|
+ }.catch {
|
|
|
|
+ Log.e("TAG message",it.message.toString())
|
|
|
|
+ ToastUtils.showShort(it.message)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fun jsonTranslate (jsons: String,value: String){
|
|
|
|
+
|
|
|
|
+ var jsonObject = JSONObject(jsons)
|
|
|
|
+ var jsonArray = jsonObject.getJSONArray(value)
|
|
|
|
+ var size = jsonArray.length()-1
|
|
|
|
+ for (i in 0 .. size){
|
|
|
|
+ var jsonObject = jsonArray.getJSONObject(i)
|
|
|
|
+ var id = jsonObject.getString("id")
|
|
|
|
+ var valueList = jsonObject.getJSONArray("value")
|
|
|
|
+ var value = valueList.getString(0)
|
|
|
|
+ if (id=="name"){
|
|
|
|
+ cookDetailInfo?.recipe?.name = value
|
|
|
|
+ }
|
|
|
|
+ if (id=="introduction"){
|
|
|
|
+ cookDetailInfo?.recipe?.introduction = value
|
|
|
|
+ }
|
|
|
|
+ if (id=="difficulty"){
|
|
|
|
+ cookDetailInfo?.recipe?.difficultyLevel = value
|
|
|
|
+ }
|
|
|
|
+ if (id.contains("foodname")){
|
|
|
|
+ var ids = id
|
|
|
|
+ var list = ids.split("_")
|
|
|
|
+ var index = list[1].toInt()
|
|
|
|
+ cookDetailInfo?.material?.get(index)?.foodName = value
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ portionSizeLiveData.postValue(cookDetailInfo?.portionSize?.firstOrNull())
|
|
|
|
+ recipeLiveData.postValue(cookDetailInfo)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ fun setTranslateDate(value :String):String{
|
|
|
|
+ var language = MMkvUtils.getString(CURRENT_LANGUAGE)?:""
|
|
|
|
+ var listLanguage = mutableListOf<String>()
|
|
|
|
+ listLanguage.add(value)
|
|
|
|
+
|
|
|
|
+ var translateList = mutableListOf<TranslateBean>()
|
|
|
|
+ translateList.add(TranslateBean("name",cookDetailInfoInit?.recipe?.name))
|
|
|
|
+ translateList.add(TranslateBean("introduction",cookDetailInfoInit?.recipe?.introduction))
|
|
|
|
+ translateList.add(TranslateBean("difficulty",cookDetailInfoInit?.recipe?.difficultyLevel))
|
|
|
|
+ var size = cookDetailInfoInit?.material?.size?:0
|
|
|
|
+
|
|
|
|
+ if (size>0){
|
|
|
|
+ size--
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (i in 0..size){
|
|
|
|
+// DevRecipeFood
|
|
|
|
+ var material = cookDetailInfoInit?.material?.get(i)
|
|
|
|
+ material?.let {
|
|
|
|
+ translateList.add(TranslateBean("foodname_"+i, it.foodName ?: ""))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var map = HashMap<String,Any>()
|
|
|
|
+ map["from"] = language.lowercase()
|
|
|
|
+ map["tos"] = listLanguage
|
|
|
|
+ map["translateList"] = translateList
|
|
|
|
+
|
|
|
|
+ var json = GsonUtils.toJson(map)
|
|
|
|
+
|
|
|
|
+ return json
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
fun getRecipeLiveData(): LiveData<CookDetailInfo> {
|
|
fun getRecipeLiveData(): LiveData<CookDetailInfo> {
|
|
return recipeLiveData
|
|
return recipeLiveData
|
|
}
|
|
}
|