From 18d5d3cf211030035ee0b7338ff27c684f0a10e4 Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Sun, 7 Oct 2018 15:27:46 +0200 Subject: [PATCH] Update token label when tip has been sent --- .../java/ctbrec/ui/CtbrecApplication.java | 14 +++--- src/main/java/ctbrec/ui/ThumbOverviewTab.java | 6 +++ src/main/java/ctbrec/ui/TipDialog.java | 6 +++ src/main/java/ctbrec/ui/TokenLabel.java | 45 +++++++++++++++++++ 4 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 src/main/java/ctbrec/ui/TokenLabel.java diff --git a/src/main/java/ctbrec/ui/CtbrecApplication.java b/src/main/java/ctbrec/ui/CtbrecApplication.java index c86e27a3..fe7b4350 100644 --- a/src/main/java/ctbrec/ui/CtbrecApplication.java +++ b/src/main/java/ctbrec/ui/CtbrecApplication.java @@ -8,10 +8,13 @@ import java.lang.reflect.Type; import java.util.List; import java.util.Objects; import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.eventbus.AsyncEventBus; +import com.google.common.eventbus.EventBus; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; import com.squareup.moshi.Types; @@ -33,7 +36,6 @@ import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Button; -import javafx.scene.control.Label; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.control.TabPane.TabClosingPolicy; @@ -57,10 +59,12 @@ public class CtbrecApplication extends Application { static HostServices hostServices; private SettingsTab settingsTab; private TabPane tabPane = new TabPane(); + static EventBus bus; @Override public void start(Stage primaryStage) throws Exception { loadConfig(); + bus = new AsyncEventBus(Executors.newSingleThreadExecutor()); hostServices = getHostServices(); client = HttpClient.getInstance(); createRecorder(); @@ -112,7 +116,7 @@ public class CtbrecApplication extends Application { Button buyTokens = new Button("Buy Tokens"); buyTokens.setFont(Font.font(11)); buyTokens.setOnAction((e) -> DesktopIntergation.open(AFFILIATE_LINK)); - Label tokenBalance = new Label("Tokens: loading…"); + TokenLabel tokenBalance = new TokenLabel(); tokenBalance.setFont(Font.font(11)); HBox tokenPanel = new HBox(5, tokenBalance, buyTokens); //tokenPanel.setBackground(new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, new Insets(0)))); @@ -173,7 +177,7 @@ public class CtbrecApplication extends Application { }); } - private void loadTokenBalance(Label label) { + private void loadTokenBalance(TokenLabel label) { Task task = new Task() { @Override protected Integer call() throws Exception { @@ -200,9 +204,7 @@ public class CtbrecApplication extends Application { protected void done() { try { int tokens = get(); - Platform.runLater(() -> { - label.setText("Tokens: " + tokens); - }); + label.update(tokens); } catch (InterruptedException | ExecutionException e) { LOG.error("Couldn't retrieve account balance", e); label.setText("Tokens: error"); diff --git a/src/main/java/ctbrec/ui/ThumbOverviewTab.java b/src/main/java/ctbrec/ui/ThumbOverviewTab.java index 708b373e..f2c3eaea 100644 --- a/src/main/java/ctbrec/ui/ThumbOverviewTab.java +++ b/src/main/java/ctbrec/ui/ThumbOverviewTab.java @@ -6,9 +6,11 @@ import java.net.SocketTimeoutException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutionException; @@ -336,6 +338,10 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener { int tokens = Integer.parseInt(tipText); try { cell.getModel().receiveTip(tokens); + Map event = new HashMap<>(); + event.put("event", "tokens.sent"); + event.put("amount", tokens); + CtbrecApplication.bus.post(event); } catch (IOException e1) { Alert alert = new AutosizeAlert(Alert.AlertType.ERROR); alert.setTitle("Error"); diff --git a/src/main/java/ctbrec/ui/TipDialog.java b/src/main/java/ctbrec/ui/TipDialog.java index 5eea4ee6..ec28a3d4 100644 --- a/src/main/java/ctbrec/ui/TipDialog.java +++ b/src/main/java/ctbrec/ui/TipDialog.java @@ -1,6 +1,8 @@ package ctbrec.ui; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.ExecutionException; import org.slf4j.Logger; @@ -47,6 +49,10 @@ public class TipDialog extends TextInputDialog { String profilePage = resp.body().string(); String tokenText = HtmlParser.getText(profilePage, "span.tokencount"); int tokens = Integer.parseInt(tokenText); + Map event = new HashMap<>(); + event.put("event", "tokens"); + event.put("amount", tokens); + CtbrecApplication.bus.post(event); return tokens; } else { throw new IOException("HTTP response: " + resp.code() + " - " + resp.message()); diff --git a/src/main/java/ctbrec/ui/TokenLabel.java b/src/main/java/ctbrec/ui/TokenLabel.java new file mode 100644 index 00000000..b9d9a23b --- /dev/null +++ b/src/main/java/ctbrec/ui/TokenLabel.java @@ -0,0 +1,45 @@ +package ctbrec.ui; + +import java.util.Map; +import java.util.Objects; + +import com.google.common.eventbus.Subscribe; + +import javafx.application.Platform; +import javafx.scene.control.Label; + +public class TokenLabel extends Label { + + private int tokens = -1; + + public TokenLabel() { + setText("Tokens: loading…"); + CtbrecApplication.bus.register(new Object() { + @Subscribe + public void tokensUpdates(Map e) { + if(Objects.equals("tokens", e.get("event"))) { + tokens = (int) e.get("amount"); + updateText(); + } else if(Objects.equals("tokens.sent", e.get("event"))) { + int _tokens = (int) e.get("amount"); + tokens -= _tokens; + updateText(); + } + } + }); + } + + public void decrease(int tokens) { + this.tokens -= tokens; + updateText(); + } + + public void update(int tokens) { + this.tokens = tokens; + updateText(); + } + + private void updateText() { + Platform.runLater(() -> setText("Tokens: " + tokens)); + } +}