refactor account switcher & management previews
Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
committed by
William Casarin
parent
22264e70f5
commit
7ebd694f11
@@ -1,7 +1,14 @@
|
|||||||
use enostr::{FullKeypair, Pubkey, RelayPool};
|
use std::path::Path;
|
||||||
use nostrdb::ProfileRecord;
|
|
||||||
|
|
||||||
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)]
|
#[allow(unused_must_use)]
|
||||||
pub fn sample_pool() -> RelayPool {
|
pub fn sample_pool() -> RelayPool {
|
||||||
@@ -86,3 +93,22 @@ pub fn get_test_accounts() -> Vec<UserAccount> {
|
|||||||
})
|
})
|
||||||
.collect()
|
.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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -244,15 +244,10 @@ fn selected_widget() -> impl egui::Widget {
|
|||||||
// PREVIEWS
|
// PREVIEWS
|
||||||
|
|
||||||
mod preview {
|
mod preview {
|
||||||
use nostrdb::{Config, Ndb};
|
use nostrdb::Ndb;
|
||||||
use ui::account_switcher::AccountSelectionWidget;
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::imgcache::ImageCache;
|
use crate::{imgcache::ImageCache, test_data::get_accmgr_and_ndb_and_imgcache};
|
||||||
use crate::key_storage::KeyStorage;
|
|
||||||
use crate::relay_generation::RelayGenerator;
|
|
||||||
use crate::test_data;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
pub struct AccountManagementPreview {
|
pub struct AccountManagementPreview {
|
||||||
account_manager: AccountManager,
|
account_manager: AccountManager,
|
||||||
@@ -260,27 +255,9 @@ mod preview {
|
|||||||
img_cache: ImageCache,
|
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 {
|
impl AccountManagementPreview {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
let mut account_manager =
|
let (account_manager, ndb, img_cache) = get_accmgr_and_ndb_and_imgcache();
|
||||||
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();
|
|
||||||
|
|
||||||
AccountManagementPreview {
|
AccountManagementPreview {
|
||||||
account_manager,
|
account_manager,
|
||||||
@@ -308,45 +285,4 @@ mod preview {
|
|||||||
AccountManagementPreview::new()
|
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ impl<'a> AccountSelectionWidget<'a> {
|
|||||||
account_switcher_card_ui(),
|
account_switcher_card_ui(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sign_out_button(&self, ui: &mut egui::Ui, account: &UserAccount) -> Option<egui::Response> {
|
fn sign_out_button(&self, ui: &mut egui::Ui, account: &UserAccount) -> Option<egui::Response> {
|
||||||
self.simple_preview_controller.show_with_nickname(
|
self.simple_preview_controller.show_with_nickname(
|
||||||
ui,
|
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));
|
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)
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -131,4 +131,12 @@ impl<'a> SimpleProfilePreviewController<'a> {
|
|||||||
}
|
}
|
||||||
None
|
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,
|
||||||
|
) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user