Skip to content

Commit afc5a51

Browse files
author
Nasim Bondar Sahebi
committed
seprating chatInteractors.
1 parent 4be9d14 commit afc5a51

File tree

3 files changed

+88
-64
lines changed

3 files changed

+88
-64
lines changed

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

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package screens.chat_screen;
22

33

4+
import use_cases.chat_initiation_use_case.ChatInteractor;
5+
import use_cases.chat_initiation_use_case.CheckUsername_Interactor;
6+
47
import javax.swing.*;
58
import javax.swing.border.EmptyBorder;
69

@@ -20,25 +23,26 @@
2023
*
2124
*/
2225

23-
//todo: we should complete the chatHistory in this class.
24-
25-
2626

2727

2828
class ChatView extends JFrame implements ActionListener{
29+
//use two Interactors .
30+
private ChatInteractor chatInteractor;
31+
private CheckUsername_Interactor checkusername_interactor;
2932

30-
// private ChatInteractor chatInteractor;
31-
private JFrame frame ;
33+
//Use Jframes, butttons, labels ,textfileds, Jpannels,JMenuBar for UI.
34+
final JFrame frame ;
3235
private JButton addbutton;
33-
private JButton sendbutton;
34-
private JLabel l;
35-
private JLabel label;
36-
private JTextField usernametextfield;
37-
private JTextField messagetextfield;
38-
private JMenuBar menubar;
39-
private JPanel panel;
40-
41-
private JPanel conversationHistoryPanel;
36+
final JButton sendbutton;
37+
final JLabel l;
38+
final JLabel label;
39+
final JTextField usernametextfield;
40+
final JTextField messagetextfield;
41+
final JMenuBar menubar;
42+
final JPanel panel;
43+
44+
final JPanel conversationHistoryPanel;
45+
4246
private JPanel messagePanel;
4347
private JLabel testMessageHeader;
4448
private JLabel testMessage;
@@ -48,11 +52,13 @@ class ChatView extends JFrame implements ActionListener{
4852

4953

5054

51-
//this is constructor od this class
52-
public ChatView(boolean isNewchat){
55+
56+
//this is constructor
57+
public ChatView( boolean isNewchat){
5358

5459
this.isNewchat = isNewchat;
5560

61+
5662
frame= new JFrame();
5763

5864
// create a menubar at the top of the frame
@@ -87,7 +93,6 @@ public ChatView(boolean isNewchat){
8793
}
8894

8995

90-
9196
// create a setup for display of buttons and other component of the frame.
9297
public void chatdisplay(){
9398

@@ -151,83 +156,76 @@ public void chatdisplay(){
151156
frame.getContentPane().add(BorderLayout.NORTH, menubar);
152157
}
153158

159+
154160
frame.getContentPane().add(BorderLayout.CENTER, conversationHistoryPanel);
155161

156162
// set the frame visibile
157163
frame.setVisible(true);
158164

165+
this.addbutton.addActionListener(this);
166+
this.sendbutton.addActionListener(this);
167+
159168

160169
}
161170

162171

163-
// we implement the Actionlistener class so we should implement this method.
164-
// this method create an action for our buttons
165-
//we use our viewmodel attribute inn this method.
172+
173+
//we implement ActionListener class and should override this method for our button's actions.
174+
166175
@Override
167176
public void actionPerformed(ActionEvent e) {
168177

169178
// STEP1: action for the "add button" at the top of frame.
170-
// goal : we want to write user'sB username in text filed and click "add buttom" to change our chat frame title
171-
// to user'sB username.
172-
// first we convert textfield input to String and set the frame title to that input.
179+
// goal : click on add button will change the chat frame's title to user'sB username(typed in the textfiled)
173180

174181

175182
if (e.getSource() == addbutton){
176-
183+
// convert textfield input to String and set the frame title to that input is username exists.
177184
String input = usernametextfield.getText();
185+
if (checkusername_interactor.checkusername(input)){
186+
frame.setTitle(input);
187+
}
178188

179-
frame.setTitle(input);
180-
//controller:
181-
182-
// set the username in our viewmodel
183-
// chatInteractor.setRecipientUsername(input);
184-
185-
//todo
186-
//we should find the user with username "input" from list of user's that have logged in and set the
187-
// user to have the same chat_ID (I did this in the viewmodel)
188-
189+
// also set the input -will change the private chat's RecipientUsername .
190+
chatInteractor.setRecipientUsername(input);
189191

190192
}
191193

194+
192195
// STEP2: action for the "send button".
193196
// goal : to write a message in txt filed and click "send button"
194-
// so the message will come in the "txtArea" in middle of frame
195-
//again we convert the txfield input to a String .
196-
197-
198-
//first is to convert textfield input to String and set the frame title to that input.
197+
// so the message will come in the middle of the frame
199198

200199
if (e.getSource() == sendbutton){
201-
//when the messge type is STring
202-
String input = usernametextfield.getText();
203-
204-
// getting the txtmessage content in our viewmodel
205-
// chatInteractor.setMessage(input);
206-
207-
//todo
208-
//this is part of the chatHisroy of UI. for now I put a text ( it may needed to change later)
209-
210-
}
211-
}
212-
public static void main(String args[]) {
213-
EventQueue.invokeLater(new Runnable() {
214-
@Override
215-
public void run() {
216-
ChatView chat = new ChatView(true);
217-
// chat.getframe().setTitle("AMMY")
218-
chat.chatdisplay();
219200

220-
chat.addbutton.addActionListener(chat);
221-
chat.sendbutton.addActionListener(chat);
201+
//convert the text field input to a String
202+
String input = usernametextfield.getText();
222203

223-
//todo
224-
//chat history
204+
// setting our messge in the chatInteractor- will add this message to conv history.
205+
chatInteractor.setMessage(input);
225206

207+
//TODO:this is chatHisroy action.
226208

227-
}
228-
});
229209

210+
}
230211
}
212+
// public static void main(String args[]) {
213+
// EventQueue.invokeLater(new Runnable() {
214+
// @Override
215+
// public void run() {
216+
// ChatView chat = new ChatView(true);
217+
//// chat.getframe().setTitle("AMMY")
218+
// chat.chatdisplay();
219+
//
220+
//
221+
// //todo
222+
// //chat history
223+
//
224+
//
225+
// }
226+
// });
227+
//
228+
// }
231229

232230
}
233231

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* this class contain Privatchat and methods to update the Private chat.
1111
*/
1212

13-
public class ChatInteractor{
13+
public class ChatInteractor {
1414

1515
PrivateChat privatechat;
1616

@@ -34,4 +34,6 @@ public void setMessage(String content) {
3434
}
3535

3636

37+
38+
3739
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package use_cases.chat_initiation_use_case;
2+
3+
4+
/**
5+
* CheckUsernameInteractor responsible checking if a username exist in our userdata base
6+
*/
7+
8+
9+
public class CheckUsername_Interactor {
10+
11+
final UserDatabase db;
12+
13+
public CheckUsername_Interactor(UserDatabase db){
14+
this.db = db;
15+
}
16+
17+
//checking if the username exist in username database via function UserExists(username).
18+
public boolean checkusername(String username){
19+
if(!(db.UserExists(username))){
20+
return false;
21+
}
22+
return true;
23+
}
24+
}

0 commit comments

Comments
 (0)