OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 import dartino.*; | |
6 | |
7 import java.io.BufferedReader; | |
8 import java.io.IOException; | |
9 import java.io.InputStreamReader; | |
10 | |
11 import java.lang.NumberFormatException; | |
12 | |
13 class TodoView { | |
14 public TodoView() { } | |
15 | |
16 public void showMenu() { | |
17 String menu = | |
18 "\n" + | |
19 " #### Todo Menu ####\n" + | |
20 "m\t-show this menu\n" + | |
21 "l\t-list todo items\n" + | |
22 "a\t-add todo item\n" + | |
23 "t\t-toggle todo item done/undone\n" + | |
24 "c\t-clear done items\n" + | |
25 "d\t-delete item\n" + | |
26 "q\t-quit application"; | |
27 System.out.println(menu); | |
28 } | |
29 | |
30 private String readLine() { | |
31 try { | |
32 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
33 String input = br.readLine(); | |
34 return input; | |
35 } catch (IOException exception) { | |
36 exception.printStackTrace(); | |
37 } | |
38 return null; | |
39 } | |
40 | |
41 public String getInput() { | |
42 return readLine(); | |
43 } | |
44 | |
45 // TODO(zarah): In a multithreaded setting this should be atomic or extend | |
46 // the service with a List<Item> getItems to use here instead. | |
47 public void listTodoItems() { | |
48 int count = TodoService.getNoItems(); | |
49 System.out.println("-------------------- ToDo's --------------------"); | |
50 System.out.printf("Listing %2d items\n", count); | |
51 for (int i = 0; i < count; ++i) { | |
52 TodoItem item = TodoService.getItem(i); | |
53 String done = item.getDone() ? "+" : " "; | |
54 System.out.printf("%2d: %s [%s] \n", item.getId(), item.getTitle(), done); | |
55 } | |
56 System.out.println("------------------------------------------------"); | |
57 } | |
58 | |
59 public void addTodoItem() { | |
60 System.out.println("Add Todo Item:"); | |
61 System.out.print("Title: "); | |
62 String title = readLine(); | |
63 System.out.println(); | |
64 | |
65 int size = 56 + BoxStringBuilder.kSize + title.length(); | |
66 MessageBuilder messageBuilder = new MessageBuilder(size); | |
67 BoxStringBuilder stringBuilder = new BoxStringBuilder(); | |
68 messageBuilder.initRoot(stringBuilder, BoxStringBuilder.kSize); | |
69 stringBuilder.setS(title); | |
70 TodoService.createItem(stringBuilder); | |
71 } | |
72 | |
73 public void toggleTodoItem() { | |
74 System.out.print("[t] Enter id: "); | |
75 String idString = readLine(); | |
76 try { | |
77 int id = Integer.parseInt(idString); | |
78 TodoService.toggle(id); | |
79 } catch (NumberFormatException exception) { | |
80 // Ignore bad input. | |
81 } | |
82 } | |
83 | |
84 public void clearDoneItems() { | |
85 TodoService.clearItems(); | |
86 } | |
87 | |
88 public void deleteItem() { | |
89 System.out.print("[d] Enter id: "); | |
90 String idString = readLine(); | |
91 try { | |
92 int id = Integer.parseInt(idString); | |
93 TodoService.deleteItem(id); | |
94 } catch (NumberFormatException exception) { | |
95 // Ignore bad input. | |
96 } | |
97 } | |
98 } | |
OLD | NEW |