feat:更新目标模块

This commit is contained in:
2025-12-02 17:55:43 +08:00
parent 8a71ea542e
commit 86852cc47d
7 changed files with 207 additions and 64 deletions

View File

@@ -6,7 +6,7 @@ bool checkHasRecordValue(HealthRecord record, RecordType type) {
case RecordType.weight:
return record.weight != 0;
case RecordType.sport:
return record.sportProject != '';
return record.sportDuration != 0;
case RecordType.sleep:
return record.sleepStartTime != '';
}
@@ -30,21 +30,25 @@ String formatTime(TimeOfDay time) {
return '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
}
int calSleepMinuteDuration(String startTime, String endTime) {
final start = parseTime(startTime);
final end = parseTime(endTime);
int startMinutes = start.hour * 60 + start.minute;
int endMinutes = end.hour * 60 + end.minute;
// 处理跨天情况(如果结束时间小于开始时间,认为是第二天)
if (endMinutes < startMinutes) {
endMinutes += 24 * 60;
}
return endMinutes - startMinutes;
}
// 计算睡眠时长
String calculateDuration(String startTime, String endTime) {
String calSleepDuration(String startTime, String endTime) {
try {
final start = parseTime(startTime);
final end = parseTime(endTime);
int startMinutes = start.hour * 60 + start.minute;
int endMinutes = end.hour * 60 + end.minute;
// 处理跨天情况(如果结束时间小于开始时间,认为是第二天)
if (endMinutes < startMinutes) {
endMinutes += 24 * 60; // 加上一天的分钟数
}
int durationMinutes = endMinutes - startMinutes;
int durationMinutes = calSleepMinuteDuration(startTime, endTime);
int hours = durationMinutes ~/ 60;
int minutes = durationMinutes % 60;
@@ -53,3 +57,28 @@ String calculateDuration(String startTime, String endTime) {
return '计算错误';
}
}
double roundNum(double value) {
return double.parse(value.toStringAsFixed(2));
}
double calWeightProgress(num currentWeight, num targetWeight, num startWeight) {
if (currentWeight == 0) return 0.0;
if (startWeight <= targetWeight) return 0.0;
if (currentWeight <= targetWeight) return 1.0;
num totalToLose = startWeight - targetWeight;
num alreadyLost = startWeight - currentWeight;
return formatProgress((alreadyLost / totalToLose).clamp(0.0, 1.0));
}
double formatProgress(double value) {
final result = roundNum(value);
if (result >= 1) {
return 1;
} else {
return result;
}
}