wallet + ui: external connection API token support, update translations

This commit is contained in:
ardocrat
2023-08-01 02:13:35 +03:00
parent 08c202519e
commit cf834f86fc
6 changed files with 158 additions and 40 deletions
+76 -23
View File
@@ -19,16 +19,21 @@ use crate::AppConfig;
use crate::gui::Colors;
use crate::gui::icons::{GLOBE, GLOBE_SIMPLE};
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{Modal, View};
use crate::gui::views::{Modal, ModalPosition, View};
use crate::gui::views::wallets::setup::ConnectionMethod;
use crate::wallet::ExternalConnection;
/// Wallet node connection method setup content.
pub struct ConnectionSetup {
/// Selected connection method.
method: ConnectionMethod,
/// Flag to check if modal was just opened.
first_modal_launch: bool,
/// External node connection URL value for [`Modal`].
ext_node_url_edit: String,
/// External node connection API secret value for [`Modal`].
ext_node_secret_edit: String,
/// Flag to show URL format error.
ext_node_url_error: bool,
}
@@ -37,7 +42,9 @@ impl Default for ConnectionSetup {
fn default() -> Self {
Self {
method: ConnectionMethod::Integrated,
first_modal_launch: true,
ext_node_url_edit: "".to_string(),
ext_node_secret_edit: "".to_string(),
ext_node_url_error: false
}
}
@@ -82,25 +89,28 @@ impl ConnectionSetup {
ui.add_space(6.0);
// Show button to add new external node connection.
let add_node_text = format!("{} {}", GLOBE_SIMPLE, t!("wallets.add_node_url"));
let add_node_text = format!("{} {}", GLOBE_SIMPLE, t!("wallets.add_node"));
View::button(ui, add_node_text, Colors::GOLD, || {
// Setup values for Modal.
self.first_modal_launch = true;
self.ext_node_url_edit = "".to_string();
self.ext_node_secret_edit = "".to_string();
self.ext_node_url_error = false;
// Show modal.
Modal::new(Self::ADD_CONNECTION_URL_MODAL)
.title(t!("wallets.ext_conn"))
.position(ModalPosition::CenterTop)
.title(t!("wallets.add_node"))
.show();
cb.show_keyboard();
});
ui.add_space(12.0);
// Show external nodes URLs selection.
for conn in AppConfig::external_nodes_urls() {
for conn in AppConfig::external_connections() {
View::radio_value(ui,
&mut self.method,
ConnectionMethod::External(conn.clone()),
conn);
ConnectionMethod::External(conn.url.clone()),
conn.url);
ui.add_space(12.0);
}
});
@@ -111,15 +121,41 @@ impl ConnectionSetup {
pub fn modal_ui(&mut self, ui: &mut egui::Ui, modal: &Modal, cb: &dyn PlatformCallbacks) {
ui.add_space(6.0);
ui.vertical_centered(|ui| {
// Draw external node URL text edit.
let text_edit_resp = egui::TextEdit::singleline(&mut self.ext_node_url_edit)
.id(Id::from(modal.id))
ui.label(RichText::new(t!("wallets.node_url"))
.size(17.0)
.color(Colors::GRAY));
ui.add_space(8.0);
// Draw node URL text edit.
let url_edit_resp = egui::TextEdit::singleline(&mut self.ext_node_url_edit)
.id(Id::from(modal.id).with("node_url_edit"))
.font(TextStyle::Heading)
.desired_width(ui.available_width())
.cursor_at_end(true)
.ui(ui);
text_edit_resp.request_focus();
if text_edit_resp.clicked() {
ui.add_space(8.0);
if self.first_modal_launch {
self.first_modal_launch = false;
url_edit_resp.request_focus();
}
if url_edit_resp.clicked() {
cb.show_keyboard();
}
ui.label(RichText::new(t!("wallets.node_secret"))
.size(17.0)
.color(Colors::GRAY));
ui.add_space(8.0);
// Draw node API secret text edit.
let secret_edit_resp = egui::TextEdit::singleline(&mut self.ext_node_secret_edit)
.id(Id::from(modal.id).with("node_secret_edit"))
.font(TextStyle::Heading)
.desired_width(ui.available_width())
.cursor_at_end(true)
.ui(ui);
ui.add_space(8.0);
if secret_edit_resp.clicked() {
cb.show_keyboard();
}
@@ -138,18 +174,6 @@ impl ConnectionSetup {
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(8.0, 0.0);
// Add button callback.
let on_add = || {
let error = Url::parse(self.ext_node_url_edit.as_str()).is_err();
self.ext_node_url_error = error;
if !error {
AppConfig::add_external_node_url(self.ext_node_url_edit.clone());
// Close modal.
cb.hide_keyboard();
modal.close();
}
};
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal.cancel"), Colors::WHITE, || {
@@ -159,6 +183,35 @@ impl ConnectionSetup {
});
});
columns[1].vertical_centered_justified(|ui| {
// Add connection button callback.
let mut on_add = || {
let error = Url::parse(self.ext_node_url_edit.as_str()).is_err();
self.ext_node_url_error = error;
if !error {
// Save external connection.
let url = self.ext_node_url_edit.to_owned();
let secret = if self.ext_node_secret_edit.is_empty() {
None
} else {
Some(self.ext_node_secret_edit.to_owned())
};
let ext_conn = ExternalConnection::new(url.clone(), secret);
AppConfig::add_external_connection(ext_conn);
// Set added method as current.
self.method = ConnectionMethod::External(url);
// Close modal.
cb.hide_keyboard();
modal.close();
}
};
// Add connection on Enter button press.
View::on_enter_key(ui, || {
(on_add)();
});
View::button(ui, t!("modal.add"), Colors::WHITE, on_add);
});
});