notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

mod.rs (3777B)


      1 use enostr::FullKeypair;
      2 use nostrdb::Ndb;
      3 
      4 use notedeck::{
      5     Accounts, AccountsAction, AddAccountAction, ImageCache, SingleUnkIdAction, SwitchAccountAction,
      6 };
      7 
      8 use crate::app::get_active_columns_mut;
      9 use crate::decks::DecksCache;
     10 use crate::{
     11     login_manager::AcquireKeyState,
     12     route::Route,
     13     ui::{
     14         account_login_view::{AccountLoginResponse, AccountLoginView},
     15         accounts::{AccountsView, AccountsViewResponse},
     16     },
     17 };
     18 use tracing::info;
     19 
     20 mod route;
     21 
     22 pub use route::{AccountsRoute, AccountsRouteResponse};
     23 
     24 /// Render account management views from a route
     25 #[allow(clippy::too_many_arguments)]
     26 pub fn render_accounts_route(
     27     ui: &mut egui::Ui,
     28     ndb: &Ndb,
     29     col: usize,
     30     img_cache: &mut ImageCache,
     31     accounts: &mut Accounts,
     32     decks: &mut DecksCache,
     33     login_state: &mut AcquireKeyState,
     34     route: AccountsRoute,
     35 ) -> AddAccountAction {
     36     let resp = match route {
     37         AccountsRoute::Accounts => AccountsView::new(ndb, accounts, img_cache)
     38             .ui(ui)
     39             .inner
     40             .map(AccountsRouteResponse::Accounts),
     41 
     42         AccountsRoute::AddAccount => AccountLoginView::new(login_state)
     43             .ui(ui)
     44             .inner
     45             .map(AccountsRouteResponse::AddAccount),
     46     };
     47 
     48     if let Some(resp) = resp {
     49         match resp {
     50             AccountsRouteResponse::Accounts(response) => {
     51                 let action = process_accounts_view_response(accounts, decks, col, response);
     52                 AddAccountAction {
     53                     accounts_action: action,
     54                     unk_id_action: SingleUnkIdAction::no_action(),
     55                 }
     56             }
     57             AccountsRouteResponse::AddAccount(response) => {
     58                 let action = process_login_view_response(accounts, decks, response);
     59                 *login_state = Default::default();
     60                 let router = get_active_columns_mut(accounts, decks)
     61                     .column_mut(col)
     62                     .router_mut();
     63                 router.go_back();
     64                 action
     65             }
     66         }
     67     } else {
     68         AddAccountAction {
     69             accounts_action: None,
     70             unk_id_action: SingleUnkIdAction::no_action(),
     71         }
     72     }
     73 }
     74 
     75 pub fn process_accounts_view_response(
     76     accounts: &mut Accounts,
     77     decks: &mut DecksCache,
     78     col: usize,
     79     response: AccountsViewResponse,
     80 ) -> Option<AccountsAction> {
     81     let router = get_active_columns_mut(accounts, decks)
     82         .column_mut(col)
     83         .router_mut();
     84     let mut selection = None;
     85     match response {
     86         AccountsViewResponse::RemoveAccount(index) => {
     87             let acc_sel = AccountsAction::Remove(index);
     88             info!("account selection: {:?}", acc_sel);
     89             selection = Some(acc_sel);
     90         }
     91         AccountsViewResponse::SelectAccount(index) => {
     92             let acc_sel = AccountsAction::Switch(SwitchAccountAction::new(Some(col), index));
     93             info!("account selection: {:?}", acc_sel);
     94             selection = Some(acc_sel);
     95         }
     96         AccountsViewResponse::RouteToLogin => {
     97             router.route_to(Route::add_account());
     98         }
     99     }
    100     accounts.needs_relay_config();
    101     selection
    102 }
    103 
    104 pub fn process_login_view_response(
    105     manager: &mut Accounts,
    106     decks: &mut DecksCache,
    107     response: AccountLoginResponse,
    108 ) -> AddAccountAction {
    109     let (r, pubkey) = match response {
    110         AccountLoginResponse::CreateNew => {
    111             let kp = FullKeypair::generate().to_keypair();
    112             let pubkey = kp.pubkey;
    113             (manager.add_account(kp), pubkey)
    114         }
    115         AccountLoginResponse::LoginWith(keypair) => {
    116             let pubkey = keypair.pubkey;
    117             (manager.add_account(keypair), pubkey)
    118         }
    119     };
    120 
    121     decks.add_deck_default(pubkey);
    122 
    123     r
    124 }