Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Registration Refactoring #34

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/emoji_manager/msg/Sender.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main.java.emoji_manager.msg;
package emoji_manager.msg;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* User_search_IA.UserPresenter makes us implement showProfile to invert the dependency
*/
public class UserPresenterClass implements UserPresenter{
public class UserPresenterClass implements UserPresenter {
@Override
public String showProfile(String username) {
// setting up access to the database of users:
Expand Down
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
@@ -0,0 +1,26 @@
package interface_adapters.user_registration_interface_adapters;
/**
* Presenter interface that presents information, or gets input from user
* */
public interface UserExistsOutputView {
/**
* Gets the verification code from the user
* */
void getVerificationCredentials();
/**
* Presents that the user exists in the database
* */
void presentUserExistsMessage();
/**
* Gets the code from the input boundary object
* @param code Verification code
* */
void getCode(int code);
/**
* Gets the user credentials from the input boundary object
* @param email Email address of the user
* @param password Password of the user
* @param username Username of the user
* */
void getUserCredentials(String username, String password, String email);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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,
* presenting the 'user exists' message, and sending the verification code, depending on the business logic
* */
public class UserExistsPresenter {
private final VerificationCodeDeliveryManager verCodeDeliveryManager;
Database database;
UserExistsOutputView existsOutputBoundary;

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
this.verCodeDeliveryManager = new VerificationCodeDeliveryManager(mailMan);
}
/**
* Proceeds to verification and sends code, or presents an error message, depending on whether a user with
* such credentials is in the database.
* @param username Username
* @param email Email
* @param password Password
* */

public void register(String username, String password, String email) {
if(!database.UserExists(username, email)){
//This may need to change if verCodeDeliveryManager decides not to create integer codes.
int code = this.verCodeDeliveryManager.getVerCode();
existsOutputBoundary.getCode(code);
existsOutputBoundary.getUserCredentials(username, password, email);
existsOutputBoundary.getVerificationCredentials();
this.verCodeDeliveryManager.deliverCode(email);
}else{
existsOutputBoundary.presentUserExistsMessage();
}
}
/**
* Sets the verification stream given by the user, to send the code
* @param type The verification stream
* */
public void setCodeDeliveryMethod(String type) {
this.verCodeDeliveryManager.setMailMan(type);
}
}
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
@@ -0,0 +1,48 @@
package interface_adapters.user_registration_interface_adapters;

import data_access.Database;

public class UserVerificationPresenter {
private final Database database;
private String username;
private String password;
private String email;

private final UserVerificationOutputView verificationOutputBoundary;

private int code;

public UserVerificationPresenter(Database database, UserVerificationOutputView verificationOutputBoundary){
this.database = database;
this.verificationOutputBoundary = verificationOutputBoundary;
}
/**
* Compares code with this.code, if they match, the program will proceed to ask for login credentials
* Else, it will present a message that verification is not possible
* @param code code inputted by the user
* */
public void verify(int code) {
System.out.println(this.code);
if(code == this.code){
database.createUser(this.username, this.password, this.email, "Basic");
verificationOutputBoundary.getLoginCredentials();
}else{
verificationOutputBoundary.cannotVerify();
}

}
/**
* Sets the code to compare for verification
* @param code verification code*/
public void setCode(int code) {
this.code = code;
}
/**
* Sets the user credentials for this object
* */
public void setCredentials(String username, String password, String email){
this.username = username;
this.password = password;
this.email = email;
}
}
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
39 changes: 39 additions & 0 deletions src/main/java/screens/login_screen/AppScreenCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package screens.login_screen;
import interface_adapters.appscreen.AppScreenLoader;
import interface_adapters.login_interface_adapters.UserChatsPresenter;
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 boolean userNotExists;
private boolean passNotMatched;
AppScreenLoader appScreenLoader;
public AppScreenCreator(){
}
@Override
public void display() {
if(userNotExists|| passNotMatched){
showUnableToLogin();
}else{
/*this.appScreen = new AppScreen(username, chats);*/
//Could be null pointer exception if setChatsPresenter is not called before the below
appScreenLoader.openScreen();
}
}

private void showUnableToLogin() {
System.out.println("unable to login");
}
@Override
public void setChatsPresenter(UserLoginOutputBoundary chatsPresenter){
String username = chatsPresenter.getUsername();
ArrayList<String> chats = (ArrayList<String>) chatsPresenter.getChats();
this.userNotExists = chatsPresenter.isNotExists();
this.passNotMatched = chatsPresenter.isNotMatched();
appScreenLoader = new AppScreenLoader(username, chats);
}
Madhavan7 marked this conversation as resolved.
Show resolved Hide resolved

}
31 changes: 24 additions & 7 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 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 use_cases.user_registration_use_cases.UserVerificationOutputBoundary;
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 UserLoginInputBoundary loginInteractor;
private final UserLoginPresenter loginPresenter;
JTextField credentialText;
JPasswordField passwordText;

public UserLoginUI(UserLoginInputBoundary 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"));
Madhavan7 marked this conversation as resolved.
Show resolved Hide resolved
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();
}
}
Loading