redesign fixing errors
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// src/client/ui.rs
|
||||
// src/client/ui/handlers/ui.rs
|
||||
|
||||
use crossterm::event::{Event, KeyCode, KeyModifiers};
|
||||
use crate::client::terminal::AppTerminal;
|
||||
@@ -9,321 +9,59 @@ use ratatui::layout::{Constraint, Direction, Layout};
|
||||
use std::env;
|
||||
use crate::proto::multieko2::PostAdresarRequest;
|
||||
|
||||
use super::form::FormState;
|
||||
use super::event::EventHandler;
|
||||
use super::render::render_ui;
|
||||
use super::state::AppState;
|
||||
|
||||
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = Config::load()?;
|
||||
let mut app_terminal = AppTerminal::new().await?;
|
||||
let mut command_mode = false;
|
||||
let mut command_input = String::new();
|
||||
let theme = Theme::dark();
|
||||
|
||||
// Initialize form fields
|
||||
let mut firma = String::new();
|
||||
let mut kz = String::new();
|
||||
let mut drc = String::new();
|
||||
let mut ulica = String::new();
|
||||
let mut psc = String::new();
|
||||
let mut mesto = String::new();
|
||||
let mut stat = String::new();
|
||||
let mut banka = String::new();
|
||||
let mut ucet = String::new();
|
||||
let mut skladm = String::new();
|
||||
let mut ico = String::new();
|
||||
let mut kontakt = String::new();
|
||||
let mut telefon = String::new();
|
||||
let mut skladu = String::new();
|
||||
let mut fax = String::new();
|
||||
|
||||
let mut current_field: usize = 0;
|
||||
let fields = vec![
|
||||
"Firma", "KZ", "DRC", "Ulica", "PSC", "Mesto", "Stat", "Banka",
|
||||
"Ucet", "Skladm", "ICO", "Kontakt", "Telefon", "Skladu", "Fax",
|
||||
];
|
||||
|
||||
// Get the current directory
|
||||
let current_dir = env::current_dir()?
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
|
||||
// Track whether the state has been saved
|
||||
let mut is_saved = false;
|
||||
|
||||
// Track the current message to display in the command line
|
||||
let mut command_message = String::new();
|
||||
let mut is_edit_mode = false;
|
||||
let mut edit_mode_cooldown = false;
|
||||
let mut form_state = FormState::new();
|
||||
let mut event_handler = EventHandler::new();
|
||||
let mut app_state = AppState::new()?;
|
||||
|
||||
// Fetch the total count of Adresar entries
|
||||
let total_count = app_terminal.get_adresar_count().await?;
|
||||
|
||||
// Track the current position in the database sequence
|
||||
let mut current_position = total_count + 1; // Start at the next position for new entries
|
||||
app_state.update_total_count(total_count);
|
||||
app_state.update_current_position(total_count + 1);
|
||||
|
||||
loop {
|
||||
// Fetch fresh total count on each iteration
|
||||
let total_count = app_terminal.get_adresar_count().await?;
|
||||
app_state.update_total_count(total_count);
|
||||
|
||||
app_terminal.draw(|f| {
|
||||
let root = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Min(10), // Main content area
|
||||
Constraint::Length(1), // Status line
|
||||
Constraint::Length(1), // Command line
|
||||
])
|
||||
.split(f.area());
|
||||
|
||||
// Main content area
|
||||
let main_chunks = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(60), Constraint::Percentage(40)])
|
||||
.split(root[0]);
|
||||
|
||||
// Left panel - Form
|
||||
render_form(
|
||||
render_ui(
|
||||
f,
|
||||
main_chunks[0],
|
||||
&fields,
|
||||
&mut current_field,
|
||||
&[
|
||||
&firma, &kz, &drc, &ulica, &psc, &mesto, &stat, &banka,
|
||||
&ucet, &skladm, &ico, &kontakt, &telefon, &skladu, &fax,
|
||||
],
|
||||
&mut form_state,
|
||||
&theme,
|
||||
is_edit_mode,
|
||||
total_count, // Pass total count
|
||||
current_position, // Pass current position
|
||||
event_handler.is_edit_mode,
|
||||
app_state.total_count,
|
||||
app_state.current_position,
|
||||
&app_state.current_dir,
|
||||
&event_handler.command_input,
|
||||
event_handler.command_mode,
|
||||
&event_handler.command_message,
|
||||
);
|
||||
|
||||
// Right panel - Preview Card
|
||||
render_preview_card(
|
||||
f,
|
||||
main_chunks[1],
|
||||
&[
|
||||
&firma, &ulica, &mesto, &psc, &ico, &kontakt, &telefon,
|
||||
],
|
||||
&theme,
|
||||
);
|
||||
|
||||
// Status line
|
||||
render_status_line(f, root[1], ¤t_dir, &theme, is_edit_mode);
|
||||
|
||||
// Command line
|
||||
render_command_line(f, root[2], &command_input, command_mode, &theme, &command_message);
|
||||
})?;
|
||||
|
||||
// Event handling
|
||||
if let Event::Key(key) = app_terminal.read_event()? {
|
||||
// Handle enter/edit mode keys
|
||||
if !is_edit_mode && config.is_enter_edit_mode(key.code, key.modifiers) {
|
||||
is_edit_mode = true;
|
||||
edit_mode_cooldown = true;
|
||||
command_message = "Edit mode".to_string();
|
||||
continue;
|
||||
} else if is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) {
|
||||
is_edit_mode = false;
|
||||
edit_mode_cooldown = true;
|
||||
command_message = "Read-only mode".to_string();
|
||||
continue;
|
||||
}
|
||||
|
||||
if !is_edit_mode {
|
||||
// Read-only mode handling
|
||||
match key.code {
|
||||
KeyCode::Char(':') => {
|
||||
command_mode = true;
|
||||
command_input.clear();
|
||||
command_message.clear();
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
command_mode = false;
|
||||
command_input.clear();
|
||||
command_message.clear();
|
||||
}
|
||||
// Allow navigation but prevent editing
|
||||
KeyCode::Tab | KeyCode::BackTab | KeyCode::Down | KeyCode::Up => {
|
||||
if key.modifiers.contains(KeyModifiers::SHIFT) {
|
||||
current_field = current_field.saturating_sub(1);
|
||||
} else {
|
||||
current_field = (current_field + 1) % fields.len();
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Block all other inputs
|
||||
if !edit_mode_cooldown {
|
||||
let default_key = "i".to_string();
|
||||
let edit_key = config.keybindings.get("enter_edit_mode")
|
||||
.and_then(|keys| keys.first())
|
||||
.unwrap_or(&default_key);
|
||||
command_message = format!("Read-only mode - press {} to edit", edit_key);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Existing edit mode handling
|
||||
if command_mode {
|
||||
match key.code {
|
||||
KeyCode::Enter => {
|
||||
// Create PostAdresarRequest from form data
|
||||
let form_data = PostAdresarRequest {
|
||||
firma: firma.clone(),
|
||||
kz: kz.clone(),
|
||||
drc: drc.clone(),
|
||||
ulica: ulica.clone(),
|
||||
psc: psc.clone(),
|
||||
mesto: mesto.clone(),
|
||||
stat: stat.clone(),
|
||||
banka: banka.clone(),
|
||||
ucet: ucet.clone(),
|
||||
skladm: skladm.clone(),
|
||||
ico: ico.clone(),
|
||||
kontakt: kontakt.clone(),
|
||||
telefon: telefon.clone(),
|
||||
skladu: skladu.clone(),
|
||||
fax: fax.clone(),
|
||||
};
|
||||
|
||||
// Validate command format
|
||||
let command = command_input.trim();
|
||||
if command.is_empty() {
|
||||
command_message = "Empty command".to_string();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Look up the action for the command string (e.g., "w")
|
||||
let action = config.get_action_for_command(command)
|
||||
.unwrap_or("unknown");
|
||||
|
||||
// Pass the resolved action to handle_command
|
||||
let (should_exit, message) = app_terminal
|
||||
.handle_command(action, &mut is_saved, &form_data)
|
||||
.await?;
|
||||
|
||||
command_message = message;
|
||||
command_mode = false;
|
||||
command_input.clear();
|
||||
|
||||
// Update current position after saving
|
||||
if action == "save" && is_saved {
|
||||
current_position = total_count + 1;
|
||||
}
|
||||
|
||||
if should_exit {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
KeyCode::Char(c) => command_input.push(c),
|
||||
KeyCode::Backspace => {
|
||||
command_input.pop();
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
command_mode = false;
|
||||
command_input.clear();
|
||||
command_message.clear();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
// Check for keybindings
|
||||
if let Some(action) = config.get_action_for_key(key.code, key.modifiers) {
|
||||
let form_data = PostAdresarRequest {
|
||||
firma: firma.clone(),
|
||||
kz: kz.clone(),
|
||||
drc: drc.clone(),
|
||||
ulica: ulica.clone(),
|
||||
psc: psc.clone(),
|
||||
mesto: mesto.clone(),
|
||||
stat: stat.clone(),
|
||||
banka: banka.clone(),
|
||||
ucet: ucet.clone(),
|
||||
skladm: skladm.clone(),
|
||||
ico: ico.clone(),
|
||||
kontakt: kontakt.clone(),
|
||||
telefon: telefon.clone(),
|
||||
skladu: skladu.clone(),
|
||||
fax: fax.clone(),
|
||||
};
|
||||
|
||||
let (should_exit, message) = app_terminal
|
||||
.handle_command(action, &mut is_saved, &form_data)
|
||||
.await?;
|
||||
command_message = message;
|
||||
if should_exit {
|
||||
return Ok(());
|
||||
}
|
||||
} else {
|
||||
match key.code {
|
||||
KeyCode::Char(':') => {
|
||||
command_mode = true;
|
||||
command_input.clear();
|
||||
command_message.clear();
|
||||
}
|
||||
KeyCode::Tab => {
|
||||
if key.modifiers.contains(KeyModifiers::SHIFT) {
|
||||
current_field = current_field.saturating_sub(1);
|
||||
} else {
|
||||
current_field = (current_field + 1) % fields.len();
|
||||
}
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
// Explicitly handle Esc even if not in command mode
|
||||
if config.is_exit_edit_mode(key.code, key.modifiers) {
|
||||
is_edit_mode = false;
|
||||
edit_mode_cooldown = true;
|
||||
command_message = "Read-only mode".to_string();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
KeyCode::BackTab => current_field = current_field.saturating_sub(1),
|
||||
KeyCode::Down => current_field = (current_field + 1) % fields.len(),
|
||||
KeyCode::Up => current_field = current_field.saturating_sub(1),
|
||||
KeyCode::Enter => current_field = (current_field + 1) % fields.len(),
|
||||
KeyCode::Char(c) => {
|
||||
match current_field {
|
||||
0 => firma.push(c),
|
||||
1 => kz.push(c),
|
||||
2 => drc.push(c),
|
||||
3 => ulica.push(c),
|
||||
4 => psc.push(c),
|
||||
5 => mesto.push(c),
|
||||
6 => stat.push(c),
|
||||
7 => banka.push(c),
|
||||
8 => ucet.push(c),
|
||||
9 => skladm.push(c),
|
||||
10 => ico.push(c),
|
||||
11 => kontakt.push(c),
|
||||
12 => telefon.push(c),
|
||||
13 => skladu.push(c),
|
||||
14 => fax.push(c),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
match current_field {
|
||||
0 => firma.pop(),
|
||||
1 => kz.pop(),
|
||||
2 => drc.pop(),
|
||||
3 => ulica.pop(),
|
||||
4 => psc.pop(),
|
||||
5 => mesto.pop(),
|
||||
6 => stat.pop(),
|
||||
7 => banka.pop(),
|
||||
8 => ucet.pop(),
|
||||
9 => skladm.pop(),
|
||||
10 => ico.pop(),
|
||||
11 => kontakt.pop(),
|
||||
12 => telefon.pop(),
|
||||
13 => skladu.pop(),
|
||||
14 => fax.pop(),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(event) = app_terminal.read_event()? {
|
||||
let (should_exit, message) = event_handler.handle_event(
|
||||
event,
|
||||
&config,
|
||||
&mut app_terminal,
|
||||
&mut form_state,
|
||||
&mut app_state.is_saved,
|
||||
app_state.total_count,
|
||||
&mut app_state.current_position,
|
||||
).await?;
|
||||
event_handler.command_message = message;
|
||||
if should_exit {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
edit_mode_cooldown = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user