deck actions

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-12-05 20:01:27 -05:00
parent 845a983592
commit 56a8ba30f3
10 changed files with 230 additions and 54 deletions

View File

@@ -1,7 +1,10 @@
use crate::{
accounts::render_accounts_route,
accounts::{render_accounts_route, AccountsAction},
actionbar::NoteAction,
app::{get_active_columns, get_active_columns_mut},
app::{get_active_columns, get_active_columns_mut, get_decks_mut},
column::ColumnsAction,
deck_state::DeckState,
decks::{Deck, DecksAction},
notes_holder::NotesHolder,
profile::Profile,
relay_pool_manager::RelayPoolManager,
@@ -15,6 +18,8 @@ use crate::{
self,
add_column::render_add_column_routes,
column::NavTitle,
configure_deck::ConfigureDeckView,
edit_deck::{EditDeckResponse, EditDeckView},
note::{PostAction, PostType},
support::SupportView,
RelayView, View,
@@ -26,11 +31,45 @@ use egui_nav::{Nav, NavAction, NavResponse, NavUiType};
use nostrdb::{Ndb, Transaction};
use tracing::{error, info};
#[allow(clippy::enum_variant_names)]
pub enum RenderNavAction {
Back,
RemoveColumn,
PostAction(PostAction),
NoteAction(NoteAction),
SwitchingAction(SwitchingAction),
}
pub enum SwitchingAction {
Accounts(AccountsAction),
Columns(ColumnsAction),
Decks(crate::decks::DecksAction),
}
impl SwitchingAction {
/// process the action, and return whether switching occured
pub fn process(&self, app: &mut Damus) -> bool {
match &self {
SwitchingAction::Accounts(account_action) => match *account_action {
AccountsAction::Switch(index) => app.accounts.select_account(index),
AccountsAction::Remove(index) => app.accounts.remove_account(index),
},
SwitchingAction::Columns(columns_action) => match *columns_action {
ColumnsAction::Remove(index) => {
get_active_columns_mut(&app.accounts, &mut app.decks_cache).delete_column(index)
}
},
SwitchingAction::Decks(decks_action) => match *decks_action {
DecksAction::Switch(index) => {
get_decks_mut(&app.accounts, &mut app.decks_cache).set_active(index)
}
DecksAction::Removing(index) => {
get_decks_mut(&app.accounts, &mut app.decks_cache).remove_deck(index)
}
},
}
true
}
}
impl From<PostAction> for RenderNavAction {
@@ -60,7 +99,7 @@ impl RenderNavResponse {
#[must_use = "Make sure to save columns if result is true"]
pub fn process_render_nav_response(&self, app: &mut Damus) -> bool {
let mut col_changed: bool = false;
let mut switching_occured: bool = false;
let col = self.column;
if let Some(action) = self
@@ -82,7 +121,7 @@ impl RenderNavResponse {
}
app.columns_mut().delete_column(col);
col_changed = true;
switching_occured = true;
}
RenderNavAction::PostAction(post_action) => {
@@ -109,6 +148,10 @@ impl RenderNavResponse {
&app.accounts.mutefun(),
);
}
RenderNavAction::SwitchingAction(switching_action) => {
switching_occured = switching_action.process(app);
}
}
}
@@ -148,7 +191,12 @@ impl RenderNavResponse {
&app.accounts.mutefun(),
);
}
col_changed = true;
if let Some(Route::EditDeck(index)) = r {
SwitchingAction::Decks(DecksAction::Removing(index)).process(app);
}
switching_occured = true;
}
NavAction::Navigated => {
@@ -157,7 +205,7 @@ impl RenderNavResponse {
if cur_router.is_replacing() {
cur_router.remove_previous_routes();
}
col_changed = true;
switching_occured = true;
}
NavAction::Dragging => {}
@@ -167,7 +215,7 @@ impl RenderNavResponse {
}
}
col_changed
switching_occured
}
}
@@ -194,7 +242,7 @@ fn render_nav_body(
ui,
),
Route::Accounts(amr) => {
let action = render_accounts_route(
let mut action = render_accounts_route(
ui,
&app.ndb,
col,
@@ -206,7 +254,9 @@ fn render_nav_body(
);
let txn = Transaction::new(&app.ndb).expect("txn");
action.process_action(&mut app.unknown_ids, &app.ndb, &txn);
None
action
.accounts_action
.map(|f| RenderNavAction::SwitchingAction(SwitchingAction::Accounts(f)))
}
Route::Relays => {
let manager = RelayPoolManager::new(app.pool_mut());
@@ -235,11 +285,69 @@ fn render_nav_body(
None
}
Route::Support => {
SupportView::new(&mut app.support).show(ui);
None
}
Route::NewDeck => {
let id = ui.id().with("new-deck");
let new_deck_state = app.view_state.id_to_deck_state.entry(id).or_default();
let mut resp = None;
if let Some(config_resp) = ConfigureDeckView::new(new_deck_state).ui(ui) {
if let Some(cur_acc) = app.accounts.get_selected_account() {
app.decks_cache.add_deck(
cur_acc.pubkey,
Deck::new(config_resp.icon, config_resp.name),
);
// set new deck as active
let cur_index = get_decks_mut(&app.accounts, &mut app.decks_cache)
.decks()
.len()
- 1;
resp = Some(RenderNavAction::SwitchingAction(SwitchingAction::Decks(
DecksAction::Switch(cur_index),
)));
}
new_deck_state.clear();
get_active_columns_mut(&app.accounts, &mut app.decks_cache)
.get_first_router()
.go_back();
}
resp
}
Route::EditDeck(index) => {
let cur_deck = get_decks_mut(&app.accounts, &mut app.decks_cache)
.decks_mut()
.get_mut(*index)
.expect("index wasn't valid");
let id = ui.id().with((
"edit-deck",
app.accounts.get_selected_account().map(|k| k.pubkey),
index,
));
let deck_state = app
.view_state
.id_to_deck_state
.entry(id)
.or_insert_with(|| DeckState::from_deck(cur_deck));
if let Some(resp) = EditDeckView::new(deck_state).ui(ui) {
match resp {
EditDeckResponse::Edit(configure_deck_response) => {
cur_deck.edit(configure_deck_response);
}
EditDeckResponse::Delete => {
deck_state.deleting = true;
}
}
get_active_columns_mut(&app.accounts, &mut app.decks_cache)
.get_first_router()
.go_back();
}
None
}
}
}