diff --git a/src/main/java/com/andrewlalis/perfin/control/AccountViewController.java b/src/main/java/com/andrewlalis/perfin/control/AccountViewController.java index d512da8..fc16078 100644 --- a/src/main/java/com/andrewlalis/perfin/control/AccountViewController.java +++ b/src/main/java/com/andrewlalis/perfin/control/AccountViewController.java @@ -91,23 +91,19 @@ public class AccountViewController implements RouteSelectionListener { Popups.message(balanceCheckerButton, msg); })); }); - - accountProperty.addListener((observable, oldValue, newValue) -> { - accountHistory.clear(); - if (newValue == null) { - balanceTextProperty.set(null); - } else { - accountHistory.setAccountId(newValue.id); - accountHistory.loadMoreHistory(); - Profile.getCurrent().dataSource().getAccountBalanceText(newValue) - .thenAccept(s -> Platform.runLater(() -> balanceTextProperty.set(s))); - } - }); } @Override public void onRouteSelected(Object context) { - this.accountProperty.set((Account) context); + accountHistory.clear(); + balanceTextProperty.set(null); + if (context instanceof Account account) { + this.accountProperty.set(account); + accountHistory.setAccountId(account.id); + accountHistory.loadMoreHistory(); + Profile.getCurrent().dataSource().getAccountBalanceText(account) + .thenAccept(s -> Platform.runLater(() -> balanceTextProperty.set(s))); + } } @FXML diff --git a/src/main/java/com/andrewlalis/perfin/control/TransactionsViewController.java b/src/main/java/com/andrewlalis/perfin/control/TransactionsViewController.java index d2018b6..26d1f23 100644 --- a/src/main/java/com/andrewlalis/perfin/control/TransactionsViewController.java +++ b/src/main/java/com/andrewlalis/perfin/control/TransactionsViewController.java @@ -47,7 +47,11 @@ import static com.andrewlalis.perfin.PerfinApp.router; * to a specific page. */ public class TransactionsViewController implements RouteSelectionListener { - public static List DEFAULT_SORTS = List.of(Sort.desc("timestamp")); + public static List DEFAULT_SORTS = List.of( + Sort.desc("timestamp"), + Sort.desc("amount"), + Sort.desc("currency") + ); public record RouteContext(Long selectedTransactionId) {} @FXML public BorderPane transactionsListBorderPane; @@ -188,15 +192,28 @@ public class TransactionsViewController implements RouteSelectionListener { private List getCurrentSearchFilters() { List filters = new ArrayList<>(); if (searchField.getText() != null && !searchField.getText().isBlank()) { + final String text = searchField.getText().strip(); // Special case: for input like "#123", search directly for the transaction id. - if (searchField.getText().strip().matches("#\\d+")) { - int idQuery = Integer.parseInt(searchField.getText().strip().substring(1)); + if (text.matches("#\\d+")) { + int idQuery = Integer.parseInt(text.substring(1)); var filter = new SearchFilter.Builder().where("id = ?").withArg(idQuery).build(); return List.of(filter); } + // Special case: for input like "tag: abc", search directly for transactions with tags like that. + if (text.matches("tag:\\s*.+")) { + String tagQuery = "%" + text.substring(4).strip().toLowerCase() + "%"; + var filter = new SearchFilter.Builder().where(""" + id IN ( + SELECT ttj.transaction_id + FROM transaction_tag_join ttj + LEFT JOIN transaction_tag tt ON tt.id = ttj.tag_id + WHERE LOWER(tt.name) LIKE ? + )""").withArg(tagQuery).build(); + return List.of(filter); + } // General case: split the input into a list of terms, then apply each term in a LIKE %term% query. - var likeTerms = Arrays.stream(searchField.getText().strip().toLowerCase().split("\\s+")) + var likeTerms = Arrays.stream(text.toLowerCase().split("\\s+")) .map(t -> '%'+t+'%') .toList(); var builder = new SearchFilter.Builder();