Files
flisp_app/lib/service/todo_service.dart

45 lines
867 B
Dart

import 'package:flisp_app/models/todo.dart';
import 'package:hive_flutter/hive_flutter.dart';
class TodoService {
static const String boxName = 'todos';
Box<Todo> get box => Hive.box<Todo>(boxName);
Future<bool> addTodo(Todo todo) async {
try {
await box.add(todo);
return true;
} catch (e) {
return false;
}
}
List<Todo> getAllTodos() {
return box.values.toList();
}
Future<bool> updateTodo(Todo todo) async {
try {
await todo.save();
return true;
} catch (e) {
return false;
}
}
Future<bool> updateTodoCompletion(Todo todo, bool isCompleted) async {
todo.isCompleted = isCompleted;
return await updateTodo(todo);
}
Future<bool> deleteTodo(Todo todo) async {
try {
await todo.delete();
return true;
} catch (e) {
return false;
}
}
}