From 1ec05a68615f2d75b604a959138d00d3dc836928 Mon Sep 17 00:00:00 2001 From: QcMrHyde Date: Tue, 10 Jun 2025 14:29:38 -0400 Subject: [PATCH] Added my pools list + code refactoring --- .../com/sparrowwallet/sparrow/io/Config.java | 12 ++++ .../sparrow/joinstr/JoinstrEvent.java | 34 +++++++++++ .../sparrow/joinstr/JoinstrPool.java | 59 ++++++------------ .../sparrow/joinstr/MyPoolsController.java | 61 +++++++++++++++++++ .../sparrow/joinstr/NewPoolController.java | 17 ++++++ .../sparrow/joinstr/OtherPoolsController.java | 31 +++------- .../sparrow/joinstr/SettingsController.java | 2 +- .../joinstr/control/JoinstrInfoPane.java | 2 +- .../joinstr/control/JoinstrPoolList.java | 2 +- .../sparrow/joinstr/joinstr.fxml | 2 +- .../sparrow/joinstr/new_pool.fxml | 24 +------- 11 files changed, 158 insertions(+), 88 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/joinstr/JoinstrEvent.java diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Config.java b/src/main/java/com/sparrowwallet/sparrow/io/Config.java index 4def51a2..d2952f30 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Config.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Config.java @@ -7,6 +7,7 @@ import com.sparrowwallet.sparrow.Mode; import com.sparrowwallet.sparrow.Theme; import com.sparrowwallet.sparrow.control.QRDensity; import com.sparrowwallet.sparrow.control.WebcamResolution; +import com.sparrowwallet.sparrow.joinstr.JoinstrPool; import com.sparrowwallet.sparrow.net.*; import com.sparrowwallet.sparrow.wallet.FeeRatesSelection; import com.sparrowwallet.sparrow.wallet.OptimizationStrategy; @@ -90,6 +91,7 @@ public class Config { private String nostrRelay; + private ArrayList poolStore; // ================ @@ -724,6 +726,16 @@ public class Config { flush(); } + public ArrayList getPoolStore() { + if(poolStore == null) + poolStore = new ArrayList(); + return poolStore; + } + + public void setPoolStore(ArrayList pools) { + this.poolStore = pools; + flush(); + } public String getNostrRelay() { return nostrRelay; diff --git a/src/main/java/com/sparrowwallet/sparrow/joinstr/JoinstrEvent.java b/src/main/java/com/sparrowwallet/sparrow/joinstr/JoinstrEvent.java new file mode 100644 index 00000000..680a4323 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/joinstr/JoinstrEvent.java @@ -0,0 +1,34 @@ +package com.sparrowwallet.sparrow.joinstr; + +import com.google.gson.Gson; + +public class JoinstrEvent { + + public String type; + public String id; + public String public_key; + public String denomination; + public String peers; + public String timeout; + public String relay; + public String fee_rate; + public String transport; + + public JoinstrEvent(String eventContent) { + + Gson gson = new Gson(); + JoinstrEvent joinstrEvent = gson.fromJson(eventContent, JoinstrEvent.class); + + this.type = joinstrEvent.type; + this.id = joinstrEvent.id; + this.public_key = joinstrEvent.public_key; + this.denomination = joinstrEvent.denomination; + this.peers = joinstrEvent.peers; + this.timeout = joinstrEvent.timeout; + this.relay = joinstrEvent.relay; + this.fee_rate = joinstrEvent.fee_rate; + this.transport = joinstrEvent.transport; + + } + +} diff --git a/src/main/java/com/sparrowwallet/sparrow/joinstr/JoinstrPool.java b/src/main/java/com/sparrowwallet/sparrow/joinstr/JoinstrPool.java index 2385be7d..deb1483d 100644 --- a/src/main/java/com/sparrowwallet/sparrow/joinstr/JoinstrPool.java +++ b/src/main/java/com/sparrowwallet/sparrow/joinstr/JoinstrPool.java @@ -23,50 +23,29 @@ import com.sparrowwallet.sparrow.net.Tor; import java.util.*; +import javafx.beans.property.SimpleStringProperty; + public class JoinstrPool { - private final Integer port; // Nostr Port ? - private final String pubkey; - private final long denomination; // Should be in sats, like transaction amounts - - public JoinstrPool(Integer port, String pubkey, long denomination) { - - this.port = port; - this.pubkey = pubkey; - this.denomination = denomination; + private final SimpleStringProperty relay; + private final SimpleStringProperty pubkey; + private final SimpleStringProperty denomination; + private final SimpleStringProperty peers; + private final SimpleStringProperty timeout; + public JoinstrPool(String relay, String pubkey, String denomination, + String peers, String timeout) { + this.relay = new SimpleStringProperty(relay); + this.pubkey = new SimpleStringProperty(pubkey); + this.denomination = new SimpleStringProperty(denomination); + this.peers = new SimpleStringProperty(peers); + this.timeout = new SimpleStringProperty(timeout); } - private String getNostrRelay() { - return Config.get().getNostrRelay(); - } - - public Integer getPort() { - return port; - } - - public String getPubkey() { - return pubkey; - } - - public long getDenomination() { - return denomination; - } - - public void getNewTorRoute() { - - Tor tor = Tor.getDefault(); - tor.changeIdentity(); - - } - - public void publicNostrEvent() { - // TODO: Publish a nostr event with pool info - } - - public void waitForPeers() { - // TODO: Wait for others to join - } - + public String getRelay() { return relay.get(); } + public String getPubkey() { return pubkey.get(); } + public String getDenomination() { return denomination.get(); } + public String getPeers() { return peers.get(); } + public String getTimeout() { return timeout.get(); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/joinstr/MyPoolsController.java b/src/main/java/com/sparrowwallet/sparrow/joinstr/MyPoolsController.java index 1cb2509f..bc44e10e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/joinstr/MyPoolsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/joinstr/MyPoolsController.java @@ -1,17 +1,78 @@ package com.sparrowwallet.sparrow.joinstr; +import com.sparrowwallet.sparrow.io.Config; +import com.sparrowwallet.sparrow.joinstr.control.JoinstrInfoPane; +import com.sparrowwallet.sparrow.joinstr.control.JoinstrPoolList; + +import java.util.ArrayList; + import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.TextField; +import javafx.scene.layout.VBox; public class MyPoolsController extends JoinstrFormController { + @FXML + private VBox contentVBox; + @FXML private TextField searchTextField; + private JoinstrPoolList joinstrPoolList; + private JoinstrInfoPane joinstrInfoPane; + @Override public void initializeView() { + try { + joinstrPoolList = new JoinstrPoolList(); + joinstrPoolList.configureWithJoinButtons(); + + // Add pool store data + addPoolStoreData(); + + joinstrInfoPane = new JoinstrInfoPane(); + joinstrInfoPane.initInfoPane(); + joinstrInfoPane.setVisible(false); + joinstrInfoPane.setManaged(false); + + joinstrPoolList.setOnPoolSelectedListener(pool -> { + if (pool != null) { + joinstrInfoPane.setVisible(true); + joinstrInfoPane.setManaged(true); + joinstrInfoPane.updatePoolInfo(pool); + } else { + joinstrInfoPane.setVisible(false); + joinstrInfoPane.setManaged(false); + } + }); + + contentVBox.getChildren().addAll(joinstrPoolList, joinstrInfoPane); + + searchTextField.textProperty().addListener((observable, oldValue, newValue) -> { + filterPools(newValue); + }); + + } catch (Exception e) { + if(e != null) { + e.printStackTrace(); + } + } + + } + + private void addPoolStoreData() { + + ArrayList pools = Config.get().getPoolStore(); + for (JoinstrPool pool: pools) { + joinstrPoolList.addPool(pool); + } + + } + + private void filterPools(String searchText) { + joinstrPoolList.filterPools(searchText); } public void handleSearchButton(ActionEvent e) { diff --git a/src/main/java/com/sparrowwallet/sparrow/joinstr/NewPoolController.java b/src/main/java/com/sparrowwallet/sparrow/joinstr/NewPoolController.java index d93f46ba..73d6adc4 100644 --- a/src/main/java/com/sparrowwallet/sparrow/joinstr/NewPoolController.java +++ b/src/main/java/com/sparrowwallet/sparrow/joinstr/NewPoolController.java @@ -1,5 +1,11 @@ package com.sparrowwallet.sparrow.joinstr; +import com.google.gson.Gson; +import com.sparrowwallet.sparrow.io.Config; +import com.sparrowwallet.sparrow.payjoin.Payjoin; + +import java.util.ArrayList; + import javafx.fxml.FXML; import javafx.scene.control.TextField; import javafx.scene.control.Alert; @@ -57,9 +63,20 @@ public class NewPoolController extends JoinstrFormController { Alert alert = new Alert(AlertType.INFORMATION); alert.setHeaderText(null); assert event != null; + + // Custom class for ease of use + JoinstrEvent joinstrEvent = new JoinstrEvent(event.getContent()); + + // Add pool to pool store in Config + ArrayList pools = Config.get().getPoolStore(); + JoinstrPool pool = new JoinstrPool(joinstrEvent.relay, joinstrEvent.public_key,joinstrEvent.denomination, joinstrEvent.peers, joinstrEvent.timeout); + pools.add(pool); + Config.get().setPoolStore(pools); + alert.setContentText("Pool created successfully!\nEvent ID: " + event.getId() + "\nDenomination: " + denomination + "\nPeers: " + peers); alert.showAndWait(); + } catch (Exception e) { showError("Error: " + e.getMessage()); } diff --git a/src/main/java/com/sparrowwallet/sparrow/joinstr/OtherPoolsController.java b/src/main/java/com/sparrowwallet/sparrow/joinstr/OtherPoolsController.java index 000ff945..934e28f1 100644 --- a/src/main/java/com/sparrowwallet/sparrow/joinstr/OtherPoolsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/joinstr/OtherPoolsController.java @@ -6,8 +6,10 @@ import com.sparrowwallet.sparrow.joinstr.control.JoinstrPoolList; import javafx.beans.property.SimpleStringProperty; import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.scene.control.Alert; import javafx.scene.control.TextField; import javafx.scene.layout.VBox; +import nostr.event.impl.GenericEvent; public class OtherPoolsController extends JoinstrFormController { @@ -60,6 +62,7 @@ public class OtherPoolsController extends JoinstrFormController { } private void addSamplePoolData() { + // Create the two sample pools JoinstrPool pool1 = new JoinstrPool( "relay.joinstr.xyz", @@ -91,26 +94,12 @@ public class OtherPoolsController extends JoinstrFormController { } } - public static class JoinstrPool { - private final SimpleStringProperty relay; - private final SimpleStringProperty pubkey; - private final SimpleStringProperty denomination; - private final SimpleStringProperty peers; - private final SimpleStringProperty timeout; - - public JoinstrPool(String relay, String pubkey, String denomination, - String peers, String timeout) { - this.relay = new SimpleStringProperty(relay); - this.pubkey = new SimpleStringProperty(pubkey); - this.denomination = new SimpleStringProperty(denomination); - this.peers = new SimpleStringProperty(peers); - this.timeout = new SimpleStringProperty(timeout); - } - - public String getRelay() { return relay.get(); } - public String getPubkey() { return pubkey.get(); } - public String getDenomination() { return denomination.get(); } - public String getPeers() { return peers.get(); } - public String getTimeout() { return timeout.get(); } + private void showError(String message) { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Error"); + alert.setHeaderText(null); + alert.setContentText(message); + alert.showAndWait(); } + } \ No newline at end of file diff --git a/src/main/java/com/sparrowwallet/sparrow/joinstr/SettingsController.java b/src/main/java/com/sparrowwallet/sparrow/joinstr/SettingsController.java index 89eb6741..4b2a21bc 100644 --- a/src/main/java/com/sparrowwallet/sparrow/joinstr/SettingsController.java +++ b/src/main/java/com/sparrowwallet/sparrow/joinstr/SettingsController.java @@ -36,7 +36,7 @@ public class SettingsController extends JoinstrFormController { } public void setDefaultNostrRelayIfEmpty() { - if(nostrRelayTextField.getText().isEmpty()) { + if(nostrRelayTextField.getText() == null || nostrRelayTextField.getText().isEmpty()) { nostrRelayTextField.setText("wss://nostr.fmt.wiz.biz"); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/joinstr/control/JoinstrInfoPane.java b/src/main/java/com/sparrowwallet/sparrow/joinstr/control/JoinstrInfoPane.java index c66698ac..38dec5e7 100644 --- a/src/main/java/com/sparrowwallet/sparrow/joinstr/control/JoinstrInfoPane.java +++ b/src/main/java/com/sparrowwallet/sparrow/joinstr/control/JoinstrInfoPane.java @@ -1,6 +1,6 @@ package com.sparrowwallet.sparrow.joinstr.control; -import com.sparrowwallet.sparrow.joinstr.OtherPoolsController.JoinstrPool; +import com.sparrowwallet.sparrow.joinstr.JoinstrPool; import javafx.geometry.Insets; import javafx.scene.control.Label; diff --git a/src/main/java/com/sparrowwallet/sparrow/joinstr/control/JoinstrPoolList.java b/src/main/java/com/sparrowwallet/sparrow/joinstr/control/JoinstrPoolList.java index 574ae93a..19f422c9 100644 --- a/src/main/java/com/sparrowwallet/sparrow/joinstr/control/JoinstrPoolList.java +++ b/src/main/java/com/sparrowwallet/sparrow/joinstr/control/JoinstrPoolList.java @@ -1,6 +1,6 @@ package com.sparrowwallet.sparrow.joinstr.control; -import com.sparrowwallet.sparrow.joinstr.OtherPoolsController.JoinstrPool; +import com.sparrowwallet.sparrow.joinstr.JoinstrPool; import javafx.collections.FXCollections; import javafx.collections.ObservableList; diff --git a/src/main/resources/com/sparrowwallet/sparrow/joinstr/joinstr.fxml b/src/main/resources/com/sparrowwallet/sparrow/joinstr/joinstr.fxml index 1b392d6a..d3d244b0 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/joinstr/joinstr.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/joinstr/joinstr.fxml @@ -13,7 +13,7 @@ - + diff --git a/src/main/resources/com/sparrowwallet/sparrow/joinstr/new_pool.fxml b/src/main/resources/com/sparrowwallet/sparrow/joinstr/new_pool.fxml index 36972b09..62feed02 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/joinstr/new_pool.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/joinstr/new_pool.fxml @@ -19,37 +19,15 @@
- +
- - - - - - - - - - - - - - - - - - - - -