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,35 +104,34 @@ 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;
self.current_tab = CurrentTab::Nothing; self.current_tab = CurrentTab::Nothing;
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());
} }
return Ok(Some(Action::SetActiveScreen(Mode::Menu))); return Ok(Some(Action::SetActiveScreen(Mode::Menu)));
}, },
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
self.move_right(); self.move_right();
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());
} }
}, },
KeyCode::Char('h') | KeyCode::Left => { KeyCode::Char('h') | KeyCode::Left => {
self.move_left(); self.move_left();
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());
} }
}, },
_ => { _ => {
for component in self.components.iter_mut() { for component in self.components.iter_mut() {
component.handle_key_event(key)?; component.handle_key_event(key)?;
} }
}, },
} }
}
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,101 +99,70 @@ 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 {
KeyCode::Esc => { // block the default key handle for popups
self.current_tab = CurrentTab::Accounts; CurrentTab::AddAccount | CurrentTab::RenameAccount => match key.code {
for component in self.components.iter_mut() { KeyCode::Esc => {
component.set_active(self.current_tab.clone()); self.current_tab = CurrentTab::Accounts;
} for component in self.components.iter_mut() {
}, component.set_active(self.current_tab.clone());
_ => { }
for component in self.components.iter_mut() { },
component.handle_key_event(key)?; _ => {
} for component in self.components.iter_mut() {
component.handle_key_event(key)?;
} }
} }
} else if self.current_tab == CurrentTab::RenameAccount { },
match key.code { _ => match key.code {
KeyCode::Esc => { KeyCode::Esc => {
self.current_tab = CurrentTab::Accounts; self.is_active = false;
for component in self.components.iter_mut() { self.current_tab = CurrentTab::Nothing;
component.set_active(self.current_tab.clone()); for component in self.components.iter_mut() {
} component.set_active(self.current_tab.clone());
},
_ => {
for component in self.components.iter_mut() {
component.handle_key_event(key)?;
}
} }
} return Ok(Some(Action::SetActiveScreen(Mode::Menu)));
} else { },
match key.code { KeyCode::Char('W') => {
KeyCode::Esc => { self.current_tab = CurrentTab::AddAccount;
self.is_active = false; for component in self.components.iter_mut() {
self.current_tab = CurrentTab::Nothing; component.set_active(self.current_tab.clone());
for component in self.components.iter_mut() { }
component.set_active(self.current_tab.clone()); },
} KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
return Ok(Some(Action::SetActiveScreen(Mode::Menu))); self.move_right();
}, for component in self.components.iter_mut() {
KeyCode::Char('W') => { component.set_active(self.current_tab.clone());
self.current_tab = CurrentTab::AddAccount; }
for component in self.components.iter_mut() { },
component.set_active(self.current_tab.clone()); KeyCode::Char('h') | KeyCode::Left => {
} self.move_left();
}, for component in self.components.iter_mut() {
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { component.set_active(self.current_tab.clone());
self.move_right(); }
for component in self.components.iter_mut() { },
component.set_active(self.current_tab.clone()); _ => {
} for component in self.components.iter_mut() {
}, component.handle_key_event(key)?;
KeyCode::Char('h') | KeyCode::Left => { }
self.move_left(); },
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
}
},
_ => {
for component in self.components.iter_mut() {
component.handle_key_event(key)?;
}
},
}
} }
} }
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(_) =>
for component in self.components.iter_mut() { self.current_tab = CurrentTab::Accounts,
component.set_active(self.current_tab.clone()); Action::RenameAccount(_) => self.current_tab = CurrentTab::RenameAccount,
} _ => {}
}
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() { for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
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());
} }
} }