Files
advotracker/advotracker_client/src/widgets/policycheck/policycheck_state.rs
2021-11-12 15:48:17 +01:00

956 lines
39 KiB
Rust

// SPDX-License-Identifier: (0BSD or MIT)
/*
* advotracker - Hotline tackingtool for Advocats
*
* Copyright 2020-2021 Ralf Zerres <ralf.zerres@networkx.de>
*/
use orbtk::{prelude::*, widgets::themes::*};
use serde::Deserialize;
use std::collections::HashMap;
use std::process;
use std::time::{Duration, SystemTime};
use tracing::{error, info, trace};
use crate::{
data::{
constants::*,
structures::{PolicyCode, PolicyDataList, PolicyList},
},
//services::imports::allianzdirectcall::import,
services::imports::allianzdirectcall,
widgets::global_state::GlobalState,
//widgets::menu::menu_view::MenuView,
//widgets::policycheck::policycheck_view::PolicycheckView,
//widgets::ticketdata::ticketdata_state::TicketdataAction,
Lang,
};
/// Enumeration of valid `action variants` that need to be handled as
/// state changes for the `PolicycheckView` widget.
#[derive(Debug, Clone)]
pub enum PolicycheckAction {
/// Clear the policy number
ClearPolicyNumber,
/// Change the active theme
ChangeTheme(),
/// Send the policy number
SendPolicynumber(),
/// Entity of the changed input text
InputTextChanged(Entity),
/// Import data to the form
ImportData,
/// Create a new ticket
NewTicket,
/// Entity-id of the policy number to parse
ParsePolicyNumber(Entity),
/// Remove the focus of current entity
RemoveFocus(Entity),
/// Entity-id of popup to bo removed
RemovePopup(Entity),
/// Reset the progress status
ResetProgress,
/// Get progress status
GetProgress,
/// Set the progress status to given value
SetProgress(f64),
/// Set the progress popup to given entity-id
SetProgressPopup(Entity),
/// Set the entitiy-id for theme to be toggled to
SetToggleTheme(Entity),
/// Set the given entity-id
SetEntry(Entity),
/// Set visibility status of given entity-id
SetVisibility(Entity),
/// Set changed status of given text entity-id
TextChanged(Entity, usize),
/// Update the given policy code
UpdatePolicyCode,
/// Update the process status to given value
UpdateProgress(f64),
}
/// Define valid environment variables provided via .env files
/// located in the current call directory.
/// This is primarily used in testing scenarios (eg. debugging).
#[derive(Debug, Deserialize)]
struct Environment {
test_lang: String,
rust_log: String,
}
/// Valid `structures` that are handled inside the state of the `Policycheck` widget.
#[derive(AsAny, Default)]
pub struct PolicycheckState {
actions: Vec<PolicycheckAction>,
button_menu: Entity,
duration: Duration,
label_result: Entity,
lang: Lang,
policy_data_count: u64,
//policy_number: Entity,
policy_numbers: HashMap<u64, PolicyCode>,
progress_bar: Entity,
progress_count: f64,
progress_popup: Entity,
// target that recieves messages
target: Entity,
ticketdata_view: Entity,
}
impl GlobalState for PolicycheckState {}
/// Method definitions, that react on any given state change inside the `Policycheck` widget.
impl PolicycheckState {
/// Create a hashmap (key: policy number, value: policy type).
pub fn create_hashmap(
&mut self,
_ctx: &mut Context<'_>,
) -> Result<(), Box<dyn std::error::Error>> {
trace!(target: "advotracker", create_hashmap = "started");
let policy_list = PolicyList::new("policy list");
trace!(target: "advotracker", policy_list = ?policy_list);
// create vector to hold imported data
let res = t!(policy_string_label_policy_data => self.lang);
let mut policy_data = PolicyDataList::new(res);
trace!(target: "advotracker", policy_data = ?policy_data);
let mut policy_numbers: HashMap<u64, 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");
match allianzdirectcall::import(
&mut csv_import_path,
&mut policy_data,
&mut policy_numbers,
&mut self.policy_data_count,
) {
Ok((count, duration)) => {
self.policy_data_count = count;
self.duration = duration;
trace!(target: "advotracker", csv_import_path = ?csv_import_path,
policy_data_count = ?&self.policy_data_count,
duration = ?&self.duration);
}
Err(err) => {
error!("error running CSV-Import: {}", err);
process::exit(1);
}
};
self.policy_numbers = policy_numbers;
trace!(target: "advotracker", create_hashmap = "finished");
Ok(())
}
/// Clear text in text box.
pub fn clear_policy_number(&mut self, ctx: &mut Context<'_>) {
TextBox::text_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_NUMBER),
String::from(""),
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Collapsed,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE),
Visibility::Collapsed,
);
Stack::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_ACTION_STACK),
Visibility::Collapsed,
);
}
/// Import policy numbers into hashmap
fn import_data(&mut self, ctx: &mut Context<'_>) -> Result<(), Box<dyn std::error::Error>> {
// WIP: for now, only import once per session
if self.policy_data_count == 0 {
TextBlock::enabled_set(&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE), true);
if self.policy_numbers.is_empty() {
// initialize popup widget
self.set_popup_progress(ctx);
self.progress_count += 0.33;
self.update_progress_bar(ctx);
for _ in 1..4 {
self.progress_count += 0.33;
self.update_progress_bar(ctx);
ctx.send_message(
PolicycheckAction::UpdateProgress(self.progress_count),
self.progress_popup,
);
}
// importing policy code elements from csv-file
match self.create_hashmap(ctx) {
Ok(()) => {
let res = t!(policy_hashmap_success => self.lang);
info!("hashmap has: {:?} entries", self.policy_data_count);
trace!(target: "advotracker",
hashmap_status = ?res,
hashmap_entries = ?self.policy_data_count);
self.progress_count = 1.;
self.update_progress_bar(ctx);
}
_ => {
let res = t!(policy_hashmap_failed => self.lang);
error!("{:?}", res);
trace!(target: "advotracker", hashmap_status = ?res);
}
}
}
} else {
trace!(target: "advotracker",
hashmap_status = "consume",
hashmap_entries = ?self.policy_data_count);
}
Ok(())
}
/// Create new ticket
pub fn new_ticket(&mut self, _ctx: &mut Context<'_>) {
println!("WIP: new ticket.");
self.ticketdata_view.0;
//ctx.widget().get_mut::<TicketdataView>(0).
//ctx.get_widget(self.ticketdata_view);
}
/// Parse validity of the given policy number.
pub fn parse_entry(&mut self, policy_check_policy_number: Entity, ctx: &mut Context<'_>) {
trace!(target: "advotracker", parse_entry = "started");
let policy_number_string = TextBox::text_clone(&ctx.get_widget(policy_check_policy_number));
let policy_number_length = policy_number_string.len();
if self.policy_data_count == 0 {
// Load data into hashmap
match self.import_data(ctx) {
Ok(()) => {
trace!(target: "advotracker", policycheck_state = "init", import_data = "success");
Stack::visibility_set(
&mut ctx.child(ID_POLICY_DATA_STACK),
Visibility::Visible,
);
let policy_data_count_string = format!("{:?}", &self.policy_data_count);
TextBlock::text_set(
&mut ctx.child(ID_POLICY_DATA_COUNT),
String::from(&policy_data_count_string),
);
}
Err(e) => {
trace!(target: "advotracker", policycheck_state = "init", import_data = ?e)
}
}
}
trace!(target: "advotracker", state = "parsing", policy_number = ?policy_number_string);
// Parse policy code: "AS-123456789"
// DION VERS POLLFNR
// 1 AS 1515735810
Stack::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_ACTION_STACK),
Visibility::Collapsed,
);
Button::background_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
String::from("transparent"),
);
if policy_number_length == 10 {
// cast policy_number_sting to <u64>
match policy_number_string.parse::<u64>() {
Ok(p) => {
TextBlock::text_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE),
String::from(""),
);
// match hashmap's key
match self.policy_numbers.get(&p) {
Some(policy_code) => {
// matching key, get associated value
trace!(target: "advotracker", state = "success",
policy_number = ?p, policy_code = ?policy_code);
let string_result = format!("1-{:?}-{}", policy_code, p);
// adapt the view properties
TextBlock::text_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE),
string_result,
);
TextBox::foreground_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_NUMBER),
String::from("#008000"),
);
Button::icon_brush_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
String::from("#008000"),
);
Button::foreground_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
String::from("#008000"),
);
Button::icon_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
material_icons_font::MD_CHECK,
);
Button::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
Visibility::Visible,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Visible,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE),
Visibility::Visible,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_HINT),
Visibility::Collapsed,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_HINT),
Visibility::Collapsed,
);
Stack::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_ACTION_STACK),
Visibility::Visible,
);
Button::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_ACTION_BUTTON_CLEAR),
Visibility::Visible,
);
}
_ => {
// no matching key
let res = t!(policy_validation_failed => self.lang);
trace!(target: "advotracker", state = ?res, policy_number = ?p);
TextBox::foreground_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_NUMBER),
String::from("#FF0000"),
);
TextBlock::text_set(
&mut ctx.child(ID_POLICY_CHECK_HINT),
String::from("The given policy number is invalid"),
);
Button::icon_brush_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
String::from("#FF0000"),
);
Button::foreground_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
String::from("#FF0000"),
);
Button::icon_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
material_icons_font::MD_CLEAR,
);
Button::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
Visibility::Visible,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Collapsed,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE),
Visibility::Collapsed,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_HINT),
Visibility::Visible,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_HINT),
Visibility::Visible,
);
}
}
}
Err(e) => {
trace!(target: "advotracker", state = "error", error_type = "invalid type", error = ?e);
TextBox::foreground_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_NUMBER),
String::from("#FF0000"),
);
TextBlock::text_set(
&mut ctx.child(ID_POLICY_CHECK_HINT),
String::from("Only numbers are valid"),
);
Button::icon_brush_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
String::from("#FF0000"),
);
Button::foreground_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
String::from("#FF0000"),
);
Button::icon_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
material_icons_font::MD_CLEAR,
);
Button::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
Visibility::Visible,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Collapsed,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE),
Visibility::Collapsed,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_HINT),
Visibility::Visible,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_HINT),
Visibility::Visible,
);
}
}
}
if policy_number_length < 10 {
let res = t!(policy_validation_failed => self.lang);
trace!(target: "advotracker", state = ?res, reason = "number to short");
TextBox::foreground_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_NUMBER),
String::from("#FF0000"),
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_HINT),
Visibility::Visible,
);
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_HINT), Visibility::Visible);
TextBlock::text_set(
&mut ctx.child(ID_POLICY_CHECK_HINT),
String::from("Policy number is to short"),
);
Button::icon_brush_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
String::from("#FF0000"),
);
Button::foreground_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
String::from("#FF0000"),
);
Button::icon_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
material_icons_font::MD_CLEAR,
);
Button::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
Visibility::Visible,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Collapsed,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE),
Visibility::Collapsed,
);
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_HINT), Visibility::Visible);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_HINT),
Visibility::Visible,
);
}
if policy_number_length > 10 {
let res = t!(policy_validation_failed => self.lang);
trace!(target: "advotracker", state = ?res, reason = "number to long");
TextBox::foreground_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_NUMBER),
String::from("#FF0000"),
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_HINT),
Visibility::Visible,
);
TextBlock::text_set(
&mut ctx.child(ID_POLICY_CHECK_HINT),
String::from("Policy number is to long"),
);
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_HINT), Visibility::Visible);
Button::icon_brush_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
String::from("#FF0000"),
);
Button::foreground_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
String::from("#FF0000"),
);
Button::icon_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
material_icons_font::MD_CLEAR,
);
Button::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
Visibility::Visible,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Collapsed,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE),
Visibility::Collapsed,
);
TextBlock::visibility_set(&mut ctx.child(ID_POLICY_CHECK_HINT), Visibility::Visible);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_HINT),
Visibility::Visible,
);
}
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);
println!("Popup {:?} removed !", id);
}
// /// 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::<String>("policy_check_policy_number").is_empty() {
// ctx.get_widget(self.policy_check_clear_button).set("enabled", false);
// } else {
// ctx.get_widget(self.policy_check_clear_button).set("enabled", true);
// }
// ctx.get_widget(self.policy_check_policy_number).update_theme_by_state(true);
// }
/// Sending message 'UpdatePolicyCode'
pub fn send_message_update_policy_code(&mut self, entity: Entity) {
println!("WIP: send_message_update_policy_code = {:?}", entity);
//self.actions.push(PolicycheckAction::UpdatePolicyNumber("4711".to_string()));
//let policy_number = (ctx.get_child(entity).get::<String>("text"));
//println!("WIP: poliy_number = {:?}", policy_number);
//self.actions.push(PolicycheckAction::UpdatePolicyNumber(self.policy_number));
}
/// Change status of given text box to edit mode.
fn set_entry(&mut self, text_box: Entity, ctx: &mut Context<'_>) {
if ctx.get_widget(text_box).get::<String16>("text").is_empty() {
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Collapsed,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE),
Visibility::Collapsed,
);
} else {
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Visible,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE),
Visibility::Visible,
);
}
}
/// Set a progress popup that updates the import status in a progress bar
fn set_popup_progress(&mut self, ctx: &mut Context<'_>) {
// create a stack as a child of entity "ID_POLICY_CHECK_POLICY_NUMBER"
let stack = ctx.entity_of_child(ID_POLICY_CHECK_POLICY_NUMBER).expect(
"PolicycheckState: Can't find entity of resource 'ID_POLICY_CHECK_POLICY_NUMBER'.",
);
let current_entity = ctx.entity();
let build_context = &mut ctx.build_context();
// create the progress_popup widget
self.progress_popup = create_popup_progress(current_entity, build_context);
info!(
"set_popup_progress: New entity 'popup_progress' {:?} created",
self.progress_popup
);
// append the stack inside the progress_popup
build_context.append_child(stack, self.progress_popup);
// make sure we have a progress bar
self.progress_bar = ctx.entity_of_child(ID_POLICY_CHECK_PROGRESS_BAR).expect(
"PolicycheckState.init: Can't find entity of resource 'ID_POLICY_CHECK_PROGRESS_BAR'.",
);
info!(
"set_popup_progress: New entity 'progress_bar' created: {:?}",
self.progress_bar
);
}
/// 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() {
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Collapsed,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
Visibility::Collapsed,
);
} else {
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Visible,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_BUTTON_RESULT),
Visibility::Visible,
);
}
}
/// Update count of elements in the policy data list.
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);
}
fn update_progress_bar(&self, ctx: &mut Context<'_>) {
let res = t!(policy_string_progress_time => self.lang);
let string_duration = format!("{}: {:?}", res, self.duration);
TextBlock::text_set(
&mut ctx.child(ID_POLICY_CHECK_PROGRESS_TIME),
string_duration,
);
let mut progress_bar = ctx.child(ID_POLICY_CHECK_PROGRESS_BAR);
progress_bar.set::<f64>("val", self.progress_count);
}
/// Update the policy code policy data list.
fn update_policy_code(&self, _ctx: &mut Context<'_>) {
println!("update internal: policy_code");
//let policy_code = ctx.widget().get::<PolicycheckState>(ID_POLICY_CHECK_POLICY_CODE);
//ctx.widget().set(PROP_POLICY_DATA_COUNT, policy_code);
}
}
/// Supported methods handled inside the `PolicycheckState`
impl State for PolicycheckState {
/// Initialize the state of widgets inside `PolicycheckState`
fn init(&mut self, _: &mut Registry, ctx: &mut Context<'_>) {
let time_start = SystemTime::now();
trace!(target: "advotracker", policycheck_state = "init", status = "started");
// Get language from environment
// self.lang = PolicycheckState::get_lang();
self.lang = Lang::De("");
// 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'.",
);
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'.",
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Collapsed,
);
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_POLICY_CODE),
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);
Stack::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_ACTION_STACK),
Visibility::Collapsed,
);
//self.policy_number = Entity::from(ctx.widget().try_clone::<u32>(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::<u32>("target")
.expect("PolicycheckState::init(): Can't find resource entity 'target'."),
);
//self.ticketdata_view = (*ctx.widget().get::<u32>("ticketdata_view")).into();
//self.ticketdata = Entity::from(ctx.widget().try_clone::<u32>(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
// // (Windows: AppData, Unix: XDG_CONFIG_HOME, MacOS: $HOME/Library/Preferences).
// // The filename is taken from the propertey PROP_ADVOTRACKER (default: 'advotracker'.ron).
// if let Ok(policy_data) = registry
// .get::<Settings>("settings")
// .load::<PolicyDataList>(PROP_ADVOTRACKER)
// {
// ctx.widget().set(PROP_ADVOTRACKER, policy_data);
// }
let time_end = SystemTime::now();
let duration = time_end.duration_since(time_start);
trace!(target: "advotracker", policycheck_state = "init", status = "finished", duration = ?duration);
}
/// Handle messages for `PolicycheckState`.
fn messages(
&mut self,
mut messages: MessageReader,
_registry: &mut Registry,
ctx: &mut Context<'_>,
) {
for message in messages.read::<PolicycheckAction>() {
match message {
PolicycheckAction::ClearPolicyNumber => {
self.clear_policy_number(ctx);
}
PolicycheckAction::NewTicket => {
self.new_ticket(ctx);
}
PolicycheckAction::UpdatePolicyCode => {
self.update_policy_code(ctx);
}
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.);
}
}
_ => {
println!("PolicycheckAction: action not implemented!");
}
}
}
}
/// Update the state of widgets inside the `Policycheck` view.
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 {
// if let Some(last_focused) = self.last_focused {
// ctx.get_widget(last_focused).set("focused", false);
// // widget is unvisible, but takes space to be considered
// ctx.get_widget(last_focused)
// .set("visibility", Visibility::Collapsed);
// }
//}
// Create `actions`, a drained iterator (".." => full range that clears the vector)
let actions: Vec<PolicycheckAction> = self.actions.drain(..).collect();
for action in actions {
match action {
PolicycheckAction::ClearPolicyNumber => {
println!("clear entry");
self.clear_policy_number(ctx);
}
PolicycheckAction::InputTextChanged(entity) => {
println!(
"entry changed: {}",
TextBox::text_clone(&ctx.get_widget(entity))
);
}
PolicycheckAction::ImportData => match self.import_data(ctx) {
Ok(()) => {
trace!(target: "advotracker", import_data = "success");
}
_ => {
error!("Importing data failed!");
trace!(target: "advotracker", import_data = "failed");
}
},
PolicycheckAction::NewTicket => {
self.new_ticket(ctx);
}
PolicycheckAction::ParsePolicyNumber(text_box) => {
self.parse_entry(text_box, ctx);
}
PolicycheckAction::RemoveFocus(policy_check_policy_number) => {
ctx.get_widget(policy_check_policy_number)
.set("enabled", false);
//ctx.EventAdapter(FocusEvent::RemoveFocus(policy_check_policy_number));
}
PolicycheckAction::RemovePopup(entity) => {
self.remove_popup(entity, ctx);
}
PolicycheckAction::ResetProgress => {
ProgressBar::val_set(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR), 0.);
}
PolicycheckAction::SetEntry(policy_check_policy_number) => {
//self.last_focused = Some();
self.set_entry(policy_check_policy_number, ctx);
}
PolicycheckAction::SetProgress(value) => {
if value >= 0. || value <= 1. {
ProgressBar::val_set(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR), value);
} else {
ProgressBar::val_set(&mut ctx.child(ID_POLICY_CHECK_PROGRESS_BAR), 0.);
}
}
PolicycheckAction::SetProgressPopup(_entity) => {
self.set_popup_progress(ctx);
}
PolicycheckAction::SetVisibility(_entity) => {
TextBlock::visibility_set(
&mut ctx.child(ID_POLICY_CHECK_LABEL_RESULT),
Visibility::Collapsed,
);
}
PolicycheckAction::TextChanged(entity, _index) => {
self.set_entry(entity, ctx);
}
PolicycheckAction::UpdatePolicyCode => {
self.update_policy_code(ctx);
//info!("Message send: 'PolicycheckAction::UpdatePolicyNumber({:?}) -> {:?}'", policy_number, self.target);
//let policy_number = (&mut ctx.get_widget(entity).get::<String>("text"));
//let policy_number = ctx.get_widget(ID_POLICY_CHECK_POLICY_NUMBER).get::<text>("text");
//ctx.send_message(PolicycheckAction::UpdatePolicyCode("AS-1-4711".to_string()), self.target);
//ctx.send_message(PolicycheckAction::UpdatePolicyCode, self.target);
//let mut widget_container : WidgetContainer = ctx.widget();
//widget_container.get::<String16>("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::<String16>("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);
}
_ => (),
}
}
}
// /// Update the view after the layout is rendered.
// fn update_post_layout(&mut self, _: &mut Registry, _ctx: &mut Context<'_>) {
// }
}
/// Create a progress popup with update status of an onging data import
fn create_popup_progress(id: Entity, ctx: &mut BuildContext<'_>) -> Entity {
Popup::new()
.id(ID_POLICY_CHECK_POPUP_PROGRESS)
.target(id.0)
.open(true)
.style("popup_progress")
.width(280)
.height(100)
.visibility(Visibility::Visible)
.on_click(move |_ctx, _| {
println!("create_popup_progress: on_click -> remove_popup(popup_progress)");
//ctx.get_mut::<PolicycheckState>(id)
// .set_action(PolicycheckAction::RemovePopup(id));
true
})
.child(
Container::new()
.style("container_progress")
.child(
Stack::new()
.style("stack_progress")
.child(
TextBlock::new()
.id(ID_POLICY_CHECK_PROGRESS_TEXT)
.style("textblock_progress")
.text("Importing data")
.build(ctx),
)
.child(
ProgressBar::new()
.id(ID_POLICY_CHECK_PROGRESS_BAR)
.style("progress_bar")
.val(0)
//.width(250)
.build(ctx),
)
.child(
TextBlock::new()
.id(ID_POLICY_CHECK_PROGRESS_TIME)
.style("textblock_progress")
.h_align("end")
.text("Processing time")
.build(ctx),
)
.build(ctx),
)
.build(ctx),
)
.build(ctx)
}