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