123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package com.develop.step.viewmodel
- import android.graphics.Color
- import android.text.SpannableStringBuilder
- import android.text.Spanned
- import android.text.style.ForegroundColorSpan
- import android.util.Log
- import androidx.lifecycle.MutableLiveData
- 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.utils.CofarUtils
- import com.develop.step.CookSettingType
- import com.develop.step.R
- import com.develop.step.ui.cook_step.model.CookStepStatus
- import com.develop.step.ui.cook_step.model.CookStepUiData
- import com.develop.step.ui.recipes_detail.model.CookStatus
- import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
- import com.kuyuntech.cofarcooking.device.sdk.constant.core.MotorDirections
- import java.util.concurrent.CopyOnWriteArrayList
- class CookStepViewModel : BaseViewModel() {
- val currentSetting = MutableLiveData(CookSettingType.WEIGHT)
- val stepUiData = CookStepUiData()
- var stepIndex = 0
- // 默认属性, 只读
- val dataCopy = mutableListOf<CookStepUiData>()
- val allSteps = CopyOnWriteArrayList<CookStepStatus>()
- val stepDisplay = MutableLiveData<CookStepStatus>()
- val recipeLiveData = MutableLiveData<DevRecipe>()
- var cookingStep: CookStepStatus? = null
- var recipe: DevRecipe?= null;
- fun queryRecipeCookStep(number: String, currentStepIndex: Int = 0) {
- FoodDataProvider.getDatabase().runInTransaction {
- allSteps.clear()
- recipe = FoodDataProvider.getDatabase().recipeDao().queryRecipe(number)
- recipe?.apply {
- recipeLiveData.postValue(this)
- }
- val cookSteps = FoodDataProvider
- .getDatabase()
- .recipeDao()
- .queryCookingStep(number)
- if (cookSteps.isEmpty()) {
- return@runInTransaction
- }
- Log.d("Step", "total count:${cookSteps.size}")
- for (cookStep in cookSteps) {
- val stepStatus = CookStepStatus(
- cookStep.workMode ?: "", cookStep
- )
- stepStatus.uiData.applyRecipeSetting(cookStep)
- allSteps.add(stepStatus)
- dataCopy.add(stepStatus.uiData.newCopy())
- }
- //默认添加最后一步享用美食
- val lastStep = cookSteps[0].copy()
- lastStep.description = globalRes().getString(com.develop.common.R.string.enjoy_your_meal_desc)
- val finalStep = CookStepStatus("Final Step",lastStep)
- allSteps.add(finalStep)
- dataCopy.add(finalStep.uiData.newCopy())
- stepIndex = currentStepIndex
- if (allSteps.isNotEmpty() && allSteps.size > currentStepIndex) {
- allSteps[currentStepIndex].let {
- stepDisplay.value = it
- cookingStep = it
- }
- }
- }
- }
- fun nextStep() {
- if (stepIndex >= allSteps.size - 1) {
- return
- }
- stepIndex++
- allSteps.getOrNull(stepIndex)?.let {
- stepDisplay.value = it
- }
- }
- fun prevStep() {
- if (stepIndex <= 0) {
- return
- }
- stepIndex--
- allSteps.getOrNull(stepIndex)?.let {
- stepDisplay.value = it
- }
- }
- fun setDisplayStep(index: Int, force: Boolean = false) {
- stepIndex = index
- allSteps.getOrNull(stepIndex)?.let {
- if (stepDisplay.value != it || force) {
- stepDisplay.value = it
- }
- }
- }
- fun getStepCount(): Int {
- return allSteps.size
- }
- fun isFirstStep(step: CookStepStatus): Boolean {
- return allSteps.indexOf(step) == 0
- }
- fun isFinalStep(step: CookStepStatus): Boolean {
- return allSteps.indexOf(step) == (allSteps.size - 1)
- }
- fun changeStep(step: CookSettingType) {
- currentSetting.value = step
- }
- fun getNextStep(step: CookStepStatus): CookStepStatus? {
- val index = allSteps.indexOf(step)
- if (index < 0) {
- return null
- }
- return allSteps.getOrNull(index + 1)
- }
- fun setTargetCookingStep(cookStep: CookStepStatus) {
- val stepIndex = allSteps.indexOf(cookStep)
- if (stepIndex < 0) {
- return
- }
- cookStep.hasComplete = false
- cookStep.hasStarted = false
- cookingStep = cookStep
- setDisplayStep(stepIndex)
- }
- fun displayStep(): CookStepStatus? {
- return stepDisplay.value
- }
- fun isCurrentOnCookingStep(): Boolean {
- return displayStep() == cookingStep
- }
- fun isCookingStepStarted(): Boolean {
- return cookingStep != null && cookingStep?.uiData?.runningStatus != DevStatus.STOP.toInt()
- }
- fun getStepCombineText(): CharSequence {
- val spanBuilder = SpannableStringBuilder()
- for (step in allSteps) {
- var description = step.source.description ?: ""
- if (step.source.description == null || step.source.description == "") {
- var minute = "0"
- var second = "0"
- var temperature = "0"
- var rotateSpeed = "0"
- var rotateDirection = MotorDirections.FORWARD.toString()
- if (step.source.minute != null) {
- minute = step.source.minute.toString();
- }
- if (step.source.second != null) {
- second = step.source.second.toString();
- }
- if (step.source.temperature != null) {
- temperature =
- CofarUtils.parseTemp(step.source.temperature!!.toShort()).toString();
- }
- if (step.source.rotateSpeed != null) {
- rotateSpeed = step.source.rotateSpeed.toString();
- }
- if (step.source.rotateDirection != null && step.source.rotateDirection != "") {
- rotateDirection = step.source.rotateDirection.toString();
- }
- description =
- "${temperature}°C/${minute}m${second}s/V${rotateSpeed}/${if (rotateDirection == MotorDirections.FORWARD.toString()) "R" else "L"}";
- }
- if (step == displayStep()) {
- spanBuilder.append(
- description,
- ForegroundColorSpan(Color.parseColor("#E60012")),
- Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
- )
- } else {
- spanBuilder.append(description)
- }
- spanBuilder.append("\n\n")
- }
- return spanBuilder
- }
- }
|