Delete settings file if no wallet
And make launcher open default network directory as the first directory with a settings file. Fallback to the Create wallet view when no settings file exist anymore or if the settings file has no wallet.
This commit is contained in:
parent
30228c6490
commit
b6f45a42ee
@ -20,7 +20,7 @@ use crate::{
|
||||
services::{self, connect::client::backend},
|
||||
};
|
||||
|
||||
pub const DEFAULT_FILE_NAME: &str = "settings.json";
|
||||
pub const SETTINGS_FILE_NAME: &str = "settings.json";
|
||||
|
||||
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
||||
pub struct Settings {
|
||||
@ -30,7 +30,7 @@ pub struct Settings {
|
||||
impl Settings {
|
||||
pub fn from_file(network_dir: &NetworkDirectory) -> Result<Settings, SettingsError> {
|
||||
let mut path = network_dir.path().to_path_buf();
|
||||
path.push(DEFAULT_FILE_NAME);
|
||||
path.push(SETTINGS_FILE_NAME);
|
||||
|
||||
std::fs::read(path)
|
||||
.map_err(|e| match e.kind() {
|
||||
@ -52,7 +52,7 @@ pub async fn update_settings_file<F>(
|
||||
where
|
||||
F: FnOnce(Settings) -> Settings,
|
||||
{
|
||||
let path = network_dir.path().join(DEFAULT_FILE_NAME);
|
||||
let path = network_dir.path().join(SETTINGS_FILE_NAME);
|
||||
let file_exists = tokio::fs::try_exists(&path).await.unwrap_or(false);
|
||||
|
||||
let mut file = OpenOptions::new()
|
||||
@ -81,6 +81,13 @@ where
|
||||
|
||||
let settings = updater(settings);
|
||||
|
||||
if settings.wallets.is_empty() {
|
||||
tokio::fs::remove_file(&path)
|
||||
.await
|
||||
.map_err(|e| SettingsError::ReadingFile(e.to_string()))?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let content = serde_json::to_vec_pretty(&settings)
|
||||
.map_err(|e| SettingsError::WritingFile(format!("Failed to serialize settings: {}", e)))?;
|
||||
|
||||
@ -351,6 +358,7 @@ impl KeySetting {
|
||||
pub enum SettingsError {
|
||||
NotFound,
|
||||
ReadingFile(String),
|
||||
DeletingFile(String),
|
||||
WritingFile(String),
|
||||
Unexpected(String),
|
||||
}
|
||||
@ -359,6 +367,7 @@ impl std::fmt::Display for SettingsError {
|
||||
match self {
|
||||
Self::NotFound => write!(f, "Settings file not found"),
|
||||
Self::ReadingFile(e) => write!(f, "Error while reading file: {}", e),
|
||||
Self::DeletingFile(e) => write!(f, "Error while deleting file: {}", e),
|
||||
Self::WritingFile(e) => write!(f, "Error while writing file: {}", e),
|
||||
Self::Unexpected(e) => write!(f, "Unexpected error: {}", e),
|
||||
}
|
||||
|
||||
@ -14,7 +14,10 @@ use lianad::config::ConfigError;
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
use crate::{
|
||||
app::{self, settings::WalletSettings},
|
||||
app::{
|
||||
self,
|
||||
settings::{self, WalletSettings},
|
||||
},
|
||||
delete::{delete_wallet, DeleteError},
|
||||
dir::{LianaDirectory, NetworkDirectory},
|
||||
installer::UserFlow,
|
||||
@ -50,7 +53,13 @@ impl Launcher {
|
||||
let network = network.unwrap_or(
|
||||
NETWORKS
|
||||
.iter()
|
||||
.find(|net| datadir_path.path().join(net.to_string()).exists())
|
||||
.find(|net| {
|
||||
datadir_path
|
||||
.path()
|
||||
.join(net.to_string())
|
||||
.join(settings::SETTINGS_FILE_NAME)
|
||||
.exists()
|
||||
})
|
||||
.cloned()
|
||||
.unwrap_or(Network::Bitcoin),
|
||||
);
|
||||
@ -560,10 +569,18 @@ async fn check_network_datadir(path: NetworkDirectory) -> Result<State, String>
|
||||
})?;
|
||||
}
|
||||
|
||||
app::settings::Settings::from_file(&path)
|
||||
.map(|s| State::Wallets {
|
||||
wallets: s.wallets,
|
||||
add_wallet: false,
|
||||
})
|
||||
.map_err(|e| e.to_string())
|
||||
match settings::Settings::from_file(&path) {
|
||||
Ok(s) => {
|
||||
if s.wallets.is_empty() {
|
||||
Ok(State::NoWallet)
|
||||
} else {
|
||||
Ok(State::Wallets {
|
||||
wallets: s.wallets,
|
||||
add_wallet: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
Err(settings::SettingsError::NotFound) => Ok(State::NoWallet),
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user