Skip to content

Commit 276362d

Browse files
author
Nasim Bondar Sahebi
committed
completed new clean architecture for the chat-initiation.
1 parent d1d566f commit 276362d

File tree

10 files changed

+163
-90
lines changed

10 files changed

+163
-90
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package entities.chat;
2+
3+
public class CommonPrivatechat implements PrivateChatfactory{
4+
5+
@Override
6+
public PrivateChat create(String name, String chatID, String recipientUsername) {
7+
return new PrivateChat(name,chatID,recipientUsername);
8+
}
9+
10+
}

src/main/java/entities/chat/PrivateChat.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,28 @@ public class PrivateChat extends Chat {
1414
Arraylist<Message> convHist;
1515
*/
1616
protected String recipientUsername;
17+
protected String senderUsername;
1718

1819
/**
1920
* Create a private chat
2021
*
2122
* @param name The name of the chat (also the username of the recipient)
2223
* @param chatID The ID of the chat
23-
* @param senderUsername The user sending the messages
2424
* @param recipientUsername The user receiving the messages
2525
*
26+
*
2627
*/
27-
public PrivateChat(String name, String chatID, String senderUsername, String recipientUsername ){
28+
public PrivateChat(String name, String chatID, String recipientUsername ){
2829
this.name = name;
2930
this.chatID = chatID;
30-
this.senderUsername = senderUsername;
3131
this.recipientUsername = recipientUsername;
3232
this.convHist = new ArrayList<Message>();
33+
3334
}
3435

3536

37+
38+
3639
/**
3740
* Get the recipient's username
3841
* @return senderRecipient
@@ -45,6 +48,10 @@ public void setRecipientUsername(String recipientUsername) {
4548
this.recipientUsername= recipientUsername;
4649
}
4750

51+
public String getRecipientUsername(String recipientUsername) {
52+
return this.recipientUsername;
53+
}
54+
4855
public String getSenderUsername(){
4956
return this.senderUsername;
5057
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package entities.chat;
2+
3+
//in Entity layer Interface tor create private chat.
4+
5+
public interface PrivateChatfactory {
6+
7+
PrivateChat create(String name, String chatID, String recipientUsername);
8+
9+
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33

44
import entities.chat.Chat;
5+
import entities.chat.CommonPrivatechat;
6+
import entities.chat.PrivateChatfactory;
7+
import screens.chat_screen.ChatController;
58
import screens.chat_screen.ChatView;
69
import use_cases.app_screen_use_case.*;
10+
import use_cases.chat_initiation_use_case.ChatInputBoundry;
11+
import use_cases.chat_initiation_use_case.ChatInteractor;
712

813
import javax.swing.*;
914
import java.awt.*;
@@ -43,8 +48,14 @@ public AppScreen(String currentUsername, ArrayList<Chat> chats) {
4348

4449
// adding the action listeners for the +private-chat and +group-chat buttons
4550
addPrivateChat.addActionListener(e -> {
46-
ChatView newChat = new ChatView(true);
47-
newChat.chatDisplay();
51+
//TODO AMY please double check this part(Nasim)
52+
53+
PrivateChatfactory chatfactory = new CommonPrivatechat();
54+
ChatInputBoundry Interactor = new ChatInteractor(chatfactory);
55+
ChatController controller = new ChatController(Interactor);
56+
new ChatView(controller,true);
57+
ChatView newChat = new ChatView(controller,true);
58+
4859

4960
});
5061
//TODO: add groupchat action
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package screens.chat_screen;
2+
3+
import entities.chat.PrivateChat;
4+
import use_cases.chat_initiation_use_case.ChatInputBoundry;
5+
import use_cases.chat_initiation_use_case.ChatModel;
6+
7+
public class ChatController {
8+
9+
ChatInputBoundry chatinputboundry;
10+
PrivateChat newprivatechat;
11+
public ChatController(ChatInputBoundry chatinputboundry) {
12+
this.chatinputboundry = chatinputboundry;
13+
}
14+
15+
// this method is used in UI to set the recipient username
16+
public ChatModel create (String username){
17+
ChatModel chatmodel = new ChatModel(username);
18+
// create a private chat and
19+
this.newprivatechat = chatinputboundry.create(chatmodel);
20+
return chatmodel ;
21+
22+
}
23+
24+
public PrivateChat getNewprivatechat() {
25+
return newprivatechat;
26+
}
27+
}

src/main/java/screens/chat_screen/ChatView.java

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
package screens.chat_screen;
22

33

4+
5+
6+
import javax.swing.*;
7+
import javax.swing.border.EmptyBorder;
8+
9+
10+
import java.awt.*;
11+
import java.awt.event.ActionEvent;
12+
import java.awt.event.ActionListener;
13+
14+
15+
import entities.chat.CommonPrivatechat;
16+
import entities.chat.PrivateChatfactory;
17+
import use_cases.chat_initiation_use_case.ChatInputBoundry;
418
import use_cases.chat_initiation_use_case.ChatInteractor;
5-
import use_cases.chat_initiation_use_case.CheckUsername_Interactor;
19+
import use_cases.chat_initiation_use_case.ChatModel;
20+
621

722
import javax.swing.*;
823
import javax.swing.border.EmptyBorder;
@@ -25,9 +40,9 @@
2540

2641

2742
public class ChatView extends JFrame implements ActionListener{
28-
//use two Interactors .
29-
private ChatInteractor chatInteractor;
30-
private CheckUsername_Interactor checkusername_interactor;
43+
//use Contrroller
44+
45+
private ChatController controller;
3146

3247
//Use Jframes, butttons, labels ,textfileds, Jpannels,JMenuBar for UI.
3348
final JFrame frame ;
@@ -52,9 +67,10 @@ public class ChatView extends JFrame implements ActionListener{
5267

5368

5469

55-
//this is constructor
56-
public ChatView( boolean isNewchat){
5770

71+
//this is constructor
72+
public ChatView( ChatController controller , boolean isNewchat){
73+
this.controller = controller;
5874
this.isNewchat = isNewchat;
5975

6076

@@ -89,11 +105,7 @@ public ChatView( boolean isNewchat){
89105
sendbutton.setFocusable(false);
90106

91107

92-
}
93-
94-
95-
// create a setup for display of buttons and other component of the frame.
96-
public void chatDisplay(){
108+
// create a setup for display of buttons and other component of the frame.
97109

98110
// set frame size and frame title
99111
frame.setSize(450, 500);
@@ -107,14 +119,12 @@ public void chatDisplay(){
107119
menubar.add(addbutton);
108120

109121

110-
111122
// adding label and textfiled1 to our panel .
112123
panel.add(label);
113124
panel.add(messagetextfield);
114125
panel.add(sendbutton);
115126

116127

117-
118128
// add content to conversationHistoryPanel
119129
conversationHistoryPanel.setLayout(new BoxLayout(conversationHistoryPanel, BoxLayout.Y_AXIS));
120130

@@ -147,11 +157,9 @@ public void chatDisplay(){
147157
conversationHistoryPanel.add(messagePanel2);
148158

149159

150-
151-
152160
//Locating the Components to the frame.
153161
frame.getContentPane().add(BorderLayout.SOUTH, panel);
154-
if (isNewchat){
162+
if (isNewchat) {
155163
frame.getContentPane().add(BorderLayout.NORTH, menubar);
156164
}
157165

@@ -168,63 +176,55 @@ public void chatDisplay(){
168176
}
169177

170178

171-
172179
//we implement ActionListener class and should override this method for our button's actions.
173180

174181
@Override
175182
public void actionPerformed(ActionEvent e) {
176183

177184
// STEP1: action for the "add button" at the top of frame.
178-
// goal : click on add button will change the chat frame's title to user'sB username(typed in the textfiled)
179-
180185

181186
if (e.getSource() == addbutton){
182187
// convert textfield input to String and set the frame title to that input is username exists.
188+
183189
String input = usernametextfield.getText();
184-
if (checkusername_interactor.checkusername(input)){
185-
frame.setTitle(input);
186-
}
190+
controller.create(input);
191+
187192

188-
// also set the input -will change the private chat's RecipientUsername .
189-
chatInteractor.setRecipientUsername(input);
193+
frame.setTitle(input);
190194

191195
}
192196

193197

194198
// STEP2: action for the "send button".
195-
// goal : to write a message in txt filed and click "send button"
196-
// so the message will come in the middle of the frame
197199

198200
if (e.getSource() == sendbutton){
199201

200-
//convert the text field input to a String
201-
String input = usernametextfield.getText();
202-
203-
// setting our messge in the chatInteractor- will add this message to conv history.
204-
chatInteractor.setMessage(input);
205-
206202
//TODO:this is chatHisroy action.
207203

208204

209205
}
206+
210207
}
208+
209+
210+
211211
// public static void main(String args[]) {
212-
// EventQueue.invokeLater(new Runnable() {
213-
// @Override
214-
// public void run() {
215-
// ChatView chat = new ChatView(true);
216-
//// chat.getframe().setTitle("AMMY")
217-
// chat.chatdisplay();
218212
//
213+
// PrivateChatfactory chatfactory = new CommonPrivatechat();
214+
// ChatInputBoundry Interactor = new ChatInteractor(chatfactory);
215+
// ChatController controller = new ChatController(Interactor);
216+
// new ChatView(controller,true);
219217
//
220-
// //todo
221-
// //chat history
218+
// controller.create(new ChatModel("Hi").getRecipientusername());
222219
//
220+
// new ChatView(controller,true);
221+
//// // find the created privatechat and the username
222+
//// System.out.println(controller.getNewprivatechat().getRecipientUsername());
223223
//
224-
// }
225-
// });
226224
//
227225
// }
228226

227+
229228
}
230229

230+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package use_cases.chat_initiation_use_case;
2+
3+
4+
5+
6+
//chat-initation use case layer
7+
8+
import entities.chat.PrivateChat;
9+
10+
public interface ChatInputBoundry {
11+
12+
PrivateChat create(ChatModel chatmodel);
13+
}

src/main/java/use_cases/chat_initiation_use_case/ChatInteractor.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,39 @@
33

44

55
import entities.chat.PrivateChat;
6+
import entities.chat.PrivateChatfactory;
67
import entities.message.MsgFactory;
78

8-
/**
9-
* Chatinteractor connects our ChatView UI to our Entities.
10-
* It provide data to the view, so that view can put that data on the screen.
11-
* this class contain Privatchat and methods to update the Private chat.
12-
*/
139

14-
public class ChatInteractor {
1510

16-
PrivateChat privatechat;
1711

12+
import java.util.UUID;
1813

19-
// constructor containing the private chat
20-
public ChatInteractor(PrivateChat privatechat) {
21-
this.privatechat = privatechat;
22-
}
14+
public class ChatInteractor implements ChatInputBoundry{
2315

2416

25-
//Update the Recipientusername when the user type in the username text field and click add button.
26-
public void setRecipientUsername(String recipientUsername) {
27-
this.privatechat.setRecipientUsername(recipientUsername) ;
28-
}
17+
PrivateChatfactory chatfactory;
18+
19+
20+
//Add a DaTa base to check if exist or not
21+
public ChatInteractor(PrivateChatfactory chatfactory){
2922

23+
this.chatfactory = chatfactory;
3024

31-
// Update the message history when the type in put in message txtfieled and click send button.
32-
public void setMessage(String content) {
33-
MsgFactory msgfactory = new MsgFactory("text");
34-
privatechat.addtoconvHist(msgfactory.createMsg(privatechat.getSenderUsername(), content));
3525
}
3626

3727

28+
//create a private chat with the Recipient username that user typed in the textfield UI of the private chat.
29+
@Override
30+
public PrivateChat create(ChatModel chatmodel) {
31+
32+
//check if in data exist later
33+
34+
PrivateChat p= chatfactory.create(chatmodel.getRecipientusername(), UUID.randomUUID().toString(),chatmodel.getRecipientusername().toString());
35+
//
36+
return p;
37+
38+
}
3839

3940

4041
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package use_cases.chat_initiation_use_case;
2+
3+
//Use case layer chat-Initiation .
4+
5+
public class ChatModel {
6+
7+
private String recipientusername;
8+
//TODO: maybe messge.
9+
10+
public ChatModel (String recipientusername){
11+
this.recipientusername = recipientusername;
12+
}
13+
14+
// A getter for the Recipient-username , late rused in the ChatInteract class.
15+
16+
public String getRecipientusername() {
17+
return recipientusername;
18+
}
19+
}

0 commit comments

Comments
 (0)