diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 6fd672e0..e7c6a198 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -5,7 +5,8 @@ import com.google.common.eventbus.Subscribe; import com.google.common.io.ByteSource; import com.sparrowwallet.drongo.SecureString; import com.sparrowwallet.drongo.Utils; -import com.sparrowwallet.drongo.crypto.*; +import com.sparrowwallet.drongo.crypto.InvalidPasswordException; +import com.sparrowwallet.drongo.crypto.Key; import com.sparrowwallet.drongo.policy.PolicyType; import com.sparrowwallet.drongo.protocol.ScriptType; import com.sparrowwallet.drongo.protocol.Transaction; @@ -25,6 +26,7 @@ import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; +import javafx.concurrent.Worker; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; @@ -45,6 +47,9 @@ import java.text.ParseException; import java.util.*; public class AppController implements Initializable { + private static final int SERVER_PING_PERIOD = 10 * 1000; + private static final int ENUMERATE_HW_PERIOD = 30 * 1000; + private static final String TRANSACTION_TAB_TYPE = "transaction"; public static final String DRAG_OVER_CLASS = "drag-over"; @@ -63,16 +68,18 @@ public class AppController implements Initializable { @FXML private StatusBar statusBar; + @FXML + private UnlabeledToggleSwitch serverToggle; + private Timeline statusTimeline; + private ElectrumServer.PingService pingService; + public static boolean showTxHexProperty; @Override public void initialize(URL location, ResourceBundle resources) { EventManager.get().register(this); - - ElectrumServer.PingService pingService = new ElectrumServer.PingService(); - //pingService. } void initializeView() { @@ -124,7 +131,45 @@ public class AppController implements Initializable { showTxHexProperty = true; exportWallet.setDisable(true); - //addWalletTab("newWallet", new Wallet(PolicyType.SINGLE, ScriptType.P2WPKH)); + serverToggle.selectedProperty().addListener((observable, oldValue, newValue) -> { + Config.get().setMode(newValue ? Mode.ONLINE : Mode.OFFLINE); + if(newValue) { + if(pingService.getState() == Worker.State.CANCELLED) { + pingService.reset(); + } + + if(!pingService.isRunning()) { + pingService.start(); + } + } else { + pingService.cancel(); + } + }); + + pingService = createPingService(); + Config config = Config.get(); + if(config.getMode() == Mode.ONLINE && config.getElectrumServer() != null && !config.getElectrumServer().isEmpty()) { + pingService.start(); + } + } + + private ElectrumServer.PingService createPingService() { + ElectrumServer.PingService pingService = new ElectrumServer.PingService(); + pingService.setPeriod(new Duration(SERVER_PING_PERIOD)); + pingService.setOnSucceeded(successEvent -> { + serverToggle.setSelected(true); + if(pingService.getValue() != null) { + statusBar.setText("Connected: " + pingService.getValue().split(System.lineSeparator(), 2)[0]); + } else { + statusBar.setText(""); + } + }); + pingService.setOnFailed(failEvent -> { + serverToggle.setSelected(false); + statusBar.setText(failEvent.getSource().getException().getMessage()); + }); + + return pingService; } public void openFromFile(ActionEvent event) { @@ -387,7 +432,7 @@ public class AppController implements Initializable { if(!storage.getWalletFile().exists() || wallet.containsSource(KeystoreSource.HW_USB)) { Hwi.ScheduledEnumerateService enumerateService = new Hwi.ScheduledEnumerateService(null); - enumerateService.setPeriod(new Duration(30 * 1000)); + enumerateService.setPeriod(new Duration(ENUMERATE_HW_PERIOD)); enumerateService.setOnSucceeded(workerStateEvent -> { List devices = enumerateService.getValue(); EventManager.get().post(new UsbDeviceEvent(devices)); diff --git a/src/main/java/com/sparrowwallet/sparrow/control/WelcomeDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/WelcomeDialog.java index 9e27189d..3e9014d6 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/WelcomeDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/WelcomeDialog.java @@ -7,7 +7,6 @@ import javafx.geometry.Insets; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; -import javafx.scene.layout.Region; import javafx.scene.layout.VBox; import org.controlsfx.control.HyperlinkLabel; import org.controlsfx.control.StatusBar; @@ -42,8 +41,8 @@ public class WelcomeDialog extends Dialog { dialogPane.setGraphic(imageView); } - final ButtonType onlineButtonType = new javafx.scene.control.ButtonType("Configure Now", ButtonBar.ButtonData.OK_DONE); - final ButtonType offlineButtonType = new javafx.scene.control.ButtonType("Configure Later or Use Offline", ButtonBar.ButtonData.CANCEL_CLOSE); + final ButtonType onlineButtonType = new javafx.scene.control.ButtonType("Configure Server", ButtonBar.ButtonData.OK_DONE); + final ButtonType offlineButtonType = new javafx.scene.control.ButtonType("Later or Offline Mode", ButtonBar.ButtonData.CANCEL_CLOSE); dialogPane.getButtonTypes().addAll(onlineButtonType, offlineButtonType); final VBox content = new VBox(20); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java b/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java index e8dec78a..643715c5 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/ElectrumServer.java @@ -7,6 +7,7 @@ import com.sparrowwallet.drongo.KeyPurpose; import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.protocol.*; import com.sparrowwallet.drongo.wallet.*; +import javafx.concurrent.ScheduledService; import javafx.concurrent.Service; import javafx.concurrent.Task; import org.jetbrains.annotations.NotNull; @@ -76,7 +77,7 @@ public class ElectrumServer { public void ping() throws ServerException { JsonRpcClient client = new JsonRpcClient(getTransport()); - client.createRequest().returnAs(Void.class).method("server.ping").id(1).execute(); + client.createRequest().method("server.ping").id(1).executeNullable(); } public List getServerVersion() throws ServerException { @@ -526,17 +527,43 @@ public class ElectrumServer { } } - public static class PingService extends Service { + public static class PingService extends ScheduledService { + private boolean firstCall = true; + @Override - protected Task createTask() { + protected Task createTask() { return new Task<>() { - protected Boolean call() throws ServerException { + protected String call() throws ServerException { ElectrumServer electrumServer = new ElectrumServer(); - electrumServer.ping(); - return true; + if(firstCall) { + electrumServer.getServerVersion(); + firstCall = false; + return electrumServer.getServerBanner(); + } else { + electrumServer.ping(); + } + + return null; } }; } + + @Override + public boolean cancel() { + try { + closeActiveConnection(); + } catch (ServerException e) { + e.printStackTrace(); + } + + return super.cancel(); + } + + @Override + public void reset() { + super.reset(); + firstCall = true; + } } public static class TransactionHistoryService extends Service { diff --git a/src/main/resources/com/sparrowwallet/sparrow/app.css b/src/main/resources/com/sparrowwallet/sparrow/app.css index d821bff3..014d467c 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/app.css +++ b/src/main/resources/com/sparrowwallet/sparrow/app.css @@ -20,6 +20,11 @@ -fx-fill: #383a42; } +.status-bar .status-label { + -fx-alignment: center-left; +} + .status-bar .right-items { + -fx-alignment: center-right; -fx-padding: 0 0 0 8; } diff --git a/src/main/resources/com/sparrowwallet/sparrow/app.fxml b/src/main/resources/com/sparrowwallet/sparrow/app.fxml index 4e16dab6..ab42fbd8 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/app.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/app.fxml @@ -5,6 +5,7 @@ + @@ -43,6 +44,10 @@ - + + + + +