cpd_2024_todo/lib/buisness/ToDoProvider.dart

58 lines
1.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:todo/database/PathProvider.dart';
import '../database/Sqlite.dart';
import 'ToDoItem.dart';
class ToDoProvider with ChangeNotifier {
final List<ToDoItem> _toDoList = [];
final safeHelper = Sqlite();
List<ToDoItem> get toDoList => _toDoList;
ToDoProvider() {
_loadFromPrefs();
}
Future<void> addToDo(ToDoItem item) async {
await safeHelper.insertToDoItem(item);
_toDoList.add(item);
notifyListeners();
}
void updateStatus(int id, String status) {
ToDoItem item = _toDoList.firstWhere((todo) => todo.id == id);
item.status = status;
safeHelper.updateToDoItem(item);
notifyListeners();
}
void deleteItem(int id) {
_toDoList.remove(_toDoList.where((toDoID) => toDoID.id == id).first);
safeHelper.deleteToDoItem(id);
notifyListeners();
}
Future<void> _loadFromPrefs() async {
List<ToDoItem> items = await safeHelper.getToDoItems();
for (var item in items) {
_toDoList.add(item);
}
notifyListeners();
}
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();
}
}