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

@@ -1,4 +1,5 @@
import 'package:fitnote/models/health.dart';
import 'package:fitnote/utils/record_utils.dart';
import 'package:hive_flutter/hive_flutter.dart';
class HealthService {
@@ -28,6 +29,67 @@ class HealthService {
return record.copyWith();
}
num getTotalSport() {
int year = DateTime.now().year;
int month = DateTime.now().month;
num totalDuration = 0;
for (final record in getAllRecords()) {
if (record.sportDuration != 0) {
final recordDate = DateTime.parse(record.date);
if (recordDate.year == year && recordDate.month == month) {
totalDuration += record.sportDuration;
}
}
}
return totalDuration;
}
num getAvgSleep() {
int year = DateTime.now().year;
int month = DateTime.now().month;
num totalDuration = 0;
int total = 0;
for (final record in getAllRecords()) {
final startTime = record.sleepStartTime;
final endTime = record.sleepEndTime;
if (startTime.isNotEmpty && endTime.isNotEmpty) {
final recordDate = DateTime.parse(record.date);
if (recordDate.year == year && recordDate.month == month) {
total++;
totalDuration += calSleepMinuteDuration(startTime, endTime);
}
}
}
if (totalDuration == 0) {
return 0;
} else {
return roundNum(totalDuration / total / 60);
}
}
num getLatestWeight() {
final records = getAllRecords();
records.sort((a, b) {
final dateA = DateTime.parse(a.date);
final dateB = DateTime.parse(b.date);
return dateB.compareTo(dateA);
});
for (final record in records) {
if (record.weight != 0) {
return record.weight;
}
}
return 0;
}
Future<bool> updateRecord(HealthRecord record) async {
try {
List<HealthRecord> records =