cpd_2024_todo/lib/buisness/ToDoProvider.dart

59 lines
1.4 KiB
Dart
Raw Permalink Normal View History

2024-10-22 10:38:22 +02:00
import 'package:flutter/material.dart';
2024-10-29 19:06:41 +01:00
import 'package:todo/database/PathProvider.dart';
2024-10-30 23:41:56 +01:00
import 'package:todo/database/SharedPref.dart';
2024-10-29 13:13:10 +01:00
import '../database/Sqlite.dart';
2024-10-22 10:38:22 +02:00
import 'ToDoItem.dart';
class ToDoProvider with ChangeNotifier {
2024-10-29 19:06:41 +01:00
final List<ToDoItem> _toDoList = [];
final safeHelper = Sqlite();
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();
}
2024-10-29 19:06:41 +01:00
void deleteItem(int id) {
_toDoList.remove(_toDoList.where((toDoID) => toDoID.id == id).first);
safeHelper.deleteToDoItem(id);
notifyListeners();
}
2024-10-22 10:38:22 +02:00
Future<void> _loadFromPrefs() async {
2024-10-29 13:13:10 +01:00
List<ToDoItem> items = await safeHelper.getToDoItems();
2024-10-29 19:06:41 +01:00
for (var item in items) {
2024-10-29 13:13:10 +01:00
_toDoList.add(item);
2024-10-29 19:06:41 +01:00
}
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();
}
}