Skip to content
Snippets Groups Projects
Commit f67eaeec authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Fikset kontrolleren så den virker...

parent 8e00b1ec
No related branches found
No related tags found
No related merge requests found
...@@ -23,18 +23,19 @@ import javafx.scene.control.ListView; ...@@ -23,18 +23,19 @@ import javafx.scene.control.ListView;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import todolist.core.TodoItem; import todolist.core.TodoItem;
import todolist.core.TodoList; import todolist.core.TodoList;
import todolist.core.TodoModel;
import todolist.json.TodoPersistence; import todolist.json.TodoPersistence;
public class TodoController { public class TodoController {
private static final String todoListWithTwoItems = 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 // makes class more testable
TodoList getTodoList() { TodoList getTodoList() {
return todoList; return todoModel.iterator().next();
} }
private TodoPersistence todoPersistence = new TodoPersistence(); private TodoPersistence todoPersistence = new TodoPersistence();
...@@ -84,12 +85,14 @@ public class TodoController { ...@@ -84,12 +85,14 @@ public class TodoController {
reader = new StringReader(todoListWithTwoItems); reader = new StringReader(todoListWithTwoItems);
} }
try { try {
todoList = todoPersistence.readTodoList(reader); todoModel = todoPersistence.readTodoModel(reader);
} catch (IOException e) { } catch (IOException e) {
todoList = new TodoList( todoModel = new TodoModel();
todoList.createTodoItem().text("Øl"), TodoList todoList = new TodoList(
todoList.createTodoItem().text("Pizza") new TodoItem().text("Øl"),
new TodoItem().text("Pizza")
); );
todoModel.addTodoList(todoList);
} finally { } finally {
try { try {
if (reader != null) { if (reader != null) {
...@@ -110,11 +113,11 @@ public class TodoController { ...@@ -110,11 +113,11 @@ public class TodoController {
// kobler data til list-controll // kobler data til list-controll
updateTodoListView(); updateTodoListView();
updateTodoListButtons(); updateTodoListButtons();
todoList.addTodoListListener(todoList -> { getTodoList().addTodoListListener(todoList -> {
autoSaveTodoList(); autoSaveTodoList();
updateTodoListView(); updateTodoListView();
}); });
TodoItemListCellDragHandler dragHandler = new TodoItemListCellDragHandler(todoList); TodoItemListCellDragHandler dragHandler = new TodoItemListCellDragHandler(getTodoList());
todoListView.setCellFactory(listView -> { todoListView.setCellFactory(listView -> {
TodoItemListCell listCell = new TodoItemListCell(); TodoItemListCell listCell = new TodoItemListCell();
dragHandler.registerHandlers(listCell); dragHandler.registerHandlers(listCell);
...@@ -127,8 +130,8 @@ public class TodoController { ...@@ -127,8 +130,8 @@ public class TodoController {
protected void updateTodoListView() { protected void updateTodoListView() {
List<TodoItem> items = new ArrayList<>(); List<TodoItem> items = new ArrayList<>();
items.addAll(todoList.getUncheckedTodoItems()); items.addAll(getTodoList().getUncheckedTodoItems());
items.addAll(todoList.getCheckedTodoItems()); items.addAll(getTodoList().getCheckedTodoItems());
TodoItem selectedItem = todoListView.getSelectionModel().getSelectedItem(); TodoItem selectedItem = todoListView.getSelectionModel().getSelectedItem();
todoListView.getItems().setAll(items); todoListView.getItems().setAll(items);
// keep selection // keep selection
...@@ -176,9 +179,9 @@ public class TodoController { ...@@ -176,9 +179,9 @@ public class TodoController {
@FXML @FXML
void handleNewTodoItemAction() { void handleNewTodoItemAction() {
TodoItem item = todoList.createTodoItem(); TodoItem item = getTodoList().createTodoItem();
item.setText(newTodoItemText.getText()); item.setText(newTodoItemText.getText());
todoList.addTodoItem(item); getTodoList().addTodoItem(item);
todoListView.getSelectionModel().select(item); todoListView.getSelectionModel().select(item);
} }
...@@ -187,7 +190,7 @@ public class TodoController { ...@@ -187,7 +190,7 @@ public class TodoController {
int index = todoListView.getSelectionModel().getSelectedIndex(); int index = todoListView.getSelectionModel().getSelectedIndex();
TodoItem item = todoListView.getItems().get(index); TodoItem item = todoListView.getItems().get(index);
if (item != null) { if (item != null) {
todoList.removeTodoItem(item); getTodoList().removeTodoItem(item);
selectWithinBounds(index); selectWithinBounds(index);
} }
} }
...@@ -217,7 +220,7 @@ public class TodoController { ...@@ -217,7 +220,7 @@ public class TodoController {
if (userTodoListPath != null) { if (userTodoListPath != null) {
Path path = Paths.get(System.getProperty("user.home"), userTodoListPath); Path path = Paths.get(System.getProperty("user.home"), userTodoListPath);
try (Writer writer = new FileWriter(path.toFile(), StandardCharsets.UTF_8)) { try (Writer writer = new FileWriter(path.toFile(), StandardCharsets.UTF_8)) {
todoPersistence.writeTodoList(todoList, writer); todoPersistence.writeTodoModel(todoModel, writer);
} catch (IOException e) { } catch (IOException e) {
System.err.println("Fikk ikke skrevet til todolist.json på hjemmeområdet"); System.err.println("Fikk ikke skrevet til todolist.json på hjemmeområdet");
} }
......
{ {
"items": [ "lists": [
{ {
"text": "Øl", "name": "todo",
"checked": false "items": [
}, {
{ "text": "Øl",
"text": "Pizza", "checked": false
"checked": false },
{
"text": "Pizza",
"checked": false
}
]
} }
] ]
} }
\ No newline at end of file
{ {
"items": [ "lists": [
{ {
"text": "Item 1", "name": "todo",
"checked": true "deadline": "2020-10-01T14:53:11",
}, "items": [
{ {
"text": "Item 2", "text": "Item 1",
"checked": false "checked": true
},
{
"text": "Item 2",
"checked": false
}
]
} }
] ]
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment