extend chat::Dialogue to accept Option<pddb_key>

extend ui.dialogue_set() to select existing Dialogue when operating offline
This commit is contained in:
nworbnhoj 2023-08-10 20:22:15 +10:00
parent defac5ed7e
commit 62a3bae502
3 changed files with 32 additions and 24 deletions

View file

@ -5,10 +5,8 @@ pub(crate) const SERVER_NAME_CHAT: &str = "Chat UI";
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum ChatOp { pub enum ChatOp {
/// change focus
ChangeFocus,
/// ///
DialogueSet, DialogueSet = 128,
/// ///
GamChangeFocus, GamChangeFocus,
/// a line of text has arrived /// a line of text has arrived
@ -42,7 +40,7 @@ pub struct Find {
#[derive(Archive, Serialize, Deserialize, Debug)] #[derive(Archive, Serialize, Deserialize, Debug)]
pub struct Dialogue { pub struct Dialogue {
pub dict: xous_ipc::String<128>, pub dict: xous_ipc::String<128>,
pub key: xous_ipc::String<128>, pub key: Option<xous_ipc::String<128>>,
} }
#[derive(Archive, Serialize, Deserialize, Debug)] #[derive(Archive, Serialize, Deserialize, Debug)]

View file

@ -42,20 +42,18 @@ impl Chat {
self.cid self.cid
} }
pub fn read_only(pddb_dict: &str, pddb_key: &str) -> Self { pub fn read_only(pddb_dict: &str, pddb_key: Option<&str>) -> Self {
let chat = Chat::new(None, None, None, None); let chat = Chat::new("_Chat Read_", "unused", None, None, None, None);
chat.dialogue_set(pddb_dict, pddb_key).unwrap(); chat.dialogue_set(pddb_dict, pddb_key).unwrap();
chat chat
} }
// set the current Dialogue // set the current Dialogue
pub fn dialogue_set(&self, pddb_dict: &str, pddb_key: &str) -> Result<(), Error> { pub fn dialogue_set(&self, pddb_dict: &str, pddb_key: Option<&str>) -> Result<(), Error> {
let mut dialogue = api::Dialogue { let dialogue = api::Dialogue {
dict: xous_ipc::String::new(), dict: xous_ipc::String::from_str(pddb_dict),
key: xous_ipc::String::new(), key: pddb_key.map(|key| xous_ipc::String::from_str(key)),
}; };
dialogue.dict.append(pddb_dict).unwrap();
dialogue.key.append(pddb_key).unwrap();
match Buffer::into_buf(dialogue) { match Buffer::into_buf(dialogue) {
Ok(buf) => buf.send(self.cid, ChatOp::DialogueSet as u32).map(|_| ()), Ok(buf) => buf.send(self.cid, ChatOp::DialogueSet as u32).map(|_| ()),
Err(_) => Err(xous::Error::InternalError), Err(_) => Err(xous::Error::InternalError),
@ -171,13 +169,15 @@ pub fn server(
log::debug!("got message {:?}", msg); log::debug!("got message {:?}", msg);
match FromPrimitive::from_usize(msg.body.id()) { match FromPrimitive::from_usize(msg.body.id()) {
Some(ChatOp::DialogueSet) => { Some(ChatOp::DialogueSet) => {
log::info!("ChatOp::DialogueSet");
let buffer = let buffer =
unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let dialogue = buffer.to_original::<Dialogue, _>().unwrap(); let dialogue = buffer.to_original::<Dialogue, _>().unwrap();
ui.dialogue_set( let dialogue_key = match dialogue.key {
dialogue.dict.as_str().unwrap(), Some(key) => Some(key.to_string()),
dialogue.key.as_str().unwrap(), None => None,
); };
ui.dialogue_set(dialogue.dict.as_str().unwrap(), dialogue_key.as_deref());
} }
Some(ChatOp::GamChangeFocus) => { Some(ChatOp::GamChangeFocus) => {
xous::msg_scalar_unpack!(msg, new_state_code, _, _, _, { xous::msg_scalar_unpack!(msg, new_state_code, _, _, _, {

View file

@ -207,17 +207,27 @@ impl Ui {
} }
// set the current Dialogue // set the current Dialogue
pub fn dialogue_set(&mut self, pddb_dict: &str, pddb_key: &str) { pub fn dialogue_set(&mut self, pddb_dict: &str, pddb_key: Option<&str>) {
self.pddb_dict = Some(pddb_dict.to_string()); self.pddb_dict = Some(pddb_dict.to_string());
self.pddb_key = Some(pddb_key.to_string()); self.pddb_key = pddb_key.map(|key| key.to_string());
log::info!("Dialogue set to {}:{}", pddb_dict, pddb_key); if self.pddb_key.is_none() {
self.dialogue_modal();
}
log::info!("Dialogue set to {:?}:{:?}", self.pddb_dict, self.pddb_key);
match self.dialogue_read() { match self.dialogue_read() {
Ok(_) => (), Ok(_) => {
log::info!("read dialogue {:?}:{:?}", self.pddb_dict, self.pddb_key);
self.redraw().expect("couldn't redraw screen");
}
Err(_) => { Err(_) => {
self.dialogue = Some(Dialogue::new(pddb_key)); if let Some(key) = &self.pddb_key {
match self.dialogue_save() { self.dialogue = Some(Dialogue::new(&key));
Ok(_) => log::info!("Dialogue created {}:{}", pddb_dict, pddb_key), match self.dialogue_save() {
Err(e) => log::warn!("Failed to create Dialogue {}:{}", pddb_dict, pddb_key), Ok(_) => log::info!("Dialogue created {}:{}", pddb_dict, key),
Err(e) => {
log::warn!("Failed to create Dialogue {}:{} : {e}", pddb_dict, key)
}
}
} }
} }
} }