profile_preview_controller.rs (4489B)
1 use nostrdb::{Ndb, Transaction}; 2 3 use crate::{Damus, DisplayName, Result}; 4 5 use super::{ 6 preview::{get_display_name, get_profile_url, SimpleProfilePreview}, 7 ProfilePic, 8 }; 9 10 #[derive(Debug)] 11 pub enum ProfilePreviewOp { 12 RemoveAccount, 13 SwitchTo, 14 } 15 16 pub fn set_profile_previews( 17 app: &mut Damus, 18 ui: &mut egui::Ui, 19 add_preview_ui: fn( 20 ui: &mut egui::Ui, 21 preview: SimpleProfilePreview, 22 width: f32, 23 is_selected: bool, 24 ) -> Option<ProfilePreviewOp>, 25 ) -> Option<Vec<usize>> { 26 let mut to_remove: Option<Vec<usize>> = None; 27 28 let width = ui.available_width(); 29 30 let txn = if let Ok(txn) = Transaction::new(&app.ndb) { 31 txn 32 } else { 33 return None; 34 }; 35 36 for i in 0..app.account_manager.num_accounts() { 37 let account = if let Some(account) = app.account_manager.get_account(i) { 38 account 39 } else { 40 continue; 41 }; 42 43 let profile = app 44 .ndb 45 .get_profile_by_pubkey(&txn, account.pubkey.bytes()) 46 .ok(); 47 48 let preview = SimpleProfilePreview::new(profile.as_ref(), &mut app.img_cache); 49 50 let is_selected = if let Some(selected) = app.account_manager.get_selected_account_index() { 51 i == selected 52 } else { 53 false 54 }; 55 56 let op = if let Some(op) = add_preview_ui(ui, preview, width, is_selected) { 57 op 58 } else { 59 continue; 60 }; 61 62 match op { 63 ProfilePreviewOp::RemoveAccount => { 64 if to_remove.is_none() { 65 to_remove = Some(Vec::new()); 66 } 67 to_remove.as_mut().unwrap().push(i); 68 } 69 ProfilePreviewOp::SwitchTo => app.account_manager.select_account(i), 70 } 71 } 72 73 to_remove 74 } 75 76 pub fn view_profile_previews( 77 app: &mut Damus, 78 ui: &mut egui::Ui, 79 add_preview_ui: fn( 80 ui: &mut egui::Ui, 81 preview: SimpleProfilePreview, 82 width: f32, 83 is_selected: bool, 84 index: usize, 85 ) -> bool, 86 ) -> Option<usize> { 87 let width = ui.available_width(); 88 89 let txn = if let Ok(txn) = Transaction::new(&app.ndb) { 90 txn 91 } else { 92 return None; 93 }; 94 95 for i in 0..app.account_manager.num_accounts() { 96 let account = if let Some(account) = app.account_manager.get_account(i) { 97 account 98 } else { 99 continue; 100 }; 101 102 let profile = app 103 .ndb 104 .get_profile_by_pubkey(&txn, account.pubkey.bytes()) 105 .ok(); 106 107 let preview = SimpleProfilePreview::new(profile.as_ref(), &mut app.img_cache); 108 109 let is_selected = if let Some(selected) = app.account_manager.get_selected_account_index() { 110 i == selected 111 } else { 112 false 113 }; 114 115 if add_preview_ui(ui, preview, width, is_selected, i) { 116 return Some(i); 117 } 118 } 119 120 None 121 } 122 123 pub fn show_with_nickname( 124 ndb: &Ndb, 125 ui: &mut egui::Ui, 126 key: &[u8; 32], 127 ui_element: fn(ui: &mut egui::Ui, username: &DisplayName) -> egui::Response, 128 ) -> Result<egui::Response> { 129 let txn = Transaction::new(ndb)?; 130 let profile = ndb.get_profile_by_pubkey(&txn, key)?; 131 Ok(ui_element(ui, &get_display_name(Some(&profile)))) 132 } 133 134 pub fn show_with_selected_pfp( 135 app: &mut Damus, 136 ui: &mut egui::Ui, 137 ui_element: fn(ui: &mut egui::Ui, pfp: ProfilePic) -> egui::Response, 138 ) -> Option<egui::Response> { 139 let selected_account = app.account_manager.get_selected_account(); 140 if let Some(selected_account) = selected_account { 141 if let Ok(txn) = Transaction::new(&app.ndb) { 142 let profile = app 143 .ndb 144 .get_profile_by_pubkey(&txn, selected_account.pubkey.bytes()); 145 146 return Some(ui_element( 147 ui, 148 ProfilePic::new(&mut app.img_cache, get_profile_url(profile.ok().as_ref())), 149 )); 150 } 151 } 152 153 None 154 } 155 156 pub fn show_with_pfp( 157 app: &mut Damus, 158 ui: &mut egui::Ui, 159 key: &[u8; 32], 160 ui_element: fn(ui: &mut egui::Ui, pfp: ProfilePic) -> egui::Response, 161 ) -> Option<egui::Response> { 162 if let Ok(txn) = Transaction::new(&app.ndb) { 163 let profile = app.ndb.get_profile_by_pubkey(&txn, key); 164 165 return Some(ui_element( 166 ui, 167 ProfilePic::new(&mut app.img_cache, get_profile_url(profile.ok().as_ref())), 168 )); 169 } 170 None 171 }