diff --git a/todolist/fxui/src/main/java/todolist/ui/TodoController.java b/todolist/fxui/src/main/java/todolist/ui/TodoController.java index 61095691bd0050dfa689f2c229c5aec990be449a..11b38770161c61c63f35e156aa6aa93a8ebbd21b 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 1ec336b1da4d00797bbfdc2117be5f7f63c824cc..4bb977756cffed7ed854defb64a30e6b88830407 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 1d36fe7ac580d1d86a95548f3e816ee48152911c..b30d9b39f84bd9f86ad72c30e74adbc0fb9b1e71 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