Skip to content

Commit ced77f7

Browse files
committed
add message history
1 parent 2a9fdc5 commit ced77f7

2 files changed

Lines changed: 80 additions & 27 deletions

File tree

voxels-client/src/main/java/client/usecases/chat/ChatComponent.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public void onKeyPressed(KeyEvent e) {
6060
case ESCAPE:
6161
controller.closeChat();
6262
return;
63+
64+
case ARROW_UP:
65+
controller.moveHistoryUp();
66+
return;
67+
68+
case ARROW_DOWN:
69+
controller.moveHistoryDown();
70+
return;
71+
72+
default:
73+
return;
6374
}
6475
}
6576

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,131 @@
11
package client.usecases.chat;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
public class ChatController {
47

58
private static final int MAX_MESSAGE_LENGTH = 256;
9+
private static final int MAX_HISTORY_SIZE = 50;
610

711
private final StringBuilder buffer = new StringBuilder();
8-
912
private int cursor = 0;
1013
private boolean open = false;
1114

15+
private final List<String> history = new ArrayList<>();
16+
private int historyIndex = -1;
17+
private String currentDraft = "";
18+
1219
private final SendChatMessageController sendController;
1320

1421
public ChatController(SendChatMessageController sendController) {
1522
this.sendController = sendController;
1623
}
1724

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+
1856
public void openChat() {
1957
open = true;
58+
historyIndex = -1;
59+
currentDraft = "";
2060
sendController.onOpenChat();
2161
}
2262

2363
public void closeChat() {
2464
open = false;
2565
buffer.setLength(0);
2666
cursor = 0;
67+
historyIndex = -1;
2768
sendController.onCloseChat();
2869
}
2970

30-
public boolean isOpen() {
31-
return open;
32-
}
71+
public void send() {
72+
String msg = buffer.toString().trim();
3373

34-
public String getText() {
35-
return buffer.toString();
36-
}
74+
if (!msg.isEmpty()) {
75+
sendController.onSendMessage(msg);
3776

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+
}
4184

42-
// =========================
43-
// Editing
44-
// =========================
85+
closeChat();
86+
}
4587

4688
public void insert(char c) {
47-
4889
if (buffer.length() >= MAX_MESSAGE_LENGTH) return;
4990

91+
if (historyIndex != -1) {
92+
historyIndex = -1;
93+
}
94+
5095
buffer.insert(cursor, c);
5196
cursor++;
5297
}
5398

5499
public void backspace() {
55-
56100
if (cursor == 0) return;
57-
58101
buffer.deleteCharAt(cursor - 1);
59102
cursor--;
103+
if (historyIndex != -1) historyIndex = -1;
60104
}
61105

62106
public void delete() {
63-
64107
if (cursor >= buffer.length()) return;
65-
66108
buffer.deleteCharAt(cursor);
109+
if (historyIndex != -1) historyIndex = -1;
67110
}
68111

69112
public void moveLeft() {
70-
71113
if (cursor > 0) cursor--;
72114
}
73115

74116
public void moveRight() {
75-
76117
if (cursor < buffer.length()) cursor++;
77118
}
78119

79-
public void send() {
80-
81-
String msg = buffer.toString().trim();
120+
public boolean isOpen() {
121+
return open;
122+
}
82123

83-
if (!msg.isEmpty()) {
84-
sendController.onSendMessage(msg);
85-
}
124+
public String getText() {
125+
return buffer.toString();
126+
}
86127

87-
closeChat();
128+
public int getCursor() {
129+
return cursor;
88130
}
89131
}

0 commit comments

Comments
 (0)