diff options
Diffstat (limited to 'ui-fx/src/main/java')
| -rw-r--r-- | ui-fx/src/main/java/market/guess/ui/desktop/controllers/AppController.java | 112 |
1 files changed, 71 insertions, 41 deletions
diff --git a/ui-fx/src/main/java/market/guess/ui/desktop/controllers/AppController.java b/ui-fx/src/main/java/market/guess/ui/desktop/controllers/AppController.java index c582051..416770e 100644 --- a/ui-fx/src/main/java/market/guess/ui/desktop/controllers/AppController.java +++ b/ui-fx/src/main/java/market/guess/ui/desktop/controllers/AppController.java | |||
| @@ -2,8 +2,12 @@ package market.guess.ui.desktop.controllers; | |||
| 2 | 2 | ||
| 3 | import java.math.BigDecimal; | 3 | import java.math.BigDecimal; |
| 4 | import java.nio.file.Paths; | 4 | import java.nio.file.Paths; |
| 5 | import java.util.concurrent.CompletableFuture; | ||
| 6 | import java.util.function.Consumer; | ||
| 7 | import java.util.function.Supplier; | ||
| 5 | import javafx.animation.FadeTransition; | 8 | import javafx.animation.FadeTransition; |
| 6 | import javafx.animation.PauseTransition; | 9 | import javafx.animation.PauseTransition; |
| 10 | import javafx.application.Platform; | ||
| 7 | import javafx.beans.binding.Bindings; | 11 | import javafx.beans.binding.Bindings; |
| 8 | import javafx.collections.ListChangeListener; | 12 | import javafx.collections.ListChangeListener; |
| 9 | import javafx.css.PseudoClass; | 13 | import javafx.css.PseudoClass; |
| @@ -20,6 +24,7 @@ import javafx.scene.layout.StackPane; | |||
| 20 | import javafx.stage.FileChooser; | 24 | import javafx.stage.FileChooser; |
| 21 | import javafx.stage.FileChooser.ExtensionFilter; | 25 | import javafx.stage.FileChooser.ExtensionFilter; |
| 22 | import javafx.util.Duration; | 26 | import javafx.util.Duration; |
| 27 | import market.guess.api.Result; | ||
| 23 | import market.guess.model.event.CreateEventRequest; | 28 | import market.guess.model.event.CreateEventRequest; |
| 24 | import market.guess.model.event.EventStatus; | 29 | import market.guess.model.event.EventStatus; |
| 25 | import market.guess.model.event.MechanismType; | 30 | import market.guess.model.event.MechanismType; |
| @@ -201,10 +206,8 @@ public class AppController { | |||
| 201 | return appView != null ? appView.getState() : null; | 206 | return appView != null ? appView.getState() : null; |
| 202 | } | 207 | } |
| 203 | 208 | ||
| 204 | /** | ||
| 205 | * Refreshes UI state across all components. Kept for lifecycle compatibility; delegates to | 209 | * Refreshes UI state across all components. Kept for lifecycle compatibility; delegates to |
| 206 | * targeted update methods. | 210 | * targeted update methods. |
| 207 | */ | ||
| 208 | public void refresh() { | 211 | public void refresh() { |
| 209 | applyInitialState(); | 212 | applyInitialState(); |
| 210 | } | 213 | } |
| @@ -532,26 +535,39 @@ public class AppController { | |||
| 532 | var state = appView.getState(); | 535 | var state = appView.getState(); |
| 533 | var e = state.getSelectedEvent(); | 536 | var e = state.getSelectedEvent(); |
| 534 | if (e == null) return; | 537 | if (e == null) return; |
| 535 | var res = appView.getMarketContext().settleEvent(state.getActingUserName(), e.id, option); | 538 | var user = state.getActingUserName(); |
| 536 | if (res.isSuccess()) { | 539 | async( |
| 537 | state.refreshData(); | 540 | () -> appView.getMarketContext().settleEvent(user, e.id, option), |
| 538 | state.setDialog(ModalDialog.NONE); | 541 | res -> { |
| 539 | toast("Event resolved as " + option); | 542 | if (res.isSuccess()) { |
| 540 | } else { | 543 | state.refreshData(); |
| 541 | toast("Failed to resolve event: " + res.getMessage()); | 544 | state.setDialog(ModalDialog.NONE); |
| 542 | } | 545 | toast("Event resolved as " + option); |
| 546 | } else { | ||
| 547 | toast("Failed to resolve event: " + res.getMessage()); | ||
| 548 | } | ||
| 549 | }); | ||
| 543 | } | 550 | } |
| 544 | 551 | ||
| 545 | public void openEvent(EventData e) { | 552 | public void openEvent(EventData e) { |
| 546 | var state = appView.getState(); | 553 | if (e.status != EventStatus.DRAFT) { |
| 547 | if (e.status == EventStatus.DRAFT) { | 554 | showTradePanel(e); |
| 548 | var res = appView.getCatalogContext().openEvent(e.id); | 555 | return; |
| 549 | if (!res.isSuccess()) { | ||
| 550 | toast("Failed to open event: " + res.getMessage()); | ||
| 551 | return; | ||
| 552 | } | ||
| 553 | state.refreshData(); | ||
| 554 | } | 556 | } |
| 557 | async( | ||
| 558 | () -> appView.getCatalogContext().openEvent(e.id), | ||
| 559 | res -> { | ||
| 560 | if (!res.isSuccess()) { | ||
| 561 | toast("Failed to open event: " + res.getMessage()); | ||
| 562 | return; | ||
| 563 | } | ||
| 564 | appView.getState().refreshData(); | ||
| 565 | showTradePanel(e); | ||
| 566 | }); | ||
| 567 | } | ||
| 568 | |||
| 569 | private void showTradePanel(EventData e) { | ||
| 570 | var state = appView.getState(); | ||
| 555 | // The trade panel lives on the Users tab and follows the selected event. | 571 | // The trade panel lives on the Users tab and follows the selected event. |
| 556 | state.setSelectedEventId(e.id); | 572 | state.setSelectedEventId(e.id); |
| 557 | state.setActiveTab(AppTab.USERS); | 573 | state.setActiveTab(AppTab.USERS); |
| @@ -565,28 +581,42 @@ public class AppController { | |||
| 565 | public void placeOrder( | 581 | public void placeOrder( |
| 566 | String eventKey, String optionKey, String side, BigDecimal price, long quantity) { | 582 | String eventKey, String optionKey, String side, BigDecimal price, long quantity) { |
| 567 | var state = appView.getState(); | 583 | var state = appView.getState(); |
| 568 | var res = | 584 | var user = state.getActingUserName(); |
| 569 | appView | 585 | async( |
| 570 | .getMarketContext() | 586 | () -> |
| 571 | .placeOrder(state.getActingUserName(), eventKey, optionKey, side, price, quantity); | 587 | appView |
| 572 | if (res.isSuccess()) { | 588 | .getMarketContext() |
| 573 | state.refreshData(); | 589 | .placeOrder(user, eventKey, optionKey, side, price, quantity), |
| 574 | var receipt = res.getData(); | 590 | res -> { |
| 575 | boolean isBuy = "BUY".equalsIgnoreCase(side); | 591 | if (res.isSuccess()) { |
| 576 | if (receipt.trades() == null || receipt.trades().isEmpty()) { | 592 | state.refreshData(); |
| 577 | toast("Resting " + side + " order placed: " + quantity + " @ " + Format.money(price)); | 593 | var receipt = res.getData(); |
| 578 | } else { | 594 | boolean isBuy = "BUY".equalsIgnoreCase(side); |
| 579 | toast( | 595 | if (receipt.trades() == null || receipt.trades().isEmpty()) { |
| 580 | "Order placed " | 596 | toast("Resting " + side + " order placed: " + quantity + " @ " + Format.money(price)); |
| 581 | + (isBuy ? "Paid " : "Received ") | 597 | } else { |
| 582 | + Format.money(new BigDecimal(receipt.totalPaid())) | 598 | toast( |
| 583 | + " for " | 599 | "Order placed " |
| 584 | + quantity | 600 | + (isBuy ? "Paid " : "Received ") |
| 585 | + " " | 601 | + Format.money(new BigDecimal(receipt.totalPaid())) |
| 586 | + optionKey); | 602 | + " for " |
| 587 | } | 603 | + quantity |
| 588 | } else { | 604 | + " " |
| 589 | toast("Trade failed: " + res.getMessage()); | 605 | + optionKey); |
| 590 | } | 606 | } |
| 607 | } else { | ||
| 608 | toast("Trade failed: " + res.getMessage()); | ||
| 609 | } | ||
| 610 | }); | ||
| 611 | } | ||
| 612 | |||
| 613 | private <T> void async(Supplier<Result<T>> call, Consumer<Result<T>> then) { | ||
| 614 | CompletableFuture.supplyAsync(call) | ||
| 615 | .thenAcceptAsync(then, Platform::runLater) | ||
| 616 | .exceptionally( | ||
| 617 | ex -> { | ||
| 618 | Platform.runLater(() -> toast("Something went wrong: " + ex.getMessage())); | ||
| 619 | return null; | ||
| 620 | }); | ||
| 591 | } | 621 | } |
| 592 | } | 622 | } |