189 lines
4.6 KiB
Dart
189 lines
4.6 KiB
Dart
import 'package:fitnote/models/health.dart';
|
|
import 'package:fitnote/utils/record_utils.dart';
|
|
import 'package:flutter_common/flutter_common.dart';
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
|
|
class HealthService {
|
|
static const String boxName = 'healths';
|
|
|
|
Box<HealthRecord> get box => Hive.box<HealthRecord>(boxName);
|
|
|
|
Future<bool> addRecord(HealthRecord record) async {
|
|
try {
|
|
await box.add(record);
|
|
return true;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
List<HealthRecord> getAllRecords() {
|
|
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),
|
|
orElse: () => HealthRecord.getEmpty(date),
|
|
);
|
|
|
|
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 += calSleepMinDuration(startTime, endTime);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (totalDuration == 0) {
|
|
return 0;
|
|
} else {
|
|
return roundNum(totalDuration / total / 60);
|
|
}
|
|
}
|
|
|
|
// 获取最新体重
|
|
num getLatestWeight() {
|
|
final records = getAllRecordsOrderByDate();
|
|
|
|
for (final record in records) {
|
|
if (record.weight != 0) {
|
|
return record.weight;
|
|
}
|
|
}
|
|
|
|
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,
|
|
);
|
|
|
|
if (index == -1) {
|
|
await box.add(record);
|
|
} else {
|
|
final key = box.keyAt(index);
|
|
await box.put(key, record);
|
|
}
|
|
|
|
return true;
|
|
} catch (e) {
|
|
print('Update record error: $e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> deleteRecord(HealthRecord record) async {
|
|
try {
|
|
await record.delete();
|
|
return true;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|