From 6ea1f95ef0eea681651bbc73cd532b733f23864b Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Wed, 7 Oct 2020 03:22:51 +0200 Subject: [PATCH] main_view: introduce configuration view * allow to load and save config parameters to ron file Signed-off-by: Ralf Zerres --- advotracker/src/widgets/main_view.rs | 186 ++++++++++++++++++++++++++- 1 file changed, 185 insertions(+), 1 deletion(-) diff --git a/advotracker/src/widgets/main_view.rs b/advotracker/src/widgets/main_view.rs index bf70180..6908d13 100644 --- a/advotracker/src/widgets/main_view.rs +++ b/advotracker/src/widgets/main_view.rs @@ -56,13 +56,17 @@ impl Template for MainView { TabWidget::new() .tab("Policynumber check", policycheck_view) .tab("Localization", LocalizationView::new().build(ctx)) + .tab("Configuration", ConfigurationView::new().build(ctx)) .build(ctx), ) //.child(policycheck_view) } } -widget!(LocalizationView { languages: List, selected_index: i32 }); +widget!(LocalizationView { + languages: List, + selected_index: i32 } +); impl Template for LocalizationView { fn template(self, id: Entity, ctx: &mut BuildContext<'_>) -> Self { @@ -97,6 +101,123 @@ impl Template for LocalizationView { } } +widget!( + ConfigurationView { + configuration_file: String, + language_id: String + } +); + +impl Template for ConfigurationView { + fn template(self, id: Entity, ctx: &mut BuildContext<'_>) -> Self { + self.child( + Grid::new() + .margin(4) + .columns( + Columns::create() + .push(120) + .push(12) + .push("auto") + ) + .rows(Rows::create() + .push("auto") + .push(4) + .push("auto") + .push(4) + .push("auto") + .push(12) + .push("auto"), + ) + .child( + TextBlock::new() + .h_align("start") + .attach(Grid::column(0)) + .attach(Grid::row(0)) + .attach(Grid::column_span(3)) + .text("Configuration settings") + .style("header") + .build(ctx), + ) + .child( + TextBlock::new() + .attach(Grid::column(0)) + .attach(Grid::row(2)) + .v_align("center") + .h_align("end") + .text("Configuration file") + .build(ctx), + ) + .child( + TextBox::new() + .attach(Grid::column(2)) + .attach(Grid::row(2)) + .text(("configuration_file", id)) + .water_mark("Filename...") + .build(ctx), + ) + .child( + TextBlock::new() + .attach(Grid::column(0)) + .attach(Grid::row(4)) + .v_align("center") + .h_align("end") + .text("Language Id") + .build(ctx), + ) + .child( + TextBox::new() + .attach(Grid::column(2)) + .attach(Grid::row(4)) + .text(("language_id", id)) + .water_mark("Locale Identifier...") + .build(ctx), + ) + .child( + Grid::new() + .h_align("center") + .attach(Grid::column(0)) + .attach(Grid::row(6)) + .attach(Grid::column_span(3)) + .columns( + Columns::create() + .push("auto") + .push(8) + .push("auto") + ) + .rows(Rows::create() + .push("auto") + ) + .child( + Button::new() + .style("button_single_content") + .attach(Grid::column(0)) + .attach(Grid::row(0)) + .text("load") + .on_click(move |ctx, _| { + ctx.send_message(ConfigurationAction::LoadConfiguration, id); + true + }) + .build(ctx), + ) + .child( + Button::new() + .text("save") + .style("button_single_content") + .attach(Grid::column(2)) + .attach(Grid::row(0)) + .on_click(move |ctx, _| { + ctx.send_message(ConfigurationAction::SaveConfiguration, id); + true + }) + .build(ctx), + ) + .build(ctx), + ) + .build(ctx), + ) + } +} + // [END] views // [START] states @@ -131,4 +252,67 @@ impl State for LocalizationState { } } +#[derive(Debug, Default, AsAny)] +struct ConfigurationState {} + +impl State for ConfigurationState { + fn messages( + &mut self, + mut messages: MessageReader, + registry: &mut Registry, + ctx: &mut Context<'_>, + ) { + for message in messages.read::() { + match message { + ConfigurationAction::LoadConfiguration => registry + .get::("settings") + .load_async::("configuration_data".to_string(), ctx.entity()), + ConfigurationAction::SaveConfiguration => { + let configuration_file: String = ConfigurationView::configuration_file_clone(&ctx.widget()); + let language_id: String = ConfigurationView::language_id_clone(&ctx.widget()); + registry.get::("settings").save_async( + "configuration_data".to_string(), + ConfigurationData( + configuration_file, + language_id + ), + ctx.entity(), + ); + } + } + } + + // save result + for message in messages.read::>() { + println!("Result {:?}", message); + } + + // load result + for message in messages.read::>() { + if let Ok(data) = message { + ConfigurationView::configuration_file_set(&mut ctx.widget(), data.0); + ConfigurationView::language_id_set(&mut ctx.widget(), data.1); + } + } + } +} + // [END] states + +// [START] Configuration data + +#[derive(Clone, Debug)] +enum ConfigurationAction { + SaveConfiguration, + LoadConfiguration, +} + +use serde::{Deserialize, Serialize}; + +#[derive(Default, Debug, Clone, Serialize, Deserialize)] +pub struct ConfigurationData( + pub String, + pub String +); + +// [END] Configuration data