Skip to content

Commit 3140a30

Browse files
committed
Made changes so that chat buttons also display the time of the last message
1 parent 6fa9584 commit 3140a30

File tree

3 files changed

+82
-26
lines changed

3 files changed

+82
-26
lines changed

src/main/java/screens/app_screen/AppScreen.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33

44
import entities.chat.Chat;
55
import screens.chat_screen.ChatView;
6-
import use_cases.app_screen_use_case.AppScreenController;
7-
import use_cases.app_screen_use_case.AppScreenPresenter;
8-
import use_cases.app_screen_use_case.ChatName;
9-
import use_cases.app_screen_use_case.Refresh;
6+
import use_cases.app_screen_use_case.*;
107

118
import javax.swing.*;
129
import java.awt.*;
10+
import java.time.LocalDateTime;
1311
import java.util.ArrayList;
1412

15-
public class AppScreen implements AppScreenPresenter, AppScreenController, ChatName, Refresh {
13+
public class AppScreen implements AppScreenPresenter, AppScreenController, ChatName, Refresh, LastUpdate {
1614

1715
private final JFrame jFrame;
1816
private JScrollPane jScrollPane;
@@ -45,7 +43,7 @@ public AppScreen(String currentUsername, ArrayList<Chat> chats) {
4543

4644
// adding the action listeners for the +private-chat and +group-chat buttons
4745
addPrivateChat.addActionListener(e -> {
48-
ChatView newChat = new ChatView(currentUsername, true, "");
46+
ChatView newChat = new ChatView(true);
4947
newChat.chatDisplay();
5048

5149
});
@@ -89,20 +87,9 @@ public void displayAppScreen(){
8987
for (int i = this.chats.size() - 1; i > -1; i--) {
9088

9189
String chatName = getChatName(this.chats.get(i));
92-
JButton b = new JButton(chatName);
93-
b.setPreferredSize(new Dimension(280, 50));
94-
JLabel jLabel = new JLabel("time");
95-
jLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
96-
jLabel.setFont(new Font(null, Font.BOLD, 11));
97-
b.add(jLabel);
98-
99-
// defines the action of opening a chat when a chat is clicked on
100-
b.addActionListener(e -> {
101-
102-
ChatView newChat = new ChatView(currentUsername, false, chatName);
103-
newChat.chatDisplay();
104-
});
105-
jPanel.add(b);
90+
LocalDateTime lastUpdated = getLastUpdatedTime(this.chats.get(i));
91+
92+
jPanel.add(ChatButton.createButton(chatName, currentUsername, lastUpdated));
10693
}
10794

10895
jPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
@@ -119,6 +106,16 @@ public void displayAppScreen(){
119106

120107
}
121108

109+
/**
110+
* Return the date and time of the last message in a chat
111+
* @param chat The given chat
112+
* @return date and time of last update
113+
*/
114+
@Override
115+
public LocalDateTime getLastUpdatedTime(Chat chat) {
116+
return chat.getLastUpdated();
117+
}
118+
122119
/**
123120
Make the chat list scrollable
124121
@param jPanel The panel containing the chats
@@ -177,6 +174,7 @@ public void addNewChat(Chat chat){
177174

178175
/**
179176
* Update the order of chats that appear on screen if there was a change to conversation history
177+
* This should not be called if chatID is not an ID of an existing chat that the current user has
180178
* @param chatID The ID of the given chat
181179
*/
182180
@Override
@@ -192,20 +190,23 @@ public void updateScreen(String chatID) {
192190
}
193191

194192
/**
195-
* Return true if the given chat as an update to its conversation history
193+
* Return true if the given existing chat has an update to its conversation history
196194
* @param chatID The ID of the given chat
197195
* @return true/false
198196
*/
199197
@Override
200198
public boolean hasUpdate(String chatID) {
201199
Chat chat = getChat(chatID);
202-
return this.chats.get(this.chats.size() - 1) != chat;
200+
if (!(this.chats.isEmpty())) {
201+
return !(this.chats.get(this.chats.size() - 1).equals(chat));
202+
}
203+
return true;
203204
}
204205

205206
/**
206-
* Get the chat object given its chat ID
207-
* @param chatID The ID of the chat
208-
* @return The chat with the given ID
207+
* Get the existing chat object given its chat ID
208+
* @param chatID The ID of the existing chat
209+
* @return The existing chat with the given ID
209210
*/
210211
@Override
211212
public Chat getChat(String chatID) {
@@ -214,7 +215,7 @@ public Chat getChat(String chatID) {
214215
return chat;
215216
}
216217
}
217-
throw new RuntimeException("Current user is not part of this chat");
218+
throw new RuntimeException("User does not currently have this chat");
218219
}
219220

220221
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package use_cases.app_screen_use_case;
2+
3+
import screens.chat_screen.ChatView;
4+
5+
import javax.swing.*;
6+
import java.awt.*;
7+
import java.awt.event.ActionEvent;
8+
import java.awt.event.ActionListener;
9+
import java.time.LocalDateTime;
10+
11+
public class ChatButton {
12+
13+
14+
/**
15+
* Create and return a button for an existing chat object
16+
* @param chatName The name of the chat
17+
* @param currentUsername The username of the current user
18+
* @param lastUpdated The time of the last update to a chat's conversation history
19+
* @return The button created
20+
*/
21+
public static JButton createButton(String chatName, String currentUsername, LocalDateTime lastUpdated){
22+
JButton jButton = new JButton(chatName);
23+
jButton.setPreferredSize(new Dimension(280, 50));
24+
25+
JLabel jLabel;
26+
if (lastUpdated != null){
27+
jLabel = new JLabel(String.valueOf(lastUpdated.toLocalDate()));
28+
}
29+
else{
30+
jLabel = new JLabel();
31+
}
32+
jLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
33+
jLabel.setFont(new Font(null, Font.BOLD, 11));
34+
jButton.add(jLabel);
35+
36+
// defines the action of opening a chat when a chat is clicked on
37+
jButton.addActionListener(new ActionListener() {
38+
39+
@Override
40+
public void actionPerformed(ActionEvent e) {
41+
ChatView chatView = new ChatView(false);
42+
}
43+
});
44+
return jButton;
45+
}
46+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package use_cases.app_screen_use_case;
2+
3+
import entities.chat.Chat;
4+
5+
import java.time.LocalDateTime;
6+
7+
public interface LastUpdate {
8+
LocalDateTime getLastUpdatedTime(Chat chat);
9+
}

0 commit comments

Comments
 (0)