Skip to content

Commit

Permalink
Add single Account filter for evaluation view
Browse files Browse the repository at this point in the history
  • Loading branch information
Bios-Marcel committed Jul 14, 2024
1 parent 473cf62 commit 8c8021b
Showing 1 changed file with 60 additions and 10 deletions.
70 changes: 60 additions & 10 deletions src/main/java/link/biosmarcel/baka/view/EvaluationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.util.Duration;
import link.biosmarcel.baka.ApplicationState;
import link.biosmarcel.baka.data.Payment;
import link.biosmarcel.baka.data.Account;
import org.jspecify.annotations.Nullable;

import java.math.BigDecimal;
Expand All @@ -37,6 +35,14 @@ public class EvaluationView extends BakaTab {
private final ObjectProperty<LocalDate> startDate;
private final ObjectProperty<LocalDate> endDate;

private final ObservableList<Account> accounts;
private final ComboBox<Account> accountFilter;

/**
* This account will be abused to specify we want to show data across all accounts. This is due to the fact that null misbehaves.
*/
private final Account ALL_ACCOUNTS = new Account();

public EvaluationView(ApplicationState state) {
super("Evaluation", state);

Expand All @@ -47,6 +53,8 @@ public EvaluationView(ApplicationState state) {
this.startDate = new SimpleObjectProperty<>();
this.endDate = new SimpleObjectProperty<>();

this.accounts = FXCollections.observableArrayList();

final var spendingChartAmountAxis = new NumberAxis();
spendingChartAmountAxis.setForceZeroInRange(true);

Expand Down Expand Up @@ -89,14 +97,21 @@ public EvaluationView(ApplicationState state) {
.with(TemporalAdjusters.lastDayOfMonth()));
endDate.bind(endDatePicker.valueProperty());

startDate.addListener((_, _, _) -> updateCharts());
endDate.addListener((_, _, _) -> updateCharts());
accountFilter = new ComboBox<>();
accountFilter.setItems(accounts);
accountFilter.setButtonCell(new AccountComboBoxCell());
accountFilter.setCellFactory(_ -> new AccountComboBoxCell());

final HBox topBar = new HBox(
startDatePicker,
endDatePicker
endDatePicker,
accountFilter
);
topBar.setSpacing(5.0);
topBar.setSpacing(2.5);

startDate.addListener((_, _, _) -> updateCharts());
endDate.addListener((_, _, _) -> updateCharts());
accountFilter.getSelectionModel().selectedItemProperty().addListener((_, _, _) -> updateCharts());

final VBox layout = new VBox(
topBar,
Expand All @@ -113,6 +128,21 @@ public EvaluationView(ApplicationState state) {
setContent(layout);
}

private class AccountComboBoxCell extends ListCell<@Nullable Account> {
@Override
protected void updateItem(@Nullable Account item, boolean empty) {
super.updateItem(item, empty);

if (ALL_ACCOUNTS.equals(item)) {
setText("All");
} else if (item != null) {
setText(item.name);
} else {
setText("");
}
}
}

private void updateCharts() {
final String unclassified = "unclassified";
final String ignore = "ignore";
Expand All @@ -123,10 +153,15 @@ private void updateCharts() {
final XYChart.Series<Number, Number> balanceSeries = new XYChart.Series<>();
balanceData.setAll(balanceSeries);

final Collection<Payment> sorted = state.data.payments
.stream()
var sortedStream = state.data.payments.stream();
final var account = accountFilter.getSelectionModel().selectedItemProperty().get();
if (!ALL_ACCOUNTS.equals(account)) {
sortedStream = sortedStream.filter(payment -> account.equals(payment.account));
}
final var sorted = sortedStream
.sorted(Comparator.comparing(a -> a.effectiveDate))
.toList();

for (final var payment : sorted) {
if (payment.effectiveDate.isBefore(startDate.get().atStartOfDay())) {
continue;
Expand Down Expand Up @@ -238,6 +273,21 @@ private static String renderMonth(final Month month) {

@Override
protected void onTabActivated() {
final var oldSelection = accountFilter.getSelectionModel().getSelectedItem();

accounts.clear();
// null is the all item
accounts.add(ALL_ACCOUNTS);
accounts.addAll(state.data.accounts);

final var oldSelectionIndex = accountFilter.getItems().indexOf(oldSelection);
if (oldSelectionIndex != -1) {
accountFilter.getSelectionModel().select(oldSelectionIndex);
} else {
// null is the all item
accountFilter.getSelectionModel().select(ALL_ACCOUNTS);
}

updateCharts();
}

Expand Down

0 comments on commit 8c8021b

Please sign in to comment.