Clean up logic for editing comments and posts
This commit is contained in:
parent
babfa15e30
commit
e0bdc593fb
|
@ -4,7 +4,11 @@ use relm4::prelude::*;
|
|||
use relm4_components::web_image::WebImage;
|
||||
|
||||
use crate::api;
|
||||
use crate::dialogs::editor::DialogMsg;
|
||||
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::util::get_web_image_url;
|
||||
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::VotingStats;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CommentRow {
|
||||
pub comment: CommentView,
|
||||
avatar: Controller<WebImage>,
|
||||
voting_row: Controller<VotingRowModel>,
|
||||
comment_editor_dialog: Controller<EditorDialog>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -25,6 +29,8 @@ pub enum CommentRowMsg {
|
|||
OpenPerson,
|
||||
DeleteComment,
|
||||
OpenEditCommentDialog,
|
||||
EditCommentRequest(EditorData),
|
||||
UpdateComment(CommentView),
|
||||
}
|
||||
|
||||
#[relm4::factory(pub)]
|
||||
|
@ -108,7 +114,7 @@ impl FactoryComponent for CommentRow {
|
|||
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()
|
||||
.launch(get_web_image_url(value.creator.avatar.clone()))
|
||||
.detach();
|
||||
|
@ -118,11 +124,19 @@ impl FactoryComponent for CommentRow {
|
|||
value.my_vote,
|
||||
))
|
||||
.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 {
|
||||
comment: value,
|
||||
avatar,
|
||||
voting_row,
|
||||
comment_editor_dialog,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -162,7 +176,27 @@ impl FactoryComponent for CommentRow {
|
|||
url: None,
|
||||
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)
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,15 +7,13 @@ use lemmy_api_common::{
|
|||
lemmy_db_schema::SubscribedType, lemmy_db_views::structs::PostView,
|
||||
lemmy_db_views_actor::structs::CommunityView,
|
||||
};
|
||||
use relm4::{factory::FactoryVecDeque, prelude::*, MessageBroker};
|
||||
use relm4::{factory::FactoryVecDeque, prelude::*};
|
||||
use relm4_components::web_image::WebImage;
|
||||
|
||||
use crate::{api, settings, util::get_web_image_msg};
|
||||
|
||||
use super::post_row::PostRow;
|
||||
|
||||
static COMMUNITY_PAGE_BROKER: MessageBroker<DialogMsg> = MessageBroker::new();
|
||||
|
||||
pub struct CommunityPage {
|
||||
info: CommunityView,
|
||||
avatar: Controller<WebImage>,
|
||||
|
@ -149,7 +147,7 @@ impl SimpleComponent for CommunityPage {
|
|||
|
||||
let dialog = EditorDialog::builder()
|
||||
.transient_for(root)
|
||||
.launch_with_broker(EditorType::Post, &COMMUNITY_PAGE_BROKER)
|
||||
.launch(EditorType::Post)
|
||||
.forward(sender.input_sender(), |msg| match msg {
|
||||
EditorOutput::CreateRequest(post, _) => CommunityInput::CreatePostRequest(post),
|
||||
_ => CommunityInput::None,
|
||||
|
@ -198,7 +196,10 @@ impl SimpleComponent for CommunityPage {
|
|||
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) => {
|
||||
self.posts.guard().push_front(post);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ use lemmy_api_common::{
|
|||
lemmy_db_views::structs::{CommentView, PostView},
|
||||
post::GetPostResponse,
|
||||
};
|
||||
use relm4::{factory::FactoryVecDeque, prelude::*, MessageBroker};
|
||||
use relm4::{factory::FactoryVecDeque, prelude::*};
|
||||
use relm4_components::web_image::WebImage;
|
||||
|
||||
use crate::{
|
||||
|
@ -18,8 +18,6 @@ use super::{
|
|||
voting_row::{VotingRowInput, VotingRowModel, VotingStats},
|
||||
};
|
||||
|
||||
pub static POST_PAGE_BROKER: MessageBroker<DialogMsg> = MessageBroker::new();
|
||||
|
||||
pub struct PostPage {
|
||||
info: GetPostResponse,
|
||||
image: Controller<WebImage>,
|
||||
|
@ -43,12 +41,9 @@ pub enum PostInput {
|
|||
EditPostRequest(EditorData),
|
||||
CreatedComment(CommentView),
|
||||
OpenEditPostDialog,
|
||||
OpenEditCommentDialog(EditorData),
|
||||
DeletePost,
|
||||
DoneEditPost(PostView),
|
||||
PassAppMessage(crate::AppMsg),
|
||||
EditCommentRequest(EditorData),
|
||||
UpdateComment(CommentView),
|
||||
}
|
||||
|
||||
#[relm4::component(pub)]
|
||||
|
@ -206,14 +201,10 @@ impl SimpleComponent for PostPage {
|
|||
let community_avatar = WebImage::builder().launch("".to_string()).detach();
|
||||
let dialog = EditorDialog::builder()
|
||||
.transient_for(root)
|
||||
.launch_with_broker(EditorType::Comment, &POST_PAGE_BROKER)
|
||||
.launch(EditorType::Comment)
|
||||
.forward(sender.input_sender(), |msg| match msg {
|
||||
EditorOutput::CreateRequest(comment, _) => PostInput::CreateCommentRequest(comment),
|
||||
EditorOutput::EditRequest(post, type_) => match type_ {
|
||||
EditorType::Post => PostInput::EditPostRequest(post),
|
||||
EditorType::Comment => PostInput::EditCommentRequest(post),
|
||||
_ => unreachable!(),
|
||||
},
|
||||
EditorOutput::EditRequest(post, _) => PostInput::EditPostRequest(post),
|
||||
});
|
||||
let voting_row = VotingRowModel::builder()
|
||||
.launch(VotingStats::default())
|
||||
|
@ -295,8 +286,9 @@ impl SimpleComponent for PostPage {
|
|||
gtk::show_uri(None::<&relm4::gtk::Window>, &link, 0);
|
||||
}
|
||||
PostInput::OpenCreateCommentDialog => {
|
||||
POST_PAGE_BROKER.send(DialogMsg::UpdateType(EditorType::Comment, true));
|
||||
POST_PAGE_BROKER.send(DialogMsg::Show);
|
||||
let sender = self.create_comment_dialog.sender();
|
||||
sender.emit(DialogMsg::UpdateType(EditorType::Comment, true));
|
||||
sender.emit(DialogMsg::Show);
|
||||
}
|
||||
PostInput::CreatedComment(comment) => {
|
||||
self.comments.guard().push_front(comment);
|
||||
|
@ -340,9 +332,10 @@ impl SimpleComponent for PostPage {
|
|||
url: reqwest::Url::parse(&url).ok(),
|
||||
id: None,
|
||||
};
|
||||
POST_PAGE_BROKER.send(DialogMsg::UpdateData(data));
|
||||
POST_PAGE_BROKER.send(DialogMsg::UpdateType(EditorType::Post, false));
|
||||
POST_PAGE_BROKER.send(DialogMsg::Show)
|
||||
let sender = self.create_comment_dialog.sender();
|
||||
sender.emit(DialogMsg::UpdateData(data));
|
||||
sender.emit(DialogMsg::UpdateType(EditorType::Post, false));
|
||||
sender.emit(DialogMsg::Show);
|
||||
}
|
||||
PostInput::EditPostRequest(post) => {
|
||||
let id = self.info.post_view.post.id.0;
|
||||
|
@ -362,36 +355,6 @@ impl SimpleComponent for PostPage {
|
|||
PostInput::DoneEditPost(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) => {
|
||||
let _ = sender.output(message);
|
||||
}
|
||||
|
|
|
@ -90,7 +90,7 @@ impl FactoryComponent for PostRow {
|
|||
add_css_class: "font-bold",
|
||||
add_controller = gtk::GestureClick {
|
||||
connect_pressed[sender] => move |_, _, _, _| {
|
||||
sender.input(PostRowMsg::OpenCommunity);
|
||||
sender.input(PostRowMsg::OpenPost);
|
||||
}
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue