This commit is contained in:
brausjonas
2024-09-15 19:51:29 +02:00
parent 8740525457
commit 77a411e4b7
8 changed files with 391 additions and 110 deletions
+34 -18
View File
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
@@ -10,15 +11,24 @@ import 'package:todoapp/modules/todowidget.dart';
class TodoProvider extends ChangeNotifier {
TodoProvider() {}
Future<void> readListsFromDisk() async {
Map js = await FileHandler.fileContent;
for (String key in js.keys) {
todoLists[key] = TodoList(title: key, widget: buildTodoListWidget(key), todos: []);
for (String key2 in js[key].keys) {
todoLists[key]!.todos.add(Todo(title: key2, widget: buildTodoWidget(key2, js[key][key2]), checked: js[key][key2]));
for (String key in js.keys) {
todoLists[key] =
TodoList(title: key, widget: buildTodoListWidget(key), todos: []);
Map decode = js[key];
for (var key2 in decode.keys) {
String title = key2;
bool checked = decode[key2]["checked"];
DateTime dueTime = DateTime.parse(decode[key2]["dueTime"]);
todoLists[key]!.todos.add(Todo(
title: title,
widget: buildTodoWidget(title, checked, dueTime),
checked: checked,
dueTime: dueTime));
}
}
@@ -79,17 +89,17 @@ class TodoProvider extends ChangeNotifier {
Map js = {};
for (Todo t in todos) {
js[t.title] = t.checked;
js["${t.title}"] = {
"checked": t.checked,
"dueTime": t.dueTime.toString()
};
}
print(js);
await FileHandler.saveList(js, selectedList);
}
void addTodo(String title) async {
void addTodo(String title, DateTime dueTime) async {
todos.add(Todo(
title: title, checked: false, widget: buildTodoWidget(title, false)));
title: title, checked: false, widget: buildTodoWidget(title, false, dueTime), dueTime: dueTime));
await writeTodoChanges();
notifyListeners();
@@ -103,10 +113,12 @@ class TodoProvider extends ChangeNotifier {
notifyListeners();
}
void updateTodo(String oldTitle, String newTitle) async {
void updateTodo(String oldTitle, String newTitle, DateTime dueTime) async {
Todo todo = todoByTitle(oldTitle);
todo.title = newTitle;
todo.widget = buildTodoWidget(newTitle, todo.checked);
todo.dueTime = dueTime;
todo.widget = buildTodoWidget(newTitle, todo.checked, dueTime);
await writeTodoChanges();
notifyListeners();
@@ -116,7 +128,7 @@ class TodoProvider extends ChangeNotifier {
Todo todo = todoByTitle(title);
todo.checked = checked;
todo.widget = buildTodoWidget(title, checked);
todo.widget = buildTodoWidget(title, checked, todo.dueTime);
await writeTodoChanges();
notifyListeners();
@@ -126,8 +138,8 @@ class TodoProvider extends ChangeNotifier {
return TodoListWidget(title: title);
}
TodoWidget buildTodoWidget(String title, bool checked) {
return TodoWidget(checked: checked, title: title);
TodoWidget buildTodoWidget(String title, bool checked, DateTime dueTime) {
return TodoWidget(checked: checked, title: title, dueTime: dueTime);
}
Todo todoByTitle(String title) {
@@ -136,6 +148,10 @@ class TodoProvider extends ChangeNotifier {
return t;
}
}
return Todo(title: "None", widget: const SizedBox(), checked: false);
return Todo(
title: "None",
widget: const SizedBox(),
checked: false,
dueTime: DateTime.now());
}
}