callbacks: policycheck_state update

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2020-07-13 00:50:51 +02:00
parent 5eeb80cdd4
commit 4d2427a058

View File

@@ -2,9 +2,10 @@ use chrono::{Local, DateTime};
use dotenv::dotenv;
use locales::t;
use orbtk::prelude::*;
use orbtk::shell::WindowRequest;
//use orbtk::shell::WindowRequest;
use serde::Deserialize;
use std::{env, process, thread, time};
//use std::{env, process, thread, time};
use std::{env, process};
use std::collections::HashMap;
use tracing::{debug, error, info, trace};
@@ -27,9 +28,9 @@ pub enum Action {
AddProgress(f64),
ResetProgress,
SetProgress(f64),
SetPopupBox(Entity),
SetProgressBox(Entity),
RemoveFocus(Entity),
RemovePopupBox(Entity),
RemoveProgressBox(Entity),
SetEntry(Entity),
SetVisibility(Entity),
TextChanged(Entity, usize),
@@ -45,7 +46,7 @@ struct Environment {
#[derive(Default, AsAny)]
pub struct PolicyCheckState {
action: Option<Action>,
label_result_text_block: Entity,
label_result: Entity,
last_focused: Option<Entity>,
menu_button: Entity,
//policy_check_clean_button: Entity,
@@ -118,15 +119,15 @@ impl PolicyCheckState {
fn import_data(&mut self, ctx: &mut Context<'_>)
-> Result<(), Box<dyn std::error::Error>> {
// WIP: for now, only import once per session
let two_seconds = time::Duration::from_secs(2);
let sender = ctx.window_sender();
//let two_seconds = time::Duration::from_secs(2);
//let sender = ctx.window_sender();
if self.policy_data_count == 0 {
//let mut text_block_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
//text_block_wrapper.set_enabled(true);
//text_block_wrapper.set_text("Importieren der Prüflisten-Elemnte ...");
let mut text_block_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
text_block_wrapper.set_enabled(true);
text_block_wrapper.set_text("Importieren der Prüflisten-Elemente ...");
// self.set_progress_box(ctx);
// self.set_popup_box(ctx);
// progress_bar(ctx.child(ID_POLICY_CHECK_PROGRESS_BAR)).set_val(0.25);
// for _ in 1..4 {
// let old_width = *progress_bar(ctx.child(ID_POLICY_CHECK_PROGRESS_BAR)).val();
@@ -135,7 +136,7 @@ impl PolicyCheckState {
// thread::sleep(two_seconds);
// }
sender.send(WindowRequest::Redraw).unwrap();
//sender.send(WindowRequest::Redraw).unwrap();
match self.create_hashmap() {
Ok(()) => {
@@ -169,7 +170,7 @@ impl PolicyCheckState {
/// Open menu.
pub fn open_menu(&mut self, text_block: Entity, ctx: &mut Context<'_>) {
let menu_string = ctx.get_widget(text_block).get::<String16>("text");
let _menu_string = ctx.get_widget(text_block).get::<String16>("text");
}
/// Parse validity of the given policy number.
@@ -221,6 +222,8 @@ impl PolicyCheckState {
// 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");
@@ -246,6 +249,7 @@ impl PolicyCheckState {
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");
text_block(ctx.child("policy_check_label_result")).set_visibility(Visibility::Visible);
}
_ => {
// no matching key
@@ -256,6 +260,10 @@ impl PolicyCheckState {
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");
let mut text_block_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
let res = t!("policy.validation.not_found", lang);
text_block_wrapper.set_text(res);
text_block(ctx.child("policy_check_label_result")).set_visibility(Visibility::Visible);
}
}
},
@@ -266,7 +274,10 @@ impl PolicyCheckState {
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);
let mut text_block_wrapper: TextBlockCtx<'_> = text_block(ctx.child("policy_check_result"));
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!");
}
@@ -278,6 +289,8 @@ impl PolicyCheckState {
text_block_wrapper.set_enabled(true);
let res = t!("policy.validation.to_short", 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);
}
if policy_number_length > 10 {
//println!("Policy number is to big!");
@@ -285,6 +298,8 @@ impl PolicyCheckState {
text_block_wrapper.set_enabled(true);
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);
}
}
@@ -292,7 +307,7 @@ impl PolicyCheckState {
fn remove_progress_box(&mut self, ctx: &mut Context<'_>) {
if let Some(progress_box) = self.progress_box {
ctx.remove_child(progress_box);
println!("PopupBox removed !");
println!("ProgressBox removed !");
}
}
@@ -328,23 +343,9 @@ impl PolicyCheckState {
}
/// 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);
text_block(ctx.child(ID_POLICY_CHECK_BUTTON_RESULT)).set_visibility(Visibility::Visible);
} else {
text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_visibility(Visibility::Collapsed);
text_block(ctx.child(ID_POLICY_CHECK_BUTTON_RESULT)).set_visibility(Visibility::Collapsed);
//ctx.get_widget(self.policy_check_label_policy_number).set("enabled", false);
}
ctx.get_widget(self.label_result_text_block).update_theme_by_state(true);
}
/// Set a popup box that updates the import status in a progress bar
fn set_progress_box(&mut self, ctx: &mut Context<'_>) {
//println!("Set up Popup box: {:?}", text_box);
//println!("Set up Progress box: {:?}", text_box);
let stack = ctx
.entity_of_child(ID_POLICY_CHECK_RESULT)
.expect("PolicyCheckState.init: Can't find entity of resource 'ID_POLICY_CHECK_RESULT'.");
@@ -355,14 +356,29 @@ impl PolicyCheckState {
// create a progress_box widget as a child of entity "ID_POLICY_CHECK_POLICY_NUMBER"
build_context.append_child(stack, progress_box);
self.progress_box = Some(progress_box);
println!("PopupBox created: {:?}", Some(progress_box));
println!("ProgressBox created: {:?}", Some(progress_box));
}
/// 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<'_>) {
let data_list_count = ctx.widget().get::<PolicyDataList>(PROP_POLICY_DATA_LIST).len();
ctx.widget().set(PROP_POLICY_DATA_COUNT, data_list_count);
}
/// 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::Collapsed);
text_block(ctx.child(ID_POLICY_CHECK_BUTTON_RESULT)).set_visibility(Visibility::Collapsed);
//ctx.get_widget(self.policy_check_label_policy_number).set("enabled", false);
} else {
text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_text("Prüfungsergebnis:");
text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_visibility(Visibility::Visible);
text_block(ctx.child(ID_POLICY_CHECK_BUTTON_RESULT)).set_visibility(Visibility::Visible);
}
ctx.get_widget(self.label_result).update_theme_by_state(true);
}
}
/// upported states for our view
@@ -371,15 +387,23 @@ impl State for PolicyCheckState {
fn init(&mut self, _: &mut Registry, ctx: &mut Context<'_>) {
let dt_start: DateTime<Local> = Local::now();
trace!(target: "advotracker", init = "started");
trace!(target: "advotracker", policycheck_state = "init", status = "started");
self.menu_button = ctx
.entity_of_child(ID_POLICY_CHECK_BUTTON_MENU)
.expect("PolicyCheckState.init: Can't find entity of resource 'ID_POLICY_CHECK_POLICY_MENU_BUTTON'.");
.expect("PolicyCheckState.init: Can't find entity of resource 'ID_POLICY_CHECK_BUTTON_MENU'.");
self.progress_box = ctx
.entity_of_child(ID_POLICY_CHECK_PROGRESS_BOX)
.expect("PolicyCheckState.init: Can't find entity of resource 'ID_POLICY_PROGRESS_BOX'.");
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'.");
let string_result = "Prüfungsergebnis:".to_string();
text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_text(string_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'.");
// // 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
@@ -398,13 +422,15 @@ impl State for PolicyCheckState {
//self.update_data_count(ctx);
// Load data into hashmap
//self.import_data(ctx);
// match self.import_data(ctx) {
// Ok(()) => trace!(target: "advotracker", policycheck_state = "init", import_data = "success"),
// Err(e) => trace!(target: "advotracker", policycheck_state = "init", import_data = ?e),
//}
let dt_end: DateTime<Local> = Local::now();
let duration = dt_end.signed_duration_since(dt_start);
trace!(target: "advotracker", init = ?duration);
trace!(target: "advotracker", policycheck_state = "init", status = "finished", duration = ?duration);
}
/// Update the widget state.
@@ -456,8 +482,8 @@ impl State for PolicyCheckState {
ctx.get_widget(policy_check_policy_number).set("enabled", false);
ctx.push_event_by_window(FocusEvent::RemoveFocus(policy_check_policy_number));
}
Action::RemovePopupBox(_entity) => {
self.remove_popup_box(ctx);
Action::RemoveProgressBox(_entity) => {
self.remove_progress_box(ctx);
}
Action::ResetProgress => {
progress_bar(ctx.child(ID_POLICY_CHECK_PROGRESS_BAR)).set_val(0.);
@@ -469,10 +495,10 @@ impl State for PolicyCheckState {
progress_bar(ctx.child(ID_POLICY_CHECK_PROGRESS_BAR)).set_val(0.);
}
}
Action::SetPopupBox(_entity) => {
//println!("set_popup_box: {:#?}", ctx.widget().get::<String16>("water_mark"));
//println!("set_popup_box: {:#?}", entity);
self.set_popup_box(ctx);
Action::SetProgressBox(_entity) => {
//println!("set_progress_box: {:#?}", ctx.widget().get::<String16>("water_mark"));
//println!("set_progress_box: {:#?}", entity);
self.set_progress_box(ctx);
//let text = text_box(ctx.child(ID_POLICY_CHECK_POLICY_NUMBER));
}
Action::SetEntry(policy_check_policy_number) => {
@@ -495,11 +521,6 @@ impl State for PolicyCheckState {
/// 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.??;
text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_text(string_result);
text_block(ctx.child(ID_POLICY_CHECK_LABEL_RESULT)).set_visibility(Visibility::Collapsed);
let mut string_data_count = "Prüflisten-Elemente:".to_string();
string_data_count = format!("{} {:?}", string_data_count, self.policy_numbers.len());