policycheck: i18n, tracing updates

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2020-07-09 00:41:01 +02:00
parent 947e79eea4
commit 8564aaaf8d
2 changed files with 67 additions and 56 deletions

View File

@@ -1,4 +1,4 @@
//use locales::t;
use locales::t;
use dotenv::dotenv;
use orbtk::prelude::*;
use serde::Deserialize;
@@ -31,7 +31,7 @@ pub enum Action {
#[derive(Debug, Deserialize)]
struct Environment {
test_lang: String,
log: String,
rust_log: String,
}
/// Handles the requests of the `PolicyCheckView`.
@@ -57,9 +57,10 @@ impl PolicyCheckState {
self.action = action.into();
}
/// Create a hashmap (key: policy number, value: policy type).
pub fn create_hashmap(&mut self) -> Result<(), Box<dyn std::error::Error>> {
// WIP: redundant lang selection (already in main!)
let mut lang = env::var("LANG").unwrap_or("en".to_string());
let mut lang = env::var("lang").unwrap_or("en".to_string());
// testing environment: read from .env file
dotenv().ok();
match envy::from_env::<Environment>() {
@@ -71,25 +72,25 @@ impl PolicyCheckState {
// importing policy code elements from csv-file
let policy_list = PolicyList::new("Allianz Versicherungsnummen-List");
println!("Policy List {:?} ", policy_list.name);
trace!(target: "advotracker", policy_list = ?policy_list);
let mut policy_data = PolicyDataList::new("Allianz-Import latest");
println!("Policy Data List {:?} ", policy_data.name);
trace!(target: "advotracker", policy_data = ?policy_data);
let mut policy_numbers : HashMap<usize, PolicyCode> = HashMap::new();
// Wip: use cli parameter stored in viperus ...
//let mut csv_import_path = v.get::<String>("import_file").unwrap();
let mut csv_import_path = String::from("POLLFNR_WOECHENTLICH.txt");
println!("Importing from: {:?}", csv_import_path);
match import(&mut csv_import_path, &mut policy_data,
&mut policy_numbers, &lang) {
Ok(count) => {
self.policy_data_count = count;
println!("Imported {:?} records", self.policy_data_count);
trace!(target: "advotracker", csv_import_path = ?csv_import_path,
policy_data_count = ?&self.policy_data_count);
}
Err(err) => {
println!("error running CSV-Import: {}", err);
error!("error running CSV-Import: {}", err);
process::exit(1);
}
};
@@ -102,21 +103,58 @@ impl PolicyCheckState {
pub fn clear_entry(&mut self, text_box: Entity, ctx: &mut Context<'_>) {
let mut text_box = TextBox::get(ctx.get_widget(text_box));
let text = text_box.text_mut();
println!("reset {}", text);
}
pub fn import_data(&mut self, text_block: Entity, ctx: &mut Context<'_>) { // import data
// WIP: for now, only import once per session
if self.policy_data_count == 0 {
match self.create_hashmap() {
Ok(()) => {
//let res = t!("policy.hashmap.success", lang);
//println!("{:?}", res);
info!("hashmap has: {:?} entries", self.policy_data_count);
trace!(target: "advotracker",
hashmap_status = "new import",
hashmap_entries = ?self.policy_data_count);
}
_ => {
// let res = t!("policy.hashmap.failed", lang);
// println
// !("{:?}", res);
error!("Creation of a hashmap failed!");
trace!(target: "advotracker", hashmap_status = "failed");
}
}
} else {
trace!(target: "advotracker",
hashmap_status = "consume",
hashmap_entries = ?self.policy_data_count);
}
}
/// Open menu.
pub fn open_menu(&mut self, text_block: Entity, ctx: &mut Context<'_>) {
let mut text_block = TextBlock::get(ctx.get_widget(text_block));
let text = text_block.text_mut();
println!("Menu text: {}", text);
}
/// 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::<String16>("text").as_string();
let policy_number_length = policy_string.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::<Environment>() {
Ok(environment) => {
if environment.test_lang != lang { lang = environment.test_lang; }
},
Err(e) => { debug!(target: "advotracker", "{}", e); }
}
trace!(target: "advotracker", state = "parsing", policy_number = ?policy_string);
// Parse policy code: "AS-123456789"
@@ -126,14 +164,15 @@ impl PolicyCheckState {
button(ctx.child("policy_check_button_result")).set_background("transparent");
if policy_number_length == 10 {
// needs to be an integer
// cast policy_string to <usize>
match policy_string.parse::<usize>() {
Ok(p) => {
let mut result_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
result_wrapper.set_text("");
// match hashmap's key
match self.policy_numbers.get(&p) {
// check hashmap value field
Some(policy_code) => {
// matching key, get associated value
trace!(target: "advotracker", state = "success",
policy_number = ?p, policy_code = ?policy_code);
result_wrapper.set_enabled(true);
@@ -148,10 +187,9 @@ impl PolicyCheckState {
button(ctx.child("policy_check_button_result")).set_background("transparent");
}
_ => {
//let res = t!("policy.validation.failed", lang);
//println!("{:?}", res);
trace!(target: "advotracker", state = "failed",
policy_number = ?p);
// no matching key
let res = t!("policy.validation.failed", lang);
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!");
@@ -178,13 +216,15 @@ impl PolicyCheckState {
//println!("Policy number is to short!");
let mut text_block_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
text_block_wrapper.set_enabled(true);
text_block_wrapper.set_text("zu kurz!");
let res = t!("policy.validation.to_short", lang);
text_block_wrapper.set_text(res);
}
if policy_number_length > 10 {
//println!("Policy number is to big!");
let mut text_block_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
text_block_wrapper.set_enabled(true);
text_block_wrapper.set_text("zu lang!");
let res = t!("policy.validation.to_long", lang);
text_block_wrapper.set_text(res);
}
}
@@ -211,7 +251,6 @@ impl PolicyCheckState {
text_box_wrapper.set_visibility( Visibility::Visible);
text_box_wrapper.set_enabled(true);
text_box_wrapper.set_text("");
text_box_wrapper.set_water_mark("Neue Eingabe ...");
return;
}
@@ -221,7 +260,7 @@ impl PolicyCheckState {
}
/// Change visibility of the result label
/// Change visibility of the result label.
fn set_visibility(&self, entity: Entity, ctx: &mut Context<'_>) {
if ctx.get_widget(entity).get::<String16>("text").is_empty() {
text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_visibility(Visibility::Visible);
@@ -237,7 +276,6 @@ impl PolicyCheckState {
// Update count of elements in the policy data list.
fn _update_data_count(&self, ctx: &mut Context<'_>) {
// old api syntax
let data_list_count = ctx.widget().get::<PolicyDataList>(PROP_POLICY_DATA_LIST).len();
ctx.widget().set(PROP_POLICY_DATA_COUNT, data_list_count);
}
@@ -245,38 +283,12 @@ impl PolicyCheckState {
/// upported states for our view
impl State for PolicyCheckState {
/// Initialize the widget state
fn init(&mut self, _: &mut Registry, ctx: &mut Context<'_>) {
self.menu_button = ctx
.entity_of_child(ID_POLICY_CHECK_BUTTON_MENU)
.expect("PolicyCheckState.init: Can't find entity id defined as resource 'ID_POLICY_CHECK_POLICY_MENU_BUTTON'.");
// import data
// WIP: for now, only import once per session
if self.policy_data_count == 0 {
match self.create_hashmap() {
Ok(()) => {
//let res = t!("policy.hashmap.success", lang);
//println!("{:?}", res);
info!("hashmap has: {:?} entries", self.policy_data_count);
trace!(target: "advotracker",
hashmap_status = "new import",
hashmap_entries = ?self.policy_data_count);
}
_ => {
// let res = t!("policy.hashmap.failed", lang);
// println
// !("{:?}", res);
error!("Creation of a hashmap failed!");
trace!(target: "advotracker", hashmap_status = "failed");
}
}
} else {
println!("Already imported {} records.", self.policy_data_count);
trace!(target: "advotracker",
hashmap_status = "consume",
hashmap_entries = ?self.policy_data_count);
}
// // 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
@@ -294,6 +306,7 @@ impl State for PolicyCheckState {
//self.update_data_count(ctx);
}
/// Update the widget state.
fn update(&mut self, _registry: &mut Registry, ctx: &mut Context<'_>) {
// clear focus on focus moved
if self.last_focused != ctx.window().get::<Global>("global").focused_widget {
@@ -334,7 +347,7 @@ impl State for PolicyCheckState {
}
Action::SetVisibility(entity) => {
//text_block(ctx.child(entity).set_visibility(Visibility::Visible));
println!("Entity: {:?}", entity);
//println!("Entity: {:?}", entity);
//text_block(ctx.child(entity).set_visibility(Visibility::Collapsed));
text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_visibility(Visibility::Collapsed);
}
@@ -346,6 +359,7 @@ impl State for PolicyCheckState {
self.action = None;
}
/// Update the view after the layout is rendered.
fn update_post_layout(&mut self, _: &mut Registry, ctx: &mut Context<'_>) {
let string_result = "Prüfungsergebnis:".to_string();
//string_result = format!("{} {:?}", string_result, self.??;

View File

@@ -49,7 +49,6 @@ impl Template for PolicyCheckView {
//.v_align("center")
//.enabled(true)
.on_mouse_down(|_, _| true)
//.child(policycheck_menu_container)
.on_click(move |ctx, _| {
ctx.get_mut::<PolicyCheckState>(id)
.action(Action::OpenMenu(policy_check_menu_text_block));
@@ -69,11 +68,11 @@ impl Template for PolicyCheckView {
let policy_data_count_block = TextBlock::new()
.id(ID_POLICY_DATA_COUNT_BLOCK)
//.class(CLASS_TEXT_BLOCK)
.attach(Grid::row(1))
.attach(Grid::row(3))
.attach(Grid::column(1))
.margin((0., 4., 0., 0.))
.h_align("end")
.v_align("end")
.v_align("top")
.enabled(true)
.build(ctx);
@@ -186,7 +185,6 @@ impl Template for PolicyCheckView {
.text("")
.on_activate(move |ctx, entity| {
// Entity is entered/activated via Mouse/Keyboard
println!("activation finished!");
ctx.get_mut::<PolicyCheckState>(id)
.action(Action::ParseEntry(entity));
})
@@ -234,7 +232,6 @@ impl Template for PolicyCheckView {
.child(
TextBlock::new()
.id(ID_POLICY_CHECK_RESULT)
//.id("policy_check_result")
.h_align("start")
.attach(Grid::row(1))
.attach(Grid::column(2))
@@ -347,9 +344,9 @@ impl Template for PolicyCheckView {
.rows(
Rows::new()
.add("auto")
.add("5.")
.add(28.)
.add("*")
.add("18.")
.add("auto")
.add("auto")
.build(),
)