Move preview controller out of account_manager.rs

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-05-22 16:35:17 -04:00
committed by William Casarin
parent 11b3effa51
commit bdf6156fff
4 changed files with 87 additions and 83 deletions

View File

@@ -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<Vec<usize>> {
let mut to_remove: Option<Vec<usize>> = 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<usize> {
let mut clicked_at: Option<usize> = 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.

View File

@@ -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> {

View File

@@ -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;

View File

@@ -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<Vec<usize>> {
let mut to_remove: Option<Vec<usize>> = 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<usize> {
let mut clicked_at: Option<usize> = 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
}
}