From cf97709ad101cafaa04ced12c486bd12a7bc4fd9 Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Sat, 7 Sep 2024 17:46:13 +0200 Subject: [PATCH] feat: improve PeopleView labels --- .../munera/views/expenses/ExpensesView.java | 1 - .../munera/views/people/PeopleView.java | 32 ++++++++++++------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/application/munera/views/expenses/ExpensesView.java b/src/main/java/com/application/munera/views/expenses/ExpensesView.java index 843147a..42496b2 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -311,5 +311,4 @@ public class ExpensesView extends Div implements BeforeEnterObserver { default -> BadgeMessage.UNKNOWN; }; } - } \ No newline at end of file diff --git a/src/main/java/com/application/munera/views/people/PeopleView.java b/src/main/java/com/application/munera/views/people/PeopleView.java index 118aa2c..c023092 100644 --- a/src/main/java/com/application/munera/views/people/PeopleView.java +++ b/src/main/java/com/application/munera/views/people/PeopleView.java @@ -1,10 +1,10 @@ package com.application.munera.views.people; -import com.application.munera.data.Expense; -import com.application.munera.data.Person; +import com.application.munera.data.*; import com.application.munera.services.ExpenseService; import com.application.munera.services.PersonService; import com.application.munera.views.MainLayout; +import com.application.munera.views.expenses.BadgeMessage; import com.vaadin.flow.component.UI; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.ButtonVariant; @@ -78,7 +78,7 @@ public class PeopleView extends Div implements BeforeEnterObserver { grid.addColumn(this::getNodeCost).setHeader("Total Expenses Value").setSortable(true); grid.addColumn(new ComponentRenderer<>(persona -> { if (persona instanceof Person) return createPersonBadge(personService.calculateNetBalance((Person) persona)); - else return createExpenseBadge(((Expense) persona).getIsPaid()); + else return createExpenseBadge(((Expense) persona)); })).setHeader("Balance Status"); List people = (List) personService.findAll(); @@ -239,18 +239,26 @@ public class PeopleView extends Div implements BeforeEnterObserver { return badge; } - private Span createExpenseBadge(Boolean isExpensePaid) { - Span badge = new Span(); - if (Boolean.TRUE.equals(isExpensePaid)) { - badge.setText("Paid"); - badge.getElement().getThemeList().add("badge success"); - } else { - badge.setText("Owed"); - badge.getElement().getThemeList().add("badge error"); - } + private Span createExpenseBadge(final Expense expense) { + final var isExpensePaid = Boolean.TRUE.equals(this.expenseService.isExpensePaid(expense)); + final var badgeMessage = determineBadgeMessage(expense.getExpenseType(), isExpensePaid); + + final var badge = new Span(); + badge.setText(badgeMessage.getText()); + badge.getElement().getThemeList().add(badgeMessage.getTheme()); + return badge; } + private BadgeMessage determineBadgeMessage(ExpenseType type, boolean isPaid) { + return switch (type) { + case CREDIT -> isPaid ? BadgeMessage.PAID_TO_SOMEONE : BadgeMessage.OWED_BY_SOMEONE; + case DEBIT -> isPaid ? BadgeMessage.PAID_TO_YOU : BadgeMessage.OWED_TO_YOU; + case NONE -> isPaid ? BadgeMessage.PAID : BadgeMessage.NOT_PAID; + default -> BadgeMessage.UNKNOWN; + }; + } + public void setGridData(List people) { for (Person user : people) { // Add the person as a root item