Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate sidebar #80

Merged
merged 18 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/icons/add_account_icon_4x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/add_column_dark_4x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/plus_icon_4x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/select_icon_3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/settings_dark_4x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/signout_icon_4x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion enostr/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Keypair {
}

impl Keypair {
pub fn new(secret_key: SecretKey) -> Self {
pub fn from_secret(secret_key: SecretKey) -> Self {
let cloned_secret_key = secret_key.clone();
let nostr_keys = nostr::Keys::new(secret_key);
Keypair {
Expand All @@ -17,6 +17,10 @@ impl Keypair {
}
}

pub fn new(pubkey: Pubkey, secret_key: Option<SecretKey>) -> Self {
Keypair { pubkey, secret_key }
}

pub fn only_pubkey(pubkey: Pubkey) -> Self {
Keypair {
pubkey,
Expand Down
166 changes: 61 additions & 105 deletions src/account_manager.rs
Original file line number Diff line number Diff line change
@@ -1,136 +1,92 @@
use enostr::FullKeypair;
use nostrdb::{Ndb, Transaction};
use std::cmp::Ordering;

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<'a>,
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
}
use enostr::Keypair;

pub fn view_profile_previews(
&mut self,
account_manager: &'a AccountManager<'a>,
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;
pub use crate::user_account::UserAccount;

/// The interface for managing the user's accounts.
/// Represents all user-facing operations related to account management.
pub struct AccountManager<'a> {
accounts: &'a mut Vec<UserAccount>,
pub struct AccountManager {
currently_selected_account: Option<usize>,
accounts: Vec<UserAccount>,
key_store: KeyStorage,
relay_generator: RelayGenerator,
}

impl<'a> AccountManager<'a> {
pub fn new(
accounts: &'a mut Vec<UserAccount>,
key_store: KeyStorage,
relay_generator: RelayGenerator,
) -> Self {
impl AccountManager {
pub fn new(currently_selected_account: Option<usize>, key_store: KeyStorage) -> Self {
let accounts = key_store.get_keys().unwrap_or_default();

AccountManager {
currently_selected_account,
accounts,
key_store,
relay_generator,
}
}

pub fn get_accounts(&'a self) -> &'a Vec<UserAccount> {
self.accounts
pub fn get_accounts(&self) -> &Vec<UserAccount> {
&self.accounts
}

pub fn get_account(&'a self, index: usize) -> Option<&'a UserAccount> {
self.accounts.get(index)
pub fn get_account(&self, ind: usize) -> Option<&UserAccount> {
self.accounts.get(ind)
}

pub fn find_account(&self, pk: &[u8; 32]) -> Option<&UserAccount> {
self.accounts.iter().find(|acc| acc.pubkey.bytes() == pk)
}

pub fn remove_account(&mut self, index: usize) {
if let Some(account) = self.accounts.get(index) {
let _ = self.key_store.remove_key(&account.key);
}
if index < self.accounts.len() {
let _ = self.key_store.remove_key(account);
self.accounts.remove(index);

if let Some(selected_index) = self.currently_selected_account {
match selected_index.cmp(&index) {
Ordering::Greater => {
self.select_account(selected_index - 1);
}
Ordering::Equal => {
self.clear_selected_account();
}
Ordering::Less => {}
}
}
}
}

pub fn add_account(&'a mut self, key: FullKeypair, ctx: &egui::Context) {
let _ = self.key_store.add_key(&key);
let relays = self.relay_generator.generate_relays_for(&key.pubkey, ctx);
let account = UserAccount { key, relays };

pub fn add_account(&mut self, account: Keypair) {
let _ = self.key_store.add_key(&account);
self.accounts.push(account)
}

pub fn num_accounts(&self) -> usize {
self.accounts.len()
}

pub fn get_selected_account_index(&self) -> Option<usize> {
self.currently_selected_account
}

pub fn get_selected_account(&self) -> Option<&UserAccount> {
if let Some(account_index) = self.currently_selected_account {
if let Some(account) = self.get_account(account_index) {
Some(account)
} else {
None
}
} else {
None
}
}

pub fn select_account(&mut self, index: usize) {
if self.accounts.get(index).is_some() {
self.currently_selected_account = Some(index)
}
}

pub fn clear_selected_account(&mut self) {
self.currently_selected_account = None
}
}
64 changes: 55 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::account_manager::AccountManager;
use crate::app_creation::setup_cc;
use crate::app_style::user_requested_visuals_change;
use crate::error::Error;
use crate::frame_history::FrameHistory;
use crate::imgcache::ImageCache;
use crate::notecache::{CachedNote, NoteCache};
use crate::route::Route;
use crate::timeline;
use crate::timeline::{NoteRef, Timeline, ViewFilter};
use crate::ui::is_mobile;
use crate::ui::profile::SimpleProfilePreviewController;
use crate::ui::{is_mobile, DesktopSidePanel};
use crate::Result;

use egui::{Context, Frame, Style};
Expand Down Expand Up @@ -36,13 +39,16 @@
note_cache: NoteCache,
pool: RelayPool,

/// global navigation for account management popups, etc.
nav: Vec<Route>,

Check warning on line 43 in src/app.rs

View workflow job for this annotation

GitHub Actions / Check

field `nav` is never read

Check failure on line 43 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

field `nav` is never read

Check warning on line 43 in src/app.rs

View workflow job for this annotation

GitHub Actions / Check

field `nav` is never read

Check failure on line 43 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

field `nav` is never read

Check warning on line 43 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

field `nav` is never read

Check warning on line 43 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

field `nav` is never read
pub textmode: bool,

pub timelines: Vec<Timeline>,
pub selected_timeline: i32,

pub img_cache: ImageCache,
pub ndb: Ndb,
pub account_manager: AccountManager,

frame_history: crate::frame_history::FrameHistory,
}
Expand Down Expand Up @@ -641,14 +647,47 @@
img_cache: ImageCache::new(imgcache_dir),
note_cache: NoteCache::default(),
selected_timeline: 0,
nav: Vec::with_capacity(6),
timelines,
textmode: false,
ndb: Ndb::new(data_path.as_ref().to_str().expect("db path ok"), &config).expect("ndb"),
account_manager: AccountManager::new(
// TODO: should pull this from settings
None,
// TODO: use correct KeyStorage mechanism for current OS arch
crate::key_storage::KeyStorage::None,
),
//compose: "".to_string(),
frame_history: FrameHistory::default(),
}
}

pub fn mock<P: AsRef<Path>>(data_path: P) -> Self {
let mut timelines: Vec<Timeline> = vec![];
let _initial_limit = 100;
let filter = serde_json::from_str(include_str!("../queries/global.json")).unwrap();
timelines.push(Timeline::new(filter));

let imgcache_dir = data_path.as_ref().join(ImageCache::rel_datadir());
let _ = std::fs::create_dir_all(imgcache_dir.clone());

let mut config = Config::new();
config.set_ingester_threads(2);
Self {
state: DamusState::Initializing,
pool: RelayPool::new(),
img_cache: ImageCache::new(imgcache_dir),
note_cache: NoteCache::default(),
selected_timeline: 0,
timelines,
nav: vec![],
textmode: false,
ndb: Ndb::new(data_path.as_ref().to_str().expect("db path ok"), &config).expect("ndb"),
account_manager: AccountManager::new(None, crate::key_storage::KeyStorage::None),
frame_history: FrameHistory::default(),
}
}

pub fn note_cache_mut(&mut self) -> &mut NoteCache {
&mut self.note_cache
}
Expand Down Expand Up @@ -809,14 +848,6 @@
Size::remainder()
};

if app.timelines.len() == 1 {
main_panel(&ctx.style()).show(ctx, |ui| {
timeline::timeline_view(ui, app, 0);
});

return;
}

main_panel(&ctx.style()).show(ctx, |ui| {
ui.spacing_mut().item_spacing.x = 0.0;
if need_scroll {
Expand All @@ -831,9 +862,24 @@

fn timelines_view(ui: &mut egui::Ui, sizes: Size, app: &mut Damus, timelines: usize) {
StripBuilder::new(ui)
.size(Size::exact(40.0))
.sizes(sizes, timelines)
.clip(true)
.horizontal(|mut strip| {
strip.cell(|ui| {
let side_panel = DesktopSidePanel::new(
app.account_manager
.get_selected_account()
.map(|a| a.pubkey.bytes()),
SimpleProfilePreviewController::new(&app.ndb, &mut app.img_cache),
)
.show(ui);

if side_panel.response.clicked() {
info!("clicked {:?}", side_panel.action);
}
});

for timeline_ind in 0..timelines {
strip.cell(|ui| timeline::timeline_view(ui, app, timeline_ind));
}
Expand Down
2 changes: 2 additions & 0 deletions src/colors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use egui::Color32;

pub const PURPLE: Color32 = Color32::from_rgb(0xCC, 0x43, 0xC5);
// TODO: This should not be exposed publicly
pub const PINK: Color32 = Color32::from_rgb(0xE4, 0x5A, 0xC9);
//pub const DARK_BG: Color32 = egui::Color32::from_rgb(40, 44, 52);
pub const GRAY_SECONDARY: Color32 = Color32::from_rgb(0x8A, 0x8A, 0x8A);
const BLACK: Color32 = Color32::from_rgb(0x00, 0x00, 0x00);
Expand Down
6 changes: 3 additions & 3 deletions src/key_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub async fn get_login_key(key: &str) -> Result<Keypair, LoginError> {
} else if let Ok(pubkey) = Pubkey::try_from_hex_str_with_verify(tmp_key) {
Ok(Keypair::only_pubkey(pubkey))
} else if let Ok(secret_key) = SecretKey::from_str(tmp_key) {
Ok(Keypair::new(secret_key))
Ok(Keypair::from_secret(secret_key))
} else {
Err(LoginError::InvalidKey)
}
Expand Down Expand Up @@ -181,7 +181,7 @@ mod tests {

promise_assert!(
assert_eq,
Ok(Keypair::new(expected_privkey)),
Ok(Keypair::from_secret(expected_privkey)),
&login_key_result
);
}
Expand All @@ -194,7 +194,7 @@ mod tests {

promise_assert!(
assert_eq,
Ok(Keypair::new(expected_privkey)),
Ok(Keypair::from_secret(expected_privkey)),
&login_key_result
);
}
Expand Down
Loading
Loading