// src/services/ui_service.rs use crate::services::grpc_client::GrpcClient; use crate::state::pages::form::FormState; use crate::tui::functions::common::form::SaveOutcome; use crate::state::app::state::AppState; use anyhow::{Context, Result}; pub struct UiService; impl UiService { pub async fn initialize_app_state( grpc_client: &mut GrpcClient, app_state: &mut AppState, ) -> Result> { // Fetch profile tree let profile_tree = grpc_client.get_profile_tree().await.context("Failed to get profile tree")?; app_state.profile_tree = profile_tree; // TODO for general tables and not hardcoded let default_profile_name = "default".to_string(); let default_table_name = "2025_customer".to_string(); // Fetch table structure for the default table let table_structure = grpc_client .get_table_structure(default_profile_name, default_table_name) .await .context("Failed to get initial table structure")?; // 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<()> { let total_count = grpc_client.get_adresar_count().await.context("Failed to get adresar count")?; 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<()> { let total_count = grpc_client.get_adresar_count().await.context("Failed to get adresar by position")?; 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)) } } } /// Handles the consequences of a save operation, like updating counts. pub async fn handle_save_outcome( save_outcome: SaveOutcome, grpc_client: &mut GrpcClient, app_state: &mut AppState, form_state: &mut FormState, ) -> Result<()> { match save_outcome { SaveOutcome::CreatedNew(new_id) => { // A new record was created, update the count! UiService::update_adresar_count(grpc_client, app_state).await?; // Navigate to the new record (now that count is updated) app_state.update_current_position(app_state.total_count); form_state.id = new_id; // Ensure ID is set (might be redundant if save already did it) } SaveOutcome::UpdatedExisting | SaveOutcome::NoChange => { // No count update needed for these outcomes } } Ok(()) } }