89 lines
2.4 KiB
Dart
89 lines
2.4 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import '../models/todo.dart';
|
|
import '../db.dart';
|
|
|
|
const List<String> categories = ['Personal', 'Work', 'Shopping', 'Health', 'Other'];
|
|
const List<String> priorityKeys = ['low', 'medium', 'high'];
|
|
const Map<String, int> priorityColors = {
|
|
'low': 0xFF4CAF50,
|
|
'medium': 0xFFFF9800,
|
|
'high': 0xFFF44336,
|
|
};
|
|
|
|
enum TodoFilter { all, active, completed }
|
|
|
|
class TodoState extends ChangeNotifier {
|
|
List<Todo> _todos = [];
|
|
TodoFilter _filter = TodoFilter.all;
|
|
|
|
List<Todo> get todos => _todos;
|
|
TodoFilter get filter => _filter;
|
|
|
|
List<Todo> 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<String, int> get counts => {
|
|
'all': _todos.length,
|
|
'active': _todos.where((t) => !t.completed).length,
|
|
'completed': _todos.where((t) => t.completed).length,
|
|
};
|
|
|
|
void setTodos(List<Todo> todos) {
|
|
_todos = todos;
|
|
notifyListeners();
|
|
}
|
|
|
|
void setFilter(TodoFilter f) {
|
|
_filter = f;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> 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<void> 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<void> 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<void> deleteTodo(int id) async {
|
|
_todos.removeWhere((t) => t.id == id);
|
|
notifyListeners();
|
|
await TodoDatabase.deleteTodo(id);
|
|
}
|
|
}
|