ui.rs gRPC code removed
This commit is contained in:
@@ -5,9 +5,11 @@ pub mod adresar;
|
|||||||
pub mod table;
|
pub mod table;
|
||||||
pub mod profile;
|
pub mod profile;
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
|
pub mod ui_service;
|
||||||
|
|
||||||
pub use grpc_client::*;
|
pub use grpc_client::*;
|
||||||
pub use adresar::*;
|
pub use adresar::*;
|
||||||
pub use table::*;
|
pub use table::*;
|
||||||
pub use profile::*;
|
pub use profile::*;
|
||||||
pub use auth::*;
|
pub use auth::*;
|
||||||
|
pub use ui_service::*;
|
||||||
|
|||||||
90
client/src/services/ui_service.rs
Normal file
90
client/src/services/ui_service.rs
Normal file
@@ -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<Vec<String>, Box<dyn std::error::Error>> {
|
||||||
|
// 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<String> = 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<dyn std::error::Error>> {
|
||||||
|
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<dyn std::error::Error>> {
|
||||||
|
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<String, Box<dyn std::error::Error>> {
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use crate::tui::terminal::TerminalCore;
|
use crate::tui::terminal::TerminalCore;
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::services::ui_service::UiService; // Add this import
|
||||||
use crate::tui::controls::CommandHandler;
|
use crate::tui::controls::CommandHandler;
|
||||||
use crate::tui::terminal::EventReader;
|
use crate::tui::terminal::EventReader;
|
||||||
use crate::config::colors::themes::Theme;
|
use crate::config::colors::themes::Theme;
|
||||||
@@ -23,19 +24,8 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// Initialize app_state first
|
// Initialize app_state first
|
||||||
let mut app_state = AppState::new()?;
|
let mut app_state = AppState::new()?;
|
||||||
|
|
||||||
// Fetch profile tree and table structure
|
// Initialize app state with profile tree and table structure
|
||||||
let profile_tree = grpc_client.get_profile_tree().await?;
|
let column_names = UiService::initialize_app_state(&mut grpc_client, &mut app_state).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<String> = table_structure
|
|
||||||
.columns
|
|
||||||
.iter()
|
|
||||||
.map(|col| col.name.clone())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// Initialize FormState with dynamic fields
|
// Initialize FormState with dynamic fields
|
||||||
let mut form_state = FormState::new(column_names);
|
let mut form_state = FormState::new(column_names);
|
||||||
@@ -45,14 +35,11 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let event_reader = EventReader::new();
|
let event_reader = EventReader::new();
|
||||||
|
|
||||||
// Fetch the total count of Adresar entries
|
// Fetch the total count of Adresar entries
|
||||||
let total_count = grpc_client.get_adresar_count().await?;
|
UiService::initialize_adresar_count(&mut grpc_client, &mut app_state).await?;
|
||||||
app_state.update_total_count(total_count);
|
|
||||||
app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode
|
|
||||||
form_state.reset_to_empty();
|
form_state.reset_to_empty();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let total_count = grpc_client.get_adresar_count().await?;
|
UiService::update_adresar_count(&mut grpc_client, &mut app_state).await?;
|
||||||
app_state.update_total_count(total_count);
|
|
||||||
|
|
||||||
terminal.draw(|f| {
|
terminal.draw(|f| {
|
||||||
render_ui(
|
render_ui(
|
||||||
@@ -109,29 +96,13 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
form_state.current_field = 0;
|
form_state.current_field = 0;
|
||||||
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
|
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
|
||||||
// Existing entry - load data
|
// Existing entry - load data
|
||||||
match grpc_client.get_adresar_by_position(app_state.current_position).await {
|
let current_position = app_state.current_position;
|
||||||
Ok(response) => {
|
let message = UiService::load_adresar_by_position(
|
||||||
// Set the ID properly
|
&mut grpc_client,
|
||||||
form_state.id = response.id;
|
&mut app_state,
|
||||||
|
&mut form_state,
|
||||||
// Update form values dynamically
|
current_position
|
||||||
form_state.values = vec![
|
).await?;
|
||||||
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 current_input = form_state.get_current_input();
|
||||||
let max_cursor_pos = if !event_handler.is_edit_mode && !current_input.is_empty() {
|
let max_cursor_pos = if !event_handler.is_edit_mode && !current_input.is_empty() {
|
||||||
@@ -140,13 +111,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
current_input.len()
|
current_input.len()
|
||||||
};
|
};
|
||||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||||
form_state.has_unsaved_changes = false;
|
event_handler.command_message = message;
|
||||||
event_handler.command_message = format!("Loaded entry {}", app_state.current_position);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
event_handler.command_message = format!("Error loading entry: {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Invalid position - reset to first entry
|
// Invalid position - reset to first entry
|
||||||
app_state.current_position = 1;
|
app_state.current_position = 1;
|
||||||
@@ -159,3 +124,4 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user