diff --git a/src/account_manager.rs b/src/account_manager.rs index c0d82848..0c03d99c 100644 --- a/src/account_manager.rs +++ b/src/account_manager.rs @@ -1,88 +1,7 @@ use enostr::FullKeypair; -use nostrdb::{Ndb, Transaction}; pub use crate::user_account::UserAccount; -use crate::{ - imgcache::ImageCache, key_storage::KeyStorage, relay_generation::RelayGenerator, - ui::profile::preview::SimpleProfilePreview, -}; - -pub struct SimpleProfilePreviewController<'a> { - ndb: &'a Ndb, - img_cache: &'a mut ImageCache, -} - -impl<'a> SimpleProfilePreviewController<'a> { - pub fn new(ndb: &'a Ndb, img_cache: &'a mut ImageCache) -> Self { - SimpleProfilePreviewController { ndb, img_cache } - } - - pub fn set_profile_previews( - &mut self, - account_manager: &AccountManager, - ui: &mut egui::Ui, - edit_mode: bool, - add_preview_ui: fn( - ui: &mut egui::Ui, - preview: SimpleProfilePreview, - edit_mode: bool, - ) -> bool, - ) -> Option> { - let mut to_remove: Option> = None; - - for i in 0..account_manager.num_accounts() { - if let Some(account) = account_manager.get_account(i) { - if let Ok(txn) = Transaction::new(self.ndb) { - let profile = self - .ndb - .get_profile_by_pubkey(&txn, account.key.pubkey.bytes()); - - if let Ok(profile) = profile { - let preview = SimpleProfilePreview::new(&profile, self.img_cache); - - if add_preview_ui(ui, preview, edit_mode) { - if to_remove.is_none() { - to_remove = Some(Vec::new()); - } - to_remove.as_mut().unwrap().push(i); - } - }; - } - } - } - - to_remove - } - - pub fn view_profile_previews( - &mut self, - account_manager: &'a AccountManager, - ui: &mut egui::Ui, - add_preview_ui: fn(ui: &mut egui::Ui, preview: SimpleProfilePreview, index: usize) -> bool, - ) -> Option { - let mut clicked_at: Option = None; - - for i in 0..account_manager.num_accounts() { - if let Some(account) = account_manager.get_account(i) { - if let Ok(txn) = Transaction::new(self.ndb) { - let profile = self - .ndb - .get_profile_by_pubkey(&txn, account.key.pubkey.bytes()); - - if let Ok(profile) = profile { - let preview = SimpleProfilePreview::new(&profile, self.img_cache); - - if add_preview_ui(ui, preview, i) { - clicked_at = Some(i) - } - } - } - } - } - - clicked_at - } -} +use crate::{key_storage::KeyStorage, relay_generation::RelayGenerator}; /// The interface for managing the user's accounts. /// Represents all user-facing operations related to account management. diff --git a/src/ui/account_management.rs b/src/ui/account_management.rs index 4b2e0013..5ae98ee7 100644 --- a/src/ui/account_management.rs +++ b/src/ui/account_management.rs @@ -1,6 +1,6 @@ use crate::ui::global_popup::FromApp; use crate::{ - account_manager::{AccountManager, SimpleProfilePreviewController, UserAccount}, + account_manager::{AccountManager, UserAccount}, app_style::NotedeckTextStyle, ui::{self, Preview, View}, }; @@ -8,6 +8,7 @@ use egui::{Align, Button, Frame, Id, Layout, Margin, RichText, ScrollArea, Sense use super::global_popup::GlobalPopupType; use super::profile::preview::SimpleProfilePreview; +use super::profile::SimpleProfilePreviewController; use super::state_in_memory::STATE_ACCOUNT_MANAGEMENT; pub struct AccountManagementView<'a> { diff --git a/src/ui/profile/mod.rs b/src/ui/profile/mod.rs index dafd7b31..c388addb 100644 --- a/src/ui/profile/mod.rs +++ b/src/ui/profile/mod.rs @@ -1,5 +1,7 @@ pub mod picture; pub mod preview; +mod profile_preview_controller; pub use picture::ProfilePic; pub use preview::ProfilePreview; +pub use profile_preview_controller::SimpleProfilePreviewController; diff --git a/src/ui/profile/profile_preview_controller.rs b/src/ui/profile/profile_preview_controller.rs new file mode 100644 index 00000000..8eb3a94e --- /dev/null +++ b/src/ui/profile/profile_preview_controller.rs @@ -0,0 +1,82 @@ +use nostrdb::{Ndb, Transaction}; + +use crate::{account_manager::AccountManager, imgcache::ImageCache}; + +use super::preview::SimpleProfilePreview; + +pub struct SimpleProfilePreviewController<'a> { + ndb: &'a Ndb, + img_cache: &'a mut ImageCache, +} + +impl<'a> SimpleProfilePreviewController<'a> { + pub fn new(ndb: &'a Ndb, img_cache: &'a mut ImageCache) -> Self { + SimpleProfilePreviewController { ndb, img_cache } + } + + pub fn set_profile_previews( + &mut self, + account_manager: &AccountManager, + ui: &mut egui::Ui, + edit_mode: bool, + add_preview_ui: fn( + ui: &mut egui::Ui, + preview: SimpleProfilePreview, + edit_mode: bool, + ) -> bool, + ) -> Option> { + let mut to_remove: Option> = None; + + for i in 0..account_manager.num_accounts() { + if let Some(account) = account_manager.get_account(i) { + if let Ok(txn) = Transaction::new(self.ndb) { + let profile = self + .ndb + .get_profile_by_pubkey(&txn, account.key.pubkey.bytes()); + + if let Ok(profile) = profile { + let preview = SimpleProfilePreview::new(&profile, self.img_cache); + + if add_preview_ui(ui, preview, edit_mode) { + if to_remove.is_none() { + to_remove = Some(Vec::new()); + } + to_remove.as_mut().unwrap().push(i); + } + }; + } + } + } + + to_remove + } + + pub fn view_profile_previews( + &mut self, + account_manager: &'a AccountManager, + ui: &mut egui::Ui, + add_preview_ui: fn(ui: &mut egui::Ui, preview: SimpleProfilePreview, index: usize) -> bool, + ) -> Option { + let mut clicked_at: Option = None; + + for i in 0..account_manager.num_accounts() { + if let Some(account) = account_manager.get_account(i) { + if let Ok(txn) = Transaction::new(self.ndb) { + let profile = self + .ndb + .get_profile_by_pubkey(&txn, account.key.pubkey.bytes()); + + if let Ok(profile) = profile { + let preview = SimpleProfilePreview::new(&profile, self.img_cache); + + if add_preview_ui(ui, preview, i) { + clicked_at = Some(i) + } + } + } + } + } + + clicked_at + } +}