CookStepViewModel.kt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package com.develop.step.viewmodel
  2. import android.graphics.Color
  3. import android.text.SpannableStringBuilder
  4. import android.text.Spanned
  5. import android.text.style.ForegroundColorSpan
  6. import android.util.Log
  7. import androidx.lifecycle.MutableLiveData
  8. import com.develop.base.ext.globalRes
  9. import com.develop.base.mvvm.BaseViewModel
  10. import com.develop.common.data_repo.FoodDataProvider
  11. import com.develop.common.data_repo.db.entity.DevRecipe
  12. import com.develop.common.data_repo.db.entity.DevRecipeCookingStep
  13. import com.develop.common.utils.CofarUtils
  14. import com.develop.common.utils.GsonUtils
  15. import com.develop.step.CookSettingType
  16. import com.develop.step.R
  17. import com.develop.step.ui.cook_step.model.CookStepStatus
  18. import com.develop.step.ui.cook_step.model.CookStepUiData
  19. import com.develop.step.ui.recipes_detail.model.CookStatus
  20. import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
  21. import com.kuyuntech.cofarcooking.device.sdk.constant.core.MotorDirections
  22. import java.util.concurrent.CopyOnWriteArrayList
  23. class CookStepViewModel : BaseViewModel() {
  24. val currentSetting = MutableLiveData(CookSettingType.WEIGHT)
  25. val stepUiData = CookStepUiData()
  26. var stepIndex = 0
  27. var tuyaStepJson = ""
  28. var tuyaRecipeJson = ""
  29. // 默认属性, 只读
  30. val dataCopy = mutableListOf<CookStepUiData>()
  31. val allSteps = CopyOnWriteArrayList<CookStepStatus>()
  32. val stepDisplay = MutableLiveData<CookStepStatus>()
  33. val recipeLiveData = MutableLiveData<DevRecipe>()
  34. var cookingStep: CookStepStatus? = null
  35. var recipe: DevRecipe?= null;
  36. fun queryRecipeCookStep(number: String, currentStepIndex: Int = 0) {
  37. FoodDataProvider.getDatabase().runInTransaction {
  38. allSteps.clear()
  39. recipe = if (tuyaRecipeJson == ""){
  40. FoodDataProvider.getDatabase().recipeDao().queryRecipe(number)
  41. }else{
  42. GsonUtils.GsonToBean<DevRecipe>(tuyaRecipeJson,DevRecipe::class.java)
  43. }
  44. recipe?.apply {
  45. recipeLiveData.postValue(this)
  46. }
  47. val cookSteps = if (tuyaStepJson==""){
  48. FoodDataProvider
  49. .getDatabase()
  50. .recipeDao()
  51. .queryCookingStep(number)
  52. }else{
  53. GsonUtils.jsonToList<DevRecipeCookingStep>(tuyaStepJson,DevRecipeCookingStep::class.java)
  54. }
  55. // var str = com.blankj.utilcode.util.GsonUtils.toJson(cookSteps)
  56. // Log.e("TAG CookStep", "json:$str")
  57. if (cookSteps.isEmpty()) {
  58. return@runInTransaction
  59. }
  60. Log.d("Step", "total count:${cookSteps.size}")
  61. for (cookStep in cookSteps) {
  62. val stepStatus = CookStepStatus(
  63. cookStep.workMode ?: "", cookStep
  64. )
  65. stepStatus.uiData.applyRecipeSetting(cookStep)
  66. allSteps.add(stepStatus)
  67. dataCopy.add(stepStatus.uiData.newCopy())
  68. }
  69. //默认添加最后一步享用美食
  70. val lastStep = cookSteps[0].copy()
  71. lastStep.description = globalRes().getString(com.develop.common.R.string.enjoy_your_meal_desc)
  72. val finalStep = CookStepStatus("Final Step",lastStep)
  73. allSteps.add(finalStep)
  74. dataCopy.add(finalStep.uiData.newCopy())
  75. stepIndex = currentStepIndex
  76. if (allSteps.isNotEmpty() && allSteps.size > currentStepIndex) {
  77. allSteps[currentStepIndex].let {
  78. stepDisplay.value = it
  79. cookingStep = it
  80. }
  81. }
  82. }
  83. }
  84. fun nextStep() {
  85. if (stepIndex >= allSteps.size - 1) {
  86. return
  87. }
  88. stepIndex++
  89. allSteps.getOrNull(stepIndex)?.let {
  90. stepDisplay.value = it
  91. }
  92. }
  93. fun prevStep() {
  94. if (stepIndex <= 0) {
  95. return
  96. }
  97. stepIndex--
  98. allSteps.getOrNull(stepIndex)?.let {
  99. stepDisplay.value = it
  100. }
  101. }
  102. fun setDisplayStep(index: Int, force: Boolean = false) {
  103. stepIndex = index
  104. allSteps.getOrNull(stepIndex)?.let {
  105. if (stepDisplay.value != it || force) {
  106. stepDisplay.value = it
  107. }
  108. }
  109. }
  110. fun getStepCount(): Int {
  111. return allSteps.size
  112. }
  113. fun isFirstStep(step: CookStepStatus): Boolean {
  114. return allSteps.indexOf(step) == 0
  115. }
  116. fun isFinalStep(step: CookStepStatus): Boolean {
  117. return allSteps.indexOf(step) == (allSteps.size - 1)
  118. }
  119. fun changeStep(step: CookSettingType) {
  120. currentSetting.value = step
  121. }
  122. fun getNextStep(step: CookStepStatus): CookStepStatus? {
  123. val index = allSteps.indexOf(step)
  124. if (index < 0) {
  125. return null
  126. }
  127. return allSteps.getOrNull(index + 1)
  128. }
  129. fun setTargetCookingStep(cookStep: CookStepStatus) {
  130. val stepIndex = allSteps.indexOf(cookStep)
  131. if (stepIndex < 0) {
  132. return
  133. }
  134. cookStep.hasComplete = false
  135. cookStep.hasStarted = false
  136. cookingStep = cookStep
  137. setDisplayStep(stepIndex)
  138. }
  139. fun displayStep(): CookStepStatus? {
  140. return stepDisplay.value
  141. }
  142. fun isCurrentOnCookingStep(): Boolean {
  143. return displayStep() == cookingStep
  144. }
  145. fun isCookingStepStarted(): Boolean {
  146. return cookingStep != null && cookingStep?.uiData?.runningStatus != DevStatus.STOP.toInt()
  147. }
  148. fun getStepCombineText(): CharSequence {
  149. val spanBuilder = SpannableStringBuilder()
  150. for (step in allSteps) {
  151. var description = step.source.description ?: ""
  152. if (step.source.description == null || step.source.description == "") {
  153. var minute = "0"
  154. var second = "0"
  155. var temperature = "0"
  156. var rotateSpeed = "0"
  157. var rotateDirection = MotorDirections.FORWARD.toString()
  158. if (step.source.minute != null) {
  159. minute = step.source.minute.toString();
  160. }
  161. if (step.source.second != null) {
  162. second = step.source.second.toString();
  163. }
  164. if (step.source.temperature != null) {
  165. temperature =
  166. CofarUtils.parseTemp(step.source.temperature!!.toShort()).toString();
  167. }
  168. if (step.source.rotateSpeed != null) {
  169. rotateSpeed = step.source.rotateSpeed.toString();
  170. }
  171. if (step.source.rotateDirection != null && step.source.rotateDirection != "") {
  172. rotateDirection = step.source.rotateDirection.toString();
  173. }
  174. description =
  175. "${temperature}°C/${minute}m${second}s/V${rotateSpeed}/${if (rotateDirection == MotorDirections.FORWARD.toString()) "R" else "L"}";
  176. }
  177. if (step == displayStep()) {
  178. spanBuilder.append(
  179. description,
  180. ForegroundColorSpan(Color.parseColor("#E60012")),
  181. Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
  182. )
  183. } else {
  184. spanBuilder.append(description)
  185. }
  186. spanBuilder.append("\n\n")
  187. }
  188. return spanBuilder
  189. }
  190. }