CookStepViewModel.kt 6.7 KB

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