Files
fitnote/lib/service/health_service.dart
2025-12-01 23:01:23 +08:00

47 lines
974 B
Dart

import 'package:fitnote/models/health.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();
}
HealthRecord getAllRecordByDate(String date) {
return box.values.toList().firstWhere(
(item) => item.date == date,
orElse: () => HealthRecord.getEmpty(),
);
}
Future<bool> updateRecord(HealthRecord record) async {
try {
await record.save();
return true;
} catch (e) {
return false;
}
}
Future<bool> deleteRecord(HealthRecord record) async {
try {
await record.delete();
return true;
} catch (e) {
return false;
}
}
}