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

Persist decks #461

Merged
merged 13 commits into from
Dec 11, 2024
Binary file added assets/icons/new_deck_icon_4x_dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
106 changes: 67 additions & 39 deletions src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use uuid::Uuid;
use enostr::{ClientMessage, FilledKeypair, FullKeypair, Keypair, RelayPool};
use nostrdb::{Filter, Ndb, Note, NoteKey, Subscription, Transaction};

use crate::app::get_active_columns_mut;
use crate::decks::DecksCache;
use crate::{
column::Columns,
imgcache::ImageCache,
login_manager::AcquireKeyState,
muted::Muted,
route::{Route, Router},
route::Route,
storage::{KeyStorageResponse, KeyStorageType},
ui::{
account_login_view::{AccountLoginResponse, AccountLoginView},
Expand All @@ -27,7 +28,7 @@ use tracing::{debug, error, info};

mod route;

pub use route::{AccountsRoute, AccountsRouteResponse};
pub use route::{AccountsAction, AccountsRoute, AccountsRouteResponse};

pub struct AccountRelayData {
filter: Filter,
Expand Down Expand Up @@ -224,19 +225,34 @@ pub struct Accounts {
needs_relay_config: bool,
}

#[must_use = "You must call process_login_action on this to handle unknown ids"]
pub struct RenderAccountAction {
pub accounts_action: Option<AccountsAction>,
pub unk_id_action: SingleUnkIdAction,
}

impl RenderAccountAction {
// 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);
}
}

/// Render account management views from a route
#[allow(clippy::too_many_arguments)]
pub fn render_accounts_route(
ui: &mut egui::Ui,
ndb: &Ndb,
col: usize,
columns: &mut Columns,
img_cache: &mut ImageCache,
accounts: &mut Accounts,
decks: &mut DecksCache,
login_state: &mut AcquireKeyState,
route: AccountsRoute,
) -> SingleUnkIdAction {
let router = columns.column_mut(col).router_mut();
) -> RenderAccountAction {
let resp = match route {
AccountsRoute::Accounts => AccountsView::new(ndb, accounts, img_cache)
.ui(ui)
Expand All @@ -252,37 +268,57 @@ pub fn render_accounts_route(
if let Some(resp) = resp {
match resp {
AccountsRouteResponse::Accounts(response) => {
process_accounts_view_response(accounts, response, router);
SingleUnkIdAction::no_action()
let action = process_accounts_view_response(accounts, decks, col, response);
RenderAccountAction {
accounts_action: action,
unk_id_action: SingleUnkIdAction::no_action(),
}
}
AccountsRouteResponse::AddAccount(response) => {
let action = process_login_view_response(accounts, response);
let action = process_login_view_response(accounts, decks, response);
*login_state = Default::default();
let router = get_active_columns_mut(accounts, decks)
.column_mut(col)
.router_mut();
router.go_back();
action
}
}
} else {
SingleUnkIdAction::no_action()
RenderAccountAction {
accounts_action: None,
unk_id_action: SingleUnkIdAction::no_action(),
}
}
}

pub fn process_accounts_view_response(
manager: &mut Accounts,
accounts: &mut Accounts,
decks: &mut DecksCache,
col: usize,
response: AccountsViewResponse,
router: &mut Router<Route>,
) {
) -> Option<AccountsAction> {
let router = get_active_columns_mut(accounts, decks)
.column_mut(col)
.router_mut();
let mut selection = None;
match response {
AccountsViewResponse::RemoveAccount(index) => {
manager.remove_account(index);
let acc_sel = AccountsAction::Remove(index);
info!("account selection: {:?}", acc_sel);
selection = Some(acc_sel);
}
AccountsViewResponse::SelectAccount(index) => {
manager.select_account(index);
let acc_sel = AccountsAction::Switch(index);
info!("account selection: {:?}", acc_sel);
selection = Some(acc_sel);
}
AccountsViewResponse::RouteToLogin => {
router.route_to(Route::add_account());
}
}

selection
}

impl Accounts {
Expand Down Expand Up @@ -375,7 +411,7 @@ impl Accounts {
}

#[must_use = "UnknownIdAction's must be handled. Use .process_unknown_id_action()"]
pub fn add_account(&mut self, account: Keypair) -> LoginAction {
pub fn add_account(&mut self, account: Keypair) -> RenderAccountAction {
let pubkey = account.pubkey;
let switch_to_index = if let Some(contains_acc) = self.contains_account(pubkey.bytes()) {
if account.secret_key.is_some() && !contains_acc.has_nsec {
Expand All @@ -397,9 +433,9 @@ impl Accounts {
self.accounts.len() - 1
};

LoginAction {
unk: SingleUnkIdAction::pubkey(pubkey),
switch_to_index,
RenderAccountAction {
accounts_action: Some(AccountsAction::Switch(switch_to_index)),
unk_id_action: SingleUnkIdAction::pubkey(pubkey),
}
}

Expand Down Expand Up @@ -619,32 +655,24 @@ fn get_selected_index(accounts: &[UserAccount], keystore: &KeyStorageType) -> Op

pub fn process_login_view_response(
manager: &mut Accounts,
decks: &mut DecksCache,
response: AccountLoginResponse,
) -> SingleUnkIdAction {
let login_action = match response {
) -> RenderAccountAction {
let (r, pubkey) = match response {
AccountLoginResponse::CreateNew => {
manager.add_account(FullKeypair::generate().to_keypair())
let kp = FullKeypair::generate().to_keypair();
let pubkey = kp.pubkey;
(manager.add_account(kp), pubkey)
}
AccountLoginResponse::LoginWith(keypair) => {
let pubkey = keypair.pubkey;
(manager.add_account(keypair), pubkey)
}
AccountLoginResponse::LoginWith(keypair) => manager.add_account(keypair),
};
manager.select_account(login_action.switch_to_index);
login_action.unk
}

#[must_use = "You must call process_login_action on this to handle unknown ids"]
pub struct LoginAction {
unk: SingleUnkIdAction,
pub switch_to_index: usize,
}
decks.add_deck_default(pubkey);

impl LoginAction {
// 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.process_action(ids, ndb, txn);
}
r
}

#[derive(Default)]
Expand Down
6 changes: 6 additions & 0 deletions src/accounts/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ pub enum AccountsRoute {
Accounts,
AddAccount,
}

#[derive(Debug)]
pub enum AccountsAction {
Switch(usize),
Remove(usize),
}
Loading
Loading