From a7dd6ce0daafa6ec7f752c20711fa1a099374ce8 Mon Sep 17 00:00:00 2001 From: Roar Date: Sat, 18 Apr 2026 15:23:23 +0200 Subject: [PATCH] Updated FrontpageController Added methods do dynamically populate a scroll pane with checkmarks for filtering categories, and changed the logic of the filtering method. Removed unused methods. --- .../team6/controller/FrontpageController.java | 108 ++++++++---------- 1 file changed, 50 insertions(+), 58 deletions(-) diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/FrontpageController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/FrontpageController.java index 752148f..6ac9ca8 100644 --- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/FrontpageController.java +++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/FrontpageController.java @@ -17,6 +17,7 @@ import ntnu.systemutvikling.team6.controller.components.NavbarFooterController; import ntnu.systemutvikling.team6.controller.components.OrganizationCardController; import ntnu.systemutvikling.team6.database.DatabaseConnection; +import ntnu.systemutvikling.team6.database.Readers.CategorySelect; import ntnu.systemutvikling.team6.database.Readers.CharitySelect; import ntnu.systemutvikling.team6.database.Readers.DonationSelect; import ntnu.systemutvikling.team6.models.Charity; @@ -41,9 +42,8 @@ public class FrontpageController extends BaseController implements NavbarFooterC @FXML private Label PreApproved_Percentage; @FXML private TextField frontSearchField; @FXML private CheckBox verifiedFilter; - @FXML private CheckBox childrenFilter; - @FXML private CheckBox healthFilter; - @FXML private CheckBox emergencyAidFilter; + @FXML private javafx.scene.layout.VBox categoryList; + private final List selectedCategories = new ArrayList<>(); private List allCharities = new ArrayList<>(); @@ -64,8 +64,27 @@ public void initialize() { DatabaseConnection conn = new DatabaseConnection(); CharitySelect cdb = new CharitySelect(conn); DonationSelect ddb = new DonationSelect(conn); + CategorySelect categoryselect = new CategorySelect(conn); CharityRegistry charities = cdb.getCharitiesFromDB(); DonationRegistry donations = ddb.getDonationFromDB(); + List categories = categoryselect.getCategoriesFromDB(); + categories.sort(String::compareToIgnoreCase); + + for (String category : categories) { + CheckBox cb = new CheckBox(category); + + cb.setOnAction( + e -> { + if (cb.isSelected()) { + selectedCategories.add(category); + } else { + selectedCategories.remove(category); + } + displayCharities(getFilteredCharities()); + }); + + categoryList.getChildren().add(cb); + } allCharities = new ArrayList<>(charities.getAllCharities()); displayCharities(allCharities); @@ -142,69 +161,42 @@ public void handleFrontSearch(ActionEvent event) { /** * This method is used to filter the charities based on the selected filters. * + *

The filters are whether the charity was pre-verified, or if it falls under one (or more) of + * the given categories. + * * @return a list of filtered charities. */ private List getFilteredCharities() { - if (!verifiedFilter.isSelected() - && !childrenFilter.isSelected() - && !healthFilter.isSelected() - && !emergencyAidFilter.isSelected()) { - return allCharities; - } - List filteredCharities = new ArrayList<>(); + List filtered = new ArrayList<>(); + for (Charity charity : allCharities) { - if (matchesSelectedFilters(charity)) { - filteredCharities.add(charity); + + if (verifiedFilter.isSelected() && !charity.getPreApproved()) { + continue; } - } - return filteredCharities; - } - /** - * This method is used to check if a charity matches the selected filters. - * - * @param charity is the charity to be checked. - * @return true if the charity matches the selected filters, false otherwise. - */ - private boolean matchesSelectedFilters(Charity charity) { - return (verifiedFilter.isSelected() && charity.getPreApproved()) - || (childrenFilter.isSelected() && matchesKeywordCategory(charity, "children")) - || (healthFilter.isSelected() && matchesKeywordCategory(charity, "health")) - || (emergencyAidFilter.isSelected() && matchesKeywordCategory(charity, "emergency")); - } + if (!selectedCategories.isEmpty()) { - /** - * This method is used to check if a charity matches a specific category. - * - * @param charity is the charity to be checked. - * @param category is the category to check against. - * @return true if the charity matches the category, false otherwise. - */ - private boolean matchesKeywordCategory(Charity charity, String category) { - String text = (charity.getName() + " " + charity.getDescription()).toLowerCase(); - - return switch (category) { - case "children" -> - text.contains("child") - || text.contains("children") - || text.contains("barn") - || text.contains("youth") - || text.contains("young"); - case "health" -> - text.contains("health") - || text.contains("medical") - || text.contains("helse") - || text.contains("hospital") - || text.contains("care"); - case "emergency" -> - text.contains("emergency") - || text.contains("relief") - || text.contains("crisis") - || text.contains("aid") - || text.contains("disaster"); - default -> false; - }; + boolean hasMatch = false; + + for (String cat : charity.getCategory()) { + if (cat == null) continue; + + if (selectedCategories.contains(cat.toLowerCase().trim())) { + hasMatch = true; + break; + } + } + + if (!hasMatch) { + continue; + } + } + filtered.add(charity); + } + + return filtered; } /**