refactor account switcher & management previews

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-05-25 17:00:21 -04:00
committed by William Casarin
parent 22264e70f5
commit 7ebd694f11
4 changed files with 89 additions and 70 deletions

View File

@@ -1,7 +1,14 @@
use enostr::{FullKeypair, Pubkey, RelayPool};
use nostrdb::ProfileRecord;
use std::path::Path;
use crate::account_manager::UserAccount;
use enostr::{FullKeypair, Pubkey, RelayPool};
use nostrdb::{Config, Ndb, ProfileRecord};
use crate::{
account_manager::{AccountManager, UserAccount},
imgcache::ImageCache,
key_storage::KeyStorage,
relay_generation::RelayGenerator,
};
#[allow(unused_must_use)]
pub fn sample_pool() -> RelayPool {
@@ -86,3 +93,22 @@ pub fn get_test_accounts() -> Vec<UserAccount> {
})
.collect()
}
pub fn get_accmgr_and_ndb_and_imgcache() -> (AccountManager, Ndb, ImageCache) {
let mut account_manager =
AccountManager::new(None, KeyStorage::None, RelayGenerator::Constant, || {});
let accounts = get_test_accounts();
accounts
.into_iter()
.for_each(|acc| account_manager.add_account(acc.key, || {}));
let mut config = Config::new();
config.set_ingester_threads(2);
let db_dir = Path::new(".");
let path = db_dir.to_str().unwrap();
let ndb = Ndb::new(path, &config).expect("ndb");
let imgcache_dir = db_dir.join("cache/img");
let img_cache = ImageCache::new(imgcache_dir);
(account_manager, ndb, img_cache)
}

View File

@@ -244,15 +244,10 @@ fn selected_widget() -> impl egui::Widget {
// PREVIEWS
mod preview {
use nostrdb::{Config, Ndb};
use ui::account_switcher::AccountSelectionWidget;
use nostrdb::Ndb;
use super::*;
use crate::imgcache::ImageCache;
use crate::key_storage::KeyStorage;
use crate::relay_generation::RelayGenerator;
use crate::test_data;
use std::path::Path;
use crate::{imgcache::ImageCache, test_data::get_accmgr_and_ndb_and_imgcache};
pub struct AccountManagementPreview {
account_manager: AccountManager,
@@ -260,27 +255,9 @@ mod preview {
img_cache: ImageCache,
}
fn get_ndb_and_img_cache() -> (Ndb, ImageCache) {
let mut config = Config::new();
config.set_ingester_threads(2);
let db_dir = Path::new(".");
let path = db_dir.to_str().unwrap();
let ndb = Ndb::new(path, &config).expect("ndb");
let imgcache_dir = db_dir.join("cache/img");
let img_cache = ImageCache::new(imgcache_dir);
(ndb, img_cache)
}
impl AccountManagementPreview {
fn new() -> Self {
let mut account_manager =
AccountManager::new(None, KeyStorage::None, RelayGenerator::Constant, || {});
let accounts = test_data::get_test_accounts();
accounts
.into_iter()
.for_each(|acc| account_manager.add_account(acc.key, || {}));
let (ndb, img_cache) = get_ndb_and_img_cache();
let (account_manager, ndb, img_cache) = get_accmgr_and_ndb_and_imgcache();
AccountManagementPreview {
account_manager,
@@ -308,45 +285,4 @@ mod preview {
AccountManagementPreview::new()
}
}
pub struct AccountSelectionPreview {
account_manager: AccountManager,
ndb: Ndb,
img_cache: ImageCache,
}
impl AccountSelectionPreview {
fn new() -> Self {
let mut account_manager =
AccountManager::new(None, KeyStorage::None, RelayGenerator::Constant, || {});
let accounts = test_data::get_test_accounts();
accounts
.into_iter()
.for_each(|acc| account_manager.add_account(acc.key, || {}));
let (ndb, img_cache) = get_ndb_and_img_cache();
AccountSelectionPreview {
account_manager,
ndb,
img_cache,
}
}
}
impl View for AccountSelectionPreview {
fn ui(&mut self, ui: &mut egui::Ui) {
AccountSelectionWidget::new(
&mut self.account_manager,
SimpleProfilePreviewController::new(&self.ndb, &mut self.img_cache),
)
.ui(ui);
}
}
impl<'a> Preview for AccountSelectionWidget<'a> {
type Prev = AccountSelectionPreview;
fn preview() -> Self::Prev {
AccountSelectionPreview::new()
}
}
}

View File

@@ -66,6 +66,7 @@ impl<'a> AccountSelectionWidget<'a> {
account_switcher_card_ui(),
);
}
fn sign_out_button(&self, ui: &mut egui::Ui, account: &UserAccount) -> Option<egui::Response> {
self.simple_preview_controller.show_with_nickname(
ui,
@@ -193,3 +194,51 @@ fn add_account_button() -> egui::Button<'static> {
let img = Image::new(img_data).fit_to_exact_size(Vec2::new(16.0, 16.0));
Button::image_and_text(img, RichText::new(" Add account").size(16.0).color(PINK)).frame(false)
}
mod previews {
use nostrdb::Ndb;
use crate::{
account_manager::AccountManager,
imgcache::ImageCache,
test_data,
ui::{profile::SimpleProfilePreviewController, Preview, View},
};
use super::AccountSelectionWidget;
pub struct AccountSelectionPreview {
account_manager: AccountManager,
ndb: Ndb,
img_cache: ImageCache,
}
impl AccountSelectionPreview {
fn new() -> Self {
let (account_manager, ndb, img_cache) = test_data::get_accmgr_and_ndb_and_imgcache();
AccountSelectionPreview {
account_manager,
ndb,
img_cache,
}
}
}
impl View for AccountSelectionPreview {
fn ui(&mut self, ui: &mut egui::Ui) {
AccountSelectionWidget::new(
&mut self.account_manager,
SimpleProfilePreviewController::new(&self.ndb, &mut self.img_cache),
)
.ui(ui);
}
}
impl<'a> Preview for AccountSelectionWidget<'a> {
type Prev = AccountSelectionPreview;
fn preview() -> Self::Prev {
AccountSelectionPreview::new()
}
}
}

View File

@@ -131,4 +131,12 @@ impl<'a> SimpleProfilePreviewController<'a> {
}
None
}
pub fn show_with_pfp(
&'a self,
ui: &mut egui::Ui,
key: &Pubkey,
ui_element: fn(ui: &mut egui::Ui, preview: SimpleProfilePreview) -> egui::Response,
) {
}
}