From 2f157d595776fd0438cf572c15089f1cbd91c7a2 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Wed, 30 Aug 2023 04:33:36 +1000 Subject: [PATCH] implement ChatOp::Dialogue::Save --- apps/mtxchat/src/lib.rs | 5 +++++ libs/chat/src/api.rs | 4 +++- libs/chat/src/lib.rs | 8 ++++++++ libs/chat/src/ui.rs | 10 ++-------- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/apps/mtxchat/src/lib.rs b/apps/mtxchat/src/lib.rs index 3cb2e5e61..dba6bd483 100644 --- a/apps/mtxchat/src/lib.rs +++ b/apps/mtxchat/src/lib.rs @@ -530,6 +530,11 @@ impl<'a> MtxChat<'a> { .expect("failed to convert post into buffer"); } } + // trigger the chat ui to save the dialogue to the pddb + xous::send_message( + chat_cid, + xous::Message::new_scalar(ChatOp::DialogueSave as usize, 0, 0, 0, 0), + ) } }); } diff --git a/libs/chat/src/api.rs b/libs/chat/src/api.rs index 1dfbbdf81..c3583e092 100644 --- a/libs/chat/src/api.rs +++ b/libs/chat/src/api.rs @@ -10,8 +10,10 @@ pub const POST_SELECTED_PREV: usize = usize::MAX - 1; #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] pub enum ChatOp { + // Sve the Dialogue to pddb (ie after PostAdd, PostDelete) + DialogueSave = 0, /// - DialogueSet = 0, + DialogueSet, /// GamChangeFocus, /// a line of text has arrived diff --git a/libs/chat/src/lib.rs b/libs/chat/src/lib.rs index 3e9ae6a64..ca4fb3abb 100644 --- a/libs/chat/src/lib.rs +++ b/libs/chat/src/lib.rs @@ -182,6 +182,14 @@ pub fn server( let msg = xous::receive_message(sid).unwrap(); log::debug!("got message {:?}", msg); match FromPrimitive::from_usize(msg.body.id()) { + Some(ChatOp::DialogueSave) => { + log::warn!("ChatOp::DialogueSave"); + ui.dialogue_save().expect("failed to save Dialogue"); + ui.dialogue_read().expect("failed to read Dialogue"); + if allow_redraw { + ui.redraw().expect("CHAT couldn't redraw"); + } + } Some(ChatOp::DialogueSet) => { log::info!("ChatOp::DialogueSet"); let buffer = diff --git a/libs/chat/src/ui.rs b/libs/chat/src/ui.rs index 809bb5925..b01ba73dd 100644 --- a/libs/chat/src/ui.rs +++ b/libs/chat/src/ui.rs @@ -145,7 +145,7 @@ impl Ui { } } - fn dialogue_read(&mut self) -> Result<(), Error> { + pub fn dialogue_read(&mut self) -> Result<(), Error> { match (&self.pddb_dict, &self.pddb_key) { (Some(dict), Some(key)) => { match self @@ -200,7 +200,7 @@ impl Ui { } } - fn dialogue_save(&self) -> Result<(), Error> { + pub fn dialogue_save(&self) -> Result<(), Error> { match (&self.dialogue, &self.pddb_dict, &self.pddb_key) { (Some(dialogue), Some(dict), Some(key)) => { let hint = Some(dialogue::MAX_BYTES + 2); @@ -314,11 +314,6 @@ impl Ui { dialogue .post_add(author, timestamp, text, attach_url) .unwrap(); - match self.dialogue_save() { - Ok(_) => log::info!("Dialogue saved"), - Err(e) => log::warn!("Failed to save Dialogue: {e}"), - } - self.redraw().expect("failed chat ui redraw"); } None => log::warn!("no Dialogue available to add Post"), } @@ -517,7 +512,6 @@ impl Ui { // error: if Dialogue == None fn layout(&mut self) -> Result { self.clear_area(); - match (&self.dialogue, &self.post_anchor) { match (&self.dialogue, &self.post_anchor ) { (Some(dialogue), Some(post_anchor)) => { log::info!("redrawing dialogue: {}", dialogue.title);