Skip to content

Commit

Permalink
Merge branch 'main' into MyEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleM7139 authored Dec 3, 2023
2 parents 2177371 + 2fd95a0 commit 633f542
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 179 deletions.
3 changes: 2 additions & 1 deletion src/app/LoginUseCaseFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app;

import data_access.CoordinatesFromIP;
import entity.Location.CommonLocationFactory;
import entity.Users.CommonUserFactory;
import entity.Users.UserFactory;
Expand Down Expand Up @@ -51,7 +52,7 @@ private static LoginController createLoginUseCase(
UserFactory userFactory = new CommonUserFactory();

LoginInputBoundary loginInteractor = new LoginInteractor(
userDataAccessObject, loginOutputBoundary,currentUserDataAccessObject, new CommonLocationFactory());
userDataAccessObject, loginOutputBoundary,currentUserDataAccessObject, new CommonLocationFactory(), new CoordinatesFromIP());

return new LoginController(loginInteractor);
}
Expand Down
23 changes: 19 additions & 4 deletions src/app/MainFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import data_access.*;
import entity.Events.CommonEventFactory;
import entity.Location.CommonLocationFactory;
import entity.Location.Location;
import entity.Users.CommonUserFactory;
import entity.Users.User;
import interface_adapter.ViewManagerModel;
Expand All @@ -15,6 +16,7 @@
import interface_adapter.generate_static_map.GenerateStaticMapViewModel;
import interface_adapter.get_current_user.GetCurrentUserController;
import interface_adapter.get_current_user.GetCurrentUserPresenter;
import interface_adapter.get_current_user.GetCurrentUserState;
import interface_adapter.get_current_user.GetCurrentUserViewModel;
import interface_adapter.get_direction.GetDirectionController;
import interface_adapter.get_direction.GetDirectionPresenter;
Expand Down Expand Up @@ -79,10 +81,8 @@ public static void main(String[] args) throws IOException {
FileEventDataAccessObject fileEventDataAccessObject = new FileEventDataAccessObject("events.csv",new CommonEventFactory(),new CommonLocationFactory(),formatter);
FileUserDataAccessObject fileUserDataAccessObject = new FileUserDataAccessObject("users.csv", new CommonUserFactory(), fileEventDataAccessObject);
InMemoryCurrentUserDAO currentUserDAO = new InMemoryCurrentUserDAO();
CommonUserFactory userFactory = new CommonUserFactory();
User temporaryUser = userFactory.create("username","123",5,"m","contact");
currentUserDAO.changeUser(temporaryUser);
fileUserDataAccessObject.save(temporaryUser);

setUpTempUser(currentUserDAO,fileUserDataAccessObject,getCurrentUserViewModel);

//Create controllers
BackOutController backOutController = BackOutUseCaseFactory.createBackOutUseCase(viewManagerModel);
Expand Down Expand Up @@ -118,6 +118,7 @@ public static void main(String[] args) throws IOException {
createEventController, createEventViewModel, getCurrentUserViewModel, generateStaticMapController,generateStaticMapViewModel,getIDsViewModel);
views.add(loggedInView.getRootPane(), loggedInView.viewName);
loggedInViewModel.addPropertyChangeListener(loggedInView); // Because HomeView constructor doesn't add the view to the view model.
getCurrentUserViewModel.addPropertyChangeListener(loggedInView);

MyEventsView myEventsView = new MyEventsView(getIDsController,getIDsViewModel,getCurrentUserController,getCurrentUserViewModel, onlyGetEventDetailsController,backOutController,getEventDetailsViewModel);
views.add(myEventsView.getRootPane(),myEventsView.viewName);
Expand All @@ -141,4 +142,18 @@ public static void main(String[] args) throws IOException {
application.pack();
application.setVisible(true);
}

private static void setUpTempUser(InMemoryCurrentUserDAO currentUserDAO, FileUserDataAccessObject fileUserDataAccessObject,
GetCurrentUserViewModel getCurrentUserViewModel) throws IOException {
CommonUserFactory userFactory = new CommonUserFactory();
CommonLocationFactory locationFactory = new CommonLocationFactory();
Location temporaryLocation = locationFactory.makeLocation("(50,-75)");
User temporaryUser = userFactory.create("username","123",5,"m","contact");
temporaryUser.setLocation(temporaryLocation);
currentUserDAO.changeUser(temporaryUser);
GetCurrentUserState tempState = getCurrentUserViewModel.getState();
tempState.setUserCoordinates(temporaryUser.getLocation().getCoordinates());
tempState.setUsername(temporaryUser.getUsername());
fileUserDataAccessObject.save(temporaryUser);
}
}
6 changes: 4 additions & 2 deletions src/data_access/CoordinatesFromIP.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package data_access;

import entity.Location.CoordinatesToAddress;
import use_case.common_interfaces.GetCoordinatesIP;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -13,13 +14,14 @@
/**
* This class is responsible for gathering the location information from one's ip using an API call.
*/
public class CoordinatesFromIP {
public class CoordinatesFromIP implements GetCoordinatesIP {
/**
* Uses a user's ip and finds the coordinates from the ip.
* @return returns the coordinates of the location given by the user's ip.
* @throws IOException error occurs with the api call. Here, empty coordinates would be returned.
*/
public static String[] getCoordinates() throws IOException {
@Override
public String[] getCoordinates() throws IOException {
String[] result = {};
try {
URL url = new URL("http://ip-api.com/csv/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public GetCurrentUserPresenter(GetCurrentUserViewModel getCurrentUserViewModel){
public void prepareView(GetCurrentUserOutputData outputData) {
GetCurrentUserState state = getCurrentUserViewModel.getState();
state.setUsername(outputData.getCurrentUser());
state.setUserCoordinates(outputData.getUserCoordinates());
getCurrentUserViewModel.setState(state);
getCurrentUserViewModel.firePropertyChanged();
}
Expand Down
15 changes: 15 additions & 0 deletions src/interface_adapter/get_current_user/GetCurrentUserState.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
public class GetCurrentUserState {
private String username;

private String[] userCoordinates;

public GetCurrentUserState(GetCurrentUserState copy){
this.username = copy.username;
this.userCoordinates = copy.userCoordinates;
}

public GetCurrentUserState(){}
Expand All @@ -18,9 +21,21 @@ public GetCurrentUserState(){}
*/
public String getUsername(){return this.username;}

/**
* Gets the coordinates of the logged in user
* @return the coordinates of the logged in user
*/
public String[] getUserCoordinates(){return this.userCoordinates;}

/**
* Sets the state's username instance.
* @param username the username that is being set
*/
public void setUsername(String username){this.username = username;}

/**
* Saves the user coordinates
* @param userCoordinates the current user's coordinates
*/
public void setUserCoordinates(String[] userCoordinates){this.userCoordinates = userCoordinates;}
}
9 changes: 6 additions & 3 deletions src/interface_adapter/login/LoginPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import interface_adapter.get_current_user.GetCurrentUserViewModel;
import interface_adapter.logged_in.LoggedInState;
import interface_adapter.logged_in.LoggedInViewModel;
import interface_adapter.signup.SignupViewModel;
import use_case.login.LoginOutputBoundary;
import use_case.login.LoginOutputData;

import java.io.IOException;

public class LoginPresenter implements LoginOutputBoundary {

private final LoginViewModel loginViewModel;
Expand All @@ -30,17 +31,19 @@ public LoginPresenter(ViewManagerModel viewManagerModel,
}

@Override
public void prepareSuccessView(LoginOutputData response) {
public void prepareSuccessView(LoginOutputData response) throws IOException {
// On success, switch to the logged in view.

LoggedInState loggedInState = loggedInViewModel.getState();
loggedInState.setUsername(response.getUsername());

GetCurrentUserState getCurrentUserState = getCurrentUserViewModel.getState();
getCurrentUserState.setUsername(response.getUsername()); // start tracking the user upon success login
getCurrentUserState.setUsername(response.getUsername());
getCurrentUserState.setUserCoordinates(response.getUserCoordinates());
getCurrentUserViewModel.setState(getCurrentUserState);
getCurrentUserViewModel.firePropertyChanged();


this.loggedInViewModel.setState(loggedInState);
this.loggedInViewModel.firePropertyChanged();

Expand Down
7 changes: 7 additions & 0 deletions src/use_case/common_interfaces/GetCoordinatesIP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package use_case.common_interfaces;

import java.io.IOException;

public interface GetCoordinatesIP {
String[] getCoordinates() throws IOException;
}
3 changes: 2 additions & 1 deletion src/use_case/get_current_user/GetCurrentUserInteractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public GetCurrentUserInteractor(GetCurrentUserOutputBoundary presenter, CurrentU
@Override
public void execute() {
User user = currentUserDataAccessObject.getCurrentUser();
String[] userCoordinates = user.getLocation().getCoordinates();
if (user != null){
String username = user.getUsername();
GetCurrentUserOutputData outputData = new GetCurrentUserOutputData(username);
GetCurrentUserOutputData outputData = new GetCurrentUserOutputData(username, userCoordinates);
presenter.prepareView(outputData);
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/use_case/get_current_user/GetCurrentUserOutputData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
public class GetCurrentUserOutputData {
private final String currentUser;

private final String[] userCoordinates;

/**
* Constructor for GetCurrentUserOutputData
* @param currentUser the username of the user logged in
* @param userCoordinates the coordinates of the current user.
*/
public GetCurrentUserOutputData(String currentUser){
public GetCurrentUserOutputData(String currentUser, String[] userCoordinates){
this.currentUser = currentUser;
this.userCoordinates = userCoordinates;
}

/**
Expand All @@ -21,4 +25,10 @@ public GetCurrentUserOutputData(String currentUser){
public String getCurrentUser() {
return currentUser;
}

/**
* Provides the coordinates of the logged in user
* @return the coordinates of the logged in user
*/
public String[] getUserCoordinates(){return userCoordinates;}
}
11 changes: 9 additions & 2 deletions src/use_case/login/LoginInteractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import interface_adapter.ViewModel;
import interface_adapter.login.LoginViewModel;
import interface_adapter.signup.SignupViewModel;
import use_case.common_interfaces.GetCoordinatesIP;
import use_case.signup.*;

import java.io.IOException;
Expand All @@ -26,12 +27,16 @@ public class LoginInteractor implements LoginInputBoundary {

final LocationFactory locationFactory;

final GetCoordinatesIP getCoordinatesIP;

public LoginInteractor(LoginUserDataAccessInterface userDataAccessInterface,
LoginOutputBoundary loginOutputBoundary, LoginCurrentUserDataAccessInterface currentUserDataAccessObject, LocationFactory locationFactory) {
LoginOutputBoundary loginOutputBoundary, LoginCurrentUserDataAccessInterface currentUserDataAccessObject, LocationFactory locationFactory,
GetCoordinatesIP getCoordinatesIP) {
this.userDataAccessObject = userDataAccessInterface;
this.loginPresenter = loginOutputBoundary;
this.currentUserDataAccessObject = currentUserDataAccessObject;
this.locationFactory = locationFactory;
this.getCoordinatesIP = getCoordinatesIP;
}

@Override
Expand All @@ -49,8 +54,9 @@ public void execute(LoginInputData loginInputData) throws IOException {
loginPresenter.prepareFailView("Incorrect password for " + username + ".");
} else {
User user = userDataAccessObject.get(loginInputData.getUsername());
String[] currentCoordinates = CoordinatesFromIP.getCoordinates();
String[] currentCoordinates = getCoordinatesIP.getCoordinates();
String formattedCoordinates = String.format("(%s,%s)",currentCoordinates[0], currentCoordinates[1]);

// Location userLocation = locationFactory.makeLocation(formattedCoordinates);
// user.setLocation(userLocation);
LoginOutputData loginOutputData = new LoginOutputData(user.getUsername(), false);
Expand Down Expand Up @@ -95,4 +101,5 @@ public void execute(LoginInputData loginInputData) throws IOException {
// interactor.execute(inputData3);
//
// }

}
4 changes: 3 additions & 1 deletion src/use_case/login/LoginOutputBoundary.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import interface_adapter.ViewModel;

import java.io.IOException;

public interface LoginOutputBoundary {
void prepareSuccessView(LoginOutputData user);
void prepareSuccessView(LoginOutputData user) throws IOException;

void prepareLinkView(ViewModel viewModel);

Expand Down
8 changes: 5 additions & 3 deletions src/use_case/login/LoginOutputData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
public class LoginOutputData{
private final String username;

private boolean useCaseFailed;
private final String[] userCoordinates;

public LoginOutputData(String username,boolean useCaseFailed){
public LoginOutputData(String username, String[] userCoordinates){
this.userCoordinates = userCoordinates;
this.username=username;
this.useCaseFailed = useCaseFailed;
}
public String getUsername(){
return username;
}

public String[] getUserCoordinates(){return userCoordinates;}

}
20 changes: 10 additions & 10 deletions src/view/HomeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public class HomeView extends javax.swing.JFrame implements PropertyChangeListen
* Creates new form HomeView
*/
public final String viewName = "Home";
private final int height = 504;
private final int width = 350;
private final LoggedInViewModel loggedInViewModel;
private final LoggedInController loggedInController;
private final SearchNearbyController searchNearbyController;
Expand Down Expand Up @@ -235,11 +237,9 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
MapImage_LABEL.setBackground(new java.awt.Color(204, 204, 255));
MapImage_LABEL.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

// TODO: This image is a placeholder, replace with Bing Maps API png # Mikee?

CoordinatesFromIP coordinatesFromIP = new CoordinatesFromIP();
String[] currentCoordinates = coordinatesFromIP.getCoordinates();
generateStaticMapController.execute(currentCoordinates, 100,350, 504);
String[] currentCoordinates = getCurrentUserViewModel.getState().getUserCoordinates();
generateStaticMapController.execute(currentCoordinates, 100,width, height);

// TODO: Import logout image icon to src/view
LogoutIcon_LABEL.setIcon(new javax.swing.ImageIcon("/Users/submergedduck/Desktop/CSC207/LogOutIcon.png"));
Expand Down Expand Up @@ -382,12 +382,6 @@ private void CreateEvent_BUTTONActionPerformed(java.awt.event.ActionEvent evt) t
private void SearchEvent_BUTTONActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
if (evt.getSource().equals(SearchEvent_BUTTON)) {
try {
// TODO right now the IP API is not working, so I will fake a location. The code commented out should be the right code to call.
// String[] coordinates = CoordinatesFromIP.getCoordinates();
// CoordinatesToAddress coordinatesToAddress = new CoordinatesToAddress(coordinates);
// String address = coordinatesToAddress.getAddress();
// LocationFactory factory = new CommonLocationFactory();
// Location userLocation = factory.create(coordinates, address, "Canada");
LocationFactory factory = new CommonLocationFactory();
Location userLocation = factory.makeLocation("(43.665510,-79.387280)");
searchNearbyController.execute(userLocation);
Expand All @@ -407,6 +401,12 @@ public void propertyChange(PropertyChangeEvent evt) {
GenerateStaticMapState state = (GenerateStaticMapState)evt.getNewValue();
BufferedImage generatedMap = state.getGeneratedMap();
MapImage_LABEL.setIcon(new javax.swing.ImageIcon(generatedMap));
} else if (evt.getNewValue() instanceof GetCurrentUserState){
GetCurrentUserState state = (GetCurrentUserState)evt.getNewValue();
try {
generateStaticMapController.execute(state.getUserCoordinates(),100,width,height);
} catch (IOException e) {
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/view/LoginView.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package view;


import data_access.CoordinatesFromIP;
import data_access.InMemoryCurrentUserDAO;
import data_access.InMemoryUsersDataAccessObject;
import entity.Location.CommonLocationFactory;
Expand Down Expand Up @@ -459,7 +460,7 @@ public static void main(String[] args) {
LoginUserDataAccessInterface inMemoryUserDAO = new InMemoryUsersDataAccessObject();
inMemoryUserDAO.save(new CommonUser("aa","aaa",1,"",""));

LoginInputBoundary interactor = new LoginInteractor(new InMemoryUsersDataAccessObject(),presenter, new InMemoryCurrentUserDAO(),new CommonLocationFactory());
LoginInputBoundary interactor = new LoginInteractor(new InMemoryUsersDataAccessObject(),presenter, new InMemoryCurrentUserDAO(),new CommonLocationFactory(), new CoordinatesFromIP());
LoginController loginController = new LoginController(interactor);
LoginView loginView = new LoginView(loginViewModel,loginController, signupViewModel);
loginViewModel.addPropertyChangeListener(loginView);
Expand Down
Loading

0 comments on commit 633f542

Please sign in to comment.