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,34 +104,33 @@ impl Component for Explorer {
}
fn handle_key_event(&mut self, key: KeyEvent) -> Result<Option<Action>> {
if self.is_active {
match key.code {
KeyCode::Esc => {
self.is_active = false;
self.current_tab = CurrentTab::Nothing;
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
}
return Ok(Some(Action::SetActiveScreen(Mode::Menu)));
},
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
self.move_right();
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() {
component.set_active(self.current_tab.clone());
}
},
_ => {
for component in self.components.iter_mut() {
component.handle_key_event(key)?;
}
},
}
if !self.is_active { return Ok(None); }
match key.code {
KeyCode::Esc => {
self.is_active = false;
self.current_tab = CurrentTab::Nothing;
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
}
return Ok(Some(Action::SetActiveScreen(Mode::Menu)));
},
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
self.move_right();
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() {
component.set_active(self.current_tab.clone());
}
},
_ => {
for component in self.components.iter_mut() {
component.handle_key_event(key)?;
}
},
}
Ok(None)
}

View File

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

View File

@ -66,10 +66,12 @@ impl AddAccount {
impl PartialComponent for AddAccount {
fn set_active(&mut self, current_tab: CurrentTab) {
self.name = Input::new(String::new());
match current_tab {
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 {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
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>> {
if self.is_active {
if self.current_tab == CurrentTab::AddAccount {
match key.code {
KeyCode::Esc => {
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)?;
}
if !self.is_active { return Ok(None) }
match self.current_tab {
// block the default key handle for popups
CurrentTab::AddAccount | 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());
}
},
_ => {
for component in self.components.iter_mut() {
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());
}
},
_ => {
for component in self.components.iter_mut() {
component.handle_key_event(key)?;
}
},
_ => match key.code {
KeyCode::Esc => {
self.is_active = false;
self.current_tab = CurrentTab::Nothing;
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
}
}
} else {
match key.code {
KeyCode::Esc => {
self.is_active = false;
self.current_tab = CurrentTab::Nothing;
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
}
return Ok(Some(Action::SetActiveScreen(Mode::Menu)));
},
KeyCode::Char('W') => {
self.current_tab = CurrentTab::AddAccount;
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
}
},
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
self.move_right();
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() {
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)));
},
KeyCode::Char('W') => {
self.current_tab = CurrentTab::AddAccount;
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
}
},
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
self.move_right();
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() {
component.set_active(self.current_tab.clone());
}
},
_ => {
for component in self.components.iter_mut() {
component.handle_key_event(key)?;
}
},
}
}
Ok(None)
}
fn update(&mut self, action: Action) -> Result<Option<Action>> {
if let Action::SetActiveScreen(Mode::Wallet) = action {
self.is_active = true;
self.current_tab = CurrentTab::Accounts;
for component in self.components.iter_mut() {
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());
}
match action {
Action::SetActiveScreen(Mode::Wallet) => self.is_active = true,
Action::UpdateAccountName(_) | Action::NewAccount(_) =>
self.current_tab = CurrentTab::Accounts,
Action::RenameAccount(_) => self.current_tab = CurrentTab::RenameAccount,
_ => {}
}
for component in self.components.iter_mut() {
component.set_active(self.current_tab.clone());
component.update(action.clone())?;
}
Ok(None)

View File

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