CPD_2024_todo/lib/storage/hive_todo_storage.dart

35 lines
812 B
Dart

import 'package:hive/hive.dart';
import '../models/todo.dart';
import 'todo_storage.dart';
class HiveToDoStorage implements ToDoStorage {
static const String _boxName = 'todoBox';
Future<Box> get _box async {
return Hive.box(_boxName);
}
@override
Future<void> addTask(ToDo todo) async {
final box = await _box;
await box.add(todo.toJson());
}
@override
Future<void> removeTask(int index) async {
final box = await _box;
await box.deleteAt(index);
}
@override
Future<void> updateTask(int index, ToDo todo) async {
final box = await _box;
await box.putAt(index, todo.toJson());
}
@override
Future<List<ToDo>> fetchTasks() async {
final box = await _box;
return box.values.map((e) => ToDo.fromJson(Map<String, dynamic>.from(e))).toList();
}
}