Skip to content

Commit 0ade6e8

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents f80cbce + 9b239a0 commit 0ade6e8

File tree

11 files changed

+411
-0
lines changed

11 files changed

+411
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package interface_adapters.User_search_IA;
2+
3+
// UI implements this interface to invert the dependency of UI on the inner layers
4+
public interface UserPresenter {
5+
String showProfile(String username);
6+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package interface_adapters.app_screen_interface_adapters;
2+
3+
import use_cases.app_screen_use_case.AppScreenPresenter;
4+
import entities.*;
5+
import screens.app_screen.AppScreen;
6+
7+
import java.util.ArrayList;
8+
9+
public class AppScreenLoader implements AppScreenPresenter {
10+
11+
private final String username;
12+
private final ArrayList<Chat> chats;
13+
public AppScreen appScreen;
14+
15+
/**
16+
* Create the app screen loader (and store its user and chat information)
17+
* @param username The username of the current user
18+
*/
19+
public AppScreenLoader(String username, ArrayList<Chat> chats){
20+
this.username = username;
21+
this.chats = chats;
22+
try {
23+
openScreen();
24+
} catch (Exception e) {
25+
throw new RuntimeException("Unexpected Interruption: cannot load screen");
26+
}
27+
28+
}
29+
30+
/**
31+
* Create the screen to show to the user
32+
*/
33+
@Override
34+
public void openScreen() {
35+
this.appScreen = new AppScreen(this.username, this.chats);
36+
}
37+
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package interface_adapters.app_screen_interface_adapters;
2+
3+
import java.util.ArrayList;
4+
import interface_adapters.*;
5+
import entities.*;
6+
import data_access.UserDatabase;
7+
import java.util.ArrayList;
8+
9+
public class UserAppScreenGateway implements Login {
10+
11+
private final UserDatabase userDatabase;
12+
private final String username;
13+
private ArrayList<Chat> userChats;
14+
15+
/**
16+
* Create gateway between user and appscreen
17+
*/
18+
public UserAppScreenGateway(String username){
19+
this.username = username;
20+
this.userDatabase = new UserDatabase();
21+
login();
22+
}
23+
24+
/**
25+
* Log the user into the system
26+
*/
27+
public void login(){;
28+
this.userChats = this.userDatabase.getUserChats(this.username);
29+
AppScreenLoader appScreenLoader = new AppScreenLoader(this.username, this.userChats);
30+
}
31+
32+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package screens.Profile_screen; /**
2+
* Provides the UI elements
3+
*/
4+
import javax.swing.*;
5+
import java.awt.*;
6+
import java.awt.event.ActionEvent;
7+
import java.awt.event.ActionListener;
8+
import java.io.File;
9+
10+
11+
public class UserSearchUI implements UserPresenter {
12+
private JLabel label;
13+
14+
public UserSearchUI() {
15+
final JFrame frame = new JFrame();
16+
frame.setSize(300, 100);
17+
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
18+
frame.setLayout(new FlowLayout());
19+
20+
final JTextField field = new JTextField("Enter a username");
21+
22+
field.addActionListener(new ActionListener() {
23+
@Override
24+
public void actionPerformed(ActionEvent e) {
25+
label.setText(showProfile(field.getText()));
26+
}
27+
28+
});
29+
frame.add(field);
30+
label = new JLabel();
31+
frame.add(label);
32+
frame.setVisible(true);
33+
}
34+
35+
// User_search_IA.UserPresenter makes UI implement showProfile to invert the use-case --> UI dependency
36+
@Override
37+
public String showProfile(String username) {
38+
// setting up access to the database of users:
39+
UserDatabase db = new UserDatabase();
40+
if (db.UserExists(username)){
41+
User user = db.getUser(username);
42+
UserReader reader = new UserReader();
43+
String[] features = reader.UserReader(user);
44+
String email = features[1];
45+
return("<html>Username: " + username + "<br>Email: " + email + "</html>");
46+
}
47+
else{
48+
return("User with given username does not exist.");
49+
}
50+
}
51+
52+
// for trying out the code:
53+
// public static void main(String[] args) {
54+
// new Profile_screen.UserSearchUI();
55+
//
56+
// }
57+
58+
}
59+
60+
61+
62+
63+
64+
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
package screens.app_screen;
2+
3+
4+
import use_cases.app_screen_use_case.AppScreenController;
5+
import use_cases.app_screen_use_case.AppScreenPresenter;
6+
import use_cases.app_screen_use_case.ChatName;
7+
import use_cases.app_screen_use_case.Refresh;
8+
import entities.*;
9+
10+
import javax.swing.*;
11+
import java.awt.*;
12+
import java.util.ArrayList;
13+
14+
public class AppScreen implements AppScreenPresenter, AppScreenController, ChatName, Refresh {
15+
16+
private final JFrame jFrame;
17+
private JScrollPane jScrollPane;
18+
private final String currentUsername;
19+
private ArrayList<Chat> chats;
20+
21+
22+
/**
23+
Create an AppScreen object
24+
@param chats This is a list of chats given by the user (the list will always come as sorted with the
25+
most recent chats at the end of the list)
26+
*/
27+
public AppScreen(String currentUsername, ArrayList<Chat> chats) {
28+
this.currentUsername = currentUsername;
29+
this.chats = chats;
30+
jFrame = new JFrame();
31+
jFrame.setSize(300, 500);
32+
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
33+
34+
35+
// top panel containing the buttons for creating a new chat
36+
JPanel topPanel = new JPanel();
37+
topPanel.setLayout(new GridLayout(1,2));
38+
39+
JButton addPrivateChat = new JButton("+ Private Chat");
40+
JButton addGroupChat = new JButton("+ Group Chat");
41+
42+
addPrivateChat.setPreferredSize(new Dimension(40, 30));
43+
addGroupChat.setPreferredSize(new Dimension(40, 30));
44+
45+
// adding the action listeners for the +private-chat and +group-chat buttons
46+
addPrivateChat.addActionListener(e -> {
47+
ChatView newChat = new ChatView(currentUsername, true, "");
48+
newChat.chatDisplay();
49+
50+
});
51+
//TODO: add groupchat action
52+
// addGroupChat.addActionListener(e -> {
53+
// ChatView newChat = new ChatView(currentUsername, true);
54+
// newChat.chatDisplay();
55+
// });
56+
57+
58+
topPanel.add(addPrivateChat);
59+
topPanel.add(addGroupChat);
60+
jFrame.add(topPanel, BorderLayout.NORTH);
61+
62+
this.chats = chats;
63+
openScreen();
64+
65+
}
66+
67+
/**
68+
* Attempts to open the screen to display to the user
69+
*/
70+
@Override
71+
public void openScreen() {
72+
try{
73+
displayAppScreen();
74+
} catch (Exception e) {
75+
throw new RuntimeException("Unable to to open screen");
76+
}
77+
78+
}
79+
80+
/**
81+
Display a screen containing an ordered list of chats to the user based on latest conversation times
82+
*/
83+
public void displayAppScreen(){
84+
85+
JPanel jPanel = new JPanel();
86+
87+
// getting the names of each chat to display and creating buttons for each chat
88+
for (int i = this.chats.size() - 1; i > -1; i--) {
89+
90+
String chatName = getChatName(this.chats.get(i));
91+
JButton b = new JButton(chatName);
92+
b.setPreferredSize(new Dimension(280, 50));
93+
JLabel jLabel = new JLabel("time");
94+
jLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
95+
jLabel.setFont(new Font(null, Font.BOLD, 11));
96+
b.add(jLabel);
97+
98+
// defines the action of opening a chat when a chat is clicked on
99+
b.addActionListener(e -> {
100+
101+
ChatView newChat = new ChatView(currentUsername, false, chatName);
102+
newChat.chatDisplay();
103+
});
104+
jPanel.add(b);
105+
}
106+
107+
jPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
108+
109+
// set height of panel to appropriate size based on the number of chats
110+
jPanel.setPreferredSize(new Dimension(100, 60 * this.chats.size()));
111+
jPanel.setBorder(BorderFactory.createTitledBorder("My Chats"));
112+
113+
114+
// making the chat list scrollable
115+
scrollableChats(jPanel);
116+
117+
jFrame.setVisible(true);
118+
119+
}
120+
121+
/**
122+
Make the chat list scrollable
123+
@param jPanel The panel containing the chats
124+
*/
125+
private void scrollableChats(JPanel jPanel) {
126+
JScrollPane scrollFrame = new JScrollPane(jPanel);
127+
jScrollPane = scrollFrame;
128+
scrollFrame.setPreferredSize(new Dimension( 200,500));
129+
scrollFrame. getVerticalScrollBar().setUnitIncrement(5);
130+
jFrame.getContentPane().add(scrollFrame);
131+
}
132+
133+
134+
/**
135+
* Get the name of the chat
136+
* @param chat The chat in context
137+
* @return name
138+
*/
139+
@Override
140+
public String getChatName(Chat chat) {
141+
return chat.getName();
142+
}
143+
144+
/**
145+
* Update the order of the chats
146+
* @param chat The chat that has an update
147+
*/
148+
public void updateChatOrder(Chat chat){
149+
150+
if (this.chats.contains(chat)) {
151+
this.chats.remove(chat);
152+
this.chats.add(chat);
153+
}
154+
else {
155+
this.chats.add(chat);
156+
}
157+
158+
}
159+
160+
/**
161+
* Add a new chat to the screen, if the chat already exists (i.e. there exists a chat with the
162+
* same ID, do nothing)
163+
* @param chat The new chat to be added
164+
*/
165+
public void addNewChat(Chat chat){
166+
167+
if (!(this.chats.contains(chat))){
168+
updateChatOrder(chat);
169+
jFrame.remove(this.jScrollPane);
170+
171+
// refresh the screen
172+
displayAppScreen();
173+
174+
}
175+
}
176+
177+
/**
178+
* Update the order of chats that appear on screen if there was a change to conversation history
179+
* @param chatID The ID of the given chat
180+
*/
181+
@Override
182+
public void updateScreen(String chatID) {
183+
if (hasUpdate(chatID)){
184+
185+
updateChatOrder(getChat(chatID));
186+
jFrame.remove(this.jScrollPane);
187+
188+
// refresh the screen
189+
displayAppScreen();
190+
}
191+
}
192+
193+
/**
194+
* Return true if the given chat as an update to its conversation history
195+
* @param chatID The ID of the given chat
196+
* @return true/false
197+
*/
198+
@Override
199+
public boolean hasUpdate(String chatID) {
200+
Chat chat = getChat(chatID);
201+
return this.chats.get(this.chats.size() - 1) != chat;
202+
}
203+
204+
/**
205+
* Get the chat object given its chat ID
206+
* @param chatID The ID of the chat
207+
* @return The chat with the given ID
208+
*/
209+
@Override
210+
public Chat getChat(String chatID) {
211+
for (Chat chat: this.chats){
212+
if (chat.getChatID().equals(chatID)){
213+
return chat;
214+
}
215+
}
216+
throw new RuntimeException("Current user is not part of this chat");
217+
}
218+
219+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package use_cases.app_screen_use_case;
2+
3+
import entities.*;
4+
public interface AppScreenController{
5+
Chat getChat(String chatID);
6+
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package use_cases.app_screen_use_case;
2+
3+
public interface AppScreenPresenter {
4+
void openScreen();
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package use_cases.app_screen_use_case;
2+
3+
import entities.*;
4+
5+
public interface ChatName {
6+
String getChatName(Chat chat);
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package use_cases.app_screen_use_case;
2+
3+
public interface Refresh {
4+
boolean hasUpdate(String chatID);
5+
void updateScreen(String chatID);
6+
}

0 commit comments

Comments
 (0)