Update token label when tip has been sent
This commit is contained in:
parent
1338cfe09c
commit
18d5d3cf21
|
@ -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<Integer> task = new Task<Integer>() {
|
||||
@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");
|
||||
|
|
|
@ -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<String, Object> 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");
|
||||
|
|
|
@ -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<String, Object> 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());
|
||||
|
|
|
@ -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<String, Object> 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue