Show preview of post thumbnail(closes #9)

This commit is contained in:
Bnyro 2023-07-16 19:53:01 +02:00
parent 8298bac336
commit 3ce4fc0c3f
1 changed files with 91 additions and 65 deletions

View File

@ -13,6 +13,7 @@ pub struct PostRow {
post: PostView, post: PostView,
author_image: Controller<WebImage>, author_image: Controller<WebImage>,
community_image: Controller<WebImage>, community_image: Controller<WebImage>,
thumbnail: Controller<WebImage>,
voting_row: Controller<VotingRowModel>, voting_row: Controller<VotingRowModel>,
} }
@ -38,82 +39,102 @@ impl FactoryComponent for PostRow {
view! { view! {
root = gtk::Box { root = gtk::Box {
set_orientation: gtk::Orientation::Vertical, set_orientation: gtk::Orientation::Vertical,
set_spacing: 10,
set_margin_end: 10, set_margin_end: 10,
set_margin_start: 10, set_margin_start: 10,
gtk::Box { gtk::Box {
set_orientation: gtk::Orientation::Horizontal, set_orientation: gtk::Orientation::Horizontal,
set_margin_top: 10,
set_spacing: 10, set_spacing: 10,
set_vexpand: false,
set_hexpand: true,
#[local_ref] #[local_ref]
community_image -> gtk::Box { thumbnail -> gtk::Box {
set_hexpand: false, set_visible: self.post.post.thumbnail_url.is_some(),
set_visible: self.post.community.icon.clone().is_some(), set_size_request: (180, 180),
},
gtk::Button {
set_label: &self.post.community.title,
connect_clicked => PostRowMsg::OpenCommunity,
},
#[local_ref]
author_image -> gtk::Box {
set_hexpand: false,
set_margin_start: 10, set_margin_start: 10,
set_visible: self.post.creator.avatar.clone().is_some(),
},
gtk::Button {
set_label: &self.post.creator.name,
connect_clicked => PostRowMsg::OpenPerson,
},
gtk::Label {
set_margin_start: 10,
set_label: &util::format_elapsed_time(self.post.post.published),
}
},
gtk::Label {
set_halign: gtk::Align::Start,
set_text: &self.post.post.name,
set_wrap: true,
add_css_class: "font-bold",
add_controller = gtk::GestureClick {
connect_pressed[sender] => move |_, _, _, _| {
sender.input(PostRowMsg::OpenPost);
}
},
},
gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
#[local_ref]
voting_row -> gtk::Box {
set_margin_end: 10, set_margin_end: 10,
set_hexpand: false,
set_valign: gtk::Align::Center,
}, },
gtk::Label {
set_halign: gtk::Align::Start, gtk::Box {
set_text: &format!("{} comments", self.post.counts.comments.clone()), set_orientation: gtk::Orientation::Vertical,
}, set_valign: gtk::Align::Center,
gtk::ToggleButton { set_spacing: 10,
set_icon_name: "bookmark-new", set_hexpand: true,
set_margin_start: 10,
connect_clicked => PostRowMsg::ToggleSaved, gtk::Box {
set_visible: settings::get_current_account().jwt.is_some(), set_orientation: gtk::Orientation::Horizontal,
#[watch] set_margin_top: 10,
set_active: self.post.saved, set_spacing: 10,
}, set_vexpand: false,
gtk::Button {
set_icon_name: "edit-delete", #[local_ref]
connect_clicked => PostRowMsg::DeletePost, community_image -> gtk::Box {
set_margin_start: 10, set_hexpand: false,
#[watch] set_visible: self.post.community.icon.clone().is_some(),
set_visible: self.post.creator.id.0 == settings::get_current_account().id, },
gtk::Button {
set_label: &self.post.community.title,
connect_clicked => PostRowMsg::OpenCommunity,
},
#[local_ref]
author_image -> gtk::Box {
set_hexpand: false,
set_margin_start: 10,
set_visible: self.post.creator.avatar.clone().is_some(),
},
gtk::Button {
set_label: &self.post.creator.name,
connect_clicked => PostRowMsg::OpenPerson,
},
gtk::Label {
set_margin_start: 10,
set_label: &util::format_elapsed_time(self.post.post.published),
}
},
gtk::Label {
set_halign: gtk::Align::Start,
set_text: &self.post.post.name,
set_wrap: true,
add_css_class: "font-bold",
add_controller = gtk::GestureClick {
connect_pressed[sender] => move |_, _, _, _| {
sender.input(PostRowMsg::OpenPost);
}
},
},
gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
#[local_ref]
voting_row -> gtk::Box {
set_margin_end: 10,
},
gtk::Label {
set_halign: gtk::Align::Start,
set_text: &format!("{} comments", self.post.counts.comments.clone()),
},
gtk::ToggleButton {
set_icon_name: "bookmark-new",
set_margin_start: 10,
connect_clicked => PostRowMsg::ToggleSaved,
set_visible: settings::get_current_account().jwt.is_some(),
#[watch]
set_active: self.post.saved,
},
gtk::Button {
set_icon_name: "edit-delete",
connect_clicked => PostRowMsg::DeletePost,
set_margin_start: 10,
#[watch]
set_visible: self.post.creator.id.0 == settings::get_current_account().id,
}
},
} }
}, },
@ -128,6 +149,9 @@ impl FactoryComponent for PostRow {
} }
fn init_model(value: Self::Init, _index: &DynamicIndex, _sender: FactorySender<Self>) -> Self { fn init_model(value: Self::Init, _index: &DynamicIndex, _sender: FactorySender<Self>) -> Self {
let thumbnail = WebImage::builder()
.launch(get_web_image_url(value.post.thumbnail_url.clone()))
.detach();
let author_image = WebImage::builder() let author_image = WebImage::builder()
.launch(get_web_image_url(value.creator.avatar.clone())) .launch(get_web_image_url(value.creator.avatar.clone()))
.detach(); .detach();
@ -143,6 +167,7 @@ impl FactoryComponent for PostRow {
author_image, author_image,
community_image, community_image,
voting_row, voting_row,
thumbnail,
} }
} }
@ -153,6 +178,7 @@ impl FactoryComponent for PostRow {
_returned_widget: &<Self::ParentWidget as relm4::factory::FactoryView>::ReturnedWidget, _returned_widget: &<Self::ParentWidget as relm4::factory::FactoryView>::ReturnedWidget,
sender: FactorySender<Self>, sender: FactorySender<Self>,
) -> Self::Widgets { ) -> Self::Widgets {
let thumbnail = self.thumbnail.widget();
let author_image = self.author_image.widget(); let author_image = self.author_image.widget();
let community_image = self.community_image.widget(); let community_image = self.community_image.widget();
let voting_row = self.voting_row.widget(); let voting_row = self.voting_row.widget();