feat:增加睡眠记录
This commit is contained in:
55
lib/utils/record_utils.dart
Normal file
55
lib/utils/record_utils.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
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.sportProject != '';
|
||||
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')}';
|
||||
}
|
||||
|
||||
// 计算睡眠时长
|
||||
String calculateDuration(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 hours = durationMinutes ~/ 60;
|
||||
int minutes = durationMinutes % 60;
|
||||
|
||||
return '${hours}小时${minutes}分钟';
|
||||
} catch (e) {
|
||||
return '计算错误';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user