|
1 | 1 | package client.usecases.chat; |
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
3 | 6 | public class ChatController { |
4 | 7 |
|
5 | 8 | private static final int MAX_MESSAGE_LENGTH = 256; |
| 9 | + private static final int MAX_HISTORY_SIZE = 50; |
6 | 10 |
|
7 | 11 | private final StringBuilder buffer = new StringBuilder(); |
8 | | - |
9 | 12 | private int cursor = 0; |
10 | 13 | private boolean open = false; |
11 | 14 |
|
| 15 | + private final List<String> history = new ArrayList<>(); |
| 16 | + private int historyIndex = -1; |
| 17 | + private String currentDraft = ""; |
| 18 | + |
12 | 19 | private final SendChatMessageController sendController; |
13 | 20 |
|
14 | 21 | public ChatController(SendChatMessageController sendController) { |
15 | 22 | this.sendController = sendController; |
16 | 23 | } |
17 | 24 |
|
| 25 | + public void moveHistoryUp() { |
| 26 | + if (history.isEmpty()) return; |
| 27 | + |
| 28 | + if (historyIndex == -1) { |
| 29 | + currentDraft = buffer.toString(); |
| 30 | + } |
| 31 | + |
| 32 | + if (historyIndex < history.size() - 1) { |
| 33 | + historyIndex++; |
| 34 | + loadHistoryEntry(history.get(history.size() - 1 - historyIndex)); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public void moveHistoryDown() { |
| 39 | + if (historyIndex == -1) return; |
| 40 | + |
| 41 | + historyIndex--; |
| 42 | + |
| 43 | + if (historyIndex == -1) { |
| 44 | + loadHistoryEntry(currentDraft); |
| 45 | + } else { |
| 46 | + loadHistoryEntry(history.get(history.size() - 1 - historyIndex)); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private void loadHistoryEntry(String text) { |
| 51 | + buffer.setLength(0); |
| 52 | + buffer.append(text); |
| 53 | + cursor = buffer.length(); |
| 54 | + } |
| 55 | + |
18 | 56 | public void openChat() { |
19 | 57 | open = true; |
| 58 | + historyIndex = -1; |
| 59 | + currentDraft = ""; |
20 | 60 | sendController.onOpenChat(); |
21 | 61 | } |
22 | 62 |
|
23 | 63 | public void closeChat() { |
24 | 64 | open = false; |
25 | 65 | buffer.setLength(0); |
26 | 66 | cursor = 0; |
| 67 | + historyIndex = -1; |
27 | 68 | sendController.onCloseChat(); |
28 | 69 | } |
29 | 70 |
|
30 | | - public boolean isOpen() { |
31 | | - return open; |
32 | | - } |
| 71 | + public void send() { |
| 72 | + String msg = buffer.toString().trim(); |
33 | 73 |
|
34 | | - public String getText() { |
35 | | - return buffer.toString(); |
36 | | - } |
| 74 | + if (!msg.isEmpty()) { |
| 75 | + sendController.onSendMessage(msg); |
37 | 76 |
|
38 | | - public int getCursor() { |
39 | | - return cursor; |
40 | | - } |
| 77 | + if (history.isEmpty() || !history.get(history.size() - 1).equals(msg)) { |
| 78 | + history.add(msg); |
| 79 | + if (history.size() > MAX_HISTORY_SIZE) { |
| 80 | + history.remove(0); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
41 | 84 |
|
42 | | - // ========================= |
43 | | - // Editing |
44 | | - // ========================= |
| 85 | + closeChat(); |
| 86 | + } |
45 | 87 |
|
46 | 88 | public void insert(char c) { |
47 | | - |
48 | 89 | if (buffer.length() >= MAX_MESSAGE_LENGTH) return; |
49 | 90 |
|
| 91 | + if (historyIndex != -1) { |
| 92 | + historyIndex = -1; |
| 93 | + } |
| 94 | + |
50 | 95 | buffer.insert(cursor, c); |
51 | 96 | cursor++; |
52 | 97 | } |
53 | 98 |
|
54 | 99 | public void backspace() { |
55 | | - |
56 | 100 | if (cursor == 0) return; |
57 | | - |
58 | 101 | buffer.deleteCharAt(cursor - 1); |
59 | 102 | cursor--; |
| 103 | + if (historyIndex != -1) historyIndex = -1; |
60 | 104 | } |
61 | 105 |
|
62 | 106 | public void delete() { |
63 | | - |
64 | 107 | if (cursor >= buffer.length()) return; |
65 | | - |
66 | 108 | buffer.deleteCharAt(cursor); |
| 109 | + if (historyIndex != -1) historyIndex = -1; |
67 | 110 | } |
68 | 111 |
|
69 | 112 | public void moveLeft() { |
70 | | - |
71 | 113 | if (cursor > 0) cursor--; |
72 | 114 | } |
73 | 115 |
|
74 | 116 | public void moveRight() { |
75 | | - |
76 | 117 | if (cursor < buffer.length()) cursor++; |
77 | 118 | } |
78 | 119 |
|
79 | | - public void send() { |
80 | | - |
81 | | - String msg = buffer.toString().trim(); |
| 120 | + public boolean isOpen() { |
| 121 | + return open; |
| 122 | + } |
82 | 123 |
|
83 | | - if (!msg.isEmpty()) { |
84 | | - sendController.onSendMessage(msg); |
85 | | - } |
| 124 | + public String getText() { |
| 125 | + return buffer.toString(); |
| 126 | + } |
86 | 127 |
|
87 | | - closeChat(); |
| 128 | + public int getCursor() { |
| 129 | + return cursor; |
88 | 130 | } |
89 | 131 | } |
0 commit comments