ui + config: modals content drawing refactoring, move connections config to separate file

This commit is contained in:
ardocrat
2023-08-03 04:11:25 +03:00
parent b34654ab00
commit ff9fcd38f2
27 changed files with 916 additions and 643 deletions
+8 -6
View File
@@ -43,7 +43,7 @@ impl WalletConfig {
pub fn create(name: String, external_node_url: Option<String>) -> WalletConfig {
let id = chrono::Utc::now().timestamp();
let chain_type = AppConfig::chain_type();
let config_path = Self::get_config_file_path(&chain_type, id);
let config_path = Self::get_config_file_path(chain_type, id);
let config = WalletConfig { chain_type, id, name, external_node_url };
Settings::write_to_file(&config, config_path);
@@ -61,8 +61,10 @@ impl WalletConfig {
}
/// Get wallets base directory path for provided [`ChainTypes`].
pub fn get_base_path(chain_type: &ChainTypes) -> PathBuf {
let mut wallets_path = Settings::get_base_path(Some(chain_type.shortname()));
pub fn get_base_path(chain_type: ChainTypes) -> PathBuf {
let chain_name = chain_type.shortname();
let sub_dir = Some(chain_name.as_str());
let mut wallets_path = Settings::get_base_path(sub_dir);
wallets_path.push(BASE_DIR_NAME);
// Create wallets base directory if it doesn't exist.
if !wallets_path.exists() {
@@ -72,7 +74,7 @@ impl WalletConfig {
}
/// Get config file path for provided [`ChainTypes`] and wallet identifier.
fn get_config_file_path(chain_type: &ChainTypes, id: i64) -> PathBuf {
fn get_config_file_path(chain_type: ChainTypes, id: i64) -> PathBuf {
let mut config_path = Self::get_base_path(chain_type);
config_path.push(id.to_string());
// Create if the config path doesn't exist.
@@ -86,14 +88,14 @@ impl WalletConfig {
/// Get current wallet data path.
pub fn get_data_path(&self) -> String {
let chain_type = AppConfig::chain_type();
let mut config_path = Self::get_base_path(&chain_type);
let mut config_path = Self::get_base_path(chain_type);
config_path.push(self.id.to_string());
config_path.to_str().unwrap().to_string()
}
/// Save wallet config.
fn save(&self) {
let config_path = Self::get_config_file_path(&self.chain_type, self.id);
let config_path = Self::get_config_file_path(self.chain_type, self.id);
Settings::write_to_file(self, config_path);
}
+16 -6
View File
@@ -18,13 +18,13 @@ use lazy_static::lazy_static;
use serde_derive::{Deserialize, Serialize};
use crate::Settings;
use crate::wallet::ExternalConnection;
use crate::wallet::{BASE_DIR_NAME, ExternalConnection};
lazy_static! {
/// Static settings state to be accessible globally.
/// Static connections state to be accessible globally.
static ref CONNECTIONS_STATE: Arc<RwLock<ConnectionsConfig>> = Arc::new(
RwLock::new(
Settings::init_config(Settings::get_config_path(CONFIG_FILE_NAME, None))
Settings::init_config(Settings::get_config_path(CONFIG_FILE_NAME, Some(BASE_DIR_NAME)))
)
);
}
@@ -36,9 +36,6 @@ pub struct ConnectionsConfig {
external: Vec<ExternalConnection>
}
/// Wallet configuration file name.
const CONFIG_FILE_NAME: &'static str = "connections.toml";
impl Default for ConnectionsConfig {
fn default() -> Self {
Self {
@@ -49,6 +46,9 @@ impl Default for ConnectionsConfig {
}
}
/// Wallet configuration file name.
const CONFIG_FILE_NAME: &'static str = "connections.toml";
impl ConnectionsConfig {
/// Save connections config to file.
pub fn save(&self) {
@@ -90,4 +90,14 @@ impl ConnectionsConfig {
}
None
}
/// Remove external node connection.
pub fn remove_external_connection(conn: &ExternalConnection) {
let mut w_config = CONNECTIONS_STATE.write().unwrap();
let index = w_config.external.iter().position(|c| c.url == conn.url);
if let Some(i) = index {
w_config.external.remove(i);
w_config.save();
}
}
}
+4 -4
View File
@@ -46,7 +46,7 @@ pub struct Wallets {
impl Default for Wallets {
fn default() -> Self {
Self {
list: Self::init(&AppConfig::chain_type()),
list: Self::init(AppConfig::chain_type()),
selected_id: None
}
}
@@ -54,7 +54,7 @@ impl Default for Wallets {
impl Wallets {
/// Initialize wallets from base directory for provided [`ChainType`].
fn init(chain_type: &ChainTypes) -> Vec<Wallet> {
fn init(chain_type: ChainTypes) -> Vec<Wallet> {
let mut wallets = Vec::new();
let wallets_dir = WalletConfig::get_base_path(chain_type);
// Load wallets from base directory.
@@ -71,7 +71,7 @@ impl Wallets {
}
/// Reinitialize wallets for provided [`ChainTypes`].
pub fn reinit(&mut self, chain_type: &ChainTypes) {
pub fn reinit(&mut self, chain_type: ChainTypes) {
self.list = Self::init(chain_type);
}
@@ -103,7 +103,7 @@ impl Wallets {
/// Open selected wallet.
pub fn open_selected(&mut self, password: String) -> Result<(), Error> {
for mut w in self.list.iter_mut() {
for w in self.list.iter_mut() {
if Some(w.config.id) == self.selected_id {
return w.open(password);
}