notedeck

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

profile_preview_controller.rs (3340B)


      1 use egui::Ui;
      2 use nostrdb::{Ndb, ProfileRecord, Transaction};
      3 
      4 use crate::{
      5     imgcache::ImageCache, ui::account_management::show_profile_card, Damus, DisplayName, Result,
      6 };
      7 
      8 use super::{
      9     preview::{get_display_name, get_profile_url, SimpleProfilePreview},
     10     ProfilePic,
     11 };
     12 
     13 #[derive(Debug)]
     14 pub enum ProfilePreviewOp {
     15     RemoveAccount,
     16     SwitchTo,
     17 }
     18 
     19 pub fn profile_preview_view(
     20     ui: &mut Ui,
     21     profile: Option<&'_ ProfileRecord<'_>>,
     22     img_cache: &mut ImageCache,
     23     is_selected: bool,
     24 ) -> Option<ProfilePreviewOp> {
     25     let width = ui.available_width();
     26 
     27     let preview = SimpleProfilePreview::new(profile, img_cache);
     28     show_profile_card(ui, preview, width, is_selected)
     29 }
     30 
     31 pub fn view_profile_previews(
     32     app: &mut Damus,
     33     ui: &mut egui::Ui,
     34     add_preview_ui: fn(
     35         ui: &mut egui::Ui,
     36         preview: SimpleProfilePreview,
     37         width: f32,
     38         is_selected: bool,
     39         index: usize,
     40     ) -> bool,
     41 ) -> Option<usize> {
     42     let width = ui.available_width();
     43 
     44     let txn = if let Ok(txn) = Transaction::new(app.ndb()) {
     45         txn
     46     } else {
     47         return None;
     48     };
     49 
     50     for i in 0..app.accounts().num_accounts() {
     51         let account = if let Some(account) = app.accounts().get_account(i) {
     52             account
     53         } else {
     54             continue;
     55         };
     56 
     57         let profile = app
     58             .ndb()
     59             .get_profile_by_pubkey(&txn, account.pubkey.bytes())
     60             .ok();
     61 
     62         let is_selected = if let Some(selected) = app.accounts().get_selected_account_index() {
     63             i == selected
     64         } else {
     65             false
     66         };
     67 
     68         let preview = SimpleProfilePreview::new(profile.as_ref(), app.img_cache_mut());
     69 
     70         if add_preview_ui(ui, preview, width, is_selected, i) {
     71             return Some(i);
     72         }
     73     }
     74 
     75     None
     76 }
     77 
     78 pub fn show_with_nickname(
     79     ndb: &Ndb,
     80     ui: &mut egui::Ui,
     81     key: &[u8; 32],
     82     ui_element: fn(ui: &mut egui::Ui, username: &DisplayName) -> egui::Response,
     83 ) -> Result<egui::Response> {
     84     let txn = Transaction::new(ndb)?;
     85     let profile = ndb.get_profile_by_pubkey(&txn, key)?;
     86     Ok(ui_element(ui, &get_display_name(Some(&profile))))
     87 }
     88 
     89 pub fn show_with_selected_pfp(
     90     app: &mut Damus,
     91     ui: &mut egui::Ui,
     92     ui_element: fn(ui: &mut egui::Ui, pfp: ProfilePic) -> egui::Response,
     93 ) -> Option<egui::Response> {
     94     let selected_account = app.accounts().get_selected_account();
     95     if let Some(selected_account) = selected_account {
     96         if let Ok(txn) = Transaction::new(app.ndb()) {
     97             let profile = app
     98                 .ndb()
     99                 .get_profile_by_pubkey(&txn, selected_account.pubkey.bytes());
    100 
    101             return Some(ui_element(
    102                 ui,
    103                 ProfilePic::new(app.img_cache_mut(), get_profile_url(profile.ok().as_ref())),
    104             ));
    105         }
    106     }
    107 
    108     None
    109 }
    110 
    111 pub fn show_with_pfp(
    112     app: &mut Damus,
    113     ui: &mut egui::Ui,
    114     key: &[u8; 32],
    115     ui_element: fn(ui: &mut egui::Ui, pfp: ProfilePic) -> egui::Response,
    116 ) -> Option<egui::Response> {
    117     if let Ok(txn) = Transaction::new(app.ndb()) {
    118         let profile = app.ndb().get_profile_by_pubkey(&txn, key);
    119 
    120         return Some(ui_element(
    121             ui,
    122             ProfilePic::new(app.img_cache_mut(), get_profile_url(profile.ok().as_ref())),
    123         ));
    124     }
    125     None
    126 }