From 6907c0c393e3fb839b7dbd8bad507bbd702e8a30 Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Tue, 14 Jul 2020 11:18:02 +0200 Subject: [PATCH] callbacks: progressing I18n handling Signed-off-by: Ralf Zerres --- .../src/callbacks/policycheck_state.rs | 112 +++++++++++++----- 1 file changed, 81 insertions(+), 31 deletions(-) diff --git a/advotracker/src/callbacks/policycheck_state.rs b/advotracker/src/callbacks/policycheck_state.rs index b8836ea..5462c03 100644 --- a/advotracker/src/callbacks/policycheck_state.rs +++ b/advotracker/src/callbacks/policycheck_state.rs @@ -176,8 +176,10 @@ impl PolicyCheckState { /// Parse validity of the given policy number. fn parse_entry(&mut self, policy_check_policy_number: Entity, ctx: &mut Context<'_>) { - let policy_string = ctx.get_widget(policy_check_policy_number).get::("text").as_string(); - let policy_number_length = policy_string.len(); + trace!(target: "advotracker", parse_entry = "started"); + + let policy_number_string = ctx.get_widget(policy_check_policy_number).get::("text").as_string(); + let policy_number_length = policy_number_string.len(); // WIP: redundant lang selection (already in main!) let mut lang = env::var("lang").unwrap_or("en".to_string()); @@ -216,20 +218,18 @@ impl PolicyCheckState { } } - - trace!(target: "advotracker", state = "parsing", policy_number = ?policy_string); + trace!(target: "advotracker", state = "parsing", policy_number = ?policy_number_string); // Parse policy code: "AS-123456789" // DION VERS POLLFNR // 1 AS 1515735810 text_block(ctx.child("policy_check_label_result")).set_visibility(Visibility::Collapsed); - text_block(ctx.child("policy_check_label_result")).set_text("Prüfungsergebnis:"); button(ctx.child("policy_check_button_result")).set_visibility(Visibility::Collapsed); button(ctx.child("policy_check_button_result")).set_background("transparent"); if policy_number_length == 10 { - // cast policy_string to - match policy_string.parse::() { + // cast policy_number_sting to + match policy_number_string.parse::() { Ok(p) => { let mut result_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result")); result_wrapper.set_text(""); @@ -245,7 +245,8 @@ impl PolicyCheckState { result_wrapper.set_text(string_result); button(ctx.child("policy_check_button_result")).set_icon(material_icons_font::MD_CHECK); button(ctx.child("policy_check_button_result")).set_icon_brush("#008000"); - button(ctx.child("policy_check_button_result")).set_text("gültig!"); + let res = t!("policy.validation.button_success", lang); + button(ctx.child("policy_check_button_result")).set_text(res); button(ctx.child("policy_check_button_result")).set_visibility(Visibility::Visible); button(ctx.child("policy_check_button_result")).set_foreground("#008000"); button(ctx.child("policy_check_button_result")).set_background("transparent"); @@ -257,7 +258,8 @@ impl PolicyCheckState { trace!(target: "advotracker", state = ?res, policy_number = ?p); button(ctx.child("policy_check_button_result")).set_icon(material_icons_font::MD_CLEAR); button(ctx.child("policy_check_button_result")).set_icon_brush("#FF0000"); - button(ctx.child("policy_check_button_result")).set_text("ungültig!"); + let res = t!("policy.validation.button_failed", lang); + button(ctx.child("policy_check_button_result")).set_text(res); button(ctx.child("policy_check_button_result")).set_visibility(Visibility::Visible); button(ctx.child("policy_check_button_result")).set_foreground("#FF0000"); let mut text_block_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result")); @@ -271,7 +273,6 @@ impl PolicyCheckState { trace!(target: "advotracker", state = "error", error_type = "invalid type", error = ?e); button(ctx.child("policy_check_button_result")).set_icon(material_icons_font::MD_CLEAR); button(ctx.child("policy_check_button_result")).set_icon_brush("#FF0000"); - button(ctx.child("policy_check_button_result")).set_text("ungültig!"); button(ctx.child("policy_check_button_result")).set_visibility(Visibility::Visible); button(ctx.child("policy_check_button_result")).set_foreground("#FF0000"); text_block(ctx.child("policy_check_label_result")).set_visibility(Visibility::Visible); @@ -279,7 +280,6 @@ impl PolicyCheckState { let res = t!("policy.validation.invalid_input", lang); text_block_wrapper.set_text(res); text_block_wrapper.set_enabled(true); - text_block_wrapper.set_text("Nur Nummern sind zulässig!"); } } } @@ -299,21 +299,25 @@ impl PolicyCheckState { let res = t!("policy.validation.to_long", lang); text_block_wrapper.set_text(res); text_block(ctx.child("policy_check_label_result")).set_visibility(Visibility::Visible); - //self.set_visibility(policy_check_policy_number, ctx); } + + trace!(target: "advotracker", parse_entry = "finished"); } /// Remove a popup box showing the data import status in a progress bar - fn remove_progress_box(&mut self, ctx: &mut Context<'_>) { - if let Some(progress_box) = self.progress_box { - ctx.remove_child(progress_box); - println!("ProgressBox removed !"); - } + fn remove_progress_popup(&mut self, ctx: &mut Context<'_>) { + text_block(ctx.child("policy_check_label_result")).set_visibility(Visibility::Collapsed); + //text_block(ctx.child("policy_check_label_result")).set_visibility(Visibility::Visible); + //if let progress_box = self.progress_box { + // ctx.remove_child(progress_box); + // println!("ProgressPopup removed !"); + //} } - /// If TextBox 'policy_check_policy_number' is empty, disable button "clear" - /// otherwise enabled it. - // fn set_policy_check_clear_button(&self, policy_check_policy_number: Entity, ctx: &mut Context<' >) { + // /// If TextBox 'policy_check_policy_number' is empty, disable button "clear" + // /// otherwise enabled it. + // fn set_policy_check_clear(&mut self, policy_check_policy_number: Entity, ctx: &mut Context<'_>) { + // button(&self, policy_check_policy_number: Entity, ctx: &mut Context<' >) { // if ctx.get_widget(clear_button).get::("policy_check_policy_number").is_empty() { // ctx.get_widget(self.policy_check_clear_button).set("enabled", false); // } else { @@ -389,21 +393,56 @@ impl State for PolicyCheckState { trace!(target: "advotracker", policycheck_state = "init", status = "started"); + // Entities self.menu_button = ctx .entity_of_child(ID_POLICY_CHECK_BUTTON_MENU) - .expect("PolicyCheckState.init: Can't find entity of resource '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 entity of resource 'ID_POLICY_CHECK_LABEL_RESULT'."); + .expect("PolicyCheckState.init: Can't find resource entity 'ID_POLICY_CHECK_LABEL_RESULT'."); - let string_result = "Prüfungsergebnis:".to_string(); - text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_text(string_result); + // self.progress_bar = ctx + // .entity_of_child(ID_POLICY_CHECK_PROGRESS_BAR) + // .expect("PolicyCheckState.init: Can't find resource entity 'ID_POLICY_CHECK_PROGRESS_BAR'."); + + // self.progress_popup = ctx + // .entity_of_child(ID_POLICY_CHECK_PROGRESS_POPUP) + // .expect("PolicyCheckState.init: Can't find resource entity 'ID_POLICY_CHECK_PROGRESS_POPUP'."); + + // WIP: redundant lang selection (already in main!) + let mut lang = env::var("lang").unwrap_or("en".to_string()); + // testing environment: read from .env file + dotenv().ok(); + match envy::from_env::() { + Ok(environment) => { + if environment.test_lang != lang { lang = environment.test_lang; } + }, + Err(e) => { debug!(target: "advotracker", "{}", e); } + } + + // Constants + let res = t!("policy.string.header", lang); + text_block(ctx.child(ID_POLICY_CHECK_HEADER)).set_text(res); + + let res = t!("policy.string.label_policy_number", lang); + let string_label_policy_number = format!("{}:", res); + text_block(ctx.child(ID_POLICY_CHECK_LABEL_POLICY_NUMBER)).set_text(string_label_policy_number); + + let res = t!("policy.string.label_result", lang); + let string_label_result = format!("{}:", res); + text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_text(string_label_result); text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_visibility(Visibility::Collapsed); - // self.progress_box = ctx - // .entity_of_child(ID_POLICY_CHECK_PROGRESS_BOX) - // .expect("PolicyCheckState.init: Can't find entity of resource 'ID_POLICY_CHECK_PROGRESS_BOX'."); + let res = t!("policy.string.progress_text", lang); + let string_progress_text = format!("{}: {:?}", res, self.policy_numbers.len()); + //text_block(ctx.child(ID_POLICY_CHECK_PROGRESS_TEXT)).set_text(string_progress_text); + + let string_pro_text = format!("{}: {:?}", res, self.policy_numbers.len()); + + // self.progress_popup = ctx + // .entity_of_child(ID_POLICY_CHECK_PROGRESS_POPUP) + // .expect("PolicyCheckState.init: Can't find entity of resource 'ID_POLICY_CHECK_PROGRESS_POPUP'."); // // 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 @@ -516,21 +555,32 @@ impl State for PolicyCheckState { } } } + // Reset action self.action = None; } /// Update the view after the layout is rendered. fn update_post_layout(&mut self, _: &mut Registry, ctx: &mut Context<'_>) { - let mut string_data_count = "Prüflisten-Elemente:".to_string(); - string_data_count = format!("{} {:?}", string_data_count, self.policy_numbers.len()); + // WIP: redundant lang selection (already in main!) + let mut lang = env::var("lang").unwrap_or("en".to_string()); + // testing environment: read from .env file + dotenv().ok(); + match envy::from_env::() { + Ok(environment) => { + if environment.test_lang != lang { lang = environment.test_lang; } + }, + Err(e) => { debug!(target: "advotracker", "{}", e); } + } + let res = t!("policy.string.data_count", lang); + let string_data_count = format!("{}: {:?}", res, self.policy_numbers.len()); text_block(ctx.child(ID_POLICY_DATA_COUNT_BLOCK)).set_text(string_data_count); } } -fn new_progress_box(target: Entity, text: &str, ctx: &mut BuildContext<'_>) -> Entity { +fn new_progress_popup(target: Entity, text: &str, ctx: &mut BuildContext<'_>) -> Entity { Popup::new() - .id(ID_POLICY_CHECK_PROGRESS_BOX) + .id(ID_POLICY_CHECK_PROGRESS_POPUP) .target(target) .open(true) .width(280.0)