// src/services/ui_service.rs use crate::services::grpc_client::GrpcClient; use crate::state::pages::form::FormState; use crate::state::state::AppState; 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)) } } } }