From 202ba81b4f788f29fb4f1e41fad279a894a2e5f0 Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Wed, 3 Mar 2021 11:46:39 +0100 Subject: [PATCH] widget:policycheck: introduce message passing Signed-off-by: Ralf Zerres --- .../widgets/policycheck/policycheck_state.rs | 121 +++++++++++++----- .../widgets/policycheck/policycheck_view.rs | 51 ++++++-- 2 files changed, 132 insertions(+), 40 deletions(-) diff --git a/advotracker/src/widgets/policycheck/policycheck_state.rs b/advotracker/src/widgets/policycheck/policycheck_state.rs index b478d52..0d4731f 100644 --- a/advotracker/src/widgets/policycheck/policycheck_state.rs +++ b/advotracker/src/widgets/policycheck/policycheck_state.rs @@ -25,14 +25,17 @@ use crate::{ services::imports::allianzdirectcall, }; -/// Valid `actions` that are handled as state changes in the `Policycheck` widget. -#[derive(Debug, Clone, Copy)] +/// Enumeration of valid `action variants` that need to be handled as +/// state changes for the `PolicycheckView` widget. +#[derive(Debug, Clone)] pub enum PolicycheckAction { ClearEntry(Entity), ChangeTheme(), + SendPolicynumber(), InputTextChanged(Entity), ImportData, - ParseEntry(Entity), + NewTicket, + ParsePolicyNumber(Entity), RemoveFocus(Entity), RemovePopup(Entity), ResetProgress, @@ -43,6 +46,7 @@ pub enum PolicycheckAction { SetEntry(Entity), SetVisibility(Entity), TextChanged(Entity, usize), + UpdatePolicyNumber(String), UpdateProgress(f64) } @@ -58,16 +62,20 @@ struct Environment { /// Valid `structures` that are handled inside the state of the `Policycheck` widget. #[derive(AsAny, Default)] pub struct PolicycheckState { - action: Option, + actions: Vec, + button_menu: Entity, duration: Duration, label_result: Entity, lang: String, - button_menu: Entity, policy_data_count: u64, + policy_number: Entity, policy_numbers: HashMap, progress_bar: Entity, progress_count: f64, - progress_popup: Entity + progress_popup: Entity, + // target that recieves messages + target: Entity, + ticketdata: Entity } impl GlobalState for PolicycheckState {} @@ -168,8 +176,16 @@ impl PolicycheckState { Ok(()) } + /// Create new ticket + pub fn new_ticket(&mut self, ctx: &mut Context<'_>) { + println!("WIP: new ticket."); + self.navigate(self.ticketdata, ctx); + //ctx.widget().get_mut::(0). + //ctx.get_widget(self.ticketdata_view); + } + /// Parse validity of the given policy number. - fn parse_entry(&mut self, policy_check_policy_number: Entity, + pub fn parse_entry(&mut self, policy_check_policy_number: Entity, ctx: &mut Context<'_>) { trace!(target: "advotracker", parse_entry = "started"); @@ -308,6 +324,11 @@ impl PolicycheckState { trace!(target: "advotracker", parse_entry = "finished"); } + /// parse message 'ParseEntry' + pub fn parse_policy_number(&mut self, entity: Entity) { + self.actions.push(PolicycheckAction::ParsePolicyNumber(entity)); + } + /// Remove the popup box fn remove_popup(&mut self, id: Entity, ctx: &mut Context<'_>) { ctx.remove_child(self.progress_popup); @@ -327,9 +348,10 @@ impl PolicycheckState { // ctx.get_widget(self.policy_check_policy_number).update_theme_by_state(true); // } - /// Sets a new action. - pub fn set_action(&mut self, action: PolicycheckAction) { - self.action = action.into(); + /// Sending message 'UpdatePolicyNumber' + pub fn send_message_update_policynumber(&mut self) { + self.actions.push(PolicycheckAction::UpdatePolicyNumber("4711".to_string())); + //self.actions.push(PolicycheckAction::UpdatePolicyNumber(self.policy_number)); } /// Change status of given text box to edit mode. @@ -408,17 +430,27 @@ impl State for PolicycheckState { // Initialize required entities self.button_menu = ctx .entity_of_child(ID_POLICY_CHECK_BUTTON_MENU) - .expect("PolicycheckState.init: Can't find resource entity 'ID_POLICY_CHECK_BUTTON_MENU'."); + .expect("PolicycheckState::init: Can't find resource entity 'ID_POLICY_CHECK_BUTTON_MENU'."); self.label_result = ctx .entity_of_child(ID_POLICY_CHECK_LABEL_RESULT) - .expect("PolicycheckState.init: Can't find resource entity 'ID_POLICY_CHECK_LABEL_RESULT'."); + .expect("PolicycheckState::init: Can't find resource entity 'ID_POLICY_CHECK_LABEL_RESULT'."); TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT), Visibility::Collapsed); TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_RESULT), Visibility::Collapsed); TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_LABEL_HINT), Visibility::Collapsed); TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_HINT), Visibility::Collapsed); + //self.policy_number = Entity::from(ctx.widget().try_clone::(ID_POLICY_CHECK_POLICY_NUMBER) + // .expect("PolicycheckState::init(): Can't find resource entity 'ID_POLICY_CHECK_POLICY_NUMBER'.")); + + self.target = Entity::from(ctx.widget().try_clone::("target") + .expect("PolicycheckState::init(): Can't find resource entity 'target'.")); + + //self.ticketdata_view = (*ctx.widget().get::("ticketdata_view")).into(); + //self.ticketdata = Entity::from(ctx.widget().try_clone::(ID_TICKET_DATA_VIEW) + // .expect("PolicycheckState::init(): Can't find resource entity 'ticketdata'.")); + // // Load the saved data from a file in 'ron' format into our data structure. // // The cargo package identifier (default: 'nwx.advotracker') is used as the // // app directory name. The directory location is OS dependant @@ -445,19 +477,24 @@ impl State for PolicycheckState { ctx: &mut Context<'_>, ) { for message in messages.read::() { - if let PolicycheckAction::UpdateProgress(increment) = message { - println!("Message received"); - let old_width = ProgressBar::val_clone(&ctx.child(ID_POLICY_CHECK_PROGRESS_BAR)); + match message { + PolicycheckAction::UpdateProgress(increment) => { + let old_width = ProgressBar::val_clone(&ctx.child(ID_POLICY_CHECK_PROGRESS_BAR)); - let new_width = old_width + increment; - // Set the ProgressBar's val property to the calculated percentage - // (whereas 0.0 means 0%, and 1.0 means 100%) - if new_width <= 1. { - ProgressBar::val_set(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR), new_width); - } else { - ProgressBar::val_set(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR), 1.); - } - } + let new_width = old_width + increment; + // Set the ProgressBar's val property to the calculated percentage + // (whereas 0.0 means 0%, and 1.0 means 100%) + if new_width <= 1. { + ProgressBar::val_set(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR), new_width); + } else { + ProgressBar::val_set(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR), 1.); + } + } + PolicycheckAction::NewTicket => { + self.new_ticket(ctx); + } + _ => { println!("TicketdataAction: action not implemented!"); } + } } } @@ -473,7 +510,10 @@ impl State for PolicycheckState { // } //} - if let Some(action) = self.action { + // Create `actions`, a drained iterator (".." => full range that clears the vector) + let actions: Vec = self.actions.drain(..).collect(); + + for action in actions { match action { PolicycheckAction::ClearEntry(policy_check_policy_number) => { ctx.get_widget(policy_check_policy_number).set("enabled", false); @@ -492,7 +532,10 @@ impl State for PolicycheckState { } } } - PolicycheckAction::ParseEntry(text_box) => { + PolicycheckAction::NewTicket => { + self.new_ticket(ctx); + } + PolicycheckAction::ParsePolicyNumber(text_box) => { self.parse_entry(text_box, ctx); } PolicycheckAction::RemoveFocus(policy_check_policy_number) => { @@ -524,11 +567,27 @@ impl State for PolicycheckState { PolicycheckAction::TextChanged(entity, _index) => { self.set_entry(entity, ctx); } + PolicycheckAction::UpdatePolicyNumber(policy_number) => { + info!("Message send: 'PolicycheckAction::UpdatePolicyNumber({:?}) -> {:?}'", policy_number, self.target); + println!("WIP: send policy_number"); + ctx.send_message(PolicycheckAction::UpdatePolicyNumber(policy_number), self.target); + + //let mut widget_container : WidgetContainer = ctx.widget(); + //widget_container.get::("text"); + //let polnum: TextBox = TextBox::get(ctx.child(ID_POLICY_CHECK_POLICY_NUMBER)); + //println!("Policy number: {:?}", polnum); + + //let policy_number = ctx.get_widget(self.policy_check_policy_number); + //let policy_number2 = TextBox::text_clone(&ctx.get_widget(ID_POLICY_CHECK_POLICY_NUMBER)); + + //let policy_number_string = ctx.get_widget(entity).get::("text").is_empty(); + //let policy_number = TextBox::text_clone(&mut ctx.child(ID_POLICY_CHECK_POLICY_NUMBER)); + //ctx.send_message(PolicycheckAction::UpdatePolicyNumber(policy_check_policy_number), self.target); + //info!("Message send: 'PolicycheckAction::UpdatePolicyNumber({:?}) -> {:?}'", policy_check_policy_number, self.target); + } _ => (), } } - // Reset action - self.action = None; } // /// Update the view after the layout is rendered. @@ -546,10 +605,10 @@ fn create_popup_progress(id: Entity, ctx: &mut BuildContext<'_>) -> Entity { .width(280) .height(100) .visibility(Visibility::Visible) - .on_click(move |ctx, _| { + .on_click(move |_ctx, _| { println!("create_popup_progress: on_click -> remove_popup(popup_progress)"); - ctx.get_mut::(id) - .set_action(PolicycheckAction::RemovePopup(id)); + //ctx.get_mut::(id) + // .set_action(PolicycheckAction::RemovePopup(id)); true }) .child( diff --git a/advotracker/src/widgets/policycheck/policycheck_view.rs b/advotracker/src/widgets/policycheck/policycheck_view.rs index 08b1854..fa2be22 100644 --- a/advotracker/src/widgets/policycheck/policycheck_view.rs +++ b/advotracker/src/widgets/policycheck/policycheck_view.rs @@ -23,10 +23,16 @@ widget!( /// This identifier is checked agains a map of valid policy codes. // PolicycheckView: KeyDownHandler { PolicycheckView { + // holds the language code lang: String, + // provides a struct with `PolicyCheck` members policy_check: PolicyCheck, + // holds number of imported data + policy_data_count: u32, + // holds the title string policy_check_title: String, - policy_data_count: u32 + // widget entity that will receive the message + target: u32 } ); @@ -113,6 +119,34 @@ impl Template for PolicycheckView { .child(policy_check_button_menu) .build(ctx); + let policy_check_form_action = Container::new() + .id(ID_POLICY_CHECK_ACTION_GRID) + .attach(Grid::row(3)) + .attach(Grid::column(1)) + //.style(STYLE_CONTAINER_ACTION) + .padding(14) + .h_align("center") + .child( + Stack::new() + //.style(STYLE_STACK_ACTION) + .orientation("horizontal") + .spacing(50) + .child( + Button::new() + .id(ID_POLICY_CHECK_ACTION_BUTTON_CREATE) + .style(STYLE_BUTTON_ACTION) + .text("Create ticket") + .on_click(move |states, _entity| { + states.get_mut::(id).send_message_update_policynumber(); + states.send_message(PolicycheckAction::NewTicket, id); + false + }) + .build(ctx), + ) + .build(ctx), + ) + .build(ctx); + let policy_check_form = Container::new() .id(ID_POLICY_CHECK_FORM) .name(ID_POLICY_CHECK_FORM) @@ -161,11 +195,9 @@ impl Template for PolicycheckView { //.lose_focus_on_activation(false) //WIP: localization for water_mark .water_mark("10-stellig") - .on_activate(move |ctx, entity| { + .on_activate(move |states, entity| { // Entity is entered/activated via Mouse/Keyboard - //ctx.get_mut::(policy_check_view) - ctx.get_mut::(id) - .set_action(PolicycheckAction::ParseEntry(entity)); + states.get_mut::(id).parse_policy_number(entity); }) .on_key_down(move |_, key_event| { if key_event.key == Key::A(true) { @@ -276,10 +308,11 @@ impl Template for PolicycheckView { .push("auto") // Bottom_Bar ) - .child(policy_check_header_bar) // row 0 - .child(policy_check_form) // row 2 - .child(policy_data_stack) // row 3 - .child(policy_check_bottom_bar) // row 4 + .child(policy_check_header_bar) // row 0 + .child(policy_check_form) // row 2 + .child(policy_data_stack) // row 3 + .child(policy_check_form_action) // row 4 + .child(policy_check_bottom_bar) // row 5 .build(ctx), ) }