|
| 1 | +use nostr_sdk::Keys; |
| 2 | +use nostrdb::{Ndb, Transaction}; |
| 3 | + |
| 4 | +pub use crate::user_account::UserAccount; |
| 5 | +use crate::{ |
| 6 | + imgcache::ImageCache, key_storage::KeyStorage, relay_generation::RelayGenerator, |
| 7 | + ui::profile::preview::SimpleProfilePreview, |
| 8 | +}; |
| 9 | + |
| 10 | +pub struct SimpleProfilePreviewController<'a> { |
| 11 | + ndb: &'a Ndb, |
| 12 | + img_cache: &'a mut ImageCache, |
| 13 | +} |
| 14 | + |
| 15 | +impl<'a> SimpleProfilePreviewController<'a> { |
| 16 | + pub fn new(ndb: &'a Ndb, img_cache: &'a mut ImageCache) -> Self { |
| 17 | + SimpleProfilePreviewController { ndb, img_cache } |
| 18 | + } |
| 19 | + |
| 20 | + pub fn set_profile_previews( |
| 21 | + &mut self, |
| 22 | + account_manager: &AccountManager<'a>, |
| 23 | + ui: &mut egui::Ui, |
| 24 | + edit_mode: bool, |
| 25 | + add_preview_ui: fn( |
| 26 | + ui: &mut egui::Ui, |
| 27 | + preview: SimpleProfilePreview, |
| 28 | + edit_mode: bool, |
| 29 | + ) -> bool, |
| 30 | + ) -> Option<Vec<usize>> { |
| 31 | + let mut to_remove: Option<Vec<usize>> = None; |
| 32 | + |
| 33 | + for i in 0..account_manager.num_accounts() { |
| 34 | + if let Some(account) = account_manager.get_account(i) { |
| 35 | + if let Ok(txn) = Transaction::new(self.ndb) { |
| 36 | + let profile = self |
| 37 | + .ndb |
| 38 | + .get_profile_by_pubkey(&txn, &account.key.public_key().to_bytes()); |
| 39 | + |
| 40 | + if let Ok(profile) = profile { |
| 41 | + let preview = SimpleProfilePreview::new(&profile, self.img_cache); |
| 42 | + |
| 43 | + if add_preview_ui(ui, preview, edit_mode) { |
| 44 | + if to_remove.is_none() { |
| 45 | + to_remove = Some(Vec::new()); |
| 46 | + } |
| 47 | + to_remove.as_mut().unwrap().push(i); |
| 48 | + } |
| 49 | + }; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + to_remove |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// The interface for managing the user's accounts. |
| 59 | +/// Represents all user-facing operations related to account management. |
| 60 | +pub struct AccountManager<'a> { |
| 61 | + accounts: &'a mut Vec<UserAccount>, |
| 62 | + key_store: KeyStorage, |
| 63 | + relay_generator: RelayGenerator, |
| 64 | +} |
| 65 | + |
| 66 | +impl<'a> AccountManager<'a> { |
| 67 | + pub fn new( |
| 68 | + accounts: &'a mut Vec<UserAccount>, |
| 69 | + key_store: KeyStorage, |
| 70 | + relay_generator: RelayGenerator, |
| 71 | + ) -> Self { |
| 72 | + AccountManager { |
| 73 | + accounts, |
| 74 | + key_store, |
| 75 | + relay_generator, |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + pub fn get_accounts(&'a self) -> &'a Vec<UserAccount> { |
| 80 | + self.accounts |
| 81 | + } |
| 82 | + |
| 83 | + pub fn get_account(&'a self, index: usize) -> Option<&'a UserAccount> { |
| 84 | + self.accounts.get(index) |
| 85 | + } |
| 86 | + |
| 87 | + pub fn remove_account(&mut self, index: usize) { |
| 88 | + if let Some(account) = self.accounts.get(index) { |
| 89 | + self.key_store.remove_key(&account.key); |
| 90 | + } |
| 91 | + if index < self.accounts.len() { |
| 92 | + self.accounts.remove(index); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + pub fn add_account(&'a mut self, key: Keys, ctx: &egui::Context) { |
| 97 | + self.key_store.add_key(&key); |
| 98 | + let relays = self.relay_generator.generate_relays_for(&key, ctx); |
| 99 | + let account = UserAccount { key, relays }; |
| 100 | + |
| 101 | + self.accounts.push(account) |
| 102 | + } |
| 103 | + |
| 104 | + pub fn num_accounts(&self) -> usize { |
| 105 | + self.accounts.len() |
| 106 | + } |
| 107 | +} |
0 commit comments