fix: external conn availability check

This commit is contained in:
ardocrat 2024-07-03 15:00:41 +03:00
parent f801b9a3f4
commit 51cb6255f6
3 changed files with 32 additions and 15 deletions

View file

@ -37,24 +37,25 @@ pub struct ConnectionsContent {
/// Editing external connection identifier for [`Modal`]. /// Editing external connection identifier for [`Modal`].
ext_conn_id_edit: Option<i64>, ext_conn_id_edit: Option<i64>,
/// Flag to check connections availability.
check_connections: bool,
/// [`Modal`] identifiers allowed at this ui container. /// [`Modal`] identifiers allowed at this ui container.
modal_ids: Vec<&'static str> modal_ids: Vec<&'static str>
} }
impl Default for ConnectionsContent { impl Default for ConnectionsContent {
fn default() -> Self { fn default() -> Self {
if AppConfig::show_connections_network_panel() {
ExternalConnection::start_ext_conn_availability_check();
}
Self { Self {
first_modal_launch: true, first_modal_launch: true,
ext_node_url_edit: "".to_string(), ext_node_url_edit: "".to_string(),
ext_node_secret_edit: "".to_string(), ext_node_secret_edit: "".to_string(),
ext_node_url_error: false, ext_node_url_error: false,
ext_conn_id_edit: None, ext_conn_id_edit: None,
check_connections: true,
modal_ids: vec![ modal_ids: vec![
Self::NETWORK_EXT_CONNECTION_MODAL Self::NETWORK_EXT_CONNECTION_MODAL
] ],
} }
} }
} }
@ -88,13 +89,17 @@ impl ConnectionsContent {
// Show network type selection. // Show network type selection.
let saved_chain_type = AppConfig::chain_type(); let saved_chain_type = AppConfig::chain_type();
NodeSetup::chain_type_ui(ui); NodeSetup::chain_type_ui(ui);
// Start external connections availability check on network type change.
if saved_chain_type != AppConfig::chain_type() {
ExternalConnection::start_ext_conn_availability_check();
}
ui.add_space(6.0); ui.add_space(6.0);
// Check connections availability.
if saved_chain_type != AppConfig::chain_type() {
self.check_connections = true;
}
if self.check_connections {
ExternalConnection::start_ext_conn_availability_check();
self.check_connections = false;
}
// Show integrated node info content. // Show integrated node info content.
Self::integrated_node_item_ui(ui, |ui| { Self::integrated_node_item_ui(ui, |ui| {
// Draw button to show integrated node info. // Draw button to show integrated node info.
@ -339,12 +344,12 @@ impl ConnectionsContent {
// Update or create new connection. // Update or create new connection.
let mut ext_conn = ExternalConnection::new(url, secret); let mut ext_conn = ExternalConnection::new(url, secret);
ext_conn.check_conn_availability();
if let Some(id) = self.ext_conn_id_edit { if let Some(id) = self.ext_conn_id_edit {
ext_conn.id = id; ext_conn.id = id;
} }
self.ext_conn_id_edit = None; self.ext_conn_id_edit = None;
ConnectionsConfig::add_ext_conn(ext_conn); ConnectionsConfig::add_ext_conn(ext_conn);
self.check_connections = true;
// Close modal. // Close modal.
cb.hide_keyboard(); cb.hide_keyboard();

View file

@ -39,6 +39,8 @@ pub struct ConnectionSetup {
/// Current wallet external connection. /// Current wallet external connection.
curr_ext_conn: Option<ExternalConnection>, curr_ext_conn: Option<ExternalConnection>,
/// Flag to check connections availability.
check_connections: bool,
/// [`Modal`] identifiers allowed at this ui container. /// [`Modal`] identifiers allowed at this ui container.
modal_ids: Vec<&'static str> modal_ids: Vec<&'static str>
@ -56,6 +58,7 @@ impl Default for ConnectionSetup {
ext_node_secret_edit: "".to_string(), ext_node_secret_edit: "".to_string(),
ext_node_url_error: false, ext_node_url_error: false,
curr_ext_conn: None, curr_ext_conn: None,
check_connections: true,
modal_ids: vec![ modal_ids: vec![
ADD_EXT_CONNECTION_MODAL ADD_EXT_CONNECTION_MODAL
] ]
@ -166,25 +169,32 @@ impl ConnectionSetup {
ui.add_space(4.0); ui.add_space(4.0);
let mut ext_conn_list = ConnectionsConfig::ext_conn_list(); let mut ext_conn_list = ConnectionsConfig::ext_conn_list();
// Check if current external connection was deleted to show at list.
// Check if current external connection was deleted to show at 1st place.
if let Some(wallet) = wallet { if let Some(wallet) = wallet {
if let Some(conn) = wallet.get_current_ext_conn() { if let Some(conn) = wallet.get_current_ext_conn() {
if ext_conn_list.iter() if ext_conn_list.iter()
.filter(|c| c.id == conn.id) .filter(|c| c.id == conn.id)
.collect::<Vec<&ExternalConnection>>().is_empty() { .collect::<Vec<&ExternalConnection>>().is_empty() {
if self.curr_ext_conn.is_none() { if self.curr_ext_conn.is_none() {
conn.check_conn_availability();
self.curr_ext_conn = Some(conn); self.curr_ext_conn = Some(conn);
} }
ext_conn_list.insert(0, self.curr_ext_conn.as_ref().unwrap().clone()); ext_conn_list.insert(0, self.curr_ext_conn.as_ref().unwrap().clone());
} }
} }
} }
// Check connections availability.
if self.check_connections {
ExternalConnection::start_ext_conn_availability_check();
self.check_connections = false;
}
if !ext_conn_list.is_empty() { if !ext_conn_list.is_empty() {
ui.add_space(8.0); ui.add_space(8.0);
for (index, conn) in ext_conn_list.iter().enumerate() { for (index, conn) in ext_conn_list.iter().enumerate() {
ui.horizontal_wrapped(|ui| { ui.horizontal_wrapped(|ui| {
// Draw connection list item. // Draw external connection item.
self.ext_conn_item_ui(ui, wallet, conn, index, ext_conn_list.len()); self.ext_conn_item_ui(ui, wallet, conn, index, ext_conn_list.len());
}); });
} }
@ -344,10 +354,12 @@ impl ConnectionSetup {
Some(self.ext_node_secret_edit.to_owned()) Some(self.ext_node_secret_edit.to_owned())
}; };
let ext_conn = ExternalConnection::new(url.clone(), secret); let ext_conn = ExternalConnection::new(url.clone(), secret);
ext_conn.check_conn_availability();
ConnectionsConfig::add_ext_conn(ext_conn.clone()); ConnectionsConfig::add_ext_conn(ext_conn.clone());
self.check_connections = true;
// Set added connection as current. // Set added connection as current.
self.method = ConnectionMethod::External(ext_conn.id); self.method = ConnectionMethod::External(ext_conn.id);
// Close modal. // Close modal.
cb.hide_keyboard(); cb.hide_keyboard();
modal.close(); modal.close();

View file

@ -48,7 +48,7 @@ impl ExternalConnection {
} }
/// Check connection availability. /// Check connection availability.
pub fn check_conn_availability(&self) { fn check_conn_availability(&self) {
// Check every connection at separate thread. // Check every connection at separate thread.
let conn = self.clone(); let conn = self.clone();
std::thread::spawn(move || { std::thread::spawn(move || {