import 'package:fitnote/models/health.dart'; import 'package:flutter/material.dart'; bool checkHasRecordValue(HealthRecord record, RecordType type) { switch (type) { case RecordType.weight: return record.weight != 0; case RecordType.sport: return record.sportDuration != 0; case RecordType.sleep: return record.sleepStartTime != ''; } } // 将字符串时间转换为 TimeOfDay TimeOfDay parseTime(String timeString) { if (timeString.isEmpty) { return TimeOfDay.now(); } try { final parts = timeString.split(':'); return TimeOfDay(hour: int.parse(parts[0]), minute: int.parse(parts[1])); } catch (e) { return TimeOfDay.now(); } } // 将 TimeOfDay 格式化为字符串 (HH:mm) 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 calSleepDuration(String startTime, String endTime) { try { int durationMinutes = calSleepMinuteDuration(startTime, endTime); int hours = durationMinutes ~/ 60; int minutes = durationMinutes % 60; return '${hours}小时${minutes}分钟'; } catch (e) { 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; } } String formatContent(HealthRecord record, RecordType type) { late String title; switch (type) { case RecordType.weight: title = "${record.weight} kg"; case RecordType.sport: title = "${record.sportProject} · ${record.sportDuration} 分钟"; case RecordType.sleep: final startTime = record.sleepStartTime; final endTime = record.sleepEndTime; if (startTime.isNotEmpty && endTime.isNotEmpty) { final duration = calSleepDuration(startTime, endTime); title = "入睡 $startTime · 起床 $endTime \n${duration}"; } else if (startTime.isNotEmpty) { title = "入睡 $startTime"; } else if (endTime.isNotEmpty) { title = "起床 $endTime"; } } return title; }