Skip to content

Commit

Permalink
Switch to Columns
Browse files Browse the repository at this point in the history
Also refactor damus app usage to only pass in things that we need in views.

Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Sep 11, 2024
1 parent 4379466 commit 0a7a580
Show file tree
Hide file tree
Showing 29 changed files with 1,251 additions and 758 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ eframe = { git = "https://github.com/emilk/egui", rev = "fcb7764e48ce00f8f8e58da
egui_extras = { git = "https://github.com/emilk/egui", rev = "fcb7764e48ce00f8f8e58da10f937410d65b0bfb", package = "egui_extras", features = ["all_loaders"] }
ehttp = "0.2.0"
egui_tabs = { git = "https://github.com/damus-io/egui-tabs", branch = "egui-0.28" }
egui_nav = { git = "https://github.com/damus-io/egui-nav", branch = "egui-0.28" }
egui_nav = { git = "https://github.com/damus-io/egui-nav", rev = "b19742503329a13df660ac8c5a3ada4a25b7cc53" }
egui_virtual_list = { git = "https://github.com/jb55/hello_egui", branch = "egui-0.28", package = "egui_virtual_list" }
reqwest = { version = "0.12.4", default-features = false, features = [ "rustls-tls-native-roots" ] }
image = { version = "0.25", features = ["jpeg", "png", "webp"] }
Expand Down
35 changes: 29 additions & 6 deletions enostr/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,52 @@ impl Keypair {
}
}

pub fn to_full(self) -> Option<FullKeypair> {
if let Some(secret_key) = self.secret_key {
Some(FullKeypair {
pubkey: self.pubkey,
secret_key,
pub fn to_full<'a>(&'a self) -> Option<FilledKeypair<'a>> {
if let Some(secret_key) = &self.secret_key {
Some(FilledKeypair {
pubkey: &self.pubkey,
secret_key: secret_key,
})
} else {
None
}
}
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct FullKeypair {
pub pubkey: Pubkey,
pub secret_key: SecretKey,
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub struct FilledKeypair<'a> {
pub pubkey: &'a Pubkey,
pub secret_key: &'a SecretKey,
}

impl<'a> FilledKeypair<'a> {
pub fn new(pubkey: &'a Pubkey, secret_key: &'a SecretKey) -> Self {
FilledKeypair { pubkey, secret_key }
}

pub fn to_full(&self) -> FullKeypair {
FullKeypair {
pubkey: self.pubkey.to_owned(),
secret_key: self.secret_key.to_owned(),
}
}
}

impl FullKeypair {
pub fn new(pubkey: Pubkey, secret_key: SecretKey) -> Self {
FullKeypair { pubkey, secret_key }
}

pub fn to_filled<'a>(&'a self) -> FilledKeypair<'a> {
FilledKeypair::new(&self.pubkey, &self.secret_key)
}

pub fn generate() -> Self {
let mut rng = nostr::secp256k1::rand::rngs::OsRng;
let (secret_key, _) = &nostr::SECP256K1.generate_keypair(&mut rng);
Expand Down
2 changes: 1 addition & 1 deletion enostr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use client::ClientMessage;
pub use error::Error;
pub use ewebsock;
pub use filter::Filter;
pub use keypair::{FullKeypair, Keypair, SerializableKeypair};
pub use keypair::{FilledKeypair, FullKeypair, Keypair, SerializableKeypair};
pub use nostr::SecretKey;
pub use note::{Note, NoteId};
pub use profile::Profile;
Expand Down
8 changes: 7 additions & 1 deletion src/account_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cmp::Ordering;

use enostr::Keypair;
use enostr::{FilledKeypair, Keypair};

use crate::key_storage::{KeyStorage, KeyStorageResponse, KeyStorageType};
pub use crate::user_account::UserAccount;
Expand Down Expand Up @@ -88,6 +88,12 @@ impl AccountManager {
self.currently_selected_account
}

pub fn selected_or_first_nsec(&self) -> Option<FilledKeypair<'_>> {
self.get_selected_account()
.and_then(|kp| kp.to_full())
.or_else(|| self.accounts.iter().find_map(|a| a.to_full()))
}

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) {
Expand Down
56 changes: 32 additions & 24 deletions src/actionbar.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::{
column::Column,
note::NoteRef,
notecache::NoteCache,
route::Route,
thread::{Thread, ThreadResult},
Damus,
thread::{Thread, ThreadResult, Threads},
};
use enostr::NoteId;
use nostrdb::Transaction;
use enostr::{NoteId, RelayPool};
use nostrdb::{Ndb, Transaction};
use tracing::{error, info};
use uuid::Uuid;

Expand All @@ -30,26 +31,28 @@ pub enum BarResult {
/// the thread view. We don't have a concept of model/view/controller etc
/// in egui, but this is the closest thing to that.
fn open_thread(
app: &mut Damus,
ndb: &Ndb,
txn: &Transaction,
timeline: usize,
column: &mut Column,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
threads: &mut Threads,
selected_note: &[u8; 32],
) -> Option<BarResult> {
{
let timeline = &mut app.timelines[timeline];
timeline
.routes
column
.routes_mut()
.push(Route::Thread(NoteId::new(selected_note.to_owned())));
timeline.navigating = true;
column.navigating = true;
}

let root_id = crate::note::root_note_id_from_selected_id(app, txn, selected_note);
let thread_res = app.threads.thread_mut(&app.ndb, txn, root_id);
let root_id = crate::note::root_note_id_from_selected_id(ndb, note_cache, txn, selected_note);
let thread_res = threads.thread_mut(ndb, txn, root_id);

let (thread, result) = match thread_res {
ThreadResult::Stale(thread) => {
// The thread is stale, let's update it
let notes = Thread::new_notes(&thread.view.notes, root_id, txn, &app.ndb);
let notes = Thread::new_notes(&thread.view.notes, root_id, txn, ndb);
let bar_result = if notes.is_empty() {
None
} else {
Expand All @@ -76,22 +79,22 @@ fn open_thread(
// an active subscription for this thread.
if thread.subscription().is_none() {
let filters = Thread::filters(root_id);
*thread.subscription_mut() = app.ndb.subscribe(&filters).ok();
*thread.subscription_mut() = ndb.subscribe(&filters).ok();

if thread.remote_subscription().is_some() {
error!("Found active remote subscription when it was not expected");
} else {
let subid = Uuid::new_v4().to_string();
*thread.remote_subscription_mut() = Some(subid.clone());
app.pool.subscribe(subid, filters);
pool.subscribe(subid, filters);
}

match thread.subscription() {
Some(_sub) => {
thread.subscribers += 1;
info!(
"Locally/remotely subscribing to thread. {} total active subscriptions, {} on this thread",
app.ndb.subscription_count(),
ndb.subscription_count(),
thread.subscribers,
);
}
Expand All @@ -104,7 +107,7 @@ fn open_thread(
thread.subscribers += 1;
info!(
"Re-using existing thread subscription. {} total active subscriptions, {} on this thread",
app.ndb.subscription_count(),
ndb.subscription_count(),
thread.subscribers,
)
}
Expand All @@ -113,24 +116,29 @@ fn open_thread(
}

impl BarAction {
#[allow(clippy::too_many_arguments)]
pub fn execute(
self,
app: &mut Damus,
timeline: usize,
ndb: &Ndb,
column: &mut Column,
threads: &mut Threads,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
replying_to: &[u8; 32],
txn: &Transaction,
) -> Option<BarResult> {
match self {
BarAction::Reply => {
let timeline = &mut app.timelines[timeline];
timeline
.routes
column
.routes_mut()
.push(Route::Reply(NoteId::new(replying_to.to_owned())));
timeline.navigating = true;
column.navigating = true;
None
}

BarAction::OpenThread => open_thread(app, txn, timeline, replying_to),
BarAction::OpenThread => {
open_thread(ndb, txn, column, note_cache, pool, threads, replying_to)
}
}
}
}
Expand Down
Loading

0 comments on commit 0a7a580

Please sign in to comment.