From db4a0ad407ca57a894085378ed3f557a17aa954a Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Sat, 7 Sep 2024 17:38:18 +0200 Subject: [PATCH] feat: improve ExpenseView labels --- .../com/application/munera/data/Expense.java | 1 + .../munera/views/expenses/BadgeMessage.java | 27 +++++++++++++++++ .../munera/views/expenses/ExpensesView.java | 29 ++++++++++++------- 3 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/application/munera/views/expenses/BadgeMessage.java diff --git a/src/main/java/com/application/munera/data/Expense.java b/src/main/java/com/application/munera/data/Expense.java index aa718f9..270ece6 100644 --- a/src/main/java/com/application/munera/data/Expense.java +++ b/src/main/java/com/application/munera/data/Expense.java @@ -38,6 +38,7 @@ public class Expense extends AbstractEntity { @Column(name = "PeriodicExpense", nullable = false) private Boolean isPeriodic; + @Enumerated(EnumType.ORDINAL) @Column(name = "PeriodUnit") private PeriodUnit periodUnit; diff --git a/src/main/java/com/application/munera/views/expenses/BadgeMessage.java b/src/main/java/com/application/munera/views/expenses/BadgeMessage.java new file mode 100644 index 0000000..ae19e6e --- /dev/null +++ b/src/main/java/com/application/munera/views/expenses/BadgeMessage.java @@ -0,0 +1,27 @@ +package com.application.munera.views.expenses; + +public enum BadgeMessage { + PAID_TO_SOMEONE("Paid to someone", "badge success"), + PAID_TO_YOU("Paid to you", "badge success"), + PAID("Paid", "badge success"), + OWED_BY_SOMEONE("Owed by someone", "badge warning"), + OWED_TO_YOU("Owed to you", "badge warning"), + NOT_PAID("Not paid", "badge warning"), + UNKNOWN("Unknown status", "badge error"); + + private final String text; + private final String theme; + + BadgeMessage(String text, String theme) { + this.text = text; + this.theme = theme; + } + + public String getText() { + return text; + } + + public String getTheme() { + return theme; + } +} 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 2a87a68..843147a 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -98,7 +98,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<>(expense1 -> createBadge(expenseService.isExpensePaid(expense1)))).setHeader("Status").setSortable(true); + grid.addColumn(new ComponentRenderer<>(this::createBadge)).setHeader("Status").setSortable(true); grid.getColumns().forEach(col -> col.setAutoWidth(true)); grid.setItems(this.expenseService.findAllOrderByDateDescending()); @@ -292,15 +292,24 @@ public class ExpensesView extends Div implements BeforeEnterObserver { periodInterval.setVisible(isPeriodicChecked); } - private Span createBadge(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 createBadge(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; + }; + } + } \ No newline at end of file