Skip to content

Commit 7c4d54c

Browse files
committed
Made changes so that chat order gets saved in UserDatabase and also made Message implement Serializable
1 parent d1d566f commit 7c4d54c

File tree

13 files changed

+109
-36
lines changed

13 files changed

+109
-36
lines changed

accounts

966 Bytes
Binary file not shown.

src/main/java/data_access/UserDatabase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ public void modifyUser(String oldUsername, User modified){
146146
@Override
147147
public ArrayList<Chat> getUserChats(String username) {
148148
for (User user: accountList){
149-
//if(getUser(username).equals(user)){
150149
if(user.getUsername().equals(username)){
151-
return user.getUserChats();
150+
return user.getChats();
152151
}
153152
}
154153
throw new RuntimeException("Invalid username");

src/main/java/entities/chat/Chat.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package entities.chat;
22
import entities.message.Message;
33

4+
import java.io.Serializable;
45
import java.time.LocalDateTime;
56
import java.util.ArrayList;
67
// Chat is an abstract class
7-
public class Chat {
8+
public class Chat implements Serializable {
89

910
protected String name;
1011
protected String chatID;
@@ -43,14 +44,14 @@ public String getSenderUsername(){
4344
* @return convHist
4445
*/
4546
public ArrayList<Message> getConvHist(){
46-
return new ArrayList<Message>(this.convHist);
47+
return new ArrayList<>(this.convHist);
4748
}
4849

4950
/**
5051
* Add a message to the chat's conversation history when a message is sent or received
5152
* @param message Message that is sent or received
5253
*/
53-
public void addtoconvHist(Message message){
54+
public void addToConvHist(Message message){
5455
this.convHist.add(message);
5556
}
5657

src/main/java/entities/message/Message.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package entities.message;
22

3+
import java.io.Serializable;
34
import java.time.LocalDateTime;
45

56
/**
67
* Message (abstract class) containing the message content and associated metadata
78
* The type of the message content is dependent on which child(ren) of Message the message is
89
* The metadata is universal for all message types and includes the ID of the sender, timestamp, ID of message
910
*/
10-
public abstract class Message {
11+
public abstract class Message implements Serializable{
1112
/**
1213
* ID of message sender (UUID)
1314
*/

src/main/java/entities/user_entities/User.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import interface_adapters.login_interface_adapters.Login;
77
import use_cases.user_attribute_modification_use_case.Changeable;
88
import interface_adapters.app_screen_interface_adapters.UserAppScreenGateway;
9-
import entities.chat.*;
109

1110
import java.io.File;
1211
import java.io.Serializable;
@@ -57,7 +56,8 @@ public Boolean PasswordMatch(String attempt){
5756
}
5857

5958
public void login(){
60-
UserAppScreenGateway appScreenGateway = new UserAppScreenGateway(this.getUsername(), new UserDatabase(new File("test9")));
59+
UserAppScreenGateway appScreenGateway = new UserAppScreenGateway(this.getUsername(), new UserDatabase(new File("accounts")));
60+
appScreenGateway.login();
6161
}
6262

6363
public ArrayList<Chat> getChats() {

src/main/java/interface_adapters/app_screen_interface_adapters/UserAppScreenGateway.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import data_access.UserDatabase;
44
import entities.chat.Chat;
5+
import entities.user_entities.User;
56
import interface_adapters.login_interface_adapters.Login;
67

78
import java.util.ArrayList;
@@ -17,7 +18,6 @@ public class UserAppScreenGateway implements Login {
1718
public UserAppScreenGateway(String username, UserDatabase userDatabase){
1819
this.username = username;
1920
this.userDatabase = userDatabase;
20-
login();
2121
}
2222

2323
/**
@@ -28,4 +28,11 @@ public void login(){
2828
AppScreenLoader appScreenLoader = new AppScreenLoader(this.username, userChats);
2929
}
3030

31+
public void updateUserChatList(String username, ArrayList<Chat> userChats){
32+
User currentUser = this.userDatabase.getUser(username);
33+
currentUser.getUserChats().clear();
34+
currentUser.getUserChats().addAll(userChats);
35+
this.userDatabase.modifyUser(username, currentUser);
36+
37+
}
3138
}

src/main/java/interface_adapters/login_interface_adapters/UserLoginController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public void allowLogin(){
2121
try{
2222
boolean allowLogin = user.PasswordMatch(this.password);
2323
if(allowLogin){
24-
System.out.println("made it here");
2524
user.login();
2625
}else{
2726
accessDenied("Wrong Password");

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package screens.app_screen;
22

33

4+
import data_access.UserDatabase;
45
import entities.chat.Chat;
6+
import interface_adapters.app_screen_interface_adapters.UserAppScreenGateway;
57
import screens.chat_screen.ChatView;
68
import use_cases.app_screen_use_case.*;
79

810
import javax.swing.*;
911
import java.awt.*;
12+
import java.io.File;
1013
import java.time.LocalDateTime;
1114
import java.util.ArrayList;
1215

@@ -165,6 +168,7 @@ public void addNewChat(Chat chat){
165168
if (!(this.chats.contains(chat))){
166169
updateChatOrder(chat);
167170
jFrame.remove(this.jScrollPane);
171+
createGateway();
168172

169173
// refresh the screen
170174
displayAppScreen();
@@ -179,28 +183,12 @@ public void addNewChat(Chat chat){
179183
*/
180184
@Override
181185
public void updateScreen(String chatID) {
182-
if (hasUpdate(chatID)){
186+
updateChatOrder(getChat(chatID));
187+
jFrame.remove(this.jScrollPane);
188+
createGateway();
183189

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();
204192
}
205193

206194
/**
@@ -218,4 +206,20 @@ public Chat getChat(String chatID) {
218206
throw new RuntimeException("User does not currently have this chat");
219207
}
220208

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

src/main/java/screens/login_screen/UserLoginUI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public void getLoginCredentials(){
4747
JButton loginButton = new JButton("login");
4848
loginButton.setBounds(210, 95, 100, 25);
4949
loginPanel.add(loginButton);
50-
loginButton.addActionListener(this::actionPerformed);
50+
loginButton.addActionListener(this);
5151
loginFrame.setVisible(true);
5252

5353
}
5454

5555
public static void main(String[] args){
56-
UserRetriever testDB = new UserDatabase(new File("Test9"));
56+
UserRetriever testDB = new UserDatabase(new File("accounts"));
5757
UserLoginUI screen = new UserLoginUI(testDB);
5858
screen.getLoginCredentials();
5959
}

src/main/java/screens/user_registration_screen/UserRegistrationUI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void getPreferredDeliveryMethod(){
9999
}
100100

101101
public static void main(String[] args){
102-
UserDatabase testDB = new UserDatabase(new File("Test9"));
102+
UserDatabase testDB = new UserDatabase(new File("accounts"));
103103
System.out.println(testDB.UserExists("RandomUser", "[email protected]"));
104104
System.out.println(testDB.getList().size());
105105
System.out.println(testDB.getList().get(0).getUsername());

0 commit comments

Comments
 (0)