Skip to content

Commit

Permalink
Merge pull request #16 from flomang/master
Browse files Browse the repository at this point in the history
Wallet default name and path
  • Loading branch information
yeastplume authored Oct 18, 2022
2 parents a603309 + bb11986 commit a056811
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 39 deletions.
14 changes: 14 additions & 0 deletions crates/core/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ pub fn get_grin_wallet_default_path(chain_type: &global::ChainTypes) -> PathBuf
grin_path
}

pub fn create_grin_wallet_path(chain_type: &global::ChainTypes, sub_dir: &str) -> PathBuf {
// Check if grin dir exists
let mut grin_path = match dirs::home_dir() {
Some(p) => p,
None => PathBuf::new(),
};
grin_path.push(GRIN_HOME);
grin_path.push(chain_type.shortname());
grin_path.push(GRIN_WALLET_TOP_LEVEL_DIR);
grin_path.push(sub_dir);

grin_path
}

pub type WalletInterfaceHttpNodeClient = WalletInterface<
DefaultLCProvider<'static, HTTPNodeClient, keychain::ExtKeychain>,
HTTPNodeClient,
Expand Down
3 changes: 2 additions & 1 deletion locale/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,6 @@
"tx-status": "Status",
"tx-net-difference": "Amount",
"tx-id": "ID",
"tx-type": "Type"
"tx-type": "Type",
"wallet-default-name": "Default"
}
1 change: 1 addition & 0 deletions locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
"status-line-title-main": "Status Mainnet",
"status-line-title-test": "Status Testnet",
"wallet-list": "Wallets",
"wallet-default-name": "Default",
"yes": "Yes",
"no": "No",
"cancel":"Cancel",
Expand Down
49 changes: 37 additions & 12 deletions src/gui/element/wallet/setup/init.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use {
super::super::super::{DEFAULT_FONT_SIZE, DEFAULT_HEADER_FONT_SIZE},
crate::gui::{style, GrinGui, Interaction, Message},
crate::gui::{style, GrinGui, Interaction, Message, element::settings::wallet},
crate::localization::localized_string,
crate::Result,
grin_gui_core::theme::ColorPalette,
grin_gui_core::{
theme::ColorPalette,
wallet::{create_grin_wallet_path, ChainTypes},
},
iced::{
alignment, button, Alignment, Button, Column, Command, Container, Element, Length, Row,
Space, Text,
Expand Down Expand Up @@ -40,8 +43,33 @@ pub fn handle_message(
) -> Result<Command<Message>> {
let state = &mut grin_gui.wallet_state.setup_state;
match message {
LocalViewInteraction::WalletSetup => state.mode = super::Mode::CreateWallet,
LocalViewInteraction::WalletList => state.mode = super::Mode::ListWallets
LocalViewInteraction::WalletSetup => {
let config = &grin_gui.config;
let wallet_default_name = localized_string("wallet-default-name");
let mut wallet_display_name = wallet_default_name.clone();
let mut i = 1;

// wallet display name must be unique: i.e. Default 1, Default 2, ...
while let Some(_) = config
.wallets
.iter()
.find(|wallet| wallet.display_name == wallet_display_name)
{
wallet_display_name = format!("{} {}", wallet_default_name, i);
i += 1;
}

// i.e. default_1, default_2, ...
let wallet_dir: String = str::replace(&wallet_display_name.to_lowercase(), " ", "_");

state
.setup_wallet_state
.advanced_options_state
.top_level_directory = create_grin_wallet_path(&ChainTypes::Mainnet, &wallet_dir);

state.mode = super::Mode::CreateWallet(wallet_display_name);
}
LocalViewInteraction::WalletList => state.mode = super::Mode::ListWallets,
}
Ok(Command::none())
}
Expand All @@ -50,21 +78,19 @@ pub fn data_container<'a>(
color_palette: ColorPalette,
state: &'a mut StateContainer,
) -> Container<'a, Message> {

// Title row
let title = Text::new(localized_string("setup-grin-first-time"))
.size(DEFAULT_HEADER_FONT_SIZE)
.horizontal_alignment(alignment::Horizontal::Center);

let title_container = Container::new(title)
.style(style::BrightBackgroundContainer(color_palette));
let title_container =
Container::new(title).style(style::BrightBackgroundContainer(color_palette));

let title_row = Row::new()
.push(title_container)
.align_items(Alignment::Center)
.padding(6)
.spacing(20);


let description = Text::new(localized_string("setup-grin-wallet-description"))
.size(DEFAULT_FONT_SIZE)
Expand Down Expand Up @@ -111,8 +137,7 @@ pub fn data_container<'a>(
.into();

let select_wallet_button_container =
Container::new(select_wallet_button.map(Message::Interaction))
.center_x();
Container::new(select_wallet_button.map(Message::Interaction)).center_x();

//let mut wallet_setup_modal_column =
/*let wallet_setup_select_column = {
Expand Down Expand Up @@ -142,14 +167,14 @@ pub fn data_container<'a>(
.push(Space::new(Length::Units(0), Length::Units(unit_spacing)))
.push(select_wallet_button_container)
.align_items(Alignment::Center);

let colum = Column::new()
.push(title_row)
.push(Space::new(Length::Units(0), Length::Units(unit_spacing)))
.push(description_container)
.push(Space::new(Length::Units(0), Length::Units(unit_spacing)))
.push(select_column)
.align_items(Alignment::Center);
.align_items(Alignment::Center);

Container::new(colum)
.center_y()
Expand Down
8 changes: 4 additions & 4 deletions src/gui/element/wallet/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct StateContainer {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Mode {
Init,
CreateWallet,
CreateWallet(String),
ListWallets,
WalletCreateSuccess,
}
Expand Down Expand Up @@ -54,10 +54,10 @@ pub fn data_container<'a>(
state: &'a mut StateContainer,
config: &Config,
) -> Container<'a, Message> {
let content = match state.mode {
let content = match &state.mode {
Mode::Init => init::data_container(color_palette, &mut state.setup_init_state),
Mode::CreateWallet => {
wallet_setup::data_container(color_palette, &mut state.setup_wallet_state)
Mode::CreateWallet(default_display_name) => {
wallet_setup::data_container(color_palette, &mut state.setup_wallet_state, default_display_name)
}
Mode::WalletCreateSuccess => {
wallet_success::data_container(color_palette, &mut state.setup_wallet_success_state)
Expand Down
55 changes: 36 additions & 19 deletions src/gui/element/wallet/setup/wallet_setup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::log_error;
use grin_gui_core::wallet::{get_grin_wallet_default_path, global};
//use futures::future::OrElse;
//use iced::button::StyleSheet;
//use iced_native::Widget;
Expand All @@ -17,6 +16,7 @@ use {
config::Wallet,
fs::PersistentData,
node::ChainTypes::{self, Mainnet, Testnet},
wallet::create_grin_wallet_path,
wallet::WalletInterface,
},
iced::{
Expand Down Expand Up @@ -51,9 +51,9 @@ impl Default for StateContainer {
}

pub struct AdvancedOptionsState {
display_name_input_state: text_input::State,
display_name_value: String,
folder_select_button_state: button::State,
pub display_name_input_state: text_input::State,
pub display_name_value: String,
pub top_level_directory: PathBuf,
}

Expand Down Expand Up @@ -99,7 +99,7 @@ pub enum LocalViewInteraction {
ToggleAdvancedOptions(bool),
ToggleIsTestnet(bool),
DisplayName(String),
CreateWallet,
CreateWallet(String, PathBuf),
WalletCreatedOk((String, String, String, ChainTypes)),
WalletCreateError(Arc<RwLock<Option<anyhow::Error>>>),
ShowFolderPicker,
Expand All @@ -112,6 +112,8 @@ pub fn handle_message<'a>(
let state = &mut grin_gui.wallet_state.setup_state.setup_wallet_state;
match message {
LocalViewInteraction::Back => {
// reset user input values
grin_gui.wallet_state.setup_state.setup_wallet_state = Default::default();
grin_gui.wallet_state.setup_state.mode = super::Mode::Init;
}
LocalViewInteraction::PasswordInput(password) => {
Expand All @@ -136,20 +138,21 @@ pub fn handle_message<'a>(
LocalViewInteraction::ToggleIsTestnet(_) => {
state.is_testnet = !state.is_testnet;
let current_tld = state.advanced_options_state.top_level_directory.clone();
let directory = current_tld.file_name().unwrap().to_str().unwrap();

if state.is_testnet {
let default_path = get_grin_wallet_default_path(&global::ChainTypes::Mainnet);
let default_path = create_grin_wallet_path(&Mainnet, directory);
// Only change if nobody's modified the default path
if default_path == current_tld {
state.advanced_options_state.top_level_directory =
get_grin_wallet_default_path(&global::ChainTypes::Testnet);
create_grin_wallet_path(&Testnet, directory);
}
} else {
let default_path = get_grin_wallet_default_path(&global::ChainTypes::Testnet);
let default_path = create_grin_wallet_path(&Testnet, directory);
// Only change if nobody's modified the default path
if default_path == current_tld {
state.advanced_options_state.top_level_directory =
get_grin_wallet_default_path(&global::ChainTypes::Mainnet);
create_grin_wallet_path(&Mainnet, directory);
}
}
}
Expand All @@ -172,12 +175,12 @@ pub fn handle_message<'a>(
}
};
}
LocalViewInteraction::CreateWallet => {
LocalViewInteraction::CreateWallet(display_name, top_level_directory) => {
grin_gui.error.take();

log::debug!(
"setup::wallet::LocalViewInteraction::CreateWallet {}",
state.advanced_options_state.display_name_value
display_name,
);

let password = state.password_state.input_value.clone();
Expand All @@ -188,8 +191,8 @@ pub fn handle_message<'a>(
WalletInterface::init(
w,
password.clone(),
state.advanced_options_state.top_level_directory.clone(),
state.advanced_options_state.display_name_value.clone(),
top_level_directory,
display_name,
chain_type,
)
};
Expand Down Expand Up @@ -217,8 +220,13 @@ pub fn handle_message<'a>(
.setup_state
.setup_wallet_success_state
.recovery_phrase = mnemonic;

grin_gui.wallet_state.setup_state.mode =
crate::gui::element::wallet::setup::Mode::WalletCreateSuccess;

// reset user input values
grin_gui.wallet_state.setup_state.setup_wallet_state = Default::default();

let _ = grin_gui.config.save();
}
LocalViewInteraction::WalletCreateError(err) => {
Expand All @@ -235,6 +243,7 @@ pub fn handle_message<'a>(
pub fn data_container<'a>(
color_palette: ColorPalette,
state: &'a mut StateContainer,
default_display_name: &str,
) -> Container<'a, Message> {
let check_password = || {
state.password_state.input_value == state.password_state.repeat_input_value
Expand Down Expand Up @@ -405,8 +414,8 @@ pub fn data_container<'a>(

let display_name_input = TextInput::new(
&mut state.advanced_options_state.display_name_input_state,
&localized_string("default"), // TODO @sheldonth
&state.advanced_options_state.display_name_value, // todo: Default2, Default3, etc...
default_display_name,
&state.advanced_options_state.display_name_value,
|s| Interaction::WalletSetupWalletViewInteraction(LocalViewInteraction::DisplayName(s)),
)
.size(DEFAULT_FONT_SIZE)
Expand All @@ -432,10 +441,11 @@ pub fn data_container<'a>(
));
let folder_select_button: Element<Interaction> = folder_select_button.into();

let tld_string = match state.advanced_options_state.top_level_directory.to_str() {
Some(s) => s,
None => "",
};
let tld_string = state
.advanced_options_state
.top_level_directory
.to_str()
.unwrap();
let current_tld = Text::new(tld_string).size(DEFAULT_FONT_SIZE);

let current_tld_container =
Expand Down Expand Up @@ -490,8 +500,15 @@ pub fn data_container<'a>(
)
.style(style::DefaultBoxedButton(color_palette));
if check_password() {
let top_level_directory = state.advanced_options_state.top_level_directory.clone();
let display_name = if state.advanced_options_state.display_name_value.is_empty() {
default_display_name.to_string()
} else {
state.advanced_options_state.display_name_value.clone()
};

submit_button = submit_button.on_press(Interaction::WalletSetupWalletViewInteraction(
LocalViewInteraction::CreateWallet,
LocalViewInteraction::CreateWallet(display_name, top_level_directory),
));
}

Expand Down
3 changes: 0 additions & 3 deletions src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ impl Application for GrinGui {
global::set_local_chain_type(wallet.chain_type);
}

grin_gui.wallet_state.setup_state.setup_wallet_state.advanced_options_state.top_level_directory =
get_grin_wallet_default_path(&global::get_chain_type());

// Check initial wallet status
/*if !config.wallet.toml_file_path.is_some()
|| !w.config_exists(
Expand Down

0 comments on commit a056811

Please sign in to comment.