Skip to content

Commit 093ca9f

Browse files
committed
Implemented login for user
1 parent c6fcbe0 commit 093ca9f

File tree

7 files changed

+323
-0
lines changed

7 files changed

+323
-0
lines changed
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
package app_screen;
2+
3+
4+
import app_screen_use_case.AppScreenController;
5+
import app_screen_use_case.AppScreenPresenter;
6+
import app_screen_use_case.ChatName;
7+
import app_screen_use_case.Refresh;
8+
import entities.Chat;
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+
addGroupChat.addActionListener(e -> {
52+
ChatView newChat = new ChatView(currentUsername, true);
53+
newChat.chatDisplay();
54+
});
55+
56+
57+
topPanel.add(addPrivateChat);
58+
topPanel.add(addGroupChat);
59+
jFrame.add(topPanel, BorderLayout.NORTH);
60+
61+
this.chats = chats;
62+
openScreen();
63+
64+
}
65+
66+
/**
67+
* Attempts to open the screen to display to the user
68+
*/
69+
@Override
70+
public void openScreen() {
71+
try{
72+
displayAppScreen();
73+
} catch (Exception e) {
74+
throw new RuntimeException("Unable to to open screen");
75+
}
76+
77+
}
78+
79+
/**
80+
Display a screen containing an ordered list of chats to the user based on latest conversation times
81+
*/
82+
public void displayAppScreen(){
83+
84+
JPanel jPanel = new JPanel();
85+
86+
// getting the names of each chat to display and creating buttons for each chat
87+
for (int i = this.chats.size() - 1; i > -1; i--) {
88+
89+
String chatName = getChatName(this.chats.get(i));
90+
JButton b = new JButton(chatName);
91+
b.setPreferredSize(new Dimension(280, 50));
92+
JLabel jLabel = new JLabel("time");
93+
jLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
94+
jLabel.setFont(new Font(null, Font.BOLD, 11));
95+
b.add(jLabel);
96+
97+
// defines the action of opening a chat when a chat is clicked on
98+
b.addActionListener(e -> {
99+
100+
ChatView newChat = new ChatView(currentUsername, false);
101+
newChat.chatDisplay();
102+
});
103+
jPanel.add(b);
104+
}
105+
106+
jPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
107+
108+
// set height of panel to appropriate size based on the number of chats
109+
jPanel.setPreferredSize(new Dimension(100, 60 * this.chats.size()));
110+
jPanel.setBorder(BorderFactory.createTitledBorder("My Chats"));
111+
112+
113+
// making the chat list scrollable
114+
scrollableChats(jPanel);
115+
116+
jFrame.setVisible(true);
117+
118+
}
119+
120+
/**
121+
Make the chat list scrollable
122+
@param jPanel The panel containing the chats
123+
*/
124+
private void scrollableChats(JPanel jPanel) {
125+
JScrollPane scrollFrame = new JScrollPane(jPanel);
126+
jScrollPane = scrollFrame;
127+
scrollFrame.setPreferredSize(new Dimension( 200,500));
128+
scrollFrame. getVerticalScrollBar().setUnitIncrement(5);
129+
jFrame.getContentPane().add(scrollFrame);
130+
}
131+
132+
133+
/**
134+
* Get the name of the chat
135+
* @param chat The chat in context
136+
* @return name
137+
*/
138+
@Override
139+
public String getChatName(Chat chat) {
140+
return chat.getName();
141+
}
142+
143+
/**
144+
* Update the order of the chats
145+
* @param chat The chat that has an update
146+
*/
147+
public void updateChatOrder(Chat chat){
148+
149+
if (this.chats.contains(chat)) {
150+
this.chats.remove(chat);
151+
this.chats.add(chat);
152+
}
153+
else {
154+
this.chats.add(chat);
155+
}
156+
157+
}
158+
159+
/**
160+
* Add a new chat to the screen, if the chat already exists (i.e. there exists a chat with the
161+
* same ID, do nothing)
162+
* @param chat The new chat to be added
163+
*/
164+
public void addNewChat(Chat chat){
165+
166+
if (!(this.chats.contains(chat))){
167+
updateChatOrder(chat);
168+
jFrame.remove(this.jScrollPane);
169+
170+
// refresh the screen
171+
displayAppScreen();
172+
173+
}
174+
}
175+
176+
/**
177+
* Update the order of chats that appear on screen if there was a change to conversation history
178+
* @param chatID The ID of the given chat
179+
*/
180+
@Override
181+
public void updateScreen(String chatID) {
182+
if (hasUpdate(chatID)){
183+
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 chat as 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+
return this.chats.get(this.chats.size() - 1) != chat;
201+
}
202+
203+
/**
204+
* Get the chat object given its chat ID
205+
* @param chatID The ID of the chat
206+
* @return The chat with the given ID
207+
*/
208+
@Override
209+
public Chat getChat(String chatID) {
210+
for (Chat chat: this.chats){
211+
if (chat.getChatID().equals(chatID)){
212+
return chat;
213+
}
214+
}
215+
throw new RuntimeException("Current user is not part of this chat");
216+
}
217+
218+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package app_screen_interface_adapters;
2+
3+
import app_screen_use_case.AppScreenPresenter;
4+
import app_screen.AppScreen;
5+
import entities.Chat;
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package app_screen_interface_adapters;
2+
3+
import Interface_adapters.Login;
4+
import entities.User;
5+
import data_access.UserDataBase;
6+
7+
8+
import java.io.File;
9+
import java.util.ArrayList;
10+
11+
public class UserAppScreenGateway implements Login {
12+
13+
private final UserDatabase userDatabase;
14+
private final File userAccounts;
15+
private String username;
16+
private ArrayList<Chat> userChats;
17+
18+
/**
19+
* Create gateway between user and appscreen
20+
*/
21+
public UserAppScreenGateway(String username){
22+
this.username = username;
23+
24+
// should be given the path of the file
25+
this.userAccounts = new File("\data_access\UserAccounts.csv");
26+
27+
this.userDatabase = new UserDatabase(userAccounts);
28+
login();
29+
}
30+
31+
32+
/**
33+
* Log the user into the system
34+
*/
35+
public void login(){;
36+
this.userChats = userDatabase.getUserChats(this.username);
37+
AppScreenLoader appScreenLoader = new AppScreenLoader(this.username, this.userChats);
38+
}
39+
40+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app_screen_use_case;
2+
3+
import entities.Chat;
4+
5+
public interface AppScreenController{
6+
Chat getChat(String chatID);
7+
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package app_screen_use_case;
2+
3+
public interface AppScreenPresenter {
4+
void openScreen();
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package app_screen_use_case;
2+
3+
4+
import entities.Chat;
5+
6+
public interface ChatName {
7+
String getChatName(Chat chat);
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package 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)