2024-10-22 20:42:08 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import '../models/todo.dart';
|
2024-10-29 17:37:12 +01:00
|
|
|
import '../storage/todo_storage.dart';
|
2024-10-22 20:42:08 +02:00
|
|
|
|
|
|
|
class ToDoProvider with ChangeNotifier {
|
|
|
|
List<ToDo> _todos = [];
|
2024-10-29 17:37:12 +01:00
|
|
|
final ToDoStorage storage;
|
|
|
|
|
|
|
|
ToDoProvider(this.storage) {
|
|
|
|
loadTasks();
|
|
|
|
}
|
2024-10-22 20:42:08 +02:00
|
|
|
|
|
|
|
List<ToDo> get todos => _todos;
|
|
|
|
|
|
|
|
void addTask(ToDo todo) {
|
|
|
|
_todos.add(todo);
|
2024-10-29 17:37:12 +01:00
|
|
|
storage.addTask(todo);
|
2024-10-22 20:42:08 +02:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeTask(int index) {
|
2024-10-29 17:37:12 +01:00
|
|
|
storage.removeTask(index);
|
2024-10-22 20:42:08 +02:00
|
|
|
_todos.removeAt(index);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void toggleCompletion(int index) {
|
|
|
|
_todos[index].isCompleted = !_todos[index].isCompleted;
|
2024-10-29 17:37:12 +01:00
|
|
|
storage.updateTask(index, _todos[index]);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> loadTasks() async {
|
|
|
|
_todos = await storage.fetchTasks();
|
2024-10-22 20:42:08 +02:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void sortByDeadline() {
|
2024-10-29 17:37:12 +01:00
|
|
|
_todos.sort((a, b) => a.deadline.compareTo(b.deadline));
|
2024-10-22 20:42:08 +02:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void sortByPriority() {
|
|
|
|
Map<String, int> priorityMap = {'High': 1, 'Medium': 2, 'Low': 3};
|
|
|
|
_todos.sort((a, b) => priorityMap[a.priority]!.compareTo(priorityMap[b.priority]!));
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void sortByStatus() {
|
2024-10-29 17:37:12 +01:00
|
|
|
_todos.sort((a, b) => a.isCompleted ? 1 : -1);
|
2024-10-22 20:42:08 +02:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|