feat:增加变化提醒功能

This commit is contained in:
2025-12-03 14:28:39 +08:00
parent e672442d6a
commit 8d23f06f74
10 changed files with 194 additions and 68 deletions

View File

@@ -21,6 +21,18 @@ class HealthService {
return box.values.toList();
}
List<HealthRecord> getAllRecordsOrderByDate() {
final records = getAllRecords();
records.sort((a, b) {
final dateA = DateTime.parse(a.date);
final dateB = DateTime.parse(b.date);
return dateB.compareTo(dateA);
});
return records;
}
HealthRecord getRecordByDate(DateTime date) {
final record = box.values.toList().firstWhere(
(item) => item.date == formatDate(date),
@@ -30,6 +42,7 @@ class HealthService {
return record.copyWith();
}
// 获取总运动时长
num getTotalSport() {
int year = DateTime.now().year;
int month = DateTime.now().month;
@@ -48,6 +61,7 @@ class HealthService {
return totalDuration;
}
// 获取平均睡眠
num getAvgSleep() {
int year = DateTime.now().year;
int month = DateTime.now().month;
@@ -61,7 +75,7 @@ class HealthService {
final recordDate = DateTime.parse(record.date);
if (recordDate.year == year && recordDate.month == month) {
total++;
totalDuration += calSleepMinuteDuration(startTime, endTime);
totalDuration += calSleepMinDuration(startTime, endTime);
}
}
}
@@ -73,14 +87,9 @@ class HealthService {
}
}
// 获取最新体重
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);
});
final records = getAllRecordsOrderByDate();
for (final record in records) {
if (record.weight != 0) {
@@ -91,10 +100,68 @@ class HealthService {
return 0;
}
// 获取上次体重
num getLastWeight(String date) {
final records = getAllRecordsOrderByDate();
final targetIndex = records.indexWhere((record) => record.date == date);
if (targetIndex == -1) return 0;
// 从目标日期往前找(更早的日期)
for (int i = targetIndex + 1; i < records.length; i++) {
final record = records[i];
if (record.weight != 0) {
return record.weight;
}
}
return 0;
}
// 获取上次运动时长
num getLastSport(String date) {
final records = getAllRecordsOrderByDate();
final targetIndex = records.indexWhere((record) => record.date == date);
if (targetIndex == -1) return 0;
// 从目标日期往前找(更早的日期)
for (int i = targetIndex + 1; i < records.length; i++) {
final record = records[i];
if (record.sportDuration != 0) {
return record.sportDuration;
}
}
return 0;
}
// 获取上次睡眠时长
num getLastSleep(String date) {
final records = getAllRecordsOrderByDate();
final targetIndex = records.indexWhere((record) => record.date == date);
if (targetIndex == -1) return 0;
// 从目标日期往前找(更早的日期)
for (int i = targetIndex + 1; i < records.length; i++) {
final record = records[i];
final startTime = record.sleepStartTime;
final endTime = record.sleepEndTime;
if (startTime.isNotEmpty && endTime.isNotEmpty) {
return calSleepMinDuration(startTime, endTime);
}
}
return 0;
}
Future<bool> updateRecord(HealthRecord record) async {
try {
final existingRecords = box.values.toList();
final index = existingRecords.indexWhere((item) => item.date == record.date);
final index = existingRecords.indexWhere(
(item) => item.date == record.date,
);
if (index == -1) {
await box.add(record);
@@ -102,7 +169,7 @@ class HealthService {
final key = box.keyAt(index);
await box.put(key, record);
}
return true;
} catch (e) {
print('Update record error: $e');