diff --git a/src/main/java/com/andrewlalis/perfin/control/AccountsViewController.java b/src/main/java/com/andrewlalis/perfin/control/AccountsViewController.java index 0228df5..bc70353 100644 --- a/src/main/java/com/andrewlalis/perfin/control/AccountsViewController.java +++ b/src/main/java/com/andrewlalis/perfin/control/AccountsViewController.java @@ -1,9 +1,8 @@ package com.andrewlalis.perfin.control; import com.andrewlalis.javafx_scene_router.RouteSelectionListener; -import com.andrewlalis.perfin.data.pagination.PageRequest; -import com.andrewlalis.perfin.data.pagination.Sort; import com.andrewlalis.perfin.data.util.CurrencyUtil; +import com.andrewlalis.perfin.model.Account; import com.andrewlalis.perfin.model.MoneyValue; import com.andrewlalis.perfin.model.Profile; import com.andrewlalis.perfin.view.component.AccountTile; @@ -14,6 +13,8 @@ import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.layout.FlowPane; +import java.util.List; + import static com.andrewlalis.perfin.PerfinApp.router; public class AccountsViewController implements RouteSelectionListener { @@ -47,14 +48,14 @@ public class AccountsViewController implements RouteSelectionListener { public void refreshAccounts() { Profile.whenLoaded(profile -> { - Thread.ofVirtual().start(() -> { - profile.getDataSource().useAccountRepository(repo -> { - var page = repo.findAll(PageRequest.unpaged(Sort.asc("created_at"))); - Platform.runLater(() -> { - accountsPane.getChildren().setAll(page.items().stream().map(AccountTile::new).toList()); - }); - }); - }); + Thread.ofVirtual().start(() -> profile.getDataSource().useAccountRepository(repo -> { + List accounts = repo.findAllOrderedByRecentHistory(); + Platform.runLater(() -> accountsPane.getChildren() + .setAll(accounts.stream() + .map(AccountTile::new) + .toList() + )); + })); // Compute grand totals! Thread.ofVirtual().start(() -> { var totals = profile.getDataSource().getCombinedAccountBalances(); diff --git a/src/main/java/com/andrewlalis/perfin/data/AccountRepository.java b/src/main/java/com/andrewlalis/perfin/data/AccountRepository.java index 53f7550..3fc4e1f 100644 --- a/src/main/java/com/andrewlalis/perfin/data/AccountRepository.java +++ b/src/main/java/com/andrewlalis/perfin/data/AccountRepository.java @@ -16,6 +16,7 @@ import java.util.Set; public interface AccountRepository extends AutoCloseable { long insert(AccountType type, String accountNumber, String name, Currency currency); Page findAll(PageRequest pagination); + List findAllOrderedByRecentHistory(); List findAllByCurrency(Currency currency); Optional findById(long id); void updateName(long id, String name); diff --git a/src/main/java/com/andrewlalis/perfin/data/impl/JdbcAccountRepository.java b/src/main/java/com/andrewlalis/perfin/data/impl/JdbcAccountRepository.java index f501afc..0d426d1 100644 --- a/src/main/java/com/andrewlalis/perfin/data/impl/JdbcAccountRepository.java +++ b/src/main/java/com/andrewlalis/perfin/data/impl/JdbcAccountRepository.java @@ -46,6 +46,19 @@ public record JdbcAccountRepository(Connection conn) implements AccountRepositor return DbUtil.findAll(conn, "SELECT * FROM account WHERE NOT archived", pagination, JdbcAccountRepository::parseAccount); } + @Override + public List findAllOrderedByRecentHistory() { + return DbUtil.findAll( + conn, + """ + SELECT DISTINCT ON (account.id) account.*, ahi.timestamp AS _ + FROM account + LEFT OUTER JOIN account_history_item ahi ON ahi.account_id = account.id + ORDER BY ahi.timestamp DESC, account.created_at DESC""", + JdbcAccountRepository::parseAccount + ); + } + @Override public List findAllByCurrency(Currency currency) { return DbUtil.findAll( diff --git a/src/main/java/com/andrewlalis/perfin/view/component/AccountTile.java b/src/main/java/com/andrewlalis/perfin/view/component/AccountTile.java index 1331512..3f5c7dc 100644 --- a/src/main/java/com/andrewlalis/perfin/view/component/AccountTile.java +++ b/src/main/java/com/andrewlalis/perfin/view/component/AccountTile.java @@ -38,9 +38,6 @@ public class AccountTile extends BorderPane { -fx-padding: 5px; -fx-cursor: hand; """); - final Color color = ACCOUNT_TYPE_COLORS.get(account.getType()); -// var fill = new BackgroundFill(color, new CornerRadii(3.0), null); -// setBackground(new Background(fill)); setTop(getHeader(account)); setBottom(getFooter(account));