cpd_2024_todo/lib/buisness/ToDoProvider.dart

52 lines
1.2 KiB
Dart
Raw Normal View History

2024-10-22 10:38:22 +02:00
import 'package:flutter/material.dart';
2024-10-29 13:13:10 +01:00
import 'package:todo/database/andereAbspeicherung.dart';
import '../database/Sqlite.dart';
2024-10-22 10:38:22 +02:00
import 'ToDoItem.dart';
class ToDoProvider with ChangeNotifier {
List<ToDoItem> _toDoList = [];
2024-10-29 13:13:10 +01:00
final safeHelper = andereAbspeicherung();
2024-10-22 10:38:22 +02:00
List<ToDoItem> get toDoList => _toDoList;
ToDoProvider() {
_loadFromPrefs();
}
2024-10-29 13:13:10 +01:00
Future<void> addToDo(ToDoItem item) async {
await safeHelper.insertToDoItem(item);
2024-10-22 10:38:22 +02:00
_toDoList.add(item);
notifyListeners();
}
2024-10-29 13:13:10 +01:00
void updateStatus(int id, String status) {
ToDoItem item = _toDoList.firstWhere((todo) => todo.id == id);
item.status = status;
safeHelper.updateToDoItem(item);
2024-10-22 10:38:22 +02:00
notifyListeners();
}
Future<void> _loadFromPrefs() async {
2024-10-29 13:13:10 +01:00
List<ToDoItem> items = await safeHelper.getToDoItems();
items.forEach((item) {
_toDoList.add(item);
});
2024-10-22 10:38:22 +02:00
notifyListeners();
}
2024-10-29 13:13:10 +01:00
2024-10-22 10:38:22 +02:00
void sortByName() {
_toDoList.sort((a, b) => a.name.compareTo(b.name));
notifyListeners();
}
void sortByDueDate() {
_toDoList.sort((a, b) => a.dueDate.compareTo(b.dueDate));
notifyListeners();
}
void sortByStatus() {
_toDoList.sort((a, b) => a.status.compareTo(b.status));
notifyListeners();
}
}