From 8af49deb340fbdbbe8f5f6712076cd29319615e9 Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Wed, 3 Mar 2021 11:47:26 +0100 Subject: [PATCH] widget:ticketdata: introduce message passing Signed-off-by: Ralf Zerres --- .../widgets/ticketdata/ticketdata_state.rs | 90 ++++++++++++++----- .../src/widgets/ticketdata/ticketdata_view.rs | 26 +++--- 2 files changed, 86 insertions(+), 30 deletions(-) diff --git a/advotracker/src/widgets/ticketdata/ticketdata_state.rs b/advotracker/src/widgets/ticketdata/ticketdata_state.rs index 2302de5..f62d14f 100644 --- a/advotracker/src/widgets/ticketdata/ticketdata_state.rs +++ b/advotracker/src/widgets/ticketdata/ticketdata_state.rs @@ -12,17 +12,17 @@ use serde::Deserialize; //use std::process; //use std::collections::HashMap; use std::time::SystemTime; -//use tracing::{error, info, trace}; -use tracing::trace; +use tracing::{info, trace}; use crate::{ data::constants::*, widgets::global_state::GlobalState, //services::exports::sendticketdata, + widgets::policycheck::policycheck_state::{PolicycheckAction, PolicycheckState}, }; /// Valid `actions` that are handled as state changes in the `Ticketdata` widget. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] pub enum TicketdataAction { ClearEntry(Entity), /// Clear text in the form @@ -35,7 +35,18 @@ pub enum TicketdataAction { SetEntry(Entity), SendForm(), SetVisibility(Entity), - TextChanged(Entity, usize) + TextChanged(Entity, usize), + UpdatePolicyNumber(String) +} + +/// Handel fields of an Email (header, body) +#[derive(Clone, PartialEq)] +pub struct Email { + recipient_to: String, + recipient_cc: String, + sender: String, + subject: String, + body: String, } /// Define valid environment variables provided via .env files @@ -50,10 +61,11 @@ struct Environment { /// Valid `structures` that are handled inside the state of the `Ticket` widget. #[derive(AsAny, Default)] pub struct TicketdataState { - action: Option, + actions: Vec, + button_menu: Entity, //duration: Duration, lang: String, - button_menu: Entity + target: Entity } impl GlobalState for TicketdataState {} @@ -61,7 +73,7 @@ impl GlobalState for TicketdataState {} /// Method definitions, that react on any given state change inside the `Ticketdata` widget. impl TicketdataState { /// Clear text in text box. - pub fn clear_entry(ctx: &mut Context<'_>, entity: Entity) { + pub fn clear_entry(entity: Entity, ctx: &mut Context<'_>) { if let Some(count) = ctx.get_widget(entity).children_count() { println!("Widget name: {:?}", ctx.get_widget(entity).get::("name")); println!("Widget id: {:?}", ctx.get_widget(entity).get::("id")); @@ -79,23 +91,25 @@ impl TicketdataState { //println!("Text: {:?}", ctx.get_widget(entity).entity_of_child(entity)); //TextBlock::text_set(&mut ctx.child(ID_TICKET_DATA_POLICY_HOLDER), ""); } - } //TextBox::text_set(&mut ctx.widget(entity), String::from("")); } - /// Clear ticket data form - pub fn send_message(&mut self) { - println!("WIP: send_message"); - //self.action.push(TicketdataAction::ClearForm()); - //self.clear_entry(ticket_data_policy_holder); + pub fn send_form(entity: Entity, _ctx: &mut Context<'_>) { + info!("WIP: Sending form to construct eMail for {:?}", entity); } - /// Sets a new action. - pub fn set_action(&mut self, action: TicketdataAction) { - self.action = action.into(); + /// sending message 'ClearForm' + pub fn send_message_clear_form(&mut self) { + info!("Sending message 'TicketdataAction::ClearForm'"); + self.actions.push(TicketdataAction::ClearForm()); } + /// sending message 'SendForm' + pub fn send_message_send_form(&mut self) { + info!("Sending message 'TicketdataAction::SendForm'"); + self.actions.push(TicketdataAction::SendForm()); + } } /// Supported methods handled inside the `TicketState` @@ -111,6 +125,9 @@ impl State for TicketdataState { .entity_of_child(ID_TICKET_DATA_BUTTON_MENU) .expect("TicketState.init: Can't find resource entity 'ID_TICKET_DATA_BUTTON_MENU'."); + self.target = Entity::from(ctx.widget().try_clone::("target") + .expect("TicketState.init: Can't find resource entity 'target'.")); + // Get language from environment self.lang = TicketdataState::get_lang(); @@ -129,15 +146,48 @@ impl State for TicketdataState { for action in messages.read::() { match action { TicketdataAction::ClearForm() => { - println!("WIP: Message 'ClearForm' received"); - TicketdataState::clear_entry(ctx, ctx.entity()) - //TicketdataState::clear_entry(ctx, ctx.entity()); + println!("message: {:?} recieved", action); + info!("message: {:?} recieved", action); + //TicketdataState::clear_entry(ctx.entity(), ctx); + //ctx.clear_children_of(self.ticket_data_form); + } TicketdataAction::SendForm() => { - println!("WIP: Message 'SendForm' received"); + println!("message: {:?} recieved", action); + info!("message: {:?} recieved", action); + TicketdataState::send_form(ctx.entity(), ctx); + } + _ => { println!("messages: action not implemented!"); } + } + } + for action in messages.read::() { + match action { + PolicycheckAction::UpdatePolicyNumber(policy_number) => { + info!("Message received: 'PolicycheckAction::UpdatePolicyNumber({:?})'", policy_number); + //progress_bar.set::("val", current_progress + amount); + //TextBlock::text_set(&mut ctx.child(ID_TICKET_DATA_POLICY_NUMBER), "9999999992"); + TextBlock::text_set(&mut ctx.child(ID_TICKET_DATA_POLICY_NUMBER), policy_number); } _ => { println!("messages: action not implemented!"); } } } } + + fn update(&mut self, _: &mut Registry, ctx: &mut Context<'_>) { + let actions: Vec = self.actions.drain(..).collect(); + + for action in actions { + match action { + TicketdataAction::ClearForm() => { + info!("update: send_message {:?}", action); + ctx.send_message(TicketdataAction::ClearForm(), self.target); + } + TicketdataAction::SendForm() => { + //ctx.send_message(TicketdataAction::SendForm(), self.ID_TICKETDATA_FORM); + info!("update: send_message {:?}", action); + } + _ => { println!("TicketdataAction: action not implemented!"); } + } + } + } } diff --git a/advotracker/src/widgets/ticketdata/ticketdata_view.rs b/advotracker/src/widgets/ticketdata/ticketdata_view.rs index 7eb67d4..48088fe 100644 --- a/advotracker/src/widgets/ticketdata/ticketdata_view.rs +++ b/advotracker/src/widgets/ticketdata/ticketdata_view.rs @@ -12,16 +12,20 @@ use orbtk::{ use crate::{ data::constants::*, - widgets::ticketdata::ticketdata_state::{TicketdataAction, TicketdataState}, + //widgets::ticketdata::ticketdata_state::{TicketdataAction, TicketdataState}, + widgets::ticketdata::ticketdata_state::TicketdataState, }; // Macro that initializes the widget structures/variables for the policy check view widget!( /// Form to enter data of a ticket record TicketdataView { + // language used inside the widget lang: String, - policy_data_count: u32, - ticket_data_title: String + // title used in the header + ticket_data_title: String, + // entity id that will receive the messages + target: u32 } ); @@ -262,9 +266,10 @@ impl Template for TicketdataView { .style(STYLE_BUTTON_ACTION) .text("Clear") .on_click(move |states, _| { - println!("WIP: clear from"); - states.send_message(TicketdataAction::ClearForm, id); - true + //println!("WIP: clear from"); + //states.send_message(TicketdataAction::ClearForm, id); + states.get_mut::(id).send_message_clear_form(); + false }) .build(ctx), ) @@ -274,10 +279,11 @@ impl Template for TicketdataView { .style(STYLE_BUTTON_ACTION) .text("Send") //.visibility(Visibility::Collapsed) - .on_click(move |states, _| { - println!("WIP: send mail"); - states.send_message(TicketdataAction::SendForm, id); - true + .on_click(move |states, _entity| { + //println!("WIP: send mail"); + //states.get_mut::(id).send_message(TicketdataAction::SendForm, id); + states.get_mut::(id).send_message_send_form(); + false }) .build(ctx), )