Clean up logic for editing comments and posts

This commit is contained in:
Bnyro 2023-06-28 10:42:42 +02:00
parent babfa15e30
commit e0bdc593fb
4 changed files with 54 additions and 56 deletions

View File

@ -4,7 +4,11 @@ use relm4::prelude::*;
use relm4_components::web_image::WebImage; use relm4_components::web_image::WebImage;
use crate::api; use crate::api;
use crate::dialogs::editor::DialogMsg;
use crate::dialogs::editor::EditorData; use crate::dialogs::editor::EditorData;
use crate::dialogs::editor::EditorDialog;
use crate::dialogs::editor::EditorOutput;
use crate::dialogs::editor::EditorType;
use crate::settings; use crate::settings;
use crate::util::get_web_image_url; use crate::util::get_web_image_url;
use crate::util::markdown_to_pango_markup; use crate::util::markdown_to_pango_markup;
@ -13,11 +17,11 @@ use super::post_page::PostInput;
use super::voting_row::VotingRowModel; use super::voting_row::VotingRowModel;
use super::voting_row::VotingStats; use super::voting_row::VotingStats;
#[derive(Debug)]
pub struct CommentRow { pub struct CommentRow {
pub comment: CommentView, pub comment: CommentView,
avatar: Controller<WebImage>, avatar: Controller<WebImage>,
voting_row: Controller<VotingRowModel>, voting_row: Controller<VotingRowModel>,
comment_editor_dialog: Controller<EditorDialog>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -25,6 +29,8 @@ pub enum CommentRowMsg {
OpenPerson, OpenPerson,
DeleteComment, DeleteComment,
OpenEditCommentDialog, OpenEditCommentDialog,
EditCommentRequest(EditorData),
UpdateComment(CommentView),
} }
#[relm4::factory(pub)] #[relm4::factory(pub)]
@ -108,7 +114,7 @@ impl FactoryComponent for CommentRow {
Some(output) Some(output)
} }
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 avatar = WebImage::builder() let avatar = WebImage::builder()
.launch(get_web_image_url(value.creator.avatar.clone())) .launch(get_web_image_url(value.creator.avatar.clone()))
.detach(); .detach();
@ -118,11 +124,19 @@ impl FactoryComponent for CommentRow {
value.my_vote, value.my_vote,
)) ))
.detach(); .detach();
let comment_editor_dialog = EditorDialog::builder().launch(EditorType::Comment).forward(
sender.input_sender(),
|msg| match msg {
EditorOutput::EditRequest(data, _) => CommentRowMsg::EditCommentRequest(data),
_ => unreachable!(),
},
);
Self { Self {
comment: value, comment: value,
avatar, avatar,
voting_row, voting_row,
comment_editor_dialog,
} }
} }
@ -162,7 +176,27 @@ impl FactoryComponent for CommentRow {
url: None, url: None,
id: Some(self.comment.comment.id.0), id: Some(self.comment.comment.id.0),
}; };
sender.output(PostInput::OpenEditCommentDialog(data)); let sender = self.comment_editor_dialog.sender();
sender.emit(DialogMsg::UpdateData(data));
sender.emit(DialogMsg::UpdateType(EditorType::Comment, false));
sender.emit(DialogMsg::Show);
}
CommentRowMsg::UpdateComment(comment) => {
self.comment = comment;
}
CommentRowMsg::EditCommentRequest(data) => {
std::thread::spawn(move || {
let message = match api::comment::edit_comment(data.body, data.id.unwrap()) {
Ok(comment) => Some(CommentRowMsg::UpdateComment(comment.comment_view)),
Err(err) => {
println!("{}", err.to_string());
None
}
};
if let Some(message) = message {
sender.input(message)
};
});
} }
} }
} }

View File

@ -7,15 +7,13 @@ use lemmy_api_common::{
lemmy_db_schema::SubscribedType, lemmy_db_views::structs::PostView, lemmy_db_schema::SubscribedType, lemmy_db_views::structs::PostView,
lemmy_db_views_actor::structs::CommunityView, lemmy_db_views_actor::structs::CommunityView,
}; };
use relm4::{factory::FactoryVecDeque, prelude::*, MessageBroker}; use relm4::{factory::FactoryVecDeque, prelude::*};
use relm4_components::web_image::WebImage; use relm4_components::web_image::WebImage;
use crate::{api, settings, util::get_web_image_msg}; use crate::{api, settings, util::get_web_image_msg};
use super::post_row::PostRow; use super::post_row::PostRow;
static COMMUNITY_PAGE_BROKER: MessageBroker<DialogMsg> = MessageBroker::new();
pub struct CommunityPage { pub struct CommunityPage {
info: CommunityView, info: CommunityView,
avatar: Controller<WebImage>, avatar: Controller<WebImage>,
@ -149,7 +147,7 @@ impl SimpleComponent for CommunityPage {
let dialog = EditorDialog::builder() let dialog = EditorDialog::builder()
.transient_for(root) .transient_for(root)
.launch_with_broker(EditorType::Post, &COMMUNITY_PAGE_BROKER) .launch(EditorType::Post)
.forward(sender.input_sender(), |msg| match msg { .forward(sender.input_sender(), |msg| match msg {
EditorOutput::CreateRequest(post, _) => CommunityInput::CreatePostRequest(post), EditorOutput::CreateRequest(post, _) => CommunityInput::CreatePostRequest(post),
_ => CommunityInput::None, _ => CommunityInput::None,
@ -198,7 +196,10 @@ impl SimpleComponent for CommunityPage {
self.posts.guard().push_back(post); self.posts.guard().push_back(post);
} }
} }
CommunityInput::OpenCreatePostDialog => COMMUNITY_PAGE_BROKER.send(DialogMsg::Show), CommunityInput::OpenCreatePostDialog => {
let sender = self.create_post_dialog.sender();
sender.emit(DialogMsg::Show);
}
CommunityInput::CreatedPost(post) => { CommunityInput::CreatedPost(post) => {
self.posts.guard().push_front(post); self.posts.guard().push_front(post);
} }

View File

@ -3,7 +3,7 @@ use lemmy_api_common::{
lemmy_db_views::structs::{CommentView, PostView}, lemmy_db_views::structs::{CommentView, PostView},
post::GetPostResponse, post::GetPostResponse,
}; };
use relm4::{factory::FactoryVecDeque, prelude::*, MessageBroker}; use relm4::{factory::FactoryVecDeque, prelude::*};
use relm4_components::web_image::WebImage; use relm4_components::web_image::WebImage;
use crate::{ use crate::{
@ -18,8 +18,6 @@ use super::{
voting_row::{VotingRowInput, VotingRowModel, VotingStats}, voting_row::{VotingRowInput, VotingRowModel, VotingStats},
}; };
pub static POST_PAGE_BROKER: MessageBroker<DialogMsg> = MessageBroker::new();
pub struct PostPage { pub struct PostPage {
info: GetPostResponse, info: GetPostResponse,
image: Controller<WebImage>, image: Controller<WebImage>,
@ -43,12 +41,9 @@ pub enum PostInput {
EditPostRequest(EditorData), EditPostRequest(EditorData),
CreatedComment(CommentView), CreatedComment(CommentView),
OpenEditPostDialog, OpenEditPostDialog,
OpenEditCommentDialog(EditorData),
DeletePost, DeletePost,
DoneEditPost(PostView), DoneEditPost(PostView),
PassAppMessage(crate::AppMsg), PassAppMessage(crate::AppMsg),
EditCommentRequest(EditorData),
UpdateComment(CommentView),
} }
#[relm4::component(pub)] #[relm4::component(pub)]
@ -206,14 +201,10 @@ impl SimpleComponent for PostPage {
let community_avatar = WebImage::builder().launch("".to_string()).detach(); let community_avatar = WebImage::builder().launch("".to_string()).detach();
let dialog = EditorDialog::builder() let dialog = EditorDialog::builder()
.transient_for(root) .transient_for(root)
.launch_with_broker(EditorType::Comment, &POST_PAGE_BROKER) .launch(EditorType::Comment)
.forward(sender.input_sender(), |msg| match msg { .forward(sender.input_sender(), |msg| match msg {
EditorOutput::CreateRequest(comment, _) => PostInput::CreateCommentRequest(comment), EditorOutput::CreateRequest(comment, _) => PostInput::CreateCommentRequest(comment),
EditorOutput::EditRequest(post, type_) => match type_ { EditorOutput::EditRequest(post, _) => PostInput::EditPostRequest(post),
EditorType::Post => PostInput::EditPostRequest(post),
EditorType::Comment => PostInput::EditCommentRequest(post),
_ => unreachable!(),
},
}); });
let voting_row = VotingRowModel::builder() let voting_row = VotingRowModel::builder()
.launch(VotingStats::default()) .launch(VotingStats::default())
@ -295,8 +286,9 @@ impl SimpleComponent for PostPage {
gtk::show_uri(None::<&relm4::gtk::Window>, &link, 0); gtk::show_uri(None::<&relm4::gtk::Window>, &link, 0);
} }
PostInput::OpenCreateCommentDialog => { PostInput::OpenCreateCommentDialog => {
POST_PAGE_BROKER.send(DialogMsg::UpdateType(EditorType::Comment, true)); let sender = self.create_comment_dialog.sender();
POST_PAGE_BROKER.send(DialogMsg::Show); sender.emit(DialogMsg::UpdateType(EditorType::Comment, true));
sender.emit(DialogMsg::Show);
} }
PostInput::CreatedComment(comment) => { PostInput::CreatedComment(comment) => {
self.comments.guard().push_front(comment); self.comments.guard().push_front(comment);
@ -340,9 +332,10 @@ impl SimpleComponent for PostPage {
url: reqwest::Url::parse(&url).ok(), url: reqwest::Url::parse(&url).ok(),
id: None, id: None,
}; };
POST_PAGE_BROKER.send(DialogMsg::UpdateData(data)); let sender = self.create_comment_dialog.sender();
POST_PAGE_BROKER.send(DialogMsg::UpdateType(EditorType::Post, false)); sender.emit(DialogMsg::UpdateData(data));
POST_PAGE_BROKER.send(DialogMsg::Show) sender.emit(DialogMsg::UpdateType(EditorType::Post, false));
sender.emit(DialogMsg::Show);
} }
PostInput::EditPostRequest(post) => { PostInput::EditPostRequest(post) => {
let id = self.info.post_view.post.id.0; let id = self.info.post_view.post.id.0;
@ -362,36 +355,6 @@ impl SimpleComponent for PostPage {
PostInput::DoneEditPost(post) => { PostInput::DoneEditPost(post) => {
self.info.post_view = post; self.info.post_view = post;
} }
PostInput::OpenEditCommentDialog(data) => {
POST_PAGE_BROKER.send(DialogMsg::UpdateData(data));
POST_PAGE_BROKER.send(DialogMsg::UpdateType(EditorType::Comment, false));
POST_PAGE_BROKER.send(DialogMsg::Show);
}
PostInput::EditCommentRequest(data) => {
std::thread::spawn(move || {
let message = match api::comment::edit_comment(data.body, data.id.unwrap()) {
Ok(comment) => Some(PostInput::UpdateComment(comment.comment_view)),
Err(err) => {
println!("{}", err.to_string());
None
}
};
if let Some(message) = message {
sender.input(message)
};
});
}
PostInput::UpdateComment(comment) => {
let mut index = 0;
let id = comment.comment.id;
loop {
if self.comments.guard().get(index).unwrap().comment.comment.id == id {
self.comments.guard().get_mut(index).unwrap().comment = comment;
break;
}
index += 1;
}
}
PostInput::PassAppMessage(message) => { PostInput::PassAppMessage(message) => {
let _ = sender.output(message); let _ = sender.output(message);
} }

View File

@ -90,7 +90,7 @@ impl FactoryComponent for PostRow {
add_css_class: "font-bold", add_css_class: "font-bold",
add_controller = gtk::GestureClick { add_controller = gtk::GestureClick {
connect_pressed[sender] => move |_, _, _, _| { connect_pressed[sender] => move |_, _, _, _| {
sender.input(PostRowMsg::OpenCommunity); sender.input(PostRowMsg::OpenPost);
} }
}, },
}, },