diff --git a/src/main/java/com/application/munera/services/ViewService.java b/src/main/java/com/application/munera/services/ViewService.java new file mode 100644 index 0000000..c5cd8d1 --- /dev/null +++ b/src/main/java/com/application/munera/services/ViewService.java @@ -0,0 +1,38 @@ +package com.application.munera.services; + +import com.application.munera.data.BadgeMessage; +import com.application.munera.data.Expense; +import com.application.munera.data.ExpenseType; +import com.vaadin.flow.component.html.Span; +import org.springframework.stereotype.Service; + +@Service +public class ViewService { + + private final ExpenseService expenseService; + + public ViewService(ExpenseService expenseService) { + this.expenseService = expenseService; + } + + public 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; + }; + } +} + 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 42496b2..d2d6c51 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -1,10 +1,7 @@ package com.application.munera.views.expenses; import com.application.munera.data.*; -import com.application.munera.services.CategoryService; -import com.application.munera.services.EventService; -import com.application.munera.services.ExpenseService; -import com.application.munera.services.PersonService; +import com.application.munera.services.*; import com.application.munera.views.MainLayout; import com.vaadin.flow.component.UI; import com.vaadin.flow.component.button.Button; @@ -17,7 +14,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; @@ -62,6 +58,7 @@ public class ExpensesView extends Div implements BeforeEnterObserver { private final CategoryService categoryService; private final PersonService personService; private final EventService eventService; + private final ViewService viewService; private TextField name; private TextField cost; private ComboBox category; @@ -74,11 +71,12 @@ public class ExpensesView extends Div implements BeforeEnterObserver { private MultiSelectComboBox creditors; private MultiSelectComboBox debtors; private ComboBox event; - public ExpensesView(ExpenseService expenseService, CategoryService categoryService, PersonService personService, EventService eventService) { + public ExpensesView(ExpenseService expenseService, CategoryService categoryService, PersonService personService, EventService eventService, ViewService viewService) { this.expenseService = expenseService; this.categoryService = categoryService; this.personService = personService; this.eventService = eventService; + this.viewService = viewService; addClassNames("expenses-view"); // Create UI @@ -98,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::createBadge)).setHeader("Status").setSortable(true); + grid.addColumn(new ComponentRenderer<>(this.viewService::createBadge)).setHeader("Status").setSortable(true); grid.getColumns().forEach(col -> col.setAutoWidth(true)); grid.setItems(this.expenseService.findAllOrderByDateDescending()); @@ -291,24 +289,4 @@ public class ExpensesView extends Div implements BeforeEnterObserver { periodUnit.setVisible(isPeriodicChecked); periodInterval.setVisible(isPeriodicChecked); } - - 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 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 354e5d4..a932c03 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,11 @@ package com.application.munera.views.people; -import com.application.munera.data.*; +import com.application.munera.data.Expense; +import com.application.munera.data.Person; import com.application.munera.services.ExpenseService; import com.application.munera.services.PersonService; +import com.application.munera.services.ViewService; import com.application.munera.views.MainLayout; -import com.application.munera.data.BadgeMessage; import com.vaadin.flow.component.UI; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.ButtonVariant; @@ -56,13 +57,15 @@ public class PeopleView extends Div implements BeforeEnterObserver { private Person person; private final PersonService personService; private final ExpenseService expenseService; + private final ViewService viewService; private TextField firstName; private TextField lastName; private EmailField email; - public PeopleView(PersonService personService, ExpenseService expenseService) { + public PeopleView(PersonService personService, ExpenseService expenseService, ViewService viewService) { this.personService = personService; this.expenseService = expenseService; + this.viewService = viewService; addClassNames("expenses-view"); // Create UI @@ -78,7 +81,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)); + else return this.viewService.createBadge(((Expense) persona)); })).setHeader("Balance Status"); List people = (List) personService.findAll(); @@ -239,26 +242,6 @@ public class PeopleView extends Div implements BeforeEnterObserver { return badge; } - 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