policycheck: i18n, tracing updates
Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
//use locales::t;
|
use locales::t;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use orbtk::prelude::*;
|
use orbtk::prelude::*;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@@ -31,7 +31,7 @@ pub enum Action {
|
|||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct Environment {
|
struct Environment {
|
||||||
test_lang: String,
|
test_lang: String,
|
||||||
log: String,
|
rust_log: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles the requests of the `PolicyCheckView`.
|
/// Handles the requests of the `PolicyCheckView`.
|
||||||
@@ -57,9 +57,10 @@ impl PolicyCheckState {
|
|||||||
self.action = action.into();
|
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>> {
|
pub fn create_hashmap(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
// WIP: redundant lang selection (already in main!)
|
// 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
|
// testing environment: read from .env file
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
match envy::from_env::<Environment>() {
|
match envy::from_env::<Environment>() {
|
||||||
@@ -71,25 +72,25 @@ impl PolicyCheckState {
|
|||||||
|
|
||||||
// importing policy code elements from csv-file
|
// importing policy code elements from csv-file
|
||||||
let policy_list = PolicyList::new("Allianz Versicherungsnummen-List");
|
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");
|
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();
|
let mut policy_numbers : HashMap<usize, PolicyCode> = HashMap::new();
|
||||||
|
|
||||||
// Wip: use cli parameter stored in viperus ...
|
// Wip: use cli parameter stored in viperus ...
|
||||||
//let mut csv_import_path = v.get::<String>("import_file").unwrap();
|
//let mut csv_import_path = v.get::<String>("import_file").unwrap();
|
||||||
let mut csv_import_path = String::from("POLLFNR_WOECHENTLICH.txt");
|
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,
|
match import(&mut csv_import_path, &mut policy_data,
|
||||||
&mut policy_numbers, &lang) {
|
&mut policy_numbers, &lang) {
|
||||||
Ok(count) => {
|
Ok(count) => {
|
||||||
self.policy_data_count = 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) => {
|
Err(err) => {
|
||||||
println!("error running CSV-Import: {}", err);
|
error!("error running CSV-Import: {}", err);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -102,21 +103,58 @@ impl PolicyCheckState {
|
|||||||
pub fn clear_entry(&mut self, text_box: Entity, ctx: &mut Context<'_>) {
|
pub fn clear_entry(&mut self, text_box: Entity, ctx: &mut Context<'_>) {
|
||||||
let mut text_box = TextBox::get(ctx.get_widget(text_box));
|
let mut text_box = TextBox::get(ctx.get_widget(text_box));
|
||||||
let text = text_box.text_mut();
|
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.
|
/// Open menu.
|
||||||
pub fn open_menu(&mut self, text_block: Entity, ctx: &mut Context<'_>) {
|
pub fn open_menu(&mut self, text_block: Entity, ctx: &mut Context<'_>) {
|
||||||
let mut text_block = TextBlock::get(ctx.get_widget(text_block));
|
let mut text_block = TextBlock::get(ctx.get_widget(text_block));
|
||||||
let text = text_block.text_mut();
|
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,
|
fn parse_entry(&mut self, policy_check_policy_number: Entity,
|
||||||
ctx: &mut Context<'_>) {
|
ctx: &mut Context<'_>) {
|
||||||
let policy_string = ctx.get_widget(policy_check_policy_number).get::<String16>("text").as_string();
|
let policy_string = ctx.get_widget(policy_check_policy_number).get::<String16>("text").as_string();
|
||||||
let policy_number_length = policy_string.len();
|
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);
|
trace!(target: "advotracker", state = "parsing", policy_number = ?policy_string);
|
||||||
|
|
||||||
// Parse policy code: "AS-123456789"
|
// Parse policy code: "AS-123456789"
|
||||||
@@ -126,14 +164,15 @@ impl PolicyCheckState {
|
|||||||
button(ctx.child("policy_check_button_result")).set_background("transparent");
|
button(ctx.child("policy_check_button_result")).set_background("transparent");
|
||||||
|
|
||||||
if policy_number_length == 10 {
|
if policy_number_length == 10 {
|
||||||
// needs to be an integer
|
// cast policy_string to <usize>
|
||||||
match policy_string.parse::<usize>() {
|
match policy_string.parse::<usize>() {
|
||||||
Ok(p) => {
|
Ok(p) => {
|
||||||
let mut result_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
|
let mut result_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
|
||||||
result_wrapper.set_text("");
|
result_wrapper.set_text("");
|
||||||
|
// match hashmap's key
|
||||||
match self.policy_numbers.get(&p) {
|
match self.policy_numbers.get(&p) {
|
||||||
// check hashmap value field
|
|
||||||
Some(policy_code) => {
|
Some(policy_code) => {
|
||||||
|
// matching key, get associated value
|
||||||
trace!(target: "advotracker", state = "success",
|
trace!(target: "advotracker", state = "success",
|
||||||
policy_number = ?p, policy_code = ?policy_code);
|
policy_number = ?p, policy_code = ?policy_code);
|
||||||
result_wrapper.set_enabled(true);
|
result_wrapper.set_enabled(true);
|
||||||
@@ -148,10 +187,9 @@ impl PolicyCheckState {
|
|||||||
button(ctx.child("policy_check_button_result")).set_background("transparent");
|
button(ctx.child("policy_check_button_result")).set_background("transparent");
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
//let res = t!("policy.validation.failed", lang);
|
// no matching key
|
||||||
//println!("{:?}", res);
|
let res = t!("policy.validation.failed", lang);
|
||||||
trace!(target: "advotracker", state = "failed",
|
trace!(target: "advotracker", state = ?res, policy_number = ?p);
|
||||||
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(material_icons_font::MD_CLEAR);
|
||||||
button(ctx.child("policy_check_button_result")).set_icon_brush("#FF0000");
|
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_text("ungültig!");
|
||||||
@@ -178,13 +216,15 @@ impl PolicyCheckState {
|
|||||||
//println!("Policy number is to short!");
|
//println!("Policy number is to short!");
|
||||||
let mut text_block_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
|
let mut text_block_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
|
||||||
text_block_wrapper.set_enabled(true);
|
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 {
|
if policy_number_length > 10 {
|
||||||
//println!("Policy number is to big!");
|
//println!("Policy number is to big!");
|
||||||
let mut text_block_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
|
let mut text_block_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
|
||||||
text_block_wrapper.set_enabled(true);
|
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_visibility( Visibility::Visible);
|
||||||
text_box_wrapper.set_enabled(true);
|
text_box_wrapper.set_enabled(true);
|
||||||
text_box_wrapper.set_text("");
|
text_box_wrapper.set_text("");
|
||||||
text_box_wrapper.set_water_mark("Neue Eingabe ...");
|
|
||||||
return;
|
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<'_>) {
|
fn set_visibility(&self, entity: Entity, ctx: &mut Context<'_>) {
|
||||||
if ctx.get_widget(entity).get::<String16>("text").is_empty() {
|
if ctx.get_widget(entity).get::<String16>("text").is_empty() {
|
||||||
text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_visibility(Visibility::Visible);
|
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.
|
// Update count of elements in the policy data list.
|
||||||
fn _update_data_count(&self, ctx: &mut Context<'_>) {
|
fn _update_data_count(&self, ctx: &mut Context<'_>) {
|
||||||
// old api syntax
|
|
||||||
let data_list_count = ctx.widget().get::<PolicyDataList>(PROP_POLICY_DATA_LIST).len();
|
let data_list_count = ctx.widget().get::<PolicyDataList>(PROP_POLICY_DATA_LIST).len();
|
||||||
ctx.widget().set(PROP_POLICY_DATA_COUNT, data_list_count);
|
ctx.widget().set(PROP_POLICY_DATA_COUNT, data_list_count);
|
||||||
}
|
}
|
||||||
@@ -245,38 +283,12 @@ impl PolicyCheckState {
|
|||||||
|
|
||||||
/// upported states for our view
|
/// upported states for our view
|
||||||
impl State for PolicyCheckState {
|
impl State for PolicyCheckState {
|
||||||
|
/// Initialize the widget state
|
||||||
fn init(&mut self, _: &mut Registry, ctx: &mut Context<'_>) {
|
fn init(&mut self, _: &mut Registry, ctx: &mut Context<'_>) {
|
||||||
self.menu_button = ctx
|
self.menu_button = ctx
|
||||||
.entity_of_child(ID_POLICY_CHECK_BUTTON_MENU)
|
.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'.");
|
.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.
|
// // 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
|
// // The cargo package identifier (default: 'nwx.advotracker') is used as the
|
||||||
// // app directory name. The directory location is OS dependant
|
// // app directory name. The directory location is OS dependant
|
||||||
@@ -294,6 +306,7 @@ impl State for PolicyCheckState {
|
|||||||
//self.update_data_count(ctx);
|
//self.update_data_count(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update the widget state.
|
||||||
fn update(&mut self, _registry: &mut Registry, ctx: &mut Context<'_>) {
|
fn update(&mut self, _registry: &mut Registry, ctx: &mut Context<'_>) {
|
||||||
// clear focus on focus moved
|
// clear focus on focus moved
|
||||||
if self.last_focused != ctx.window().get::<Global>("global").focused_widget {
|
if self.last_focused != ctx.window().get::<Global>("global").focused_widget {
|
||||||
@@ -334,7 +347,7 @@ impl State for PolicyCheckState {
|
|||||||
}
|
}
|
||||||
Action::SetVisibility(entity) => {
|
Action::SetVisibility(entity) => {
|
||||||
//text_block(ctx.child(entity).set_visibility(Visibility::Visible));
|
//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(entity).set_visibility(Visibility::Collapsed));
|
||||||
text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).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;
|
self.action = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update the view after the layout is rendered.
|
||||||
fn update_post_layout(&mut self, _: &mut Registry, ctx: &mut Context<'_>) {
|
fn update_post_layout(&mut self, _: &mut Registry, ctx: &mut Context<'_>) {
|
||||||
let string_result = "Prüfungsergebnis:".to_string();
|
let string_result = "Prüfungsergebnis:".to_string();
|
||||||
//string_result = format!("{} {:?}", string_result, self.??;
|
//string_result = format!("{} {:?}", string_result, self.??;
|
||||||
|
|||||||
@@ -49,7 +49,6 @@ impl Template for PolicyCheckView {
|
|||||||
//.v_align("center")
|
//.v_align("center")
|
||||||
//.enabled(true)
|
//.enabled(true)
|
||||||
.on_mouse_down(|_, _| true)
|
.on_mouse_down(|_, _| true)
|
||||||
//.child(policycheck_menu_container)
|
|
||||||
.on_click(move |ctx, _| {
|
.on_click(move |ctx, _| {
|
||||||
ctx.get_mut::<PolicyCheckState>(id)
|
ctx.get_mut::<PolicyCheckState>(id)
|
||||||
.action(Action::OpenMenu(policy_check_menu_text_block));
|
.action(Action::OpenMenu(policy_check_menu_text_block));
|
||||||
@@ -69,11 +68,11 @@ impl Template for PolicyCheckView {
|
|||||||
let policy_data_count_block = TextBlock::new()
|
let policy_data_count_block = TextBlock::new()
|
||||||
.id(ID_POLICY_DATA_COUNT_BLOCK)
|
.id(ID_POLICY_DATA_COUNT_BLOCK)
|
||||||
//.class(CLASS_TEXT_BLOCK)
|
//.class(CLASS_TEXT_BLOCK)
|
||||||
.attach(Grid::row(1))
|
.attach(Grid::row(3))
|
||||||
.attach(Grid::column(1))
|
.attach(Grid::column(1))
|
||||||
.margin((0., 4., 0., 0.))
|
.margin((0., 4., 0., 0.))
|
||||||
.h_align("end")
|
.h_align("end")
|
||||||
.v_align("end")
|
.v_align("top")
|
||||||
.enabled(true)
|
.enabled(true)
|
||||||
.build(ctx);
|
.build(ctx);
|
||||||
|
|
||||||
@@ -186,7 +185,6 @@ impl Template for PolicyCheckView {
|
|||||||
.text("")
|
.text("")
|
||||||
.on_activate(move |ctx, entity| {
|
.on_activate(move |ctx, entity| {
|
||||||
// Entity is entered/activated via Mouse/Keyboard
|
// Entity is entered/activated via Mouse/Keyboard
|
||||||
println!("activation finished!");
|
|
||||||
ctx.get_mut::<PolicyCheckState>(id)
|
ctx.get_mut::<PolicyCheckState>(id)
|
||||||
.action(Action::ParseEntry(entity));
|
.action(Action::ParseEntry(entity));
|
||||||
})
|
})
|
||||||
@@ -234,7 +232,6 @@ impl Template for PolicyCheckView {
|
|||||||
.child(
|
.child(
|
||||||
TextBlock::new()
|
TextBlock::new()
|
||||||
.id(ID_POLICY_CHECK_RESULT)
|
.id(ID_POLICY_CHECK_RESULT)
|
||||||
//.id("policy_check_result")
|
|
||||||
.h_align("start")
|
.h_align("start")
|
||||||
.attach(Grid::row(1))
|
.attach(Grid::row(1))
|
||||||
.attach(Grid::column(2))
|
.attach(Grid::column(2))
|
||||||
@@ -347,9 +344,9 @@ impl Template for PolicyCheckView {
|
|||||||
.rows(
|
.rows(
|
||||||
Rows::new()
|
Rows::new()
|
||||||
.add("auto")
|
.add("auto")
|
||||||
.add("5.")
|
.add(28.)
|
||||||
.add("*")
|
.add("*")
|
||||||
.add("18.")
|
.add("auto")
|
||||||
.add("auto")
|
.add("auto")
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user