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;
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 ctbrec.GlobalThreadPool;
import ctbrec.event.EventBusHolder;
import ctbrec.sites.Site;
@ -17,12 +8,19 @@ import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.control.Label;
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 {
private static final Logger LOG = LoggerFactory.getLogger(TokenLabel.class);
private double tokens = -1;
private Site site;
private final Site site;
public TokenLabel(Site site) {
this.site = site;
@ -59,7 +57,10 @@ public class TokenLabel extends Label {
}
public void loadBalance() {
Task<Double> task = new Task<Double>() {
if (tokens != -1) {
return;
}
Task<Double> task = new Task<>() {
@Override
protected Double call() throws Exception {
if (!Objects.equals(System.getenv("CTBREC_DEV"), "1")) {

View File

@ -1,26 +1,5 @@
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.GlobalThreadPool;
import ctbrec.Model;
@ -33,17 +12,9 @@ import ctbrec.ui.DesktopIntegration;
import ctbrec.ui.SiteUiFactory;
import ctbrec.ui.TokenLabel;
import ctbrec.ui.action.SetThumbAsPortraitAction;
import ctbrec.ui.controls.CustomMouseBehaviorContextMenu;
import ctbrec.ui.controls.FasterVerticalScrollPaneSkin;
import ctbrec.ui.controls.SearchBox;
import ctbrec.ui.controls.SearchPopover;
import ctbrec.ui.controls.SearchPopoverTreeList;
import ctbrec.ui.controls.*;
import ctbrec.ui.menu.ModelMenuContributor;
import javafx.animation.FadeTransition;
import javafx.animation.Interpolator;
import javafx.animation.ParallelTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.TranslateTransition;
import javafx.animation.*;
import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
@ -60,28 +31,20 @@ import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Button;
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.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.KeyCode;
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.scene.input.*;
import javafx.scene.layout.*;
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 {
private static final Logger LOG = LoggerFactory.getLogger(ThumbOverviewTab.class);
@ -115,6 +78,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
ProgressIndicator progressIndicator;
Label noResultsFound = new Label("Nothing found!");
Label errorLabel = new Label("");
TokenLabel tokenBalance;
private ComboBox<Integer> thumbWidth;
@ -179,11 +143,10 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
if (site.supportsTips() && site.credentialsAvailable()) {
var buyTokens = new Button("Buy Tokens");
buyTokens.setOnAction(e -> DesktopIntegration.open(site.getBuyTokensLink()));
var tokenBalance = new TokenLabel(site);
tokenBalance = new TokenLabel(site);
tokenBalance.setAlignment(Pos.CENTER_RIGHT);
tokenBalance.prefHeightProperty().bind(buyTokens.heightProperty());
topBar.getChildren().addAll(tokenBalance, buyTokens);
tokenBalance.loadBalance();
}
if (site.supportsSearch()) {
topBar.getChildren().add(searchInput);
@ -259,7 +222,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
private void keyReleased(KeyEvent event) {
if (event.getCode() == KeyCode.F5) {
refresh();
} else if (event.getCode() == KeyCode.A &&event.isControlDown()) {
} else if (event.getCode() == KeyCode.A && event.isControlDown()) {
selectAll();
} else if (event.getCode() == KeyCode.RIGHT) {
nextPage();
@ -271,7 +234,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
}
public void selectAll() {
grid.getChildren().stream().filter(ThumbCell.class::isInstance).forEach(tc -> ((ThumbCell)tc).setSelected(true));
grid.getChildren().stream().filter(ThumbCell.class::isInstance).forEach(tc -> ((ThumbCell) tc).setSelected(true));
}
private void nextPage() {
@ -310,8 +273,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
int width = Config.getInstance().getSettings().thumbWidth;
thumbWidth.getSelectionModel().select(Integer.valueOf(width));
for (Node node : grid.getChildren()) {
if (node instanceof ThumbCell) {
ThumbCell cell = (ThumbCell) node;
if (node instanceof ThumbCell cell) {
cell.setThumbWidth(width);
}
}
@ -396,9 +358,8 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
for (Model model : models) {
var found = false;
for (Node node : nodes) { // NOSONAR
if (!(node instanceof ThumbCell))
if (!(node instanceof ThumbCell cell))
continue;
ThumbCell cell = (ThumbCell) node;
if (cell.getModel().equals(model)) {
found = true;
cell.setModel(model);
@ -431,11 +392,10 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
}
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();
if (!(node instanceof ThumbCell))
if (!(node instanceof ThumbCell cell))
continue;
ThumbCell cell = (ThumbCell) node;
if (!models.contains(cell.getModel())) {
iterator.remove();
}
@ -480,31 +440,31 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
var selectedModels = getSelectedThumbCells(cell).stream().map(ThumbCell::getModel).collect(Collectors.toList());
ModelMenuContributor.newContributor(getTabPane(), Config.getInstance(), recorder)
.withStartStopCallback(m -> {
Platform.runLater(() -> {
getTabPane().setCursor(Cursor.DEFAULT);
getThumbCell(m).ifPresent(ThumbCell::update);
});
})
.withFollowCallback( (mdl, fllw, success) -> {
if (Boolean.TRUE.equals(fllw) && Boolean.TRUE.equals(success)) {
Platform.runLater(() -> getThumbCell(mdl).ifPresent(this::showAddToFollowedAnimation));
}
if (Boolean.FALSE.equals(fllw)) {
Platform.runLater(() -> {
if (this instanceof FollowedTab) {
getThumbCell(mdl).ifPresent(thumbCell -> grid.getChildren().remove(thumbCell));
.withStartStopCallback(m ->
Platform.runLater(() -> {
getTabPane().setCursor(Cursor.DEFAULT);
getThumbCell(m).ifPresent(ThumbCell::update);
})
)
.withFollowCallback((mdl, fllw, success) -> {
if (Boolean.TRUE.equals(fllw) && Boolean.TRUE.equals(success)) {
Platform.runLater(() -> getThumbCell(mdl).ifPresent(this::showAddToFollowedAnimation));
}
selectedThumbCells.clear();
});
}
})
.withIgnoreCallback(m -> getThumbCell(m).ifPresent(thumbCell -> {
grid.getChildren().remove(thumbCell);
selectedThumbCells.remove(thumbCell);
}))
.afterwards(() -> selectedModels.forEach(m -> getThumbCell(m).ifPresent(ThumbCell::update)))
.contributeToMenu(selectedModels, contextMenu);
if (Boolean.FALSE.equals(fllw)) {
Platform.runLater(() -> {
if (this instanceof FollowedTab) {
getThumbCell(mdl).ifPresent(thumbCell -> grid.getChildren().remove(thumbCell));
}
selectedThumbCells.clear();
});
}
})
.withIgnoreCallback(m -> getThumbCell(m).ifPresent(thumbCell -> {
grid.getChildren().remove(thumbCell);
selectedThumbCells.remove(thumbCell);
}))
.afterwards(() -> selectedModels.forEach(m -> getThumbCell(m).ifPresent(ThumbCell::update)))
.contributeToMenu(selectedModels, contextMenu);
var useImageAsPortrait = new MenuItem("Use As Portrait");
useImageAsPortrait.setOnAction(e -> new SetThumbAsPortraitAction(getTabPane(), cell.getModel(), cell.getImage()).execute());
@ -525,11 +485,8 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
private Optional<ThumbCell> getThumbCell(Model model) {
for (Node node : grid.getChildren()) {
if (node instanceof ThumbCell) {
var thumbCell = (ThumbCell) node;
if (Objects.equals(thumbCell.getModel(), model)) {
return Optional.of(thumbCell);
}
if (node instanceof ThumbCell thumbCell && Objects.equals(thumbCell.getModel(), model)) {
return Optional.of(thumbCell);
}
}
return Optional.empty();
@ -672,10 +629,9 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
filteredThumbCells.clear();
} else {
// 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();
if (node instanceof ThumbCell) {
ThumbCell cell = (ThumbCell) node;
if (node instanceof ThumbCell cell) {
var m = cell.getModel();
if (!matches(m, filter)) {
iterator.remove();
@ -686,7 +642,7 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
}
// add the ones, which might have been filtered before, but now match
for (Iterator<ThumbCell> iterator = filteredThumbCells.iterator(); iterator.hasNext();) {
for (Iterator<ThumbCell> iterator = filteredThumbCells.iterator(); iterator.hasNext(); ) {
var thumbCell = iterator.next();
var m = thumbCell.getModel();
if (matches(m, filter)) {
@ -814,6 +770,13 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
}
}
updateThumbSize();
updateTokenLabel();
}
private void updateTokenLabel() {
if (tokenBalance != null) {
tokenBalance.loadBalance();
}
}
@Override
@ -823,10 +786,9 @@ public class ThumbOverviewTab extends Tab implements TabSelectionListener {
}
queue.clear();
for (Iterator<Node> iterator = grid.getChildren().iterator(); iterator.hasNext();) {
for (Iterator<Node> iterator = grid.getChildren().iterator(); iterator.hasNext(); ) {
var node = iterator.next();
if (node instanceof ThumbCell) {
var thumbCell = (ThumbCell) node;
if (node instanceof ThumbCell thumbCell) {
thumbCell.releaseResources();
iterator.remove();
}