working inputting characters
This commit is contained in:
@@ -11,6 +11,7 @@ use crate::config::binds::config::Config;
|
||||
use crate::ui::handlers::render::render_ui;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
use crate::modes::handlers::event::EventHandler;
|
||||
use crate::state::state::AppState;
|
||||
|
||||
@@ -18,10 +19,10 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = Config::load()?;
|
||||
let mut terminal = TerminalCore::new()?;
|
||||
let mut grpc_client = GrpcClient::new().await?;
|
||||
let auth_client = AuthClient::new().await?;
|
||||
// let auth_client = AuthClient::new().await?; // AuthClient is now inside EventHandler
|
||||
let mut command_handler = CommandHandler::new();
|
||||
let theme = Theme::from_str(&config.colors.theme);
|
||||
let mut auth_state = AuthState::default();
|
||||
let mut auth_state = AuthState::default(); // The single source of truth for AuthState
|
||||
|
||||
// Initialize app_state first
|
||||
let mut app_state = AppState::new()?;
|
||||
@@ -32,7 +33,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Initialize FormState with dynamic fields
|
||||
let mut form_state = FormState::new(column_names);
|
||||
|
||||
// The rest of your UI initialization remains the same
|
||||
// Initialize EventHandler (which now contains AuthClient)
|
||||
let mut event_handler = EventHandler::new().await?;
|
||||
let event_reader = EventReader::new();
|
||||
|
||||
@@ -43,13 +44,16 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
loop {
|
||||
UiService::update_adresar_count(&mut grpc_client, &mut app_state).await?;
|
||||
|
||||
// Determine edit mode based on EventHandler state
|
||||
let is_edit_mode = event_handler.is_edit_mode;
|
||||
|
||||
terminal.draw(|f| {
|
||||
render_ui(
|
||||
f,
|
||||
&mut form_state,
|
||||
&mut auth_state,
|
||||
&mut auth_state, // Pass the single AuthState instance
|
||||
&theme,
|
||||
event_handler.is_edit_mode,
|
||||
is_edit_mode, // Use determined edit mode
|
||||
app_state.total_count,
|
||||
app_state.current_position,
|
||||
&app_state.current_dir,
|
||||
@@ -71,6 +75,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
&mut grpc_client,
|
||||
&mut command_handler,
|
||||
&mut form_state,
|
||||
&mut auth_state, // Pass the single AuthState instance here too
|
||||
&mut app_state,
|
||||
total_count,
|
||||
&mut current_position,
|
||||
@@ -78,49 +83,74 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
app_state.current_position = current_position;
|
||||
|
||||
// Handle position changes and update form state
|
||||
if !event_handler.is_edit_mode {
|
||||
let current_input = form_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
current_input.len() - 1 // Limit to last character in readonly mode
|
||||
} else {
|
||||
0
|
||||
};
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
|
||||
// Ensure position never exceeds total_count + 1
|
||||
if app_state.current_position > total_count + 1 {
|
||||
app_state.current_position = total_count + 1;
|
||||
}
|
||||
if app_state.current_position > total_count {
|
||||
// New entry - reset form
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
|
||||
// Existing entry - load data
|
||||
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?;
|
||||
|
||||
// Handle position changes and update form state (Only when form is shown)
|
||||
if app_state.ui.show_form { // Added check
|
||||
if !event_handler.is_edit_mode {
|
||||
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
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
current_input.len() - 1 // Limit to last character in readonly mode
|
||||
} else {
|
||||
current_input.len()
|
||||
0
|
||||
};
|
||||
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;
|
||||
|
||||
// Ensure position never exceeds total_count + 1
|
||||
if app_state.current_position > total_count + 1 {
|
||||
app_state.current_position = total_count + 1;
|
||||
}
|
||||
if app_state.current_position > total_count {
|
||||
// New entry - reset form
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
|
||||
// Existing entry - load data
|
||||
let current_position_to_load = app_state.current_position; // Use a copy
|
||||
let load_message = UiService::load_adresar_by_position(
|
||||
&mut grpc_client,
|
||||
&mut app_state, // Pass app_state mutably if needed by the service
|
||||
&mut form_state,
|
||||
current_position_to_load
|
||||
).await?;
|
||||
|
||||
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);
|
||||
// Don't overwrite message from handle_event if load_message is simple success
|
||||
if !load_message.starts_with("Loaded entry") || message.is_empty() {
|
||||
event_handler.command_message = load_message;
|
||||
}
|
||||
} else {
|
||||
// Invalid position (e.g., 0) - reset to first entry or new entry mode
|
||||
app_state.current_position = 1.min(total_count + 1); // Go to 1 or new entry if empty
|
||||
if app_state.current_position > total_count {
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if app_state.ui.show_login {
|
||||
// Handle cursor updates for AuthState if needed, similar to FormState
|
||||
if !event_handler.is_edit_mode {
|
||||
let current_input = auth_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
current_input.len() - 1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
auth_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Only update command message if handle_event provided one
|
||||
if !message.is_empty() {
|
||||
event_handler.command_message = message;
|
||||
}
|
||||
|
||||
event_handler.command_message = message;
|
||||
if should_exit {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user