Skip to content

Commit

Permalink
refactor account switcher & management previews
Browse files Browse the repository at this point in the history
Signed-off-by: kernelkind <[email protected]>
  • Loading branch information
kernelkind authored and jb55 committed May 27, 2024
1 parent 22264e7 commit 7ebd694
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 69 deletions.
30 changes: 28 additions & 2 deletions src/test_data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use std::path::Path;

use enostr::{FullKeypair, Pubkey, RelayPool};
use nostrdb::ProfileRecord;
use nostrdb::{Config, Ndb, ProfileRecord};

use crate::account_manager::UserAccount;
use crate::{
account_manager::{AccountManager, UserAccount},
imgcache::ImageCache,
key_storage::KeyStorage,
relay_generation::RelayGenerator,
};

#[allow(unused_must_use)]
pub fn sample_pool() -> RelayPool {
Expand Down Expand Up @@ -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)
}
70 changes: 3 additions & 67 deletions src/ui/account_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,43 +244,20 @@ 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,
ndb: Ndb,
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,
Expand Down Expand Up @@ -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()
}
}
}
49 changes: 49 additions & 0 deletions src/ui/account_switcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}
}
}
8 changes: 8 additions & 0 deletions src/ui/profile/profile_preview_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}
}

0 comments on commit 7ebd694

Please sign in to comment.