commit c77246c231c19134b690a036202349246a18cf39
parent b1215f193291ff5039ed2804f6b4f305c88b6908
Author: kernelkind <kernelkind@gmail.com>
Date: Wed, 19 Mar 2025 13:06:23 -0400
accounts: update & optimised find
Signed-off-by: kernelkind <kernelkind@gmail.com>
Diffstat:
1 file changed, 48 insertions(+), 0 deletions(-)
diff --git a/crates/notedeck/src/accounts.rs b/crates/notedeck/src/accounts.rs
@@ -370,12 +370,22 @@ impl Accounts {
self.accounts.get(ind)
}
+ pub fn get_account_mut(&mut self, ind: usize) -> Option<&mut UserAccount> {
+ self.accounts.get_mut(ind)
+ }
+
pub fn find_account(&self, pk: &[u8; 32]) -> Option<&UserAccount> {
self.accounts
.iter()
.find(|acc| acc.key.pubkey.bytes() == pk)
}
+ pub fn find_account_mut(&mut self, pk: &[u8; 32]) -> Option<&mut UserAccount> {
+ self.accounts
+ .iter_mut()
+ .find(|acc| acc.key.pubkey.bytes() == pk)
+ }
+
pub fn remove_account(&mut self, index: usize) {
if let Some(account) = self.accounts.get(index) {
if let Some(key_store) = &self.key_store {
@@ -475,6 +485,28 @@ impl Accounts {
}
}
+ pub fn update_current_account(&mut self, update: impl FnOnce(&mut UserAccount)) {
+ {
+ let Some(cur_account) = self.get_selected_account_mut() else {
+ return;
+ };
+
+ update(cur_account);
+ }
+
+ let Some(cur_acc) = self.get_selected_account() else {
+ return;
+ };
+
+ let Some(key_store) = &self.key_store else {
+ return;
+ };
+
+ if let Err(err) = key_store.write_account(cur_acc) {
+ tracing::error!("Could not add account {:?} to storage: {err}", cur_acc.key);
+ }
+ }
+
pub fn num_accounts(&self) -> usize {
self.accounts.len()
}
@@ -504,6 +536,22 @@ impl Accounts {
.map(|i| self.get_account(i))?
}
+ pub fn get_selected_account_mut(&mut self) -> Option<&mut UserAccount> {
+ self.currently_selected_account
+ .map(|i| self.get_account_mut(i))?
+ }
+
+ pub fn get_account_mut_optimized(&mut self, pk: &[u8; 32]) -> Option<&mut UserAccount> {
+ if let Some(ind) = self.currently_selected_account {
+ if ind < self.accounts.len() {
+ return Some(&mut self.accounts[ind]);
+ }
+ }
+ self.accounts
+ .iter_mut()
+ .find(|acc| acc.key.pubkey.bytes() == pk)
+ }
+
pub fn get_selected_account_data(&mut self) -> Option<&mut AccountData> {
let account_pubkey = *self.selected_account_pubkey_bytes()?;
self.account_data.get_mut(&account_pubkey)