Skip to content

Commit 734bafe

Browse files
committed
Refactored some parts to make it more clean
1 parent 61755c2 commit 734bafe

File tree

8 files changed

+143
-78
lines changed

8 files changed

+143
-78
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package interface_adapters.app_screen_interface_adapters;
2+
3+
import data_access.UserDatabase;
4+
import entities.chat.Chat;
5+
import use_cases.app_screen_use_case.ChatOrder;
6+
7+
import java.io.File;
8+
import java.util.ArrayList;
9+
10+
public class AppScreenController{
11+
12+
private final String username;
13+
private final String chatID;
14+
private final UserDatabase userDatabase;
15+
16+
17+
/**
18+
* Create an app screen controller
19+
* @param username Username of the user
20+
* @param chatID ID the chat with an update
21+
*/
22+
public AppScreenController(String username, String chatID){
23+
this.username = username;
24+
this.chatID = chatID;
25+
this.userDatabase = new UserDatabase(new File("user_accounts"));
26+
27+
}
28+
29+
/**
30+
* Return the chat with the given ID
31+
* @return Chat with given ID
32+
*/
33+
public Chat getChat(){
34+
ArrayList<Chat> userChats = this.userDatabase.getUser(this.username).getUserChats();
35+
for (Chat chat: userChats){
36+
if (chat.getChatID().equals(this.chatID)){
37+
return chat;
38+
}
39+
}
40+
throw new RuntimeException("User is not part of this chat");
41+
42+
}
43+
44+
/**
45+
* Controller to update the screen and change the order chats of in user database
46+
*/
47+
public void updateScreen(){
48+
ChatOrder chatOrder = new ChatOrder(this.username);
49+
ArrayList<Chat> newOrder = chatOrder.changeOrder(this.chatID);
50+
createGateway(newOrder);
51+
52+
}
53+
54+
/**
55+
* Create the gateway to save a user's chat list order in the user database
56+
*/
57+
public void createGateway(ArrayList<Chat> newOrder){
58+
UserAppScreenGateway gateway = new UserAppScreenGateway(this.username,
59+
new UserDatabase(new File("user_accounts")));
60+
try{
61+
gateway.updateUserChatList(this.username, newOrder);
62+
} catch (NullPointerException e) {
63+
throw new NullPointerException("New chat list is empty");
64+
}
65+
catch (Exception e){
66+
throw new RuntimeException("Unable to update chat order");
67+
}
68+
}
69+
70+
}

src/main/java/interface_adapters/app_screen_interface_adapters/AppScreenLoader.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package interface_adapters.app_screen_interface_adapters;
22

33
import entities.chat.Chat;
4-
import use_cases.app_screen_use_case.AppScreenPresenter;
54
import screens.app_screen.AppScreen;
65

76
import java.util.ArrayList;
@@ -19,11 +18,6 @@ public class AppScreenLoader implements AppScreenPresenter {
1918
public AppScreenLoader(String username, ArrayList<Chat> chats){
2019
this.username = username;
2120
this.chats = chats;
22-
try {
23-
openScreen();
24-
} catch (Exception e) {
25-
throw new RuntimeException("Unexpected Interruption: cannot load screen");
26-
}
2721

2822
}
2923

@@ -32,7 +26,11 @@ public AppScreenLoader(String username, ArrayList<Chat> chats){
3226
*/
3327
@Override
3428
public void openScreen() {
35-
this.appScreen = new AppScreen(this.username, this.chats);
29+
try {
30+
this.appScreen = new AppScreen(this.username, this.chats);
31+
} catch (Exception e) {
32+
throw new RuntimeException("Unexpected Interruption: cannot load screen");
33+
}
3634
}
3735

3836
}

src/main/java/use_cases/app_screen_use_case/AppScreenPresenter.java renamed to src/main/java/interface_adapters/app_screen_interface_adapters/AppScreenPresenter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package use_cases.app_screen_use_case;
1+
package interface_adapters.app_screen_interface_adapters;
22

33
public interface AppScreenPresenter {
44
void openScreen();

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ public UserAppScreenGateway(String username, UserDatabase userDatabase){
2626
public void login(){
2727
ArrayList<Chat> userChats = this.userDatabase.getUserChats(this.username);
2828
AppScreenLoader appScreenLoader = new AppScreenLoader(this.username, userChats);
29+
appScreenLoader.openScreen();
2930
}
3031

32+
/**
33+
* Update and save the order of chats for a user in UserDatabase
34+
* @param username Username of the current user
35+
* @param userChats Updated list of chats in order
36+
*/
3137
public void updateUserChatList(String username, ArrayList<Chat> userChats){
3238
User currentUser = this.userDatabase.getUser(username);
3339
currentUser.getUserChats().clear();

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

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package screens.app_screen;
22

3-
4-
import data_access.UserDatabase;
53
import entities.chat.Chat;
64
import entities.chat.CommonPrivatechat;
75
import entities.chat.PrivateChatfactory;
8-
import interface_adapters.app_screen_interface_adapters.UserAppScreenGateway;
6+
import interface_adapters.app_screen_interface_adapters.AppScreenController;
7+
import interface_adapters.app_screen_interface_adapters.AppScreenPresenter;
98
import screens.chat_screen.ChatController;
109
import screens.chat_screen.ChatView;
1110
import use_cases.app_screen_use_case.*;
@@ -14,11 +13,10 @@
1413

1514
import javax.swing.*;
1615
import java.awt.*;
17-
import java.io.File;
1816
import java.time.LocalDateTime;
1917
import java.util.ArrayList;
2018

21-
public class AppScreen implements AppScreenPresenter, AppScreenController, ChatName, Refresh, LastUpdate {
19+
public class AppScreen implements AppScreenPresenter, ChatName, LastUpdate {
2220

2321
private final JFrame jFrame;
2422
private JScrollPane jScrollPane;
@@ -149,66 +147,23 @@ public String getChatName(Chat chat) {
149147
return chat.getName();
150148
}
151149

152-
/**
153-
* Update the order of the chats
154-
* @param chat The chat that has an update
155-
*/
156-
public void updateChatOrder(Chat chat){
157-
158-
if (this.chats.contains(chat)) {
159-
this.chats.remove(chat);
160-
this.chats.add(chat);
161-
}
162-
else {
163-
this.chats.add(chat);
164-
}
165-
166-
}
167150

168151
/**
169152
* Update the order of chats that appear on screen if there was a change to conversation history
170153
* This should be called if a new chat was added or if an existing chat has a new message
171-
* @param chat The chat with an update
154+
* @param chatID The ID of the chat with an update
172155
*/
173-
@Override
174-
public void updateScreen(Chat chat) {
175-
updateChatOrder(chat);
156+
public void refreshScreen(String chatID) {
157+
AppScreenController appScreenController = new AppScreenController(currentUsername, chatID);
158+
appScreenController.updateScreen();
159+
160+
ChatOrder chatOrder = new ChatOrder(currentUsername);
161+
this.chats = chatOrder.getUserChats();
162+
176163
jFrame.remove(this.jScrollPane);
177-
createGateway();
178164

179165
// refresh the screen
180166
displayAppScreen();
181167
}
182168

183-
/**
184-
* Get the existing chat object given its chat ID
185-
* @param chatID The ID of the existing chat
186-
* @return The existing chat with the given ID
187-
*/
188-
@Override
189-
public Chat getChat(String chatID) {
190-
for (Chat chat: this.chats){
191-
if (chat.getChatID().equals(chatID)){
192-
return chat;
193-
}
194-
}
195-
throw new RuntimeException("User does not currently have this chat");
196-
}
197-
198-
/**
199-
* Create the gateway to save a user's chat list order in the user database
200-
*/
201-
public void createGateway(){
202-
UserAppScreenGateway gateway = new UserAppScreenGateway(currentUsername,
203-
new UserDatabase(new File("user_accounts")));
204-
try{
205-
gateway.updateUserChatList(currentUsername, this.chats);
206-
} catch (NullPointerException e) {
207-
throw new NullPointerException("New chat list is empty");
208-
}
209-
catch (Exception e){
210-
throw new RuntimeException("Unable to update chat order");
211-
}
212-
}
213-
214169
}

src/main/java/use_cases/app_screen_use_case/AppScreenController.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package use_cases.app_screen_use_case;
2+
3+
import data_access.UserDatabase;
4+
import entities.chat.Chat;
5+
import interface_adapters.app_screen_interface_adapters.AppScreenController;
6+
7+
import java.io.File;
8+
import java.util.ArrayList;
9+
10+
public class ChatOrder {
11+
12+
private final String username;
13+
private final ArrayList<Chat> userChats;
14+
15+
/**
16+
* Create a chat order object
17+
* @param username Username of the current user
18+
*/
19+
public ChatOrder(String username){
20+
this.username = username;
21+
UserDatabase userDatabase = new UserDatabase(new File("user_accounts"));
22+
this.userChats = userDatabase.getUserChats(username);
23+
}
24+
25+
/**
26+
* Return the chats that the given user has
27+
* @return List of the given user's chats
28+
*/
29+
public ArrayList<Chat> getUserChats(){
30+
return this.userChats;
31+
}
32+
33+
/**
34+
* Update the order of the chats
35+
* @param chatID The ID of the chat that has an update
36+
*/
37+
public ArrayList<Chat> changeOrder(String chatID){
38+
39+
AppScreenController appScreenController = new AppScreenController(this.username, chatID);
40+
Chat updatedChat = appScreenController.getChat();
41+
if (this.userChats.contains(updatedChat)) {
42+
this.userChats.remove(updatedChat);
43+
this.userChats.add(updatedChat);
44+
}
45+
else {
46+
this.userChats.add(updatedChat);
47+
}
48+
return this.userChats;
49+
}
50+
}

src/main/java/use_cases/app_screen_use_case/Refresh.java

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

0 commit comments

Comments
 (0)