feat:增加数据存储
This commit is contained in:
46
lib/service/health_service.dart
Normal file
46
lib/service/health_service.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user