notedeck

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

mod.rs (3678B)


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