Browse Source

食谱步骤翻译兼容

zhangshenjie 1 week ago
parent
commit
c6bece3c3d

+ 15 - 0
BusinessStep/src/main/java/com/develop/step/ui/cook_step/CookStepActivity.kt

@@ -101,6 +101,18 @@ class CookStepActivity : CookStepBaseActivity() {
     @Autowired(name = "portionSizelJson")
     var portionSizelJson: String? = null
 
+    @JvmField
+    @Autowired(name = "translate_step")
+    var translate_step: String? = null
+
+
+    @JvmField
+    @Autowired(name = "translate_recipe")
+    var translate_recipe: String? = null
+
+
+
+
     private var titleName = ""
     private var currModes = ""
 
@@ -361,6 +373,9 @@ class CookStepActivity : CookStepBaseActivity() {
     }
 
     private fun initData() {
+        viewModel.translateRecipe = translate_recipe?:""
+        viewModel.translateStep = translate_step?:""
+
         viewModel.queryRecipeCookStep(recipeNumber ?: "", currentStepIndex)
         viewModel.stepDisplay.observe(this) { it ->
             coverPath = FoodDataProvider.getImagePath(it.source.photoVideoFilePath)

+ 46 - 20
BusinessStep/src/main/java/com/develop/step/ui/recipes_detail/CookDetailActivity.kt

@@ -14,6 +14,7 @@ import com.develop.base.ext.load
 import com.alibaba.android.arouter.facade.annotation.Autowired
 import com.alibaba.android.arouter.facade.annotation.Route
 import com.alibaba.android.arouter.launcher.ARouter
+import com.blankj.utilcode.util.GsonUtils
 import com.blankj.utilcode.util.ToastUtils
 import com.develop.base.ext.*
 import com.develop.base.util.MMkvUtils
@@ -89,6 +90,7 @@ class CookDetailActivity : CommonBVMActivity<ActivityCookDetailBinding, CookDeta
     private var is011 = false
     private var isResetZero = -1  //初始扭橙的值
     private var sn = getSN()
+    private var bean :CookDetailInfo? = null
     private val shareQRCodeDialog by lazy {
         ShareQRCodeDialog()
     }
@@ -127,6 +129,7 @@ class CookDetailActivity : CommonBVMActivity<ActivityCookDetailBinding, CookDeta
 
     private fun initData() {
         viewModel.getRecipeLiveData().observe(this) {
+            this.bean = it
             dismissloadingDialog()
             cookDetailJson = Json.encodeToString(it)
             updateCookDetail(it)
@@ -515,12 +518,23 @@ class CookDetailActivity : CommonBVMActivity<ActivityCookDetailBinding, CookDeta
     }
 
     fun startAcCookStep(step: Int) {
+        var translate_step = ""
+        var translate_recipe = ""
+        if (isTranslate){
+            translate_step = GsonUtils.toJson(viewModel.translateCookingStep)
+            translate_recipe = GsonUtils.toJson(viewModel.cookDetailInfo?.recipe)
+        }
+
         navigateTo(Screens.Cook.COOK_STEP2) {
             withString("number", recipeNumber)
             withBoolean("isLike", isLike)
             withString("cook_step_detail", cookDetailJson)
             withString("portionSizelJson", portionSizelJson)
             withInt("stepIndex", step)
+            if (isTranslate){
+                withString("translate_step",translate_step)
+                withString("translate_recipe",translate_recipe)
+            }
         }
     }
 
@@ -703,27 +717,35 @@ class CookDetailActivity : CommonBVMActivity<ActivityCookDetailBinding, CookDeta
         }
 
         binding.startLayout.setCommentNumber(detail.recipe.useNum.toString())
-        FoodDataProvider.getUserDatabase().runInTransaction {
-            val result = FoodDataProvider.getUserDatabase().userInfoDao()
-                .queryUserTag(CURRENT_USER_ID, detail.recipe.number ?: "")
-            val like = FoodDataProvider.getUserDatabase().userInfoDao()
-                .queryFavoriteRecipes(CURRENT_USER_ID, detail.recipe.number ?: "")
-
-            runOnUiThread {
-                if (like.isNotEmpty()) {
-                    binding.viewLike.src = com.develop.common.R.drawable.ic_like
-                } else {
-                    binding.viewLike.src = com.develop.common.R.drawable.ic_unlike
-                }
-                val count1 = result?.starCount ?: 0
-                val count2 = detail.recipe.score?.toInt() ?: 0
-                if (count1 != 0) {
-                    binding.startLayout.setStarCount(count1)
-                } else if (count2 != 0) {
-                    binding.startLayout.setStarCount(count2)
+        isUserLike()
+
+    }
+
+    fun isUserLike(){
+        bean?.let {
+            FoodDataProvider.getUserDatabase().runInTransaction {
+                val result = FoodDataProvider.getUserDatabase().userInfoDao()
+                    .queryUserTag(CURRENT_USER_ID, it.recipe.number ?: "")
+                val like = FoodDataProvider.getUserDatabase().userInfoDao()
+                    .queryFavoriteRecipes(CURRENT_USER_ID, it.recipe.number ?: "")
+
+                runOnUiThread {
+                    if (like.isNotEmpty()) {
+                        binding.viewLike.src = com.develop.common.R.drawable.ic_like
+                    } else {
+                        binding.viewLike.src = com.develop.common.R.drawable.ic_unlike
+                    }
+                    val count1 = result?.starCount ?: 0
+                    val count2 = it.recipe.score?.toInt() ?: 0
+                    if (count1 != 0) {
+                        binding.startLayout.setStarCount(count1)
+                    } else if (count2 != 0) {
+                        binding.startLayout.setStarCount(count2)
+                    }
                 }
             }
         }
+
     }
 
     @Suppress("UNCHECKED_CAST")
@@ -781,7 +803,8 @@ class CookDetailActivity : CommonBVMActivity<ActivityCookDetailBinding, CookDeta
         isResetZero = CofarSDK.devInfo().resetZero.toInt()
         super.onResume()
         viewModel.getStarCount(recipeNumber ?: "")
-        if (isOnResume) {
+        if (isOnResume&&!isTranslate) {
+            //翻译的时候不重新刷新
             viewModel.queryRecipe(
                 recipeNumber ?: "",
                 cookSource == "remote",
@@ -791,7 +814,10 @@ class CookDetailActivity : CommonBVMActivity<ActivityCookDetailBinding, CookDeta
             if (cookSource == "remote") {
                 isOnResume = false
             }
-
+        }
+        //由于翻译回来不刷新,只刷新是否添加喜欢
+        if (isTranslate){
+            isUserLike()
         }
 
     }

+ 1 - 1
BusinessStep/src/main/java/com/develop/step/ui/recipes_detail/model/CookDetailInfo.kt

@@ -10,4 +10,4 @@ data class CookDetailInfo(
     val material: List<DevRecipeFood>,
     val nutrition: List<DevRecipeNutrition>,
     val portionSize: List<DevRecipePortionSize>
-)
+)

+ 0 - 16
BusinessStep/src/main/java/com/develop/step/ui/recipes_detail/model/CookDetailInfoBean.kt

@@ -1,16 +0,0 @@
-package com.develop.step.ui.recipes_detail.model
-
-import com.develop.common.data_repo.db.entity.*
-import kotlinx.serialization.Serializable
-
-@Serializable
-data class CookDetailInfoBean(
-    /**
-     * 翻译实体bean
-     * */
-    val recipe: DevRecipe,
-    val accessory: List<DevAccessory>,
-    val material: List<DevRecipeFood>,
-    val nutrition: List<DevRecipeNutrition>,
-    val portionSize: List<DevRecipePortionSize>
-)

+ 60 - 0
BusinessStep/src/main/java/com/develop/step/ui/recipes_detail/model/CookDetailTranslateBean.java

@@ -0,0 +1,60 @@
+package com.develop.step.ui.recipes_detail.model;
+
+import com.develop.common.data_repo.db.entity.DevAccessory;
+import com.develop.common.data_repo.db.entity.DevRecipe;
+import com.develop.common.data_repo.db.entity.DevRecipeFood;
+import com.develop.common.data_repo.db.entity.DevRecipeNutrition;
+import com.develop.common.data_repo.db.entity.DevRecipePortionSize;
+
+import java.util.List;
+
+public class CookDetailTranslateBean  {
+    private DevRecipe recipe;
+    private List<DevAccessory> accessory;
+    private List<DevRecipeFood> material;
+    private List<DevRecipeNutrition> nutrition;
+    private List<DevRecipePortionSize> portionSize;
+
+
+    public DevRecipe getRecipe() {
+        return recipe;
+    }
+
+    public void setRecipe(DevRecipe recipe) {
+        this.recipe = recipe;
+    }
+
+    public List<DevAccessory> getAccessory() {
+        return accessory;
+    }
+
+    public void setAccessory(List<DevAccessory> accessory) {
+        this.accessory = accessory;
+    }
+
+    public List<DevRecipeFood> getMaterial() {
+        return material;
+    }
+
+    public void setMaterial(List<DevRecipeFood> material) {
+        this.material = material;
+    }
+
+    public List<DevRecipeNutrition> getNutrition() {
+        return nutrition;
+    }
+
+    public void setNutrition(List<DevRecipeNutrition> nutrition) {
+        this.nutrition = nutrition;
+    }
+
+    public List<DevRecipePortionSize> getPortionSize() {
+        return portionSize;
+    }
+
+    public void setPortionSize(List<DevRecipePortionSize> portionSize) {
+        this.portionSize = portionSize;
+    }
+
+
+}

+ 67 - 36
BusinessStep/src/main/java/com/develop/step/viewmodel/CookDetailViewModel.kt

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

+ 22 - 6
BusinessStep/src/main/java/com/develop/step/viewmodel/CookStepViewModel.kt

@@ -3,6 +3,7 @@ package com.develop.step.viewmodel
 import android.graphics.Color
 import android.text.SpannableStringBuilder
 import android.text.Spanned
+import android.text.TextUtils
 import android.text.style.ForegroundColorSpan
 import android.util.Log
 import androidx.lifecycle.MutableLiveData
@@ -10,7 +11,9 @@ import com.develop.base.ext.globalRes
 import com.develop.base.mvvm.BaseViewModel
 import com.develop.common.data_repo.FoodDataProvider
 import com.develop.common.data_repo.db.entity.DevRecipe
+import com.develop.common.data_repo.db.entity.DevRecipeCookingStep
 import com.develop.common.utils.CofarUtils
+import com.develop.common.utils.GsonUtils
 import com.develop.step.CookSettingType
 import com.develop.step.R
 import com.develop.step.ui.cook_step.model.CookStepStatus
@@ -24,7 +27,8 @@ class CookStepViewModel : BaseViewModel() {
 
     val currentSetting = MutableLiveData(CookSettingType.WEIGHT)
     val stepUiData = CookStepUiData()
-
+    var translateStep = ""
+    var translateRecipe = ""
     var stepIndex = 0
 
     // 默认属性, 只读
@@ -38,14 +42,26 @@ class CookStepViewModel : BaseViewModel() {
     fun queryRecipeCookStep(number: String, currentStepIndex: Int = 0) {
         FoodDataProvider.getDatabase().runInTransaction {
             allSteps.clear()
-            recipe =  FoodDataProvider.getDatabase().recipeDao().queryRecipe(number)
+            //做翻译功能 ,如果有翻译数据,就用翻译数据生成实体
+            if (TextUtils.isEmpty(translateRecipe)){
+                recipe =  FoodDataProvider.getDatabase().recipeDao().queryRecipe(number)
+            }else{
+                recipe = GsonUtils.GsonToBean(translateRecipe,DevRecipe::class.java)
+            }
             recipe?.apply {
                 recipeLiveData.postValue(this)
             }
-            val cookSteps = FoodDataProvider
-                .getDatabase()
-                .recipeDao()
-                .queryCookingStep(number)
+            var cookSteps  = mutableListOf<DevRecipeCookingStep>()
+            if (TextUtils.isEmpty(translateStep)){
+                cookSteps    = FoodDataProvider
+                    .getDatabase()
+                    .recipeDao()
+                    .queryCookingStep(number).toMutableList()
+            }else{
+                cookSteps = GsonUtils.jsonToList(translateStep,DevRecipeCookingStep::class.java).toMutableList()
+            }
+
+
             if (cookSteps.isEmpty()) {
                 return@runInTransaction
             }