Skip to content

Commit

Permalink
improve status bar feedback when starting bwt, syncing and scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
craigraw committed Jan 13, 2021
1 parent 64a9cce commit ff828b4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
23 changes: 19 additions & 4 deletions src/main/java/com/sparrowwallet/sparrow/AppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ public void statusUpdated(StatusEvent event) {
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("");
Expand Down Expand Up @@ -1272,28 +1272,40 @@ public void usbDevicesFound(UsbDeviceEvent event) {
@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);
}
}
}

@Subscribe
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
Expand All @@ -1302,6 +1314,9 @@ public void disconnection(DisconnectionEvent event) {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -20,7 +20,19 @@ public boolean isCompleted() {
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 "";
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -23,4 +27,8 @@ public boolean isCompleted() {
public Date getTip() {
return tip;
}

public String getTipAsString() {
return tip == null ? "" : DATE_FORMAT.format(tip);
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/sparrowwallet/sparrow/event/StatusEvent.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/sparrowwallet/sparrow/net/Bwt.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -254,12 +255,12 @@ public void onSyncProgress(float progress, int tip) {
}

@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)));
}
}

Expand Down

0 comments on commit ff828b4

Please sign in to comment.