Update token label only when a tab is selected

This commit is contained in:
0xb00bface 2022-10-22 13:48:49 +02:00
parent 8b31df73c5
commit d1c5bd4850
2 changed files with 72 additions and 109 deletions

View File

@ -1,15 +1,6 @@
package ctbrec.ui; package ctbrec.ui;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import ctbrec.GlobalThreadPool; import ctbrec.GlobalThreadPool;
import ctbrec.event.EventBusHolder; import ctbrec.event.EventBusHolder;
import ctbrec.sites.Site; import ctbrec.sites.Site;
@ -17,12 +8,19 @@ import javafx.application.Platform;
import javafx.concurrent.Task; import javafx.concurrent.Task;
import javafx.scene.control.Label; import javafx.scene.control.Label;
import javafx.scene.control.Tooltip; import javafx.scene.control.Tooltip;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
public class TokenLabel extends Label { public class TokenLabel extends Label {
private static final Logger LOG = LoggerFactory.getLogger(TokenLabel.class); private static final Logger LOG = LoggerFactory.getLogger(TokenLabel.class);
private double tokens = -1; private double tokens = -1;
private Site site; private final Site site;
public TokenLabel(Site site) { public TokenLabel(Site site) {
this.site = site; this.site = site;
@ -59,7 +57,10 @@ public class TokenLabel extends Label {
} }
public void loadBalance() { public void loadBalance() {
Task<Double> task = new Task<Double>() { if (tokens != -1) {
return;
}
Task<Double> task = new Task<>() {
@Override @Override
protected Double call() throws Exception { protected Double call() throws Exception {
if (!Objects.equals(System.getenv("CTBREC_DEV"), "1")) { if (!Objects.equals(System.getenv("CTBREC_DEV"), "1")) {

View File

@ -1,26 +1,5 @@
package ctbrec.ui.tabs; package ctbrec.ui.tabs;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Config; import ctbrec.Config;
import ctbrec.GlobalThreadPool; import ctbrec.GlobalThreadPool;
import ctbrec.Model; import ctbrec.Model;
@ -33,17 +12,9 @@ import ctbrec.ui.DesktopIntegration;
import ctbrec.ui.SiteUiFactory; import ctbrec.ui.SiteUiFactory;
import ctbrec.ui.TokenLabel; import ctbrec.ui.TokenLabel;
import ctbrec.ui.action.SetThumbAsPortraitAction; import ctbrec.ui.action.SetThumbAsPortraitAction;
import ctbrec.ui.controls.CustomMouseBehaviorContextMenu; import ctbrec.ui.controls.*;
import ctbrec.ui.controls.FasterVerticalScrollPaneSkin;
import ctbrec.ui.controls.SearchBox;
import ctbrec.ui.controls.SearchPopover;
import ctbrec.ui.controls.SearchPopoverTreeList;
import ctbrec.ui.menu.ModelMenuContributor; import ctbrec.ui.menu.ModelMenuContributor;
import javafx.animation.FadeTransition; import javafx.animation.*;
import javafx.animation.Interpolator;
import javafx.animation.ParallelTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.property.BooleanProperty; import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleBooleanProperty;
@ -60,28 +31,20 @@ import javafx.geometry.Pos;
import javafx.scene.Cursor; import javafx.scene.Cursor;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.control.Button; import javafx.scene.control.*;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.input.ContextMenuEvent; import javafx.scene.input.*;
import javafx.scene.input.KeyCode; import javafx.scene.layout.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.util.Duration; import javafx.util.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
public class ThumbOverviewTab extends Tab implements TabSelectionListener { public class ThumbOverviewTab extends Tab implements TabSelectionListener {
private static final Logger LOG = LoggerFactory.getLogger(ThumbOverviewTab.class); private static final Logger LOG = LoggerFactory.getLogger(ThumbOverviewTab.class);
@ -115,6 +78,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
ProgressIndicator progressIndicator; ProgressIndicator progressIndicator;
Label noResultsFound = new Label("Nothing found!"); Label noResultsFound = new Label("Nothing found!");
Label errorLabel = new Label(""); Label errorLabel = new Label("");
TokenLabel tokenBalance;
private ComboBox<Integer> thumbWidth; private ComboBox<Integer> thumbWidth;
@ -179,11 +143,10 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
if (site.supportsTips() && site.credentialsAvailable()) { if (site.supportsTips() && site.credentialsAvailable()) {
var buyTokens = new Button("Buy Tokens"); var buyTokens = new Button("Buy Tokens");
buyTokens.setOnAction(e -> DesktopIntegration.open(site.getBuyTokensLink())); buyTokens.setOnAction(e -> DesktopIntegration.open(site.getBuyTokensLink()));
var tokenBalance = new TokenLabel(site); tokenBalance = new TokenLabel(site);
tokenBalance.setAlignment(Pos.CENTER_RIGHT); tokenBalance.setAlignment(Pos.CENTER_RIGHT);
tokenBalance.prefHeightProperty().bind(buyTokens.heightProperty()); tokenBalance.prefHeightProperty().bind(buyTokens.heightProperty());
topBar.getChildren().addAll(tokenBalance, buyTokens); topBar.getChildren().addAll(tokenBalance, buyTokens);
tokenBalance.loadBalance();
} }
if (site.supportsSearch()) { if (site.supportsSearch()) {
topBar.getChildren().add(searchInput); topBar.getChildren().add(searchInput);
@ -310,8 +273,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
int width = Config.getInstance().getSettings().thumbWidth; int width = Config.getInstance().getSettings().thumbWidth;
thumbWidth.getSelectionModel().select(Integer.valueOf(width)); thumbWidth.getSelectionModel().select(Integer.valueOf(width));
for (Node node : grid.getChildren()) { for (Node node : grid.getChildren()) {
if (node instanceof ThumbCell) { if (node instanceof ThumbCell cell) {
ThumbCell cell = (ThumbCell) node;
cell.setThumbWidth(width); cell.setThumbWidth(width);
} }
} }
@ -396,9 +358,8 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
for (Model model : models) { for (Model model : models) {
var found = false; var found = false;
for (Node node : nodes) { // NOSONAR for (Node node : nodes) { // NOSONAR
if (!(node instanceof ThumbCell)) if (!(node instanceof ThumbCell cell))
continue; continue;
ThumbCell cell = (ThumbCell) node;
if (cell.getModel().equals(model)) { if (cell.getModel().equals(model)) {
found = true; found = true;
cell.setModel(model); cell.setModel(model);
@ -433,9 +394,8 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
private void removeModelsMissingInUpdate(ObservableList<Node> nodes, List<? extends Model> models) { private void removeModelsMissingInUpdate(ObservableList<Node> nodes, List<? extends Model> models) {
for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) { for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext(); ) {
var node = iterator.next(); var node = iterator.next();
if (!(node instanceof ThumbCell)) if (!(node instanceof ThumbCell cell))
continue; continue;
ThumbCell cell = (ThumbCell) node;
if (!models.contains(cell.getModel())) { if (!models.contains(cell.getModel())) {
iterator.remove(); iterator.remove();
} }
@ -480,12 +440,12 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
var selectedModels = getSelectedThumbCells(cell).stream().map(ThumbCell::getModel).collect(Collectors.toList()); var selectedModels = getSelectedThumbCells(cell).stream().map(ThumbCell::getModel).collect(Collectors.toList());
ModelMenuContributor.newContributor(getTabPane(), Config.getInstance(), recorder) ModelMenuContributor.newContributor(getTabPane(), Config.getInstance(), recorder)
.withStartStopCallback(m -> { .withStartStopCallback(m ->
Platform.runLater(() -> { Platform.runLater(() -> {
getTabPane().setCursor(Cursor.DEFAULT); getTabPane().setCursor(Cursor.DEFAULT);
getThumbCell(m).ifPresent(ThumbCell::update); getThumbCell(m).ifPresent(ThumbCell::update);
});
}) })
)
.withFollowCallback((mdl, fllw, success) -> { .withFollowCallback((mdl, fllw, success) -> {
if (Boolean.TRUE.equals(fllw) && Boolean.TRUE.equals(success)) { if (Boolean.TRUE.equals(fllw) && Boolean.TRUE.equals(success)) {
Platform.runLater(() -> getThumbCell(mdl).ifPresent(this::showAddToFollowedAnimation)); Platform.runLater(() -> getThumbCell(mdl).ifPresent(this::showAddToFollowedAnimation));
@ -525,13 +485,10 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
private Optional<ThumbCell> getThumbCell(Model model) { private Optional<ThumbCell> getThumbCell(Model model) {
for (Node node : grid.getChildren()) { for (Node node : grid.getChildren()) {
if (node instanceof ThumbCell) { if (node instanceof ThumbCell thumbCell && Objects.equals(thumbCell.getModel(), model)) {
var thumbCell = (ThumbCell) node;
if (Objects.equals(thumbCell.getModel(), model)) {
return Optional.of(thumbCell); return Optional.of(thumbCell);
} }
} }
}
return Optional.empty(); return Optional.empty();
} }
@ -674,8 +631,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
// remove the ones from grid, which don't match // remove the ones from grid, which don't match
for (Iterator<Node> iterator = grid.getChildren().iterator(); iterator.hasNext(); ) { for (Iterator<Node> iterator = grid.getChildren().iterator(); iterator.hasNext(); ) {
var node = iterator.next(); var node = iterator.next();
if (node instanceof ThumbCell) { if (node instanceof ThumbCell cell) {
ThumbCell cell = (ThumbCell) node;
var m = cell.getModel(); var m = cell.getModel();
if (!matches(m, filter)) { if (!matches(m, filter)) {
iterator.remove(); iterator.remove();
@ -814,6 +770,13 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
} }
} }
updateThumbSize(); updateThumbSize();
updateTokenLabel();
}
private void updateTokenLabel() {
if (tokenBalance != null) {
tokenBalance.loadBalance();
}
} }
@Override @Override
@ -825,8 +788,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
for (Iterator<Node> iterator = grid.getChildren().iterator(); iterator.hasNext(); ) { for (Iterator<Node> iterator = grid.getChildren().iterator(); iterator.hasNext(); ) {
var node = iterator.next(); var node = iterator.next();
if (node instanceof ThumbCell) { if (node instanceof ThumbCell thumbCell) {
var thumbCell = (ThumbCell) node;
thumbCell.releaseResources(); thumbCell.releaseResources();
iterator.remove(); iterator.remove();
} }