1
1
package screens .app_screen ;
2
2
3
3
4
+ import data_access .UserDatabase ;
4
5
import entities .chat .Chat ;
6
+ import interface_adapters .app_screen_interface_adapters .UserAppScreenGateway ;
5
7
import screens .chat_screen .ChatView ;
6
8
import use_cases .app_screen_use_case .*;
7
9
8
10
import javax .swing .*;
9
11
import java .awt .*;
12
+ import java .io .File ;
10
13
import java .time .LocalDateTime ;
11
14
import java .util .ArrayList ;
12
15
@@ -165,6 +168,7 @@ public void addNewChat(Chat chat){
165
168
if (!(this .chats .contains (chat ))){
166
169
updateChatOrder (chat );
167
170
jFrame .remove (this .jScrollPane );
171
+ createGateway ();
168
172
169
173
// refresh the screen
170
174
displayAppScreen ();
@@ -179,28 +183,12 @@ public void addNewChat(Chat chat){
179
183
*/
180
184
@ Override
181
185
public void updateScreen (String chatID ) {
182
- if (hasUpdate (chatID )){
186
+ updateChatOrder (getChat (chatID ));
187
+ jFrame .remove (this .jScrollPane );
188
+ createGateway ();
183
189
184
- updateChatOrder (getChat (chatID ));
185
- jFrame .remove (this .jScrollPane );
186
-
187
- // refresh the screen
188
- displayAppScreen ();
189
- }
190
- }
191
-
192
- /**
193
- * Return true if the given existing chat has an update to its conversation history
194
- * @param chatID The ID of the given chat
195
- * @return true/false
196
- */
197
- @ Override
198
- public boolean hasUpdate (String chatID ) {
199
- Chat chat = getChat (chatID );
200
- if (!(this .chats .isEmpty ())) {
201
- return !(this .chats .get (this .chats .size () - 1 ).equals (chat ));
202
- }
203
- return true ;
190
+ // refresh the screen
191
+ displayAppScreen ();
204
192
}
205
193
206
194
/**
@@ -218,4 +206,20 @@ public Chat getChat(String chatID) {
218
206
throw new RuntimeException ("User does not currently have this chat" );
219
207
}
220
208
209
+ /**
210
+ * Create the gateway to save a user's chat list order in the user database
211
+ */
212
+ public void createGateway (){
213
+ UserAppScreenGateway gateway = new UserAppScreenGateway (currentUsername ,
214
+ new UserDatabase (new File ("accounts" )));
215
+ try {
216
+ gateway .updateUserChatList (currentUsername , this .chats );
217
+ } catch (NullPointerException e ) {
218
+ throw new NullPointerException ("New chat list is empty" );
219
+ }
220
+ catch (Exception e ){
221
+ throw new RuntimeException ("Unable to update chat order" );
222
+ }
223
+ }
224
+
221
225
}
0 commit comments