From 81f05270852906c547d434f9930a67924b9edca2 Mon Sep 17 00:00:00 2001 From: filipriec Date: Mon, 31 Mar 2025 09:54:16 +0200 Subject: [PATCH] ui.rs gRPC code removed --- client/src/services/mod.rs | 2 + client/src/services/ui_service.rs | 90 +++++++++++++++++++++++++++++++ client/src/ui/handlers/ui.rs | 76 ++++++++------------------ 3 files changed, 113 insertions(+), 55 deletions(-) create mode 100644 client/src/services/ui_service.rs diff --git a/client/src/services/mod.rs b/client/src/services/mod.rs index 5bf5075..f24605d 100644 --- a/client/src/services/mod.rs +++ b/client/src/services/mod.rs @@ -5,9 +5,11 @@ pub mod adresar; pub mod table; pub mod profile; pub mod auth; +pub mod ui_service; pub use grpc_client::*; pub use adresar::*; pub use table::*; pub use profile::*; pub use auth::*; +pub use ui_service::*; diff --git a/client/src/services/ui_service.rs b/client/src/services/ui_service.rs new file mode 100644 index 0000000..7eb0455 --- /dev/null +++ b/client/src/services/ui_service.rs @@ -0,0 +1,90 @@ +// src/services/ui_service.rs + +use crate::services::grpc_client::GrpcClient; +use crate::state::pages::form::FormState; +use crate::state::state::AppState; +use common::proto::multieko2::adresar::AdresarResponse; + +pub struct UiService; + +impl UiService { + pub async fn initialize_app_state( + grpc_client: &mut GrpcClient, + app_state: &mut AppState, + ) -> Result, Box> { + // Fetch profile tree + let profile_tree = grpc_client.get_profile_tree().await?; + app_state.profile_tree = profile_tree; + + // Fetch table structure + let table_structure = grpc_client.get_table_structure().await?; + + // Extract the column names from the response + let column_names: Vec = table_structure + .columns + .iter() + .map(|col| col.name.clone()) + .collect(); + + Ok(column_names) + } + + pub async fn initialize_adresar_count( + grpc_client: &mut GrpcClient, + app_state: &mut AppState, + ) -> Result<(), Box> { + let total_count = grpc_client.get_adresar_count().await?; + app_state.update_total_count(total_count); + app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode + Ok(()) + } + + pub async fn update_adresar_count( + grpc_client: &mut GrpcClient, + app_state: &mut AppState, + ) -> Result<(), Box> { + let total_count = grpc_client.get_adresar_count().await?; + app_state.update_total_count(total_count); + Ok(()) + } + + pub async fn load_adresar_by_position( + grpc_client: &mut GrpcClient, + app_state: &mut AppState, + form_state: &mut FormState, + position: u64, + ) -> Result> { + match grpc_client.get_adresar_by_position(position).await { + Ok(response) => { + // Set the ID properly + form_state.id = response.id; + + // Update form values dynamically + form_state.values = vec![ + response.firma, + response.kz, + response.drc, + response.ulica, + response.psc, + response.mesto, + response.stat, + response.banka, + response.ucet, + response.skladm, + response.ico, + response.kontakt, + response.telefon, + response.skladu, + response.fax, + ]; + + form_state.has_unsaved_changes = false; + Ok(format!("Loaded entry {}", position)) + } + Err(e) => { + Ok(format!("Error loading entry: {}", e)) + } + } + } +} + diff --git a/client/src/ui/handlers/ui.rs b/client/src/ui/handlers/ui.rs index d617807..2a5a89e 100644 --- a/client/src/ui/handlers/ui.rs +++ b/client/src/ui/handlers/ui.rs @@ -2,6 +2,7 @@ use crate::tui::terminal::TerminalCore; use crate::services::grpc_client::GrpcClient; +use crate::services::ui_service::UiService; // Add this import use crate::tui::controls::CommandHandler; use crate::tui::terminal::EventReader; use crate::config::colors::themes::Theme; @@ -23,19 +24,8 @@ pub async fn run_ui() -> Result<(), Box> { // Initialize app_state first let mut app_state = AppState::new()?; - // Fetch profile tree and table structure - let profile_tree = grpc_client.get_profile_tree().await?; - app_state.profile_tree = profile_tree; - - // Fetch table structure at startup (one-time) - let table_structure = grpc_client.get_table_structure().await?; - - // Extract the column names from the response - let column_names: Vec = table_structure - .columns - .iter() - .map(|col| col.name.clone()) - .collect(); + // Initialize app state with profile tree and table structure + let column_names = UiService::initialize_app_state(&mut grpc_client, &mut app_state).await?; // Initialize FormState with dynamic fields let mut form_state = FormState::new(column_names); @@ -45,14 +35,11 @@ pub async fn run_ui() -> Result<(), Box> { let event_reader = EventReader::new(); // Fetch the total count of Adresar entries - let total_count = grpc_client.get_adresar_count().await?; - app_state.update_total_count(total_count); - app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode + UiService::initialize_adresar_count(&mut grpc_client, &mut app_state).await?; form_state.reset_to_empty(); loop { - let total_count = grpc_client.get_adresar_count().await?; - app_state.update_total_count(total_count); + UiService::update_adresar_count(&mut grpc_client, &mut app_state).await?; terminal.draw(|f| { render_ui( @@ -109,44 +96,22 @@ pub async fn run_ui() -> Result<(), Box> { form_state.current_field = 0; } else if app_state.current_position >= 1 && app_state.current_position <= total_count { // Existing entry - load data - match grpc_client.get_adresar_by_position(app_state.current_position).await { - Ok(response) => { - // Set the ID properly - form_state.id = response.id; + let current_position = app_state.current_position; + let message = UiService::load_adresar_by_position( + &mut grpc_client, + &mut app_state, + &mut form_state, + current_position + ).await?; - // Update form values dynamically - form_state.values = vec![ - response.firma, - response.kz, - response.drc, - response.ulica, - response.psc, - response.mesto, - response.stat, - response.banka, - response.ucet, - response.skladm, - response.ico, - response.kontakt, - response.telefon, - response.skladu, - response.fax, - ]; - - let current_input = form_state.get_current_input(); - let max_cursor_pos = if !event_handler.is_edit_mode && !current_input.is_empty() { - current_input.len() - 1 // In readonly mode, limit to last character - } else { - current_input.len() - }; - form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos); - form_state.has_unsaved_changes = false; - event_handler.command_message = format!("Loaded entry {}", app_state.current_position); - } - Err(e) => { - event_handler.command_message = format!("Error loading entry: {}", e); - } - } + let current_input = form_state.get_current_input(); + let max_cursor_pos = if !event_handler.is_edit_mode && !current_input.is_empty() { + current_input.len() - 1 // In readonly mode, limit to last character + } else { + current_input.len() + }; + form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos); + event_handler.command_message = message; } else { // Invalid position - reset to first entry app_state.current_position = 1; @@ -159,3 +124,4 @@ pub async fn run_ui() -> Result<(), Box> { } } } +