refactor: move profile_body to fn

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-09-13 14:12:15 -04:00
parent 50293a6f34
commit 11700d6217

View File

@@ -76,7 +76,9 @@ impl<'a, 'd> ProfileView<'a, 'd> {
.get_profile_by_pubkey(&txn, self.pubkey.bytes()) .get_profile_by_pubkey(&txn, self.pubkey.bytes())
.ok(); .ok();
if let Some(profile_view_action) = self.profile_body(ui, profile.as_ref()) { if let Some(profile_view_action) =
profile_body(ui, self.pubkey, self.note_context, profile.as_ref())
{
action = Some(profile_view_action); action = Some(profile_view_action);
} }
@@ -123,138 +125,136 @@ impl<'a, 'd> ProfileView<'a, 'd> {
output.inner output.inner
} }
}
fn profile_body( fn profile_body(
&mut self, ui: &mut egui::Ui,
ui: &mut egui::Ui, pubkey: &Pubkey,
profile: Option<&ProfileRecord<'_>>, note_context: &mut NoteContext,
) -> Option<ProfileViewAction> { profile: Option<&ProfileRecord<'_>>,
let mut action = None; ) -> Option<ProfileViewAction> {
ui.vertical(|ui| { let mut action = None;
banner( ui.vertical(|ui| {
ui, banner(
profile ui,
.map(|p| p.record().profile()) profile
.and_then(|p| p.and_then(|p| p.banner())), .map(|p| p.record().profile())
120.0, .and_then(|p| p.and_then(|p| p.banner())),
); 120.0,
);
let padding = 12.0; let padding = 12.0;
notedeck_ui::padding(padding, ui, |ui| { notedeck_ui::padding(padding, ui, |ui| {
let mut pfp_rect = ui.available_rect_before_wrap(); let mut pfp_rect = ui.available_rect_before_wrap();
let size = 80.0; let size = 80.0;
pfp_rect.set_width(size); pfp_rect.set_width(size);
pfp_rect.set_height(size); pfp_rect.set_height(size);
let pfp_rect = pfp_rect.translate(egui::vec2(0.0, -(padding + 2.0 + (size / 2.0)))); let pfp_rect = pfp_rect.translate(egui::vec2(0.0, -(padding + 2.0 + (size / 2.0))));
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.put( ui.put(
pfp_rect, pfp_rect,
&mut ProfilePic::new(self.note_context.img_cache, get_profile_url(profile)) &mut ProfilePic::new(note_context.img_cache, get_profile_url(profile))
.size(size) .size(size)
.border(ProfilePic::border_stroke(ui)), .border(ProfilePic::border_stroke(ui)),
); );
if ui if ui
.add(copy_key_widget(&pfp_rect, self.note_context.i18n)) .add(copy_key_widget(&pfp_rect, note_context.i18n))
.clicked() .clicked()
{ {
let to_copy = if let Some(bech) = self.pubkey.npub() { let to_copy = if let Some(bech) = pubkey.npub() {
bech bech
} else { } else {
error!("Could not convert Pubkey to bech"); error!("Could not convert Pubkey to bech");
String::new() String::new()
}; };
ui.ctx().copy_text(to_copy) ui.ctx().copy_text(to_copy)
} }
ui.with_layout(Layout::right_to_left(egui::Align::RIGHT), |ui| { ui.with_layout(Layout::right_to_left(egui::Align::RIGHT), |ui| {
ui.add_space(24.0); ui.add_space(24.0);
let target_key = self.pubkey; let target_key = pubkey;
let selected = self.note_context.accounts.get_selected_account(); let selected = note_context.accounts.get_selected_account();
let profile_type = if selected.key.secret_key.is_none() { let profile_type = if selected.key.secret_key.is_none() {
ProfileType::ReadOnly ProfileType::ReadOnly
} else if &selected.key.pubkey == self.pubkey { } else if &selected.key.pubkey == pubkey {
ProfileType::MyProfile ProfileType::MyProfile
} else { } else {
ProfileType::Followable(selected.is_following(target_key.bytes())) ProfileType::Followable(selected.is_following(target_key.bytes()))
}; };
match profile_type { match profile_type {
ProfileType::MyProfile => { ProfileType::MyProfile => {
if ui if ui.add(edit_profile_button(note_context.i18n)).clicked() {
.add(edit_profile_button(self.note_context.i18n)) action = Some(ProfileViewAction::EditProfile);
.clicked()
{
action = Some(ProfileViewAction::EditProfile);
}
} }
ProfileType::Followable(is_following) => { }
let follow_button = ui.add(follow_button(is_following)); ProfileType::Followable(is_following) => {
let follow_button = ui.add(follow_button(is_following));
if follow_button.clicked() { if follow_button.clicked() {
action = match is_following { action = match is_following {
IsFollowing::Unknown => { IsFollowing::Unknown => {
// don't do anything, we don't have contact list // don't do anything, we don't have contact list
None None
} }
IsFollowing::Yes => { IsFollowing::Yes => {
Some(ProfileViewAction::Unfollow(target_key.to_owned())) Some(ProfileViewAction::Unfollow(target_key.to_owned()))
} }
IsFollowing::No => { IsFollowing::No => {
Some(ProfileViewAction::Follow(target_key.to_owned())) Some(ProfileViewAction::Follow(target_key.to_owned()))
} }
}; };
}
} }
ProfileType::ReadOnly => {}
} }
}); ProfileType::ReadOnly => {}
});
ui.add_space(18.0);
ui.add(display_name_widget(&get_display_name(profile), false));
ui.add_space(8.0);
ui.add(about_section_widget(profile));
ui.horizontal_wrapped(|ui| {
let website_url = profile
.as_ref()
.map(|p| p.record().profile())
.and_then(|p| p.and_then(|p| p.website()).filter(|s| !s.is_empty()));
let lud16 = profile
.as_ref()
.map(|p| p.record().profile())
.and_then(|p| p.and_then(|p| p.lud16()).filter(|s| !s.is_empty()));
if let Some(website_url) = website_url {
ui.horizontal(|ui| {
handle_link(ui, website_url);
});
}
if let Some(lud16) = lud16 {
if website_url.is_some() {
ui.end_row();
}
ui.horizontal(|ui| {
handle_lud16(ui, lud16);
});
} }
}); });
}); });
});
action ui.add_space(18.0);
}
ui.add(display_name_widget(&get_display_name(profile), false));
ui.add_space(8.0);
ui.add(about_section_widget(profile));
ui.horizontal_wrapped(|ui| {
let website_url = profile
.as_ref()
.map(|p| p.record().profile())
.and_then(|p| p.and_then(|p| p.website()).filter(|s| !s.is_empty()));
let lud16 = profile
.as_ref()
.map(|p| p.record().profile())
.and_then(|p| p.and_then(|p| p.lud16()).filter(|s| !s.is_empty()));
if let Some(website_url) = website_url {
ui.horizontal(|ui| {
handle_link(ui, website_url);
});
}
if let Some(lud16) = lud16 {
if website_url.is_some() {
ui.end_row();
}
ui.horizontal(|ui| {
handle_lud16(ui, lud16);
});
}
});
});
});
action
} }
enum ProfileType { enum ProfileType {