From 6bafd06fc0c181525eadea2bb8b7df45db069659 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Wed, 7 Feb 2024 09:05:50 -0500 Subject: [PATCH] Added historical balance checker and cleaned up actions layout on account page. --- .../perfin/control/AccountViewController.java | 39 +++++++- .../perfin/view/component/StyledText.java | 3 + src/main/resources/account-view.fxml | 89 +++++++++++-------- 3 files changed, 90 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/andrewlalis/perfin/control/AccountViewController.java b/src/main/java/com/andrewlalis/perfin/control/AccountViewController.java index 07d4fa7..4dc0fca 100644 --- a/src/main/java/com/andrewlalis/perfin/control/AccountViewController.java +++ b/src/main/java/com/andrewlalis/perfin/control/AccountViewController.java @@ -2,17 +2,25 @@ package com.andrewlalis.perfin.control; import com.andrewlalis.javafx_scene_router.RouteSelectionListener; import com.andrewlalis.perfin.data.AccountRepository; +import com.andrewlalis.perfin.data.util.CurrencyUtil; import com.andrewlalis.perfin.data.util.DateUtil; import com.andrewlalis.perfin.model.Account; +import com.andrewlalis.perfin.model.MoneyValue; import com.andrewlalis.perfin.model.Profile; import com.andrewlalis.perfin.view.component.AccountHistoryView; +import com.andrewlalis.perfin.view.component.validation.ValidationApplier; +import com.andrewlalis.perfin.view.component.validation.validators.PredicateValidator; +import javafx.application.Platform; import javafx.beans.binding.BooleanExpression; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; import javafx.fxml.FXML; import javafx.scene.control.Button; +import javafx.scene.control.DatePicker; import javafx.scene.control.Label; -import javafx.scene.layout.VBox; +import javafx.scene.layout.HBox; + +import java.time.*; import static com.andrewlalis.perfin.PerfinApp.router; @@ -30,10 +38,13 @@ public class AccountViewController implements RouteSelectionListener { @FXML public AccountHistoryView accountHistory; - @FXML public VBox actionsVBox; + @FXML public HBox actionsBox; + + @FXML public DatePicker balanceCheckerDatePicker; + @FXML public Button balanceCheckerButton; @FXML public void initialize() { - actionsVBox.getChildren().forEach(node -> { + actionsBox.getChildren().forEach(node -> { Button button = (Button) node; BooleanExpression buttonActive = accountArchivedProperty; if (button.getText().equalsIgnoreCase("Unarchive")) { @@ -43,6 +54,28 @@ public class AccountViewController implements RouteSelectionListener { button.managedProperty().bind(button.visibleProperty()); button.visibleProperty().bind(button.disableProperty().not()); }); + + var datePickerValid = new ValidationApplier<>(new PredicateValidator() + .addPredicate(date -> date.isBefore(LocalDate.now()), "Date must be in the past.") + ).attach(balanceCheckerDatePicker, balanceCheckerDatePicker.valueProperty()); + balanceCheckerButton.disableProperty().bind(datePickerValid.not()); + balanceCheckerButton.setOnAction(event -> { + LocalDate date = balanceCheckerDatePicker.getValue(); + final Instant timestamp = date.atStartOfDay(ZoneId.systemDefault()) + .withZoneSameInstant(ZoneOffset.UTC) + .toInstant(); + Profile.getCurrent().dataSource().mapRepoAsync( + AccountRepository.class, + repo -> repo.deriveBalance(account.id, timestamp) + ).thenAccept(balance -> Platform.runLater(() -> { + String msg = String.format( + "Your balance as of %s is %s, according to Perfin's data.", + date, + CurrencyUtil.formatMoney(new MoneyValue(balance, account.getCurrency())) + ); + Popups.message(balanceCheckerButton, msg); + })); + }); } @Override diff --git a/src/main/java/com/andrewlalis/perfin/view/component/StyledText.java b/src/main/java/com/andrewlalis/perfin/view/component/StyledText.java index cbba7fb..c5c0566 100644 --- a/src/main/java/com/andrewlalis/perfin/view/component/StyledText.java +++ b/src/main/java/com/andrewlalis/perfin/view/component/StyledText.java @@ -81,6 +81,7 @@ public class StyledText extends VBox { idx = 0; currentRun.setLength(0); currentParagraph = new TextFlow(); + currentParagraph.setStyle("-fx-text-fill: inherit;"); while (idx < text.length()) { if (text.startsWith("**", idx)) { @@ -118,6 +119,7 @@ public class StyledText extends VBox { int endIdx = text.indexOf(marker, idx + marker.length()); Text textItem = new Text(text.substring(idx + marker.length(), endIdx)); textItem.getStyleClass().add(styleClass); + textItem.setStyle("-fx-text-fill: inherit;"); currentParagraph.getChildren().add(textItem); idx = endIdx + marker.length(); } @@ -183,6 +185,7 @@ public class StyledText extends VBox { if (!currentParagraph.getChildren().isEmpty()) { flows.add(currentParagraph); currentParagraph = new TextFlow(); + currentParagraph.setStyle("-fx-text-fill: inherit;"); } } } diff --git a/src/main/resources/account-view.fxml b/src/main/resources/account-view.fxml index 89e8c50..a5ecd4e 100644 --- a/src/main/resources/account-view.fxml +++ b/src/main/resources/account-view.fxml @@ -5,6 +5,8 @@ + + - -
- - - - -
- - -