From f67eaeec7fc463d15388c3dc7beeae416ab31254 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hallvard=20Tr=C3=A6tteberg?= <hal@ntnu.no>
Date: Thu, 1 Oct 2020 10:13:50 +0000
Subject: [PATCH] =?UTF-8?q?Fikset=20kontrolleren=20s=C3=A5=20den=20virker.?=
 =?UTF-8?q?..?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../main/java/todolist/ui/TodoController.java | 33 ++++++++++---------
 .../todolist/ui/sample-todolist.json          | 19 +++++++----
 .../resources/todolist/ui/test-todolist.json  | 20 +++++++----
 3 files changed, 43 insertions(+), 29 deletions(-)

diff --git a/todolist/fxui/src/main/java/todolist/ui/TodoController.java b/todolist/fxui/src/main/java/todolist/ui/TodoController.java
index 6109569..11b3877 100644
--- a/todolist/fxui/src/main/java/todolist/ui/TodoController.java
+++ b/todolist/fxui/src/main/java/todolist/ui/TodoController.java
@@ -23,18 +23,19 @@ import javafx.scene.control.ListView;
 import javafx.scene.control.TextField;
 import todolist.core.TodoItem;
 import todolist.core.TodoList;
+import todolist.core.TodoModel;
 import todolist.json.TodoPersistence;
 
 public class TodoController {
 
   private static final String todoListWithTwoItems =
-      "{\"items\":[{\"text\":\"Øl\",\"checked\":false},{\"text\":\"Pizza\",\"checked\":true}]}";
+      "{\"lists\":[{\"name\":\"todo\",\"items\":[{\"text\":\"item1\",\"checked\":false},{\"text\":\"item2\",\"checked\":true,\"deadline\":\"2020-10-01T14:53:11\"}]}]}";
 
-  private TodoList todoList;
+  private TodoModel todoModel;
 
   // makes class more testable
   TodoList getTodoList() {
-    return todoList;
+    return todoModel.iterator().next();
   }
 
   private TodoPersistence todoPersistence = new TodoPersistence();
@@ -84,12 +85,14 @@ public class TodoController {
       reader = new StringReader(todoListWithTwoItems);
     }
     try {
-      todoList = todoPersistence.readTodoList(reader);
+      todoModel = todoPersistence.readTodoModel(reader);
     } catch (IOException e) {
-      todoList = new TodoList(
-        todoList.createTodoItem().text("Øl"),
-        todoList.createTodoItem().text("Pizza")
+      todoModel = new TodoModel();
+      TodoList todoList = new TodoList(
+        new TodoItem().text("Øl"),
+         new TodoItem().text("Pizza")
       );
+      todoModel.addTodoList(todoList);
     } finally {
       try {
         if (reader != null) {
@@ -110,11 +113,11 @@ public class TodoController {
     // kobler data til list-controll
     updateTodoListView();
     updateTodoListButtons();
-    todoList.addTodoListListener(todoList -> {
+    getTodoList().addTodoListListener(todoList -> {
       autoSaveTodoList();
       updateTodoListView();
     });
-    TodoItemListCellDragHandler dragHandler = new TodoItemListCellDragHandler(todoList);
+    TodoItemListCellDragHandler dragHandler = new TodoItemListCellDragHandler(getTodoList());
     todoListView.setCellFactory(listView -> {
       TodoItemListCell listCell = new TodoItemListCell();
       dragHandler.registerHandlers(listCell);
@@ -127,8 +130,8 @@ public class TodoController {
 
   protected void updateTodoListView() {
     List<TodoItem> items = new ArrayList<>();
-    items.addAll(todoList.getUncheckedTodoItems());
-    items.addAll(todoList.getCheckedTodoItems());
+    items.addAll(getTodoList().getUncheckedTodoItems());
+    items.addAll(getTodoList().getCheckedTodoItems());
     TodoItem selectedItem = todoListView.getSelectionModel().getSelectedItem();
     todoListView.getItems().setAll(items);
     // keep selection
@@ -176,9 +179,9 @@ public class TodoController {
 
   @FXML
   void handleNewTodoItemAction() {
-    TodoItem item = todoList.createTodoItem();
+    TodoItem item = getTodoList().createTodoItem();
     item.setText(newTodoItemText.getText());
-    todoList.addTodoItem(item);
+    getTodoList().addTodoItem(item);
     todoListView.getSelectionModel().select(item);
   }
 
@@ -187,7 +190,7 @@ public class TodoController {
     int index = todoListView.getSelectionModel().getSelectedIndex();
     TodoItem item = todoListView.getItems().get(index);
     if (item != null) {
-      todoList.removeTodoItem(item);
+      getTodoList().removeTodoItem(item);
       selectWithinBounds(index);
     }
   }
@@ -217,7 +220,7 @@ public class TodoController {
     if (userTodoListPath != null) {
       Path path = Paths.get(System.getProperty("user.home"), userTodoListPath);
       try (Writer writer = new FileWriter(path.toFile(), StandardCharsets.UTF_8)) {
-        todoPersistence.writeTodoList(todoList, writer);
+        todoPersistence.writeTodoModel(todoModel, writer);
       } catch (IOException e) {
         System.err.println("Fikk ikke skrevet til todolist.json på hjemmeområdet");
       }
diff --git a/todolist/fxui/src/main/resources/todolist/ui/sample-todolist.json b/todolist/fxui/src/main/resources/todolist/ui/sample-todolist.json
index 1ec336b..4bb9777 100644
--- a/todolist/fxui/src/main/resources/todolist/ui/sample-todolist.json
+++ b/todolist/fxui/src/main/resources/todolist/ui/sample-todolist.json
@@ -1,12 +1,17 @@
 {
-    "items": [
+    "lists": [
         {
-            "text": "Øl",
-            "checked": false
-        },
-        {
-            "text": "Pizza",
-            "checked": false
+            "name": "todo",
+            "items": [
+                {
+                    "text": "Øl",
+                    "checked": false
+                },
+                {
+                    "text": "Pizza",
+                    "checked": false
+                }
+            ]
         }
     ]
 }
\ No newline at end of file
diff --git a/todolist/fxui/src/test/resources/todolist/ui/test-todolist.json b/todolist/fxui/src/test/resources/todolist/ui/test-todolist.json
index 1d36fe7..b30d9b3 100644
--- a/todolist/fxui/src/test/resources/todolist/ui/test-todolist.json
+++ b/todolist/fxui/src/test/resources/todolist/ui/test-todolist.json
@@ -1,12 +1,18 @@
 {
-    "items": [
+    "lists": [
         {
-            "text": "Item 1",
-            "checked": true
-        },
-        {
-            "text": "Item 2",
-            "checked": false
+            "name": "todo",
+            "deadline": "2020-10-01T14:53:11",
+            "items": [
+                {
+                    "text": "Item 1",
+                    "checked": true
+                },
+                {
+                    "text": "Item 2",
+                    "checked": false
+                }
+            ]
         }
     ]
 }
\ No newline at end of file
-- 
GitLab