From ff828b483b5c03d18c6f6c4f957406a89c74139a Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 13 Jan 2021 20:25:55 +0200 Subject: [PATCH] improve status bar feedback when starting bwt, syncing and scanning --- .../sparrowwallet/sparrow/AppController.java | 23 ++++++++++++++---- .../sparrow/event/BwtScanStatusEvent.java | 24 ++++++++++++++----- .../sparrow/event/BwtSyncStatusEvent.java | 8 +++++++ .../sparrow/event/StatusEvent.java | 12 ++++++++++ .../com/sparrowwallet/sparrow/net/Bwt.java | 7 +++--- 5 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index b396bbe1..4204bbe6 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -1196,7 +1196,7 @@ public class AppController implements Initializable { if(wait != null && wait.getStatus() == Animation.Status.RUNNING) { wait.stop(); } - wait = new PauseTransition(Duration.seconds(20)); + wait = new PauseTransition(Duration.seconds(event.getShowDuration())); wait.setOnFinished((e) -> { if(statusBar.getText().equals(event.getStatus())) { statusBar.setText(""); @@ -1272,14 +1272,20 @@ public class AppController implements Initializable { @Subscribe public void bwtBootStatus(BwtBootStatusEvent event) { serverToggle.setDisable(true); - statusUpdated(new StatusEvent(event.getStatus())); + statusUpdated(new StatusEvent(event.getStatus(), 60)); + if(statusTimeline == null || statusTimeline.getStatus() != Animation.Status.RUNNING) { + statusBar.setProgress(0.01); + } } @Subscribe public void bwtSyncStatus(BwtSyncStatusEvent event) { serverToggle.setDisable(false); if((AppServices.isConnecting() || AppServices.isConnected()) && !event.isCompleted()) { - statusUpdated(new StatusEvent(event.getStatus())); + statusUpdated(new StatusEvent("Syncing... (" + event.getProgress() + "% complete, synced to " + event.getTipAsString() + ")")); + if(event.getProgress() > 0 && (statusTimeline == null || statusTimeline.getStatus() != Animation.Status.RUNNING)) { + statusBar.setProgress((double)event.getProgress() / 100); + } } } @@ -1287,13 +1293,19 @@ public class AppController implements Initializable { public void bwtScanStatus(BwtScanStatusEvent event) { serverToggle.setDisable(true); if((AppServices.isConnecting() || AppServices.isConnected()) && !event.isCompleted()) { - statusUpdated(new StatusEvent(event.getStatus())); + statusUpdated(new StatusEvent("Scanning... (" + event.getProgress() + "% complete" + (event.getProgress() > 30 ? ", " + event.getRemainingAsString() + " remaining)" : ")"))); + if(event.getProgress() > 0 && (statusTimeline == null || statusTimeline.getStatus() != Animation.Status.RUNNING)) { + statusBar.setProgress((double)event.getProgress() / 100); + } } } @Subscribe public void bwtReadyStatus(BwtReadyStatusEvent event) { serverToggle.setDisable(false); + if(statusTimeline == null || statusTimeline.getStatus() != Animation.Status.RUNNING) { + statusBar.setProgress(0); + } } @Subscribe @@ -1302,6 +1314,9 @@ public class AppController implements Initializable { if(!AppServices.isConnecting() && !AppServices.isConnected() && !statusBar.getText().startsWith("Connection error")) { statusUpdated(new StatusEvent("Disconnected")); } + if(statusTimeline == null || statusTimeline.getStatus() != Animation.Status.RUNNING) { + statusBar.setProgress(0); + } } @Subscribe diff --git a/src/main/java/com/sparrowwallet/sparrow/event/BwtScanStatusEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/BwtScanStatusEvent.java index 0c41adbf..ebb81726 100644 --- a/src/main/java/com/sparrowwallet/sparrow/event/BwtScanStatusEvent.java +++ b/src/main/java/com/sparrowwallet/sparrow/event/BwtScanStatusEvent.java @@ -1,15 +1,15 @@ package com.sparrowwallet.sparrow.event; -import java.util.Date; +import java.time.Duration; public class BwtScanStatusEvent extends BwtStatusEvent { private final int progress; - private final Date eta; + private final Duration remainingDuration; - public BwtScanStatusEvent(String status, int progress, Date eta) { + public BwtScanStatusEvent(String status, int progress, Duration remainingDuration) { super(status); this.progress = progress; - this.eta = eta; + this.remainingDuration = remainingDuration; } public int getProgress() { @@ -20,7 +20,19 @@ public class BwtScanStatusEvent extends BwtStatusEvent { return progress == 100; } - public Date getEta() { - return eta; + public Duration getRemaining() { + return remainingDuration; + } + + public String getRemainingAsString() { + if(remainingDuration != null) { + if(progress < 30) { + return Math.round((double)remainingDuration.toSeconds() / 60) + "m"; + } else { + return remainingDuration.toMinutesPart() + "m " + remainingDuration.toSecondsPart() + "s"; + } + } + + return ""; } } diff --git a/src/main/java/com/sparrowwallet/sparrow/event/BwtSyncStatusEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/BwtSyncStatusEvent.java index 426cecce..1c2608df 100644 --- a/src/main/java/com/sparrowwallet/sparrow/event/BwtSyncStatusEvent.java +++ b/src/main/java/com/sparrowwallet/sparrow/event/BwtSyncStatusEvent.java @@ -1,8 +1,12 @@ package com.sparrowwallet.sparrow.event; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Date; public class BwtSyncStatusEvent extends BwtStatusEvent { + private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm"); + private final int progress; private final Date tip; @@ -23,4 +27,8 @@ public class BwtSyncStatusEvent extends BwtStatusEvent { public Date getTip() { return tip; } + + public String getTipAsString() { + return tip == null ? "" : DATE_FORMAT.format(tip); + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/event/StatusEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/StatusEvent.java index f4ef2c41..8e3ee41b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/event/StatusEvent.java +++ b/src/main/java/com/sparrowwallet/sparrow/event/StatusEvent.java @@ -1,13 +1,25 @@ package com.sparrowwallet.sparrow.event; public class StatusEvent { + public static final int DEFAULT_SHOW_DURATION_SECS = 20; + private final String status; + private final int showDuration; public StatusEvent(String status) { + this(status, DEFAULT_SHOW_DURATION_SECS); + } + + public StatusEvent(String status, int showDuration) { this.status = status; + this.showDuration = showDuration; } public String getStatus() { return status; } + + public int getShowDuration() { + return showDuration; + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java b/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java index 2b3e5933..78aa7e05 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/Bwt.java @@ -19,6 +19,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.time.Duration; import java.util.*; import java.util.stream.Collectors; @@ -254,12 +255,12 @@ public class Bwt { } @Override - public void onScanProgress(float progress, int eta) { + public void onScanProgress(float progress, int remaining) { int percent = (int) (progress * 100.0); - Date date = new Date((long) eta * 1000); + Duration remainingDuration = Duration.ofSeconds(remaining); log.debug("Scanning " + percent + "%"); if(!terminating) { - Platform.runLater(() -> EventManager.get().post(new BwtScanStatusEvent("Scanning" + (percent < 100 ? " (" + percent + "%)" : ""), percent, date))); + Platform.runLater(() -> EventManager.get().post(new BwtScanStatusEvent("Scanning" + (percent < 100 ? " (" + percent + "%)" : ""), percent, remainingDuration))); } }