mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Double loading call fix, LoadingWidget docs
This commit is contained in:
@@ -13,12 +13,10 @@ import org.toop.app.widget.popup.ErrorPopup;
|
|||||||
import org.toop.app.widget.popup.SendChallengePopup;
|
import org.toop.app.widget.popup.SendChallengePopup;
|
||||||
import org.toop.app.widget.view.ServerView;
|
import org.toop.app.widget.view.ServerView;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.framework.eventbus.ListenerHandler;
|
|
||||||
import org.toop.framework.networking.clients.TournamentNetworkingClient;
|
import org.toop.framework.networking.clients.TournamentNetworkingClient;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
import org.toop.framework.networking.types.NetworkingConnector;
|
import org.toop.framework.networking.types.NetworkingConnector;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
@@ -77,13 +75,9 @@ public final class Server {
|
|||||||
|
|
||||||
final int reconnectAttempts = 5;
|
final int reconnectAttempts = 5;
|
||||||
|
|
||||||
LoadingWidget loading = new LoadingWidget(Primitive.text("connecting"), 0, reconnectAttempts);
|
LoadingWidget loading = new LoadingWidget(
|
||||||
loading.setOnFailure(() -> {
|
Primitive.text("connecting"), 0, 0, reconnectAttempts
|
||||||
WidgetContainer.getCurrentView().transitionPrevious();
|
);
|
||||||
WidgetContainer.add(Pos.CENTER, new ErrorPopup(AppContext.getString(
|
|
||||||
"connecting-failed") + " " + ip + ":" + port)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
WidgetContainer.getCurrentView().transitionNext(loading);
|
WidgetContainer.getCurrentView().transitionNext(loading);
|
||||||
|
|
||||||
@@ -93,6 +87,15 @@ public final class Server {
|
|||||||
new NetworkingConnector(ip, parsedPort, reconnectAttempts, 1, TimeUnit.SECONDS)
|
new NetworkingConnector(ip, parsedPort, reconnectAttempts, 1, TimeUnit.SECONDS)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
loading.setOnFailure(() -> {
|
||||||
|
WidgetContainer.getCurrentView().transitionPrevious();
|
||||||
|
a.unsubscribe("connecting");
|
||||||
|
WidgetContainer.add(
|
||||||
|
Pos.CENTER,
|
||||||
|
new ErrorPopup(AppContext.getString("connecting-failed") + " " + ip + ":" + port)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
a.onResponse(NetworkEvents.StartClientResponse.class, e -> {
|
a.onResponse(NetworkEvents.StartClientResponse.class, e -> {
|
||||||
|
|
||||||
if (!e.successful()) {
|
if (!e.successful()) {
|
||||||
@@ -122,9 +125,19 @@ public final class Server {
|
|||||||
startPopulateScheduler();
|
startPopulateScheduler();
|
||||||
populateGameList();
|
populateGameList();
|
||||||
}, false, "startclient")
|
}, false, "startclient")
|
||||||
.listen(NetworkEvents.ConnectTry.class,
|
.listen(
|
||||||
e -> Platform.runLater(() ->
|
NetworkEvents.ConnectTry.class,
|
||||||
loading.setAmount(e.amount())), false, "connecting")
|
e -> Platform.runLater(
|
||||||
|
() -> {
|
||||||
|
try {
|
||||||
|
loading.setAmount(e.amount());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
),
|
||||||
|
false, "connecting"
|
||||||
|
)
|
||||||
.postEvent();
|
.postEvent();
|
||||||
|
|
||||||
new EventFlow()
|
new EventFlow()
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ package org.toop.app.widget.complex;
|
|||||||
|
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
import javafx.scene.control.ProgressBar;
|
import javafx.scene.control.ProgressBar;
|
||||||
import javafx.scene.layout.HBox;
|
|
||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
import javafx.scene.text.Text;
|
import javafx.scene.text.Text;
|
||||||
import org.toop.app.widget.Primitive;
|
import org.toop.app.widget.Primitive;
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
public class LoadingWidget extends ViewWidget implements Update { // TODO make of widget type
|
public class LoadingWidget extends ViewWidget implements Update { // TODO make of widget type
|
||||||
private final ProgressBar progressBar;
|
private final ProgressBar progressBar;
|
||||||
private final Text loadingText;
|
private final Text loadingText;
|
||||||
@@ -16,14 +17,25 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
|
|||||||
private boolean successTriggered = false;
|
private boolean successTriggered = false;
|
||||||
private boolean failureTriggered = false;
|
private boolean failureTriggered = false;
|
||||||
private int maxAmount;
|
private int maxAmount;
|
||||||
|
private int minAmount;
|
||||||
private int amount;
|
private int amount;
|
||||||
|
private Callable<Boolean> successTrigger = () -> (amount >= maxAmount);
|
||||||
|
private Callable<Boolean> failureTrigger = () -> (amount < minAmount);
|
||||||
private float percentage = 0.0f;
|
private float percentage = 0.0f;
|
||||||
|
|
||||||
|
/**
|
||||||
public LoadingWidget(Text loadingText, int startAmount, int maxAmount) {
|
*
|
||||||
|
* Widget that shows a loading bar.
|
||||||
amount = startAmount;
|
*
|
||||||
|
* @param loadingText Text above the loading bar.
|
||||||
|
* @param minAmount The minimum amount.
|
||||||
|
* @param startAmount The starting amount.
|
||||||
|
* @param maxAmount The max amount.
|
||||||
|
*/
|
||||||
|
public LoadingWidget(Text loadingText, int minAmount, int startAmount, int maxAmount) {
|
||||||
this.maxAmount = maxAmount;
|
this.maxAmount = maxAmount;
|
||||||
|
this.minAmount = minAmount;
|
||||||
|
amount = startAmount;
|
||||||
|
|
||||||
progressBar = new ProgressBar();
|
progressBar = new ProgressBar();
|
||||||
this.loadingText = loadingText;
|
this.loadingText = loadingText;
|
||||||
@@ -36,44 +48,86 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
|
|||||||
this.maxAmount = maxAmount;
|
this.maxAmount = maxAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAmount(int amount) {
|
public void setAmount(int amount) throws Exception {
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAmount() {
|
public int getMaxAmount() {
|
||||||
setAmount(this.amount+1);
|
return maxAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getAmount() {
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getPercentage() {
|
||||||
|
return percentage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isTriggered() {
|
||||||
|
return (failureTriggered || successTriggered);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What to do when success is triggered.
|
||||||
|
* @param onSuccess The lambda that gets run on success.
|
||||||
|
*/
|
||||||
public void setOnSuccess(Runnable onSuccess) {
|
public void setOnSuccess(Runnable onSuccess) {
|
||||||
success = onSuccess;
|
success = onSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What to do when failure is triggered.
|
||||||
|
* @param onFailure The lambda that gets run on failure.
|
||||||
|
*/
|
||||||
public void setOnFailure(Runnable onFailure) {
|
public void setOnFailure(Runnable onFailure) {
|
||||||
failure = onFailure;
|
failure = onFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The trigger to activate onSuccess.
|
||||||
|
* @param trigger The lambda that triggers onSuccess.
|
||||||
|
*/
|
||||||
|
public void setSuccessTrigger(Callable<Boolean> trigger) {
|
||||||
|
successTrigger = trigger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The trigger to activate onFailure.
|
||||||
|
* @param trigger The lambda that triggers onFailure.
|
||||||
|
*/
|
||||||
|
public void setFailureTrigger(Callable<Boolean> trigger) {
|
||||||
|
failureTrigger = trigger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forcefully trigger success.
|
||||||
|
*/
|
||||||
public void triggerSuccess() {
|
public void triggerSuccess() {
|
||||||
successTriggered = true; // TODO, else it will double call... why?
|
successTriggered = true; // TODO, else it will double call... why?
|
||||||
success.run();
|
success.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Forcefully trigger failure.
|
||||||
|
*/
|
||||||
public void triggerFailure() {
|
public void triggerFailure() {
|
||||||
failureTriggered = true; // TODO, else it will double call... why?
|
failureTriggered = true; // TODO, else it will double call... why?
|
||||||
failure.run();
|
failure.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() throws Exception { // TODO Better exception
|
||||||
if (successTriggered || failureTriggered) {
|
if (successTriggered || failureTriggered) { // If already triggered, throw exception.
|
||||||
return;
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (amount >= maxAmount) {
|
if (successTrigger.call()) {
|
||||||
triggerSuccess();
|
triggerSuccess();
|
||||||
this.remove(this);
|
this.remove(this);
|
||||||
return;
|
return;
|
||||||
} else if (amount < 0) {
|
} else if (failureTrigger.call()) {
|
||||||
triggerFailure();
|
triggerFailure();
|
||||||
this.remove(this);
|
this.remove(this);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
package org.toop.app.widget.complex;
|
package org.toop.app.widget.complex;
|
||||||
|
|
||||||
public interface Update {
|
public interface Update {
|
||||||
void update();
|
void update() throws Exception;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user