Skip to content

Commit

Permalink
Massive Refactoring, implemented Login Use case according to the MVP …
Browse files Browse the repository at this point in the history
…pattern
  • Loading branch information
Madhavan7 committed Dec 2, 2022
1 parent d07586b commit aa10e64
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package interface_adapters.login_interface_adapters;

import use_cases.user_login_use_cases.UserLoginOutputBoundary;

import java.util.ArrayList;
import java.util.List;

public class UserChatsPresenter implements UserLoginOutputBoundary {
private List<String> chats;
private boolean notExists;
private boolean notMatched;
private String username;

public UserChatsPresenter(){
this.chats = new ArrayList<>();
}

@Override
public void setUsername(String username) {
this.username = username;
}

@Override
public void setChats(List<String> chats) {
this.chats = chats;
}
public List<String> getChats(){
return this.chats;
}

@Override
public void setUserNotExists(boolean notExists) {
this.notExists = notExists;
}

public boolean isNotExists() {
return notExists;
}

@Override
public void setPasswordNotMatched(boolean notMatched) {
this.notMatched = notMatched;
}

@Override
public String getUsername() {
return this.username;
}

public boolean isNotMatched() {
return notMatched;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package interface_adapters.login_interface_adapters;

import data_access.Database;
import use_cases.user_login_use_cases.UserLoginInputBoundary;

public class UserLoginPresenter {
private final UserLoginInputBoundary loginGuard;
private String username;
private String password;
Database database;
private UserLoginViewI loginView;

public UserLoginPresenter(Database database, UserLoginInputBoundary loginGuard){
this.database = database;
this.loginGuard = loginGuard;
}

public void tryLogin() {
loginGuard.login(this.username, this.password);
loginView.setChatsPresenter(loginGuard.getChatsPresenter());
loginView.display();
}

public void setLoginCredentials(String username, String password) {
this.username = username;
this.password = password;
}
public void setLoginView(UserLoginViewI loginView){
this.loginView = loginView;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package interface_adapters.login_interface_adapters;

import use_cases.user_login_use_cases.UserLoginOutputBoundary;

public interface UserLoginViewI {
void display();
void setChatsPresenter(UserLoginOutputBoundary outputBoundary);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package use_cases.user_registration_use_cases;
package interface_adapters.user_registration_interface_adapters;
/**
* Presenter interface that presents information, or gets input from user
* */
public interface UserExistsOutputBoundary {
public interface UserExistsOutputView {
/**
* Gets the verification code from the user
* */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package use_cases.user_registration_use_cases;
package interface_adapters.user_registration_interface_adapters;

import data_access.Database;
import use_cases.user_registration_use_cases.VerificationCodeDeliveryManager;
import use_cases.user_registration_use_cases.createMailMan;

/**
* This is the class responsible for getting processing the input given by user, and either allowing verification,
Expand All @@ -9,9 +11,9 @@
public class UserExistsPresenter {
private final VerificationCodeDeliveryManager verCodeDeliveryManager;
Database database;
UserExistsOutputBoundary existsOutputBoundary;
UserExistsOutputView existsOutputBoundary;

public UserExistsPresenter(Database database, UserExistsOutputBoundary existsOutputBoundary, createMailMan mailMan){
public UserExistsPresenter(Database database, UserExistsOutputView existsOutputBoundary, createMailMan mailMan){
this.database = database;
this.existsOutputBoundary = existsOutputBoundary;
//The responsibility of dealing with verification is passed onto this class
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package interface_adapters.user_registration_interface_adapters;

public interface UserVerificationOutputView {
void getLoginCredentials();
void cannotVerify();

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package use_cases.user_registration_use_cases;
package interface_adapters.user_registration_interface_adapters;

import data_access.Database;

Expand All @@ -8,11 +8,11 @@ public class UserVerificationPresenter {
private String password;
private String email;

private final UserVerificationOutputBoundary verificationOutputBoundary;
private final UserVerificationOutputView verificationOutputBoundary;

private int code;

public UserVerificationPresenter(Database database, UserVerificationOutputBoundary verificationOutputBoundary){
public UserVerificationPresenter(Database database, UserVerificationOutputView verificationOutputBoundary){
this.database = database;
this.verificationOutputBoundary = verificationOutputBoundary;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package use_cases.user_registration_use_cases;
package interface_adapters.user_registration_interface_adapters;

public interface userRegCredentialsRetriever {
void getUserCredentials();
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/screens/login_screen/AppScreenCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package screens.login_screen;
import interface_adapters.login_interface_adapters.UserLoginViewI;
import screens.appscreen.AppScreen;
import use_cases.user_login_use_cases.UserLoginOutputBoundary;

import java.util.ArrayList;

public class AppScreenCreator implements UserLoginViewI {
private String username ;
private ArrayList<String> chats;
private boolean userNotExists;
private boolean passNotMatched;
AppScreen appScreen;
public AppScreenCreator(){
}
@Override
public void display() {
if(userNotExists|| passNotMatched){
showUnableToLogin();
}else{
/*this.appScreen = new AppScreen(username, chats);*/
System.out.println(username);
}
}

private void showUnableToLogin() {
System.out.println("unable to login");
}
@Override
public void setChatsPresenter(UserLoginOutputBoundary chatsPresenter){
this.username = chatsPresenter.getUsername();
this.chats = (ArrayList<String>) chatsPresenter.getChats();
this.userNotExists = chatsPresenter.isNotExists();
this.passNotMatched = chatsPresenter.isNotMatched();
}

}
33 changes: 25 additions & 8 deletions src/main/java/screens/login_screen/UserLoginUI.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
package screens.login_screen;
import use_cases.user_login_use_cases.UserLoginPresenter;
import use_cases.user_registration_use_cases.UserVerificationOutputBoundary;
import data_access.Database;
import data_access.UserDatabase;
import interface_adapters.login_interface_adapters.UserChatsPresenter;
import interface_adapters.login_interface_adapters.UserLoginViewI;
import use_cases.user_login_use_cases.UserLoginInputBoundary;
import interface_adapters.login_interface_adapters.UserLoginPresenter;
import use_cases.user_login_use_cases.UserLoginInteractor2;
import interface_adapters.user_registration_interface_adapters.UserVerificationOutputView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/** This is the screen on which the user enters his credentials in order to login **/
public class UserLoginUI implements ActionListener, UserVerificationOutputBoundary {
public class UserLoginUI implements ActionListener, UserVerificationOutputView {

private final UserLoginPresenter loginInteractor;
private final UserLoginPresenter loginPresenter;
JTextField credentialText;
JPasswordField passwordText;

public UserLoginUI(UserLoginPresenter loginInteractor){
this.loginInteractor = loginInteractor;
public UserLoginUI(UserLoginPresenter loginPresenter){
UserLoginViewI loginViewI = new AppScreenCreator();
this.loginPresenter = loginPresenter;
this.loginPresenter.setLoginView(loginViewI);
}
//For Testing Purposes
public static void main(String[] args){
Database userDB = new UserDatabase(new File("new"));
UserLoginInputBoundary inputBoundary = new UserLoginInteractor2(userDB, new UserChatsPresenter());
UserLoginPresenter loginPresenter1 = new UserLoginPresenter(userDB, inputBoundary);
UserLoginUI loginUI = new UserLoginUI(loginPresenter1);
loginUI.getLoginCredentials();
}
@Override
public void getLoginCredentials(){
Expand Down Expand Up @@ -69,7 +86,7 @@ public void cannotVerify() {
public void actionPerformed(ActionEvent e) {
String username = credentialText.getText();
String password = passwordText.getText();
loginInteractor.setLoginCredentials(username, password);
loginInteractor.tryLogin();
loginPresenter.setLoginCredentials(username, password);
loginPresenter.tryLogin();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package screens.user_registration_screen;
import data_access.Database;
import data_access.UserDatabase;
import interface_adapters.login_interface_adapters.UserChatsPresenter;
import interface_adapters.user_registration_interface_adapters.*;
import screens.login_screen.UserLoginUI;
import use_cases.user_login_use_cases.UserLoginPresenter;
import use_cases.user_login_use_cases.UserLoginInteractor2;
import interface_adapters.login_interface_adapters.UserLoginPresenter;
import use_cases.user_registration_use_cases.*;

import javax.swing.*;
Expand Down Expand Up @@ -84,10 +87,11 @@ public void getUserCredentials(){

public static void main(String[] args){
Database testDB = new UserDatabase(new File("newAccounts2"));
UserLoginPresenter userLoginInteractor = new UserLoginPresenter(testDB);
UserVerificationOutputBoundary loginUI = new UserLoginUI(userLoginInteractor);
UserLoginInteractor2 userLoginInteractor2 = new UserLoginInteractor2(testDB, new UserChatsPresenter());
UserLoginPresenter userLoginPresenter = new UserLoginPresenter(testDB, userLoginInteractor2);
UserVerificationOutputView loginUI = new UserLoginUI(userLoginPresenter);
UserVerificationPresenter verificationInteractor = new UserVerificationPresenter(testDB, loginUI);
UserExistsOutputBoundary verificationScreen = new UserVerificationScreen(verificationInteractor);
UserExistsOutputView verificationScreen = new UserVerificationScreen(verificationInteractor);
UserExistsPresenter existsInteractor = new UserExistsPresenter(testDB, verificationScreen, new verificationMethodFactory());
UserRegistrationUI testUI = new UserRegistrationUI(existsInteractor);
testUI.getUserCredentials();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package screens.user_registration_screen;
import use_cases.user_registration_use_cases.*;
import interface_adapters.user_registration_interface_adapters.UserExistsOutputView;
import interface_adapters.user_registration_interface_adapters.UserVerificationPresenter;

import javax.swing.*;
import java.awt.event.ActionEvent;
Expand All @@ -8,7 +9,7 @@
* This is the class that is responsible for retrieving the verification code form the user, or presenting if
* verification is not possible
* */
public class UserVerificationScreen implements UserExistsOutputBoundary, ActionListener {
public class UserVerificationScreen implements UserExistsOutputView, ActionListener {
private final JTextField verText = new JTextField(20);
private final UserVerificationPresenter verificationInputBoundary;
private int code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package use_cases.user_login_use_cases;

public interface UserLoginInputBoundary {
void tryLogin();
void login(String username, String password);

UserLoginOutputBoundary getChatsPresenter();

void setLoginCredentials(String username, String password);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package use_cases.user_login_use_cases;

import data_access.Database;
import entities.user_entities.User;

import java.util.ArrayList;

public class UserLoginInteractor2 implements UserLoginInputBoundary {
private final UserLoginOutputBoundary chatPresenter;
private final Database database;

private User user;

public UserLoginInteractor2(Database database, UserLoginOutputBoundary chatPresenter){
this.database = database;
this.chatPresenter = chatPresenter;
}

@Override
public void login(String username, String password) {
//will update this.chatPresenter
//TODO: change below, as its just temporary
chatPresenter.setChats(new ArrayList<>());
chatPresenter.setUsername(username);
chatPresenter.setUserNotExists(false);
chatPresenter.setPasswordNotMatched(false);

}

@Override
public UserLoginOutputBoundary getChatsPresenter() {
return this.chatPresenter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package use_cases.user_login_use_cases;

import java.util.List;

public interface UserLoginOutputBoundary {
void setUsername(String username);
void setChats(List<String> chats);
void setUserNotExists(boolean notExists);
void setPasswordNotMatched(boolean notMatched);

String getUsername();
List<String> getChats();
boolean isNotExists();
boolean isNotMatched();
}

This file was deleted.

Loading

0 comments on commit aa10e64

Please sign in to comment.