From ef6ee57f6cb1842a88898e08ae09804c09d95b9f Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 19 Jun 2023 07:34:10 +0200 Subject: [PATCH] Drop RefCell's and use Relm component mutability instead --- src/components/community_page.rs | 19 +++++++------------ src/components/post_page.rs | 29 ++++++++++++++--------------- src/components/profile_page.rs | 15 +++++++-------- 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/components/community_page.rs b/src/components/community_page.rs index f1ae2bd..32f3f9e 100644 --- a/src/components/community_page.rs +++ b/src/components/community_page.rs @@ -2,14 +2,13 @@ use lemmy_api_common::{community::GetCommunityResponse, lemmy_db_views::structs: use relm4::{prelude::*, factory::FactoryVecDeque}; use gtk::prelude::*; use relm4_components::web_image::WebImage; -use std::cell::RefCell; use crate::{api, util::get_web_image_msg}; use super::post_row::PostRow; pub struct CommunityPage { - info: RefCell, + info: GetCommunityResponse, avatar: Controller, posts: FactoryVecDeque } @@ -43,16 +42,16 @@ impl SimpleComponent for CommunityPage { }, gtk::Label { #[watch] - set_text: &model.info.borrow().community_view.community.name, + set_text: &model.info.community_view.community.name, add_css_class: "font-very-bold", }, gtk::Label { #[watch] - set_text: &model.info.borrow().clone().community_view.community.description.unwrap_or("".to_string()), + set_text: &model.info.clone().community_view.community.description.unwrap_or("".to_string()), }, gtk::Label { #[watch] - set_text: &format!("{} subscribers, ", model.info.borrow().community_view.counts.subscribers), + set_text: &format!("{} subscribers", model.info.community_view.counts.subscribers), }, gtk::Box { @@ -64,11 +63,7 @@ impl SimpleComponent for CommunityPage { gtk::Label { #[watch] - set_text: &format!("{} posts, ", model.info.borrow().community_view.counts.posts), - }, - gtk::Label { - #[watch] - set_text: &format!("{} comments", model.info.borrow().clone().community_view.counts.comments), + set_text: &format!("{} posts, {} comments", model.info.community_view.counts.posts, model.info.community_view.counts.comments), }, }, @@ -90,7 +85,7 @@ impl SimpleComponent for CommunityPage { ) -> relm4::ComponentParts { let avatar = WebImage::builder().launch("".to_string()).detach(); let posts = FactoryVecDeque::new(gtk::Box::default(), sender.output_sender()); - let model = CommunityPage { info: RefCell::new(init), avatar, posts }; + let model = CommunityPage { info: init, avatar, posts }; let avatar = model.avatar.widget(); let posts = model.posts.widget(); let widgets = view_output!(); @@ -101,7 +96,7 @@ impl SimpleComponent for CommunityPage { fn update(&mut self, message: Self::Input, sender: ComponentSender) { match message { CommunityInput::UpdateCommunity(community) => { - *self.info.borrow_mut() = community.clone(); + self.info = community.clone(); self.avatar.emit(get_web_image_msg(community.community_view.community.icon)); self.posts.guard().clear(); diff --git a/src/components/post_page.rs b/src/components/post_page.rs index bf102f6..d5fd41f 100644 --- a/src/components/post_page.rs +++ b/src/components/post_page.rs @@ -2,14 +2,13 @@ use lemmy_api_common::{lemmy_db_views::structs::{CommentView}, post::GetPostResp use relm4::{prelude::*, factory::FactoryVecDeque}; use gtk::prelude::*; use relm4_components::web_image::WebImage; -use std::cell::RefCell; use crate::{api, util::{get_web_image_msg, get_web_image_url}}; use super::comment_row::CommentRow; pub struct PostPage { - info: RefCell, + info: GetPostResponse, image: Controller, creator_avatar: Controller, community_avatar: Controller, @@ -46,12 +45,12 @@ impl SimpleComponent for PostPage { }, gtk::Label { #[watch] - set_text: &model.info.borrow().post_view.post.name, + set_text: &model.info.post_view.post.name, add_css_class: "font-very-bold", }, gtk::Label { #[watch] - set_text: &model.info.borrow().clone().post_view.post.body.unwrap_or("".to_string()), + set_text: &model.info.clone().post_view.post.body.unwrap_or("".to_string()), set_margin_top: 10, }, @@ -65,7 +64,7 @@ impl SimpleComponent for PostPage { set_text: "posted by " }, - if model.info.borrow().post_view.creator.avatar.is_some() { + if model.info.post_view.creator.avatar.is_some() { gtk::Box { set_hexpand: false, set_margin_start: 10, @@ -77,7 +76,7 @@ impl SimpleComponent for PostPage { }, gtk::Button { - set_label: &model.info.borrow().post_view.creator.name, + set_label: &model.info.post_view.creator.name, connect_clicked => PostInput::OpenPerson, }, @@ -85,7 +84,7 @@ impl SimpleComponent for PostPage { set_text: " in " }, - if model.info.borrow().community_view.community.icon.is_some() { + if model.info.community_view.community.icon.is_some() { gtk::Box { set_hexpand: false, #[local_ref] @@ -96,7 +95,7 @@ impl SimpleComponent for PostPage { }, gtk::Button { - set_label: &model.info.borrow().community_view.community.title, + set_label: &model.info.community_view.community.title, connect_clicked => PostInput::OpenCommunity, }, @@ -117,11 +116,11 @@ impl SimpleComponent for PostPage { gtk::Label { #[watch] - set_text: &format!("{} comments, ", model.info.borrow().post_view.counts.comments), + set_text: &format!("{} comments, ", model.info.post_view.counts.comments), }, gtk::Label { #[watch] - set_text: &format!("{} score", model.info.borrow().post_view.counts.score), + set_text: &format!("{} score", model.info.post_view.counts.score), }, }, @@ -144,7 +143,7 @@ impl SimpleComponent for PostPage { let comments = FactoryVecDeque::new(gtk::Box::default(), sender.output_sender()); let creator_avatar = WebImage::builder().launch("".to_string()).detach(); let community_avatar = WebImage::builder().launch("".to_string()).detach(); - let model = PostPage { info: RefCell::new(init), image, comments, creator_avatar, community_avatar }; + let model = PostPage { info: init, image, comments, creator_avatar, community_avatar }; let image = model.image.widget(); let comments = model.comments.widget(); @@ -158,7 +157,7 @@ impl SimpleComponent for PostPage { fn update(&mut self, message: Self::Input, sender: ComponentSender) { match message { PostInput::UpdatePost(post) => { - *self.info.borrow_mut() = post.clone(); + self.info = post.clone(); self.image.emit(get_web_image_msg(post.post_view.post.thumbnail_url)); self.community_avatar.emit(get_web_image_msg(post.community_view.community.icon)); @@ -180,15 +179,15 @@ impl SimpleComponent for PostPage { } } PostInput::OpenPerson => { - let name = self.info.borrow().post_view.creator.name.clone(); + let name = self.info.post_view.creator.name.clone(); let _ = sender.output(crate::AppMsg::OpenPerson(name)); } PostInput::OpenCommunity => { - let community_name = self.info.borrow().community_view.community.name.clone(); + let community_name = self.info.community_view.community.name.clone(); let _ = sender.output(crate::AppMsg::OpenCommunity(community_name)); } PostInput::OpenLink => { - let post = self.info.borrow().post_view.post.clone(); + let post = self.info.post_view.post.clone(); let mut link = get_web_image_url(post.url); if link.is_empty() { link = get_web_image_url(post.thumbnail_url); diff --git a/src/components/profile_page.rs b/src/components/profile_page.rs index ab3cf82..93207a3 100644 --- a/src/components/profile_page.rs +++ b/src/components/profile_page.rs @@ -2,14 +2,13 @@ use lemmy_api_common::person::GetPersonDetailsResponse; use relm4::{prelude::*, factory::FactoryVecDeque}; use gtk::prelude::*; use relm4_components::web_image::WebImage; -use std::cell::RefCell; use crate::util::get_web_image_msg; use super::post_row::PostRow; pub struct ProfilePage { - info: RefCell, + info: GetPersonDetailsResponse, avatar: Controller, posts: FactoryVecDeque } @@ -40,12 +39,12 @@ impl SimpleComponent for ProfilePage { }, gtk::Label { #[watch] - set_text: &model.info.borrow().person_view.person.name, + set_text: &model.info.person_view.person.name, add_css_class: "font-very-bold", }, gtk::Label { #[watch] - set_text: &model.info.borrow().clone().person_view.person.bio.unwrap_or("".to_string()), + set_text: &model.info.person_view.person.bio.clone().unwrap_or("".to_string()), }, gtk::Box { @@ -57,11 +56,11 @@ impl SimpleComponent for ProfilePage { gtk::Label { #[watch] - set_text: &format!("{} posts, ", model.info.borrow().person_view.counts.post_count), + set_text: &format!("{} posts, ", model.info.person_view.counts.post_count), }, gtk::Label { #[watch] - set_text: &format!("{} comments", model.info.borrow().person_view.counts.comment_count), + set_text: &format!("{} comments", model.info.person_view.counts.comment_count), }, }, @@ -83,7 +82,7 @@ impl SimpleComponent for ProfilePage { ) -> relm4::ComponentParts { let avatar = WebImage::builder().launch("".to_string()).detach(); let posts = FactoryVecDeque::new(gtk::Box::default(), sender.output_sender()); - let model = ProfilePage { info: RefCell::new(init), avatar, posts }; + let model = ProfilePage { info: init, avatar, posts }; let avatar = model.avatar.widget(); let posts = model.posts.widget(); let widgets = view_output!(); @@ -94,7 +93,7 @@ impl SimpleComponent for ProfilePage { fn update(&mut self, message: Self::Input, _sender: ComponentSender) { match message { ProfileInput::UpdatePerson(person) => { - *self.info.borrow_mut() = person.clone(); + self.info = person.clone(); self.avatar.emit(get_web_image_msg(person.person_view.person.avatar)); self.posts.guard().clear(); for post in person.posts {