From a05c77e8ff17d10a0aa7d895f63950d99954dba5 Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Sat, 7 Sep 2024 18:09:36 +0200 Subject: [PATCH] feat: added stiff to ViewService --- .../munera/services/ViewService.java | 19 ++++++++++++++++- .../munera/views/expenses/ExpensesView.java | 2 +- .../munera/views/people/PeopleView.java | 21 ++----------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/application/munera/services/ViewService.java b/src/main/java/com/application/munera/services/ViewService.java index c5cd8d1..4cbc673 100644 --- a/src/main/java/com/application/munera/services/ViewService.java +++ b/src/main/java/com/application/munera/services/ViewService.java @@ -6,6 +6,8 @@ import com.application.munera.data.ExpenseType; import com.vaadin.flow.component.html.Span; import org.springframework.stereotype.Service; +import java.math.BigDecimal; + @Service public class ViewService { @@ -15,7 +17,7 @@ public class ViewService { this.expenseService = expenseService; } - public Span createBadge(final Expense expense) { + public Span createExpenseBadge(final Expense expense) { final var isExpensePaid = Boolean.TRUE.equals(this.expenseService.isExpensePaid(expense)); final var badgeMessage = determineBadgeMessage(expense.getExpenseType(), isExpensePaid); @@ -26,6 +28,21 @@ public class ViewService { return badge; } + public Span createPersonBadge(BigDecimal netBalance) { + Span badge = new Span(); + if (netBalance.compareTo(BigDecimal.ZERO) < 0) { + badge.setText("Credit"); + badge.getElement().getThemeList().add("badge success"); + } else if (netBalance.compareTo(BigDecimal.ZERO) > 0) { + badge.setText("Debit"); + badge.getElement().getThemeList().add("badge error"); + } else { + badge.setText("Clear"); + badge.getElement().getThemeList().add("badge contrast"); + } + return badge; + } + private BadgeMessage determineBadgeMessage(ExpenseType type, boolean isPaid) { return switch (type) { case CREDIT -> isPaid ? BadgeMessage.PAID_TO_SOMEONE : BadgeMessage.OWED_BY_SOMEONE; 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 d2d6c51..69cac84 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -96,7 +96,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { grid.addColumn(Expense::getDate).setHeader("Date").setSortable(true).setSortProperty("date"); // grid.addColumn(expenseEvent -> expenseEvent.getEvent().getName()).setHeader("Event").setSortable(true); - grid.addColumn(new ComponentRenderer<>(this.viewService::createBadge)).setHeader("Status").setSortable(true); + grid.addColumn(new ComponentRenderer<>(this.viewService::createExpenseBadge)).setHeader("Status").setSortable(true); grid.getColumns().forEach(col -> col.setAutoWidth(true)); grid.setItems(this.expenseService.findAllOrderByDateDescending()); 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 a932c03..7105ce5 100644 --- a/src/main/java/com/application/munera/views/people/PeopleView.java +++ b/src/main/java/com/application/munera/views/people/PeopleView.java @@ -13,7 +13,6 @@ import com.vaadin.flow.component.dependency.Uses; import com.vaadin.flow.component.formlayout.FormLayout; import com.vaadin.flow.component.grid.GridVariant; import com.vaadin.flow.component.html.Div; -import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.icon.Icon; import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.notification.Notification.Position; @@ -33,7 +32,6 @@ import com.vaadin.flow.router.Route; import jakarta.annotation.security.PermitAll; import org.springframework.orm.ObjectOptimisticLockingFailureException; -import java.math.BigDecimal; import java.util.List; import java.util.Optional; @@ -80,8 +78,8 @@ public class PeopleView extends Div implements BeforeEnterObserver { grid.addHierarchyColumn(this::getNodeName).setHeader("Name"); 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 this.viewService.createBadge(((Expense) persona)); + if (persona instanceof Person) return this.viewService.createPersonBadge(personService.calculateNetBalance((Person) persona)); + else return this.viewService.createExpenseBadge(((Expense) persona)); })).setHeader("Balance Status"); List people = (List) personService.findAll(); @@ -227,21 +225,6 @@ public class PeopleView extends Div implements BeforeEnterObserver { } - private Span createPersonBadge(BigDecimal netBalance) { - Span badge = new Span(); - if (netBalance.compareTo(BigDecimal.ZERO) < 0) { - badge.setText("Credit"); - badge.getElement().getThemeList().add("badge success"); - } else if (netBalance.compareTo(BigDecimal.ZERO) > 0) { - badge.setText("Debit"); - badge.getElement().getThemeList().add("badge error"); - } else { - badge.setText("Clear"); - badge.getElement().getThemeList().add("badge contrast"); - } - return badge; - } - public void setGridData(List people) { for (Person user : people) { // Add the person as a root item