move switching related actions from notedeck -> columns

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-06-26 14:24:31 -04:00
parent b9cfe87974
commit d092f5c23e
6 changed files with 86 additions and 72 deletions

View File

@@ -143,7 +143,7 @@ impl Accounts {
}
#[must_use = "UnknownIdAction's must be handled. Use .process_unknown_id_action()"]
pub fn add_account(&mut self, key: Keypair) -> AddAccountAction {
pub fn add_account(&mut self, key: Keypair) -> Option<AddAccountResponse> {
let pubkey = key.pubkey;
let switch_to_index = if let Some(contains_acc) = self.contains_account(pubkey.bytes()) {
if key.secret_key.is_some() && !contains_acc.has_nsec {
@@ -174,14 +174,10 @@ impl Accounts {
self.accounts.len() - 1
};
let source: Option<usize> = None;
AddAccountAction {
accounts_action: Some(AccountsAction::Switch(SwitchAccountAction::new(
source,
switch_to_index,
))),
Some(AddAccountResponse {
switch_to: switch_to_index,
unk_id_action: SingleUnkIdAction::pubkey(pubkey),
}
})
}
/// Update the `UserAccount` via callback and save the result to disk.
@@ -356,8 +352,9 @@ impl Accounts {
}
fn handle_no_accounts(&mut self, unknown_ids: &mut UnknownIds, ndb: &Ndb, txn: &Transaction) {
self.add_account(Keypair::new(self.fallback, None))
.process_action(unknown_ids, ndb, txn);
if let Some(resp) = self.add_account(Keypair::new(self.fallback, None)) {
resp.unk_id_action.process_action(unknown_ids, ndb, txn);
}
self.select_account(self.num_accounts() - 1);
}
@@ -609,50 +606,18 @@ fn get_selected_index(accounts: &[UserAccount], keystore: &AccountStorage) -> Op
None
}
impl AddAccountAction {
// Simple wrapper around processing the unknown action to expose too
// much internal logic. This allows us to have a must_use on our
// LoginAction type, otherwise the SingleUnkIdAction's must_use will
// be lost when returned in the login action
pub fn process_action(&mut self, ids: &mut UnknownIds, ndb: &Ndb, txn: &Transaction) {
self.unk_id_action.process_action(ids, ndb, txn);
}
}
#[derive(Debug, Clone)]
pub struct SwitchAccountAction {
/// Some index representing the source of the action
pub source: Option<usize>,
/// The account index to switch to
pub switch_to: usize,
}
impl SwitchAccountAction {
pub fn new(source: Option<usize>, switch_to: usize) -> Self {
SwitchAccountAction { source, switch_to }
}
}
#[derive(Debug)]
pub enum AccountsAction {
Switch(SwitchAccountAction),
Remove(usize),
}
#[derive(Default)]
pub struct ContainsAccount {
pub has_nsec: bool,
pub index: usize,
}
#[must_use = "You must call process_login_action on this to handle unknown ids"]
pub struct AddAccountAction {
pub accounts_action: Option<AccountsAction>,
pub unk_id_action: SingleUnkIdAction,
}
pub struct AccountData {
pub(crate) relay: AccountRelayData,
pub(crate) muted: AccountMutedData,
}
pub struct AddAccountResponse {
pub switch_to: usize,
pub unk_id_action: SingleUnkIdAction,
}

View File

@@ -188,9 +188,10 @@ impl Notedeck {
let txn = Transaction::new(&ndb).expect("txn");
for key in &parsed_args.keys {
info!("adding account: {}", &key.pubkey);
accounts
.add_account(key.clone())
.process_action(&mut unknown_ids, &ndb, &txn);
if let Some(resp) = accounts.add_account(key.clone()) {
resp.unk_id_action
.process_action(&mut unknown_ids, &ndb, &txn);
}
}
}

View File

@@ -33,9 +33,7 @@ mod user_account;
mod wallet;
mod zaps;
pub use account::accounts::{
AccountData, Accounts, AccountsAction, AddAccountAction, SwitchAccountAction,
};
pub use account::accounts::{AccountData, Accounts};
pub use account::FALLBACK_PUBKEY;
pub use app::{App, AppAction, Notedeck};
pub use args::Args;

View File

@@ -1,9 +1,7 @@
use enostr::FullKeypair;
use nostrdb::Ndb;
use nostrdb::{Ndb, Transaction};
use notedeck::{
Accounts, AccountsAction, AddAccountAction, Images, SingleUnkIdAction, SwitchAccountAction,
};
use notedeck::{Accounts, Images, SingleUnkIdAction, UnknownIds};
use crate::app::get_active_columns_mut;
use crate::decks::DecksCache;
@@ -22,6 +20,45 @@ mod route;
pub use route::{AccountsRoute, AccountsRouteResponse};
impl AddAccountAction {
// Simple wrapper around processing the unknown action to expose too
// much internal logic. This allows us to have a must_use on our
// LoginAction type, otherwise the SingleUnkIdAction's must_use will
// be lost when returned in the login action
pub fn process_action(&mut self, ids: &mut UnknownIds, ndb: &Ndb, txn: &Transaction) {
self.unk_id_action.process_action(ids, ndb, txn);
}
}
#[derive(Debug, Clone)]
pub struct SwitchAccountAction {
pub source_column: usize,
/// The account to switch to
pub switch_to: usize,
}
impl SwitchAccountAction {
pub fn new(source_column: usize, switch_to: usize) -> Self {
SwitchAccountAction {
source_column,
switch_to,
}
}
}
#[derive(Debug)]
pub enum AccountsAction {
Switch(SwitchAccountAction),
Remove(usize),
}
#[must_use = "You must call process_login_action on this to handle unknown ids"]
pub struct AddAccountAction {
pub accounts_action: Option<AccountsAction>,
pub unk_id_action: SingleUnkIdAction,
}
/// Render account management views from a route
#[allow(clippy::too_many_arguments)]
pub fn render_accounts_route(
@@ -57,7 +94,7 @@ pub fn render_accounts_route(
}
}
AccountsRouteResponse::AddAccount(response) => {
let action = process_login_view_response(accounts, decks, response);
let action = process_login_view_response(accounts, decks, col, response);
*login_state = Default::default();
let router = get_active_columns_mut(accounts, decks)
.column_mut(col)
@@ -91,7 +128,7 @@ pub fn process_accounts_view_response(
selection = Some(acc_sel);
}
AccountsViewResponse::SelectAccount(index) => {
let acc_sel = AccountsAction::Switch(SwitchAccountAction::new(Some(col), index));
let acc_sel = AccountsAction::Switch(SwitchAccountAction::new(col, index));
info!("account selection: {:?}", acc_sel);
selection = Some(acc_sel);
}
@@ -106,6 +143,7 @@ pub fn process_accounts_view_response(
pub fn process_login_view_response(
manager: &mut Accounts,
decks: &mut DecksCache,
col: usize,
response: AccountLoginResponse,
) -> AddAccountAction {
let (r, pubkey) = match response {
@@ -122,5 +160,18 @@ pub fn process_login_view_response(
decks.add_deck_default(pubkey);
r
if let Some(resp) = r {
AddAccountAction {
accounts_action: Some(AccountsAction::Switch(SwitchAccountAction {
source_column: col,
switch_to: resp.switch_to,
})),
unk_id_action: resp.unk_id_action,
}
} else {
AddAccountAction {
accounts_action: None,
unk_id_action: SingleUnkIdAction::NoAction,
}
}
}

View File

@@ -771,9 +771,11 @@ pub fn set_demo(
unk_ids: &mut UnknownIds,
) {
let txn = Transaction::new(ndb).expect("txn");
accounts
.add_account(Keypair::only_pubkey(*decks_cache.get_fallback_pubkey()))
.process_action(unk_ids, ndb, &txn);
if let Some(resp) =
accounts.add_account(Keypair::only_pubkey(*decks_cache.get_fallback_pubkey()))
{
resp.unk_id_action.process_action(unk_ids, ndb, &txn);
}
accounts.select_account(accounts.num_accounts() - 1);
}

View File

@@ -1,5 +1,5 @@
use crate::{
accounts::render_accounts_route,
accounts::{render_accounts_route, AccountsAction},
app::{get_active_columns_mut, get_decks_mut},
column::ColumnsAction,
deck_state::DeckState,
@@ -31,8 +31,7 @@ use crate::{
use egui_nav::{Nav, NavAction, NavResponse, NavUiType, Percent, PopupResponse, PopupSheet};
use nostrdb::Transaction;
use notedeck::{
get_current_default_msats, get_current_wallet, AccountsAction, AppContext, NoteAction,
NoteContext,
get_current_default_msats, get_current_wallet, AppContext, NoteAction, NoteContext,
};
use notedeck_ui::View;
use tracing::error;
@@ -81,12 +80,10 @@ impl SwitchingAction {
AccountsAction::Switch(switch_action) => {
ctx.accounts.select_account(switch_action.switch_to);
// pop nav after switch
if let Some(src) = switch_action.source {
get_active_columns_mut(ctx.accounts, decks_cache)
.column_mut(src)
.router_mut()
.go_back();
}
get_active_columns_mut(ctx.accounts, decks_cache)
.column_mut(switch_action.source_column)
.router_mut()
.go_back();
}
AccountsAction::Remove(index) => ctx.accounts.remove_account(*index),
},