notedeck

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

commit 724042b70085f438740bd9870c1b21b265a13fed
parent a890c9e97077e14d1ed45a7f9a1538826ebdf280
Author: William Casarin <jb55@jb55.com>
Date:   Tue,  2 Jul 2024 10:34:42 -0700

allow missing profiles in account switcher

Fixes: https://github.com/damus-io/notedeck/issues/119
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Msrc/ui/profile/preview.rs | 19+++++++++++--------
Msrc/ui/profile/profile_preview_controller.rs | 46+++++++++++++++++++---------------------------
2 files changed, 30 insertions(+), 35 deletions(-)

diff --git a/src/ui/profile/preview.rs b/src/ui/profile/preview.rs @@ -64,8 +64,11 @@ impl<'a, 'cache> ProfilePreview<'a, 'cache> { fn body(self, ui: &mut egui::Ui) { crate::ui::padding(12.0, ui, |ui| { - ui.add(ProfilePic::new(self.cache, get_profile_url(self.profile)).size(80.0)); - ui.add(display_name_widget(get_display_name(self.profile), false)); + ui.add(ProfilePic::new(self.cache, get_profile_url(Some(self.profile))).size(80.0)); + ui.add(display_name_widget( + get_display_name(Some(self.profile)), + false, + )); ui.add(about_section_widget(self.profile)); }); } @@ -85,12 +88,12 @@ impl<'a, 'cache> egui::Widget for ProfilePreview<'a, 'cache> { } pub struct SimpleProfilePreview<'a, 'cache> { - profile: &'a ProfileRecord<'a>, + profile: Option<&'a ProfileRecord<'a>>, cache: &'cache mut ImageCache, } impl<'a, 'cache> SimpleProfilePreview<'a, 'cache> { - pub fn new(profile: &'a ProfileRecord<'a>, cache: &'cache mut ImageCache) -> Self { + pub fn new(profile: Option<&'a ProfileRecord<'a>>, cache: &'cache mut ImageCache) -> Self { SimpleProfilePreview { profile, cache } } } @@ -148,16 +151,16 @@ mod previews { } } -pub fn get_display_name<'a>(profile: &'a ProfileRecord<'a>) -> DisplayName<'a> { - if let Some(name) = crate::profile::get_profile_name(profile) { +pub fn get_display_name<'a>(profile: Option<&'a ProfileRecord<'a>>) -> DisplayName<'a> { + if let Some(name) = profile.and_then(|p| crate::profile::get_profile_name(p)) { name } else { DisplayName::One("??") } } -pub fn get_profile_url<'a>(profile: &'a ProfileRecord<'a>) -> &'a str { - if let Some(url) = profile.record().profile().and_then(|p| p.picture()) { +pub fn get_profile_url<'a>(profile: Option<&'a ProfileRecord<'a>>) -> &'a str { + if let Some(url) = profile.and_then(|pr| pr.record().profile().and_then(|p| p.picture())) { url } else { ProfilePic::no_pfp_url() diff --git a/src/ui/profile/profile_preview_controller.rs b/src/ui/profile/profile_preview_controller.rs @@ -40,14 +40,12 @@ pub fn set_profile_previews( continue; }; - let profile = - if let Ok(profile) = app.ndb.get_profile_by_pubkey(&txn, account.pubkey.bytes()) { - profile - } else { - continue; - }; + let profile = app + .ndb + .get_profile_by_pubkey(&txn, account.pubkey.bytes()) + .ok(); - let preview = SimpleProfilePreview::new(&profile, &mut app.img_cache); + let preview = SimpleProfilePreview::new(profile.as_ref(), &mut app.img_cache); let is_selected = if let Some(selected) = app.account_manager.get_selected_account_index() { i == selected @@ -101,14 +99,12 @@ pub fn view_profile_previews( continue; }; - let profile = - if let Ok(profile) = app.ndb.get_profile_by_pubkey(&txn, account.pubkey.bytes()) { - profile - } else { - continue; - }; + let profile = app + .ndb + .get_profile_by_pubkey(&txn, account.pubkey.bytes()) + .ok(); - let preview = SimpleProfilePreview::new(&profile, &mut app.img_cache); + let preview = SimpleProfilePreview::new(profile.as_ref(), &mut app.img_cache); let is_selected = if let Some(selected) = app.account_manager.get_selected_account_index() { i == selected @@ -132,7 +128,7 @@ pub fn show_with_nickname( ) -> Result<egui::Response> { let txn = Transaction::new(ndb)?; let profile = ndb.get_profile_by_pubkey(&txn, key)?; - Ok(ui_element(ui, &get_display_name(&profile))) + Ok(ui_element(ui, &get_display_name(Some(&profile)))) } pub fn show_with_selected_pfp( @@ -147,12 +143,10 @@ pub fn show_with_selected_pfp( .ndb .get_profile_by_pubkey(&txn, selected_account.pubkey.bytes()); - if let Ok(profile) = profile { - return Some(ui_element( - ui, - ProfilePic::new(&mut app.img_cache, get_profile_url(&profile)), - )); - } + return Some(ui_element( + ui, + ProfilePic::new(&mut app.img_cache, get_profile_url(profile.ok().as_ref())), + )); } } @@ -168,12 +162,10 @@ pub fn show_with_pfp( if let Ok(txn) = Transaction::new(&app.ndb) { let profile = app.ndb.get_profile_by_pubkey(&txn, key); - if let Ok(profile) = profile { - return Some(ui_element( - ui, - ProfilePic::new(&mut app.img_cache, get_profile_url(&profile)), - )); - } + return Some(ui_element( + ui, + ProfilePic::new(&mut app.img_cache, get_profile_url(profile.ok().as_ref())), + )); } None }