Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions src/main/java/app/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import persistence.OrganizationDao;
import persistence.db.Database;

public class Main extends Application {

import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -72,7 +74,6 @@ public static void main(String[] args) {
} catch (Exception e) {
e.printStackTrace();
}


@Override
public void start(Stage stage) throws Exception {
Expand All @@ -86,7 +87,7 @@ public void start(Stage stage) throws Exception {

SceneManager.setStage(stage);

Parent root = FXMLLoader.load(getClass().getResource("/fxml/Home.fxml"));
Parent root = FXMLLoader.load(getClass().getResource("/view/MainView.fxml"));
Scene scene = new Scene(root, 1275, 725);

stage.setTitle("GiveHope");
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/app/SceneManager.java

This file was deleted.

21 changes: 21 additions & 0 deletions src/main/java/ui/Page.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ui;

public enum Page {
HOME("Home.fxml"),
PROFILE("MyProfile.fxml"),
SIGNIN("SignIn.fxml"),
REGISTER("Register.fxml"),
ORGANIZATIONS("OrganizationsView.fxml"),
MYPROFILE("MyProfile.fxml");


private final String fileName;

Page(String fileName) {
this.fileName = fileName;
}

public String getFileName() {
return fileName;
}
}
21 changes: 13 additions & 8 deletions src/main/java/ui/controller/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import persistence.UserDao;
import ui.Page;

import java.io.IOException;
import java.util.function.Consumer;

public class HomeController implements NavigationAware {

public class HomeController {
private Consumer<Page> onNavigate;

public void setOnNavigate(Consumer<Page> onNavigate) {
this.onNavigate = onNavigate;
}

public Button signInButton;

@FXML
private void handleSignIn() throws IOException {
UserDao userDao = new UserDao();
PasswordHasher hasher = new Sha256PasswordHasher();
UserSignIn userSignIn = new UserSignIn(hasher, userDao);

SceneManager.switchTo("/fxml/signIn.fxml", new SignInController(userSignIn));
onNavigate.accept(Page.SIGNIN);
}

@FXML
private void handleBrowseOrganizations() throws IOException {
SceneManager.switchTo("/fxml/OrganizationsView.fxml");
private void handleBrowseOrganizations() {
onNavigate.accept(Page.ORGANIZATIONS);
}

}
62 changes: 62 additions & 0 deletions src/main/java/ui/controller/MainController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ui.controller;

import application.user.UserRegister;
import application.user.UserSignIn;
import integration.security.Sha256PasswordHasher;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.layout.BorderPane;
import persistence.UserDao;
import ui.Page;

import java.io.IOException;

public class MainController {

@FXML
private BorderPane mainPane;

@FXML
private NavbarController navbarController;

public void initialize() {
try {
navbarController.setOnNavigate(page -> {
try {
loadPage(page);
} catch (IOException e) {

}
});
loadPage(Page.HOME);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public void loadPage(Page page) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/" + page.getFileName()));
Parent root = loader.load();

Object controller = loader.getController();
if (controller instanceof NavigationAware navigationAware) {
navigationAware.setOnNavigate(p -> {
try {
loadPage(p);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
if (controller instanceof SignInController c) {
c.setUserSignIn(new UserSignIn(new Sha256PasswordHasher(), new UserDao()));
}
if (controller instanceof RegisterController c) {
c.setUserRegister(new UserRegister(new Sha256PasswordHasher(), new UserDao()));
}

mainPane.setCenter(root);
navbarController.setActivePage(page);
}
}
53 changes: 48 additions & 5 deletions src/main/java/ui/controller/NavbarController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
package ui.controller;

import app.SceneManager;
import javafx.event.ActionEvent;
import java.util.function.Consumer;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import ui.Page;

public class NavbarController {

@javafx.fxml.FXML
private void handleGoHome(ActionEvent event) throws Exception {
SceneManager.switchTo("/fxml/Home.fxml");
private Consumer<Page> onNavigate;
@FXML
private Button homeBtn;
@FXML
private Button orgBtn;
@FXML
private Button signInBtn;

public void setOnNavigate(Consumer<Page> onNavigate) {
this.onNavigate = onNavigate;
}

public void setActivePage(Page page) {
homeBtn.getStyleClass().remove("nav-button-active");
orgBtn.getStyleClass().remove("nav-button-active");
signInBtn.getStyleClass().remove("nav-button-active");

Button buttonForPage = switch (page) {
case HOME -> homeBtn;
case ORGANIZATIONS -> orgBtn;
case SIGNIN -> signInBtn;
default -> null;
};

if (buttonForPage != null) {
buttonForPage.getStyleClass().add("nav-button-active");
}
}

public void goHome() {
onNavigate.accept(Page.HOME);
}

public void goToSignIn() {
onNavigate.accept(Page.SIGNIN);
}

public void goToOrganizations() {
onNavigate.accept(Page.ORGANIZATIONS);
}

public void goToMyProfile() {
onNavigate.accept(Page.PROFILE);
}
}
9 changes: 9 additions & 0 deletions src/main/java/ui/controller/NavigationAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ui.controller;

import ui.Page;

import java.util.function.Consumer;

public interface NavigationAware {
void setOnNavigate(Consumer<Page> onNavigate);
}
43 changes: 12 additions & 31 deletions src/main/java/ui/controller/OrganizationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import javafx.scene.layout.VBox;
import persistence.OrganizationDao;
import persistence.UserDao;
import ui.Page;
import util.SessionManager;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class OrganizationController {
Expand All @@ -42,31 +44,14 @@ public class OrganizationController {
private int page = 0;
private final int PER_PAGE = 15;

private Consumer<Page> onNavigate;

public void setOnNavigate(Consumer<Page> onNavigate) {
this.onNavigate = onNavigate;
}

public void initialize() {
try {
if (SessionManager.isSignedIn()) {
signInAndMyProfileBtn.setText("My profile");
signInAndMyProfileBtn.setOnAction(e -> {
try {
SceneManager.switchTo("/fxml/MyProfile.fxml");
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
} else {
signInAndMyProfileBtn.setText("Sign In");
UserDao userDao = new UserDao();
PasswordHasher hasher = new Sha256PasswordHasher();
UserSignIn userSignIn = new UserSignIn(hasher, userDao);
signInAndMyProfileBtn.setOnAction(e -> {
try {
SceneManager.switchTo("/fxml/signIn.fxml", new SignInController(userSignIn));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
}

OrganizationDao dao = new OrganizationDao();
allOrgs = dao.getAll();
System.out.println("Fetched: " + allOrgs.size() + " organizations");
Expand Down Expand Up @@ -154,17 +139,13 @@ private void nextPage() {
}

@FXML
private void goHome() throws IOException {
SceneManager.switchTo("/fxml/Home.fxml");
private void handleGoHome() {
onNavigate.accept(Page.HOME);
}

@FXML
private void goToSignIn() throws IOException {
UserDao userDao = new UserDao();
PasswordHasher hasher = new Sha256PasswordHasher();
UserSignIn userSignIn = new UserSignIn(hasher, userDao);

SceneManager.switchTo("/fxml/signIn.fxml", new SignInController(userSignIn));
private void handleSignIn() {
onNavigate.accept(Page.SIGNIN);
}

}
29 changes: 15 additions & 14 deletions src/main/java/ui/controller/RegisterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import persistence.UserDao;
import ui.Page;

import java.io.IOException;
import java.util.function.Consumer;

public class RegisterController {
public class RegisterController implements NavigationAware {

@FXML private TextField usernameField;
@FXML private TextField emailField;
Expand All @@ -22,12 +24,17 @@ public class RegisterController {
@FXML private PasswordField confirmPasswordField;
@FXML private Label errorLabel;

private final UserRegister userRegister;
private UserRegister userRegister;
private Consumer<Page> onNavigate;

public RegisterController(UserRegister userRegister) {
public void setUserRegister(UserRegister userRegister) {
this.userRegister = userRegister;
}

public void setOnNavigate(Consumer<Page> onNavigate) {
this.onNavigate = onNavigate;
}

@FXML
private void handleRegister() {
String username = usernameField.getText().trim();
Expand All @@ -50,25 +57,19 @@ private void handleRegister() {

try {
userRegister.execute(username, phoneNr, password, email);
SceneManager.switchTo("/fxml/signIn.fxml", new SignInController(
new UserSignIn(new Sha256PasswordHasher(), new UserDao())
));
onNavigate.accept(Page.SIGNIN);
} catch (IllegalArgumentException e) {
errorLabel.setText(e.getMessage());
} catch (IOException e) {
errorLabel.setText("Something went wrong, please try again.");
}
}

@FXML
private void handleSignIn() throws IOException {
PasswordHasher hasher = new Sha256PasswordHasher();
UserSignIn userSignIn = new UserSignIn(hasher, new UserDao());
SceneManager.switchTo("/fxml/signIn.fxml", new SignInController(userSignIn));
private void handleSignIn() {
onNavigate.accept(Page.SIGNIN);
}

@FXML
private void handleGoHome() throws IOException {
SceneManager.switchTo("/fxml/Home.fxml");
private void handleGoHome() {
onNavigate.accept(Page.HOME);
}
}
Loading