调整了个人营养数据计算逻辑,修复了摄像头逻辑
This commit is contained in:
@@ -2,8 +2,12 @@ package com.sw.dualscreen.presentation
|
||||
|
||||
|
||||
import android.text.TextUtils
|
||||
import com.sw.dualscreen.ext.toSafeDouble
|
||||
import com.sw.dualscreen.model.response.FoodInfo
|
||||
import com.sw.dualscreen.model.response.UserNutritionData
|
||||
import timber.log.Timber
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
object UserNutritionUtils {
|
||||
|
||||
@@ -16,12 +20,12 @@ object UserNutritionUtils {
|
||||
weight: Double,
|
||||
dinnerType: String
|
||||
): CalcResultInfo {
|
||||
// 1. 初始化变量
|
||||
// 初始化变量
|
||||
var foodKcal = 0.0
|
||||
var totalKcal = 0.0
|
||||
var (vegetable, meat, fruits, grain) = List(4) { 0.0 }
|
||||
|
||||
// 2. 处理食物信息
|
||||
// 处理食物信息
|
||||
foodKcal = calculateValue(foodInfo.stFoodInfoMaterial?.energyKcal, weight)
|
||||
|
||||
grain = calculateValue(foodInfo.stFoodInfoPagodaAPPVO?.grainValue(), weight)
|
||||
@@ -29,33 +33,52 @@ object UserNutritionUtils {
|
||||
vegetable = calculateValue(foodInfo.stFoodInfoPagodaAPPVO?.vegetableValue(), weight)
|
||||
meat = calculateValue(foodInfo.stFoodInfoPagodaAPPVO?.meatValue(), weight)
|
||||
|
||||
// 3. 合并用户数据
|
||||
// 合并用户数据
|
||||
grain += userModel.stFoodInfoPagoda?.grainValue() ?: 0.0
|
||||
fruits += userModel.stFoodInfoPagoda?.fruitsValue() ?: 0.0
|
||||
vegetable = vegetable.plus(userModel.stFoodInfoPagoda?.vegetableValue() ?: 0.0)
|
||||
meat += userModel.stFoodInfoPagoda?.meatValue() ?: 0.0
|
||||
|
||||
totalKcal = foodKcal + userModel.energyValue()
|
||||
val fruitsAndVeg = fruits + vegetable
|
||||
|
||||
// 4. 判断当餐最大热量
|
||||
if (totalKcal < 0){
|
||||
totalKcal = 0.0
|
||||
}
|
||||
// 判断当餐最大热量
|
||||
val maxKcal =
|
||||
userModel.totalEnergyCalculateScoreValue() * calculateDinnerTypeRatio(dinnerType) / 10
|
||||
|
||||
val pagoda = userModel.stFoodInfoPagoda
|
||||
val fruitsInfo = parseRecommend(pagoda?.fruitsRecommend, fruits)
|
||||
val vegetableInfo = parseRecommend(pagoda?.vegetableRecommend, vegetable)
|
||||
val calcResult = CalcResultInfo(
|
||||
totalKcal = maxKcal to totalKcal,
|
||||
grain = parseMax(pagoda?.grainRecommend) to grain,
|
||||
fruits = parseMax(pagoda?.fruitsRecommend) + parseMax(pagoda?.vegetableRecommend) to fruitsAndVeg,
|
||||
meat = parseMax(pagoda?.meatRecommend) to meat
|
||||
totalKcal = CalcInfo(
|
||||
max = maxKcal,
|
||||
min = 0.0,
|
||||
current = totalKcal
|
||||
),
|
||||
grain = parseRecommend(pagoda?.grainRecommend, grain),
|
||||
fruits = CalcInfo( // 果蔬 = 水果+蔬菜
|
||||
max = fruitsInfo.max + vegetableInfo.max,
|
||||
min = fruitsInfo.min + vegetableInfo.min,
|
||||
current = fruitsInfo.current + vegetableInfo.current
|
||||
),
|
||||
meat = parseRecommend(pagoda?.meatRecommend, meat)
|
||||
)
|
||||
return calcResult
|
||||
}
|
||||
|
||||
fun parseMax(recommend: String?): Double {
|
||||
if (TextUtils.isEmpty(recommend) || !recommend!!.contains("-")) return 0.0
|
||||
fun parseRecommend(recommend: String?, current: Double): CalcInfo {
|
||||
var newCurrent = if (current < 0) 0.0 else current
|
||||
if (TextUtils.isEmpty(recommend) || !recommend!!.contains("-")) {
|
||||
Timber.e("parseRecommend recommend 格式错误")
|
||||
return CalcInfo(
|
||||
newCurrent,
|
||||
0.0,
|
||||
newCurrent
|
||||
)
|
||||
}
|
||||
val (min, max) = recommend.split("-")
|
||||
return max.toDouble()
|
||||
return CalcInfo(max = max.toSafeDouble(), min = min.toSafeDouble(), current = newCurrent)
|
||||
}
|
||||
|
||||
private fun calculateDinnerTypeRatio(dinnerType: String): Int {
|
||||
@@ -66,14 +89,28 @@ object UserNutritionUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算每百克含量
|
||||
*/
|
||||
private fun calculateValue(nutrient: Double?, weight: Double): Double {
|
||||
return weight * (nutrient ?: 1.0)
|
||||
return weight * (nutrient ?: 1.0) / 100
|
||||
}
|
||||
|
||||
fun getMaxInt(calcResultInfo: CalcResultInfo): Int {
|
||||
val max1 = max(calcResultInfo.grain.max, calcResultInfo.fruits.max)
|
||||
return max(max1, calcResultInfo.meat.max).toInt()
|
||||
}
|
||||
|
||||
data class CalcInfo(
|
||||
var max: Double,
|
||||
var min: Double,
|
||||
var current: Double,
|
||||
)
|
||||
|
||||
data class CalcResultInfo(
|
||||
var totalKcal: Pair<Double, Double>,
|
||||
var grain: Pair<Double, Double>,
|
||||
var fruits: Pair<Double, Double>,
|
||||
var meat: Pair<Double, Double>,
|
||||
var totalKcal: CalcInfo,
|
||||
var grain: CalcInfo,
|
||||
var fruits: CalcInfo,
|
||||
var meat: CalcInfo,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user