make wallet mod code prettier

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2024-12-03 20:57:35 +03:00
parent 9e911fce01
commit b5ad10ad6d
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA
5 changed files with 88 additions and 121 deletions

View File

@ -104,7 +104,7 @@ impl Component for Explorer {
} }
fn handle_key_event(&mut self, key: KeyEvent) -> Result<Option<Action>> { fn handle_key_event(&mut self, key: KeyEvent) -> Result<Option<Action>> {
if self.is_active { if !self.is_active { return Ok(None); }
match key.code { match key.code {
KeyCode::Esc => { KeyCode::Esc => {
self.is_active = false; self.is_active = false;
@ -132,7 +132,6 @@ impl Component for Explorer {
} }
}, },
} }
}
Ok(None) Ok(None)
} }

View File

@ -69,6 +69,7 @@ impl Accounts {
.to_ss58check_with_version(Ss58AddressFormat::custom(1996)); .to_ss58check_with_version(Ss58AddressFormat::custom(1996));
self.wallet_keys.push((name, address, secret_seed, pair_signer)); self.wallet_keys.push((name, address, secret_seed, pair_signer));
self.last_row();
self.save_to_file(); self.save_to_file();
} }

View File

@ -66,10 +66,12 @@ impl AddAccount {
impl PartialComponent for AddAccount { impl PartialComponent for AddAccount {
fn set_active(&mut self, current_tab: CurrentTab) { fn set_active(&mut self, current_tab: CurrentTab) {
self.name = Input::new(String::new());
match current_tab { match current_tab {
CurrentTab::AddAccount => self.is_active = true, CurrentTab::AddAccount => self.is_active = true,
_ => self.is_active = false, _ => {
self.is_active = false;
self.name = Input::new(String::new());
},
}; };
} }
} }

View File

@ -81,10 +81,6 @@ impl Wallet {
} }
} }
impl PartialComponent for Wallet {
fn set_active(&mut self, _current_tab: CurrentTab) {}
}
impl Component for Wallet { impl Component for Wallet {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> { fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
for component in self.components.iter_mut() { for component in self.components.iter_mut() {
@ -103,9 +99,11 @@ impl Component for Wallet {
} }
fn handle_key_event(&mut self, key: KeyEvent) -> Result<Option<Action>> { fn handle_key_event(&mut self, key: KeyEvent) -> Result<Option<Action>> {
if self.is_active { if !self.is_active { return Ok(None) }
if self.current_tab == CurrentTab::AddAccount {
match key.code { match self.current_tab {
// block the default key handle for popups
CurrentTab::AddAccount | CurrentTab::RenameAccount => match key.code {
KeyCode::Esc => { KeyCode::Esc => {
self.current_tab = CurrentTab::Accounts; self.current_tab = CurrentTab::Accounts;
for component in self.components.iter_mut() { for component in self.components.iter_mut() {
@ -117,23 +115,8 @@ impl Component for Wallet {
component.handle_key_event(key)?; component.handle_key_event(key)?;
} }
} }
}
} else if self.current_tab == CurrentTab::RenameAccount {
match key.code {
KeyCode::Esc => {
self.current_tab = CurrentTab::Accounts;
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
}
}, },
_ => { _ => match key.code {
for component in self.components.iter_mut() {
component.handle_key_event(key)?;
}
}
}
} else {
match key.code {
KeyCode::Esc => { KeyCode::Esc => {
self.is_active = false; self.is_active = false;
self.current_tab = CurrentTab::Nothing; self.current_tab = CurrentTab::Nothing;
@ -167,37 +150,19 @@ impl Component for Wallet {
}, },
} }
} }
}
Ok(None) Ok(None)
} }
fn update(&mut self, action: Action) -> Result<Option<Action>> { fn update(&mut self, action: Action) -> Result<Option<Action>> {
if let Action::SetActiveScreen(Mode::Wallet) = action { match action {
self.is_active = true; Action::SetActiveScreen(Mode::Wallet) => self.is_active = true,
self.current_tab = CurrentTab::Accounts; Action::UpdateAccountName(_) | Action::NewAccount(_) =>
self.current_tab = CurrentTab::Accounts,
Action::RenameAccount(_) => self.current_tab = CurrentTab::RenameAccount,
_ => {}
}
for component in self.components.iter_mut() { for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone()); component.set_active(self.current_tab.clone());
}
}
if let Action::NewAccount(_) = action {
self.current_tab = CurrentTab::Accounts;
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
}
}
if let Action::UpdateAccountName(_) = action {
self.current_tab = CurrentTab::Accounts;
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
}
}
if let Action::RenameAccount(_) = action {
self.current_tab = CurrentTab::RenameAccount;
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
}
}
for component in self.components.iter_mut() {
component.update(action.clone())?; component.update(action.clone())?;
} }
Ok(None) Ok(None)

View File

@ -73,9 +73,9 @@ impl PartialComponent for RenameAccount {
_ => { _ => {
self.is_active = false; self.is_active = false;
self.old_name = String::new(); self.old_name = String::new();
self.name = Input::new(String::new());
} }
}; };
self.name = Input::new(String::new());
} }
} }