columns: navigate back when switching account

Fixes: https://github.com/damus-io/notedeck/issues/600
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-12-20 13:54:20 -08:00
parent 40c5dbf418
commit 475314da75
5 changed files with 45 additions and 15 deletions

View File

@@ -13,9 +13,24 @@ use uuid::Uuid;
// TODO: remove this // TODO: remove this
use std::sync::Arc; use std::sync::Arc;
#[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)] #[derive(Debug)]
pub enum AccountsAction { pub enum AccountsAction {
Switch(usize), Switch(SwitchAccountAction),
Remove(usize), Remove(usize),
} }
@@ -338,8 +353,12 @@ impl Accounts {
self.accounts.len() - 1 self.accounts.len() - 1
}; };
let source: Option<usize> = None;
AddAccountAction { AddAccountAction {
accounts_action: Some(AccountsAction::Switch(switch_to_index)), accounts_action: Some(AccountsAction::Switch(SwitchAccountAction::new(
source,
switch_to_index,
))),
unk_id_action: SingleUnkIdAction::pubkey(pubkey), unk_id_action: SingleUnkIdAction::pubkey(pubkey),
} }
} }

View File

@@ -20,7 +20,7 @@ pub mod ui;
mod unknowns; mod unknowns;
mod user_account; mod user_account;
pub use accounts::{AccountData, Accounts, AccountsAction, AddAccountAction}; pub use accounts::{AccountData, Accounts, AccountsAction, AddAccountAction, SwitchAccountAction};
pub use app::App; pub use app::App;
pub use args::Args; pub use args::Args;
pub use context::AppContext; pub use context::AppContext;

View File

@@ -1,7 +1,9 @@
use enostr::FullKeypair; use enostr::FullKeypair;
use nostrdb::Ndb; use nostrdb::Ndb;
use notedeck::{Accounts, AccountsAction, AddAccountAction, ImageCache, SingleUnkIdAction}; use notedeck::{
Accounts, AccountsAction, AddAccountAction, ImageCache, SingleUnkIdAction, SwitchAccountAction,
};
use crate::app::get_active_columns_mut; use crate::app::get_active_columns_mut;
use crate::decks::DecksCache; use crate::decks::DecksCache;
@@ -87,7 +89,7 @@ pub fn process_accounts_view_response(
selection = Some(acc_sel); selection = Some(acc_sel);
} }
AccountsViewResponse::SelectAccount(index) => { AccountsViewResponse::SelectAccount(index) => {
let acc_sel = AccountsAction::Switch(index); let acc_sel = AccountsAction::Switch(SwitchAccountAction::new(Some(col), index));
info!("account selection: {:?}", acc_sel); info!("account selection: {:?}", acc_sel);
selection = Some(acc_sel); selection = Some(acc_sel);
} }

View File

@@ -620,7 +620,7 @@ fn timelines_view(ui: &mut egui::Ui, sizes: Size, app: &mut Damus, ctx: &mut App
let mut save_cols = false; let mut save_cols = false;
if let Some(action) = side_panel_action { if let Some(action) = side_panel_action {
save_cols = save_cols || action.process(app, ctx); save_cols = save_cols || action.process(&mut app.decks_cache, ctx);
} }
let num_cols = app.columns(ctx.accounts).num_columns(); let num_cols = app.columns(ctx.accounts).num_columns();

View File

@@ -4,7 +4,7 @@ use crate::{
app::{get_active_columns, get_active_columns_mut, get_decks_mut}, app::{get_active_columns, get_active_columns_mut, get_decks_mut},
column::ColumnsAction, column::ColumnsAction,
deck_state::DeckState, deck_state::DeckState,
decks::{Deck, DecksAction}, decks::{Deck, DecksAction, DecksCache},
notes_holder::NotesHolder, notes_holder::NotesHolder,
profile::Profile, profile::Profile,
relay_pool_manager::RelayPoolManager, relay_pool_manager::RelayPoolManager,
@@ -50,23 +50,32 @@ pub enum SwitchingAction {
impl SwitchingAction { impl SwitchingAction {
/// process the action, and return whether switching occured /// process the action, and return whether switching occured
pub fn process(&self, app: &mut Damus, ctx: &mut AppContext<'_>) -> bool { pub fn process(&self, decks_cache: &mut DecksCache, ctx: &mut AppContext<'_>) -> bool {
match &self { match &self {
SwitchingAction::Accounts(account_action) => match *account_action { SwitchingAction::Accounts(account_action) => match account_action {
AccountsAction::Switch(index) => ctx.accounts.select_account(index), AccountsAction::Switch(switch_action) => {
AccountsAction::Remove(index) => ctx.accounts.remove_account(index), 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();
}
}
AccountsAction::Remove(index) => ctx.accounts.remove_account(*index),
}, },
SwitchingAction::Columns(columns_action) => match *columns_action { SwitchingAction::Columns(columns_action) => match *columns_action {
ColumnsAction::Remove(index) => { ColumnsAction::Remove(index) => {
get_active_columns_mut(ctx.accounts, &mut app.decks_cache).delete_column(index) get_active_columns_mut(ctx.accounts, decks_cache).delete_column(index)
} }
}, },
SwitchingAction::Decks(decks_action) => match *decks_action { SwitchingAction::Decks(decks_action) => match *decks_action {
DecksAction::Switch(index) => { DecksAction::Switch(index) => {
get_decks_mut(ctx.accounts, &mut app.decks_cache).set_active(index) get_decks_mut(ctx.accounts, decks_cache).set_active(index)
} }
DecksAction::Removing(index) => { DecksAction::Removing(index) => {
get_decks_mut(ctx.accounts, &mut app.decks_cache).remove_deck(index) get_decks_mut(ctx.accounts, decks_cache).remove_deck(index)
} }
}, },
} }
@@ -157,7 +166,7 @@ impl RenderNavResponse {
} }
RenderNavAction::SwitchingAction(switching_action) => { RenderNavAction::SwitchingAction(switching_action) => {
switching_occured = switching_action.process(app, ctx); switching_occured = switching_action.process(&mut app.decks_cache, ctx);
} }
} }
} }