import 'package:flutter/foundation.dart'; import '../models/todo.dart'; import '../db.dart'; const List categories = ['Personal', 'Work', 'Shopping', 'Health', 'Other']; const List priorityKeys = ['low', 'medium', 'high']; const Map priorityColors = { 'low': 0xFF4CAF50, 'medium': 0xFFFF9800, 'high': 0xFFF44336, }; enum TodoFilter { all, active, completed } class TodoState extends ChangeNotifier { List _todos = []; TodoFilter _filter = TodoFilter.all; List get todos => _todos; TodoFilter get filter => _filter; List get filteredTodos { switch (_filter) { case TodoFilter.active: return _todos.where((t) => !t.completed).toList(); case TodoFilter.completed: return _todos.where((t) => t.completed).toList(); case TodoFilter.all: return _todos; } } Map get counts => { 'all': _todos.length, 'active': _todos.where((t) => !t.completed).length, 'completed': _todos.where((t) => t.completed).length, }; void setTodos(List todos) { _todos = todos; notifyListeners(); } void setFilter(TodoFilter f) { _filter = f; notifyListeners(); } Future addTodo(String title, String category, String priority) async { final id = await TodoDatabase.insertTodo(title, category, priority); _todos.insert(0, Todo( id: id, title: title, category: category, priority: priority, createdAt: DateTime.now().millisecondsSinceEpoch ~/ 1000, )); notifyListeners(); } Future toggleTodo(int id) async { final idx = _todos.indexWhere((t) => t.id == id); if (idx == -1) return; final todo = _todos[idx]; final newCompleted = !todo.completed; _todos[idx] = todo.copyWith(completed: newCompleted); notifyListeners(); await TodoDatabase.toggleTodo(id, newCompleted); } Future updateTodo(int id, String title, String category, String priority) async { final idx = _todos.indexWhere((t) => t.id == id); if (idx == -1) return; _todos[idx] = _todos[idx].copyWith( title: title, category: category, priority: priority, ); notifyListeners(); await TodoDatabase.updateTodo(id, title, category, priority); } Future deleteTodo(int id) async { _todos.removeWhere((t) => t.id == id); notifyListeners(); await TodoDatabase.deleteTodo(id); } }