import 'package:fitnote/models/health.dart'; import 'package:hive_flutter/hive_flutter.dart'; class HealthService { static const String boxName = 'healths'; Box get box => Hive.box(boxName); Future addRecord(HealthRecord record) async { try { await box.add(record); return true; } catch (e) { return false; } } List getAllRecords() { return box.values.toList(); } HealthRecord getAllRecordByDate(String date) { final record = box.values.toList().firstWhere( (item) => item.date == date, orElse: () => HealthRecord.getEmpty(date), ); return record.copyWith(); } Future updateRecord(HealthRecord record) async { try { List records = box.values.where((item) => item.date == record.date).toList(); if (records.isEmpty) { await addRecord(record); } else { await record.save(); } return true; } catch (e) { return false; } } Future deleteRecord(HealthRecord record) async { try { await record.delete(); return true; } catch (e) { return false; } } }