3
3
4
4
import entities .chat .Chat ;
5
5
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 .*;
10
7
11
8
import javax .swing .*;
12
9
import java .awt .*;
10
+ import java .time .LocalDateTime ;
13
11
import java .util .ArrayList ;
14
12
15
- public class AppScreen implements AppScreenPresenter , AppScreenController , ChatName , Refresh {
13
+ public class AppScreen implements AppScreenPresenter , AppScreenController , ChatName , Refresh , LastUpdate {
16
14
17
15
private final JFrame jFrame ;
18
16
private JScrollPane jScrollPane ;
@@ -45,7 +43,7 @@ public AppScreen(String currentUsername, ArrayList<Chat> chats) {
45
43
46
44
// adding the action listeners for the +private-chat and +group-chat buttons
47
45
addPrivateChat .addActionListener (e -> {
48
- ChatView newChat = new ChatView (currentUsername , true , "" );
46
+ ChatView newChat = new ChatView (true );
49
47
newChat .chatDisplay ();
50
48
51
49
});
@@ -89,20 +87,9 @@ public void displayAppScreen(){
89
87
for (int i = this .chats .size () - 1 ; i > -1 ; i --) {
90
88
91
89
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 ));
106
93
}
107
94
108
95
jPanel .setAlignmentY (Component .CENTER_ALIGNMENT );
@@ -119,6 +106,16 @@ public void displayAppScreen(){
119
106
120
107
}
121
108
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
+
122
119
/**
123
120
Make the chat list scrollable
124
121
@param jPanel The panel containing the chats
@@ -177,6 +174,7 @@ public void addNewChat(Chat chat){
177
174
178
175
/**
179
176
* 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
180
178
* @param chatID The ID of the given chat
181
179
*/
182
180
@ Override
@@ -192,20 +190,23 @@ public void updateScreen(String chatID) {
192
190
}
193
191
194
192
/**
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
196
194
* @param chatID The ID of the given chat
197
195
* @return true/false
198
196
*/
199
197
@ Override
200
198
public boolean hasUpdate (String chatID ) {
201
199
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 ;
203
204
}
204
205
205
206
/**
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
209
210
*/
210
211
@ Override
211
212
public Chat getChat (String chatID ) {
@@ -214,7 +215,7 @@ public Chat getChat(String chatID) {
214
215
return chat ;
215
216
}
216
217
}
217
- throw new RuntimeException ("Current user is not part of this chat" );
218
+ throw new RuntimeException ("User does not currently have this chat" );
218
219
}
219
220
220
221
}
0 commit comments