Skip to content

Commit b71bebc

Browse files
committed
Fixed a bug that was causing issues with displaying an updated chat order
1 parent cc6a216 commit b71bebc

File tree

8 files changed

+75
-53
lines changed

8 files changed

+75
-53
lines changed

src/main/java/Refresh.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/java/AppScreen.java renamed to src/main/java/appscreen/AppScreen.java

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package appscreen;
2+
13
import javax.swing.*;
24
import java.awt.*;
35
import java.awt.event.ActionEvent;
@@ -8,8 +10,9 @@
810

911
public class AppScreen implements AppScreenPresenter, AppScreenController, ChatName, Refresh {
1012

11-
final JFrame jFrame;
12-
final String currentUserName;
13+
private final JFrame jFrame;
14+
private JScrollPane jScrollPane;
15+
private final String currentUsername;
1316
private ArrayList<Chat> chats;
1417

1518

@@ -18,12 +21,12 @@ public class AppScreen implements AppScreenPresenter, AppScreenController, ChatN
1821
@param chats This is a list of chats given by the user (the list will always come as sorted with the
1922
most recent chats at the end of the list)
2023
*/
21-
public AppScreen(String currentUserName, ArrayList<Chat> chats) {
22-
this.currentUserName = currentUserName;
24+
public AppScreen(String currentUsername, ArrayList<Chat> chats) {
25+
this.currentUsername = currentUsername;
2326
this.chats = chats;
24-
this.jFrame = new JFrame();
25-
this.jFrame.setSize(300, 500);
26-
this.jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
27+
jFrame = new JFrame();
28+
jFrame.setSize(300, 500);
29+
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
2730

2831

2932
// top panel containing the buttons for creating a new chat
@@ -35,30 +38,26 @@ public AppScreen(String currentUserName, ArrayList<Chat> chats) {
3538
addPrivateChat.setPreferredSize(new Dimension(40, 30));
3639
addGroupChat.setPreferredSize(new Dimension(40, 30));
3740

38-
// TODO: implement the action listeners for +PrivateChat and +GroupChat
41+
// TODO: implement the action listeners for the buttons +PrivateChat and +GroupChat
3942

4043
topPanel.add(addPrivateChat);
4144
topPanel.add(addGroupChat);
42-
this.jFrame.add(topPanel, BorderLayout.NORTH);
45+
jFrame.add(topPanel, BorderLayout.NORTH);
4346

4447
this.chats = chats;
4548
openScreen();
4649

4750
}
4851

4952
/**
50-
* Update the order of the chats
51-
* @param chat The chat that has an update
53+
* Attempts to open the screen to display to the user
5254
*/
53-
54-
public void updateChatOrder(Chat chat){
55-
56-
if (this.chats.contains(chat)) {
57-
this.chats.remove(chat);
58-
this.chats.add(chat);
59-
}
60-
else {
61-
this.chats.add(chat);
55+
@Override
56+
public void openScreen() {
57+
try{
58+
displayAppScreen();
59+
} catch (Exception e) {
60+
throw new RuntimeException("Unable to to open screen");
6261
}
6362

6463
}
@@ -102,12 +101,11 @@ public void actionPerformed(ActionEvent e) {
102101
jPanel.setMaximumSize(new Dimension(100, 500));
103102
jPanel.setBorder(BorderFactory.createTitledBorder("My Chats"));
104103

105-
//jFrame.getContentPane().add(jPanel);
106104

107105
// making the chat list scrollable
108106
scrollableChats(jPanel);
109107

110-
this.jFrame.setVisible(true);
108+
jFrame.setVisible(true);
111109

112110
}
113111

@@ -117,9 +115,9 @@ public void actionPerformed(ActionEvent e) {
117115
*/
118116
private void scrollableChats(JPanel jPanel) {
119117
JScrollPane scrollFrame = new JScrollPane(jPanel);
120-
//scrollFrame.setAutoscroll(true);
118+
jScrollPane = scrollFrame;
121119
scrollFrame.setPreferredSize(new Dimension( 200,500));
122-
this.jFrame.add(scrollFrame);
120+
jFrame.add(scrollFrame);
123121
}
124122

125123

@@ -134,36 +132,51 @@ public String getChatName(Chat chat) {
134132
}
135133

136134
/**
137-
* Return true if the given chat as an update to its conversation history
138-
* @param chat The given chat
139-
* @return true/false
135+
* Update the order of the chats
136+
* @param chat The chat that has an update
140137
*/
141-
@Override
142-
public boolean hasUpdate(Chat chat) {
143-
return this.chats.get(this.chats.size() - 1) != chat;
138+
public void updateChatOrder(Chat chat){
139+
140+
if (this.chats.contains(chat)) {
141+
this.chats.remove(chat);
142+
this.chats.add(chat);
143+
}
144+
else {
145+
this.chats.add(chat);
146+
}
147+
144148
}
145149

150+
146151
/**
147152
* Update the screen if the given chat has been updated
148-
* @param chat The given chat
153+
* @param chatID The ID of the given chat
149154
*/
150155
@Override
151-
public void updateScreen(Chat chat) {
152-
if (hasUpdate(chat)){
153-
updateChatOrder(chat);
156+
public void updateScreen(String chatID) {
157+
if (hasUpdate(chatID)){
158+
159+
updateChatOrder(getChat(chatID));
160+
jFrame.remove(this.jScrollPane);
154161

155162
// refresh the screen
156163
displayAppScreen();
157164
}
158165
}
159166

167+
/**
168+
* Return true if the given chat as an update to its conversation history
169+
* @param chatID The ID of the given chat
170+
* @return true/false
171+
*/
160172
@Override
161-
public void openScreen() {
162-
displayAppScreen();
173+
public boolean hasUpdate(String chatID) {
174+
Chat chat = getChat(chatID);
175+
return this.chats.get(this.chats.size() - 1) != chat;
163176
}
164177

165178
@Override
166-
public Chat getUpdatedChat(String chatID) {
179+
public Chat getChat(String chatID) {
167180
for (Chat chat: this.chats){
168181
if (chat.getChatID().equals(chatID)){
169182
return chat;
@@ -172,4 +185,11 @@ public Chat getUpdatedChat(String chatID) {
172185
throw new RuntimeException("Current user is not part of this chat");
173186
}
174187

188+
/**
189+
* Get the username of the current user
190+
* @return currentUserName
191+
*/
192+
public String getCurrentUsername() {
193+
return currentUsername;
194+
}
175195
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
package appscreen;
2+
13
import testerEntities.*;
24
public interface AppScreenController extends LoginSuccess{
3-
Chat getUpdatedChat(String chatID);
5+
Chat getChat(String chatID);
46

57
}

src/main/java/AppScreenLoader.java renamed to src/main/java/appscreen/AppScreenLoader.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
package appscreen;
2+
13
import testerEntities.*;
2-
import java.io.File;
34
import java.util.ArrayList;
45

56
public class AppScreenLoader implements LoginSuccess {
@@ -11,11 +12,11 @@ public class AppScreenLoader implements LoginSuccess {
1112
/**
1213
* Create the app screen loader (and store its user and chat information)
1314
* @param username The username of the current user
15+
* @param userDataBase User information
1416
*/
15-
public AppScreenLoader(String username, File accounts){
17+
public AppScreenLoader(String username, UserDataBase userDataBase){
1618
this.username = username;
17-
UserChatGateway userChatsGateway = new UserChatGateway(accounts);
18-
this.chats = userChatsGateway.getChats(username);
19+
this.chats = userDataBase.getUserChats(username);
1920
try {
2021
openScreen();
2122
} catch (Exception e) {
@@ -25,7 +26,7 @@ public AppScreenLoader(String username, File accounts){
2526
}
2627

2728
/**
28-
* Open an app screen for the user to see containing all their chats
29+
* Open an app screen for the user to see all their chats
2930
*/
3031
@Override
3132
public void openScreen() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
package appscreen;
12
public interface AppScreenPresenter {
23
void displayAppScreen();
34
}

src/main/java/ChatName.java renamed to src/main/java/appscreen/ChatName.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package appscreen;
2+
13
import testerEntities.*;
24
public interface ChatName {
35
String getChatName(Chat chat);
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import testerEntities.*;
2-
import java.util.ArrayList;
3-
1+
package appscreen;
42
public interface LoginSuccess {
53
void openScreen();
64
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package appscreen;
2+
public interface Refresh {
3+
boolean hasUpdate(String chatID);
4+
void updateScreen(String chatID);
5+
}

0 commit comments

Comments
 (0)