import 'dart:convert'; 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 readListsFromDisk() async { Map js = await FileHandler.fileContent; 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)); } } todos = todoLists[selectedList]!.todos; notifyListeners(); } Map todoLists = {}; List 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 writeTodoChanges() async { Map js = {}; for (Todo t in todos) { js["${t.title}"] = { "checked": t.checked, "dueTime": t.dueTime.toString() }; } await FileHandler.saveList(js, selectedList); } void addTodo(String title, DateTime dueTime) async { todos.add(Todo( title: title, checked: false, widget: buildTodoWidget(title, false, dueTime), dueTime: dueTime)); await writeTodoChanges(); notifyListeners(); } void removeTodo(String title) async { Todo todo = todoByTitle(title); todos.remove(todo); await writeTodoChanges(); notifyListeners(); } void updateTodo(String oldTitle, String newTitle, DateTime dueTime) async { Todo todo = todoByTitle(oldTitle); todo.title = newTitle; todo.dueTime = dueTime; todo.widget = buildTodoWidget(newTitle, todo.checked, dueTime); await writeTodoChanges(); notifyListeners(); } void setTodoState(String title, bool checked) async { Todo todo = todoByTitle(title); todo.checked = checked; todo.widget = buildTodoWidget(title, checked, todo.dueTime); await writeTodoChanges(); notifyListeners(); } TodoListWidget buildTodoListWidget(String title) { return TodoListWidget(title: title); } TodoWidget buildTodoWidget(String title, bool checked, DateTime dueTime) { return TodoWidget(checked: checked, title: title, dueTime: dueTime); } Todo todoByTitle(String title) { for (Todo t in todos) { if (t.title == title) { return t; } } return Todo( title: "None", widget: const SizedBox(), checked: false, dueTime: DateTime.now()); } }