Skip to content

Commit

Permalink
Updated FrontpageController
Browse files Browse the repository at this point in the history
Added methods do dynamically populate a scroll pane with checkmarks for filtering categories, and changed the logic of the filtering method. Removed unused methods.
  • Loading branch information
roaraf committed Apr 18, 2026
1 parent fda1312 commit a7dd6ce
Showing 1 changed file with 50 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> selectedCategories = new ArrayList<>();

private List<Charity> allCharities = new ArrayList<>();

Expand All @@ -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<String> 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);
Expand Down Expand Up @@ -142,69 +161,42 @@ public void handleFrontSearch(ActionEvent event) {
/**
* This method is used to filter the charities based on the selected filters.
*
* <p>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<Charity> getFilteredCharities() {
if (!verifiedFilter.isSelected()
&& !childrenFilter.isSelected()
&& !healthFilter.isSelected()
&& !emergencyAidFilter.isSelected()) {
return allCharities;
}

List<Charity> filteredCharities = new ArrayList<>();
List<Charity> 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;
}

/**
Expand Down

0 comments on commit a7dd6ce

Please sign in to comment.