Initial commit

This commit is contained in:
brausjonas
2024-09-15 17:03:37 +02:00
commit 8740525457
42 changed files with 1515 additions and 0 deletions
+141
View File
@@ -0,0 +1,141 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todoapp/elems/filehandler.dart';
import 'package:todoapp/elems/todo.dart';
import 'package:todoapp/elems/todolist.dart';
import 'package:todoapp/modules/todolistwidget.dart';
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]));
}
}
todos = todoLists[selectedList]!.todos;
notifyListeners();
}
Map<String, TodoList> todoLists = {};
List<Todo> todos = [];
String selectedList = "Personal";
void renameList(String oldTitle, String newTitle) async {
TodoList tempList = todoLists[oldTitle]!;
tempList.title = newTitle;
tempList.widget = buildTodoListWidget(newTitle);
todoLists.remove(oldTitle);
todoLists[newTitle] = tempList;
selectedList = newTitle;
todos = todoLists[newTitle]!.todos;
await FileHandler.renameList(oldTitle, newTitle);
notifyListeners();
}
void removeList(String title) async {
todoLists.remove(title);
selectedList = "Personal";
todos = todoLists["Personal"]!.todos;
await FileHandler.removeList(title);
notifyListeners();
}
void addList(String title) async {
TodoList list =
TodoList(title: title, widget: buildTodoListWidget(title), todos: []);
todoLists[title] = list;
await FileHandler.addList(title);
notifyListeners();
}
void changeList(String title) {
if (!todoLists.containsKey(title)) {
return;
}
selectedList = title;
todos = todoLists[title]!.todos;
notifyListeners();
}
Future<void> writeTodoChanges() async {
Map js = {};
for (Todo t in todos) {
js[t.title] = t.checked;
}
print(js);
await FileHandler.saveList(js, selectedList);
}
void addTodo(String title) async {
todos.add(Todo(
title: title, checked: false, widget: buildTodoWidget(title, false)));
await writeTodoChanges();
notifyListeners();
}
void removeTodo(String title) async {
Todo todo = todoByTitle(title);
todos.remove(todo);
await writeTodoChanges();
notifyListeners();
}
void updateTodo(String oldTitle, String newTitle) async {
Todo todo = todoByTitle(oldTitle);
todo.title = newTitle;
todo.widget = buildTodoWidget(newTitle, todo.checked);
await writeTodoChanges();
notifyListeners();
}
void setTodoState(String title, bool checked) async {
Todo todo = todoByTitle(title);
todo.checked = checked;
todo.widget = buildTodoWidget(title, checked);
await writeTodoChanges();
notifyListeners();
}
TodoListWidget buildTodoListWidget(String title) {
return TodoListWidget(title: title);
}
TodoWidget buildTodoWidget(String title, bool checked) {
return TodoWidget(checked: checked, title: title);
}
Todo todoByTitle(String title) {
for (Todo t in todos) {
if (t.title == title) {
return t;
}
}
return Todo(title: "None", widget: const SizedBox(), checked: false);
}
}