complete redesign
This commit is contained in:
422
client/src/ui/handlers/event.rs
Normal file
422
client/src/ui/handlers/event.rs
Normal file
@@ -0,0 +1,422 @@
|
||||
// src/client/ui/handlers/event.rs
|
||||
|
||||
use crossterm::event::{Event, KeyCode, KeyModifiers};
|
||||
use crossterm::cursor::{SetCursorStyle};
|
||||
use crate::client::terminal::AppTerminal;
|
||||
use crate::client::config::Config;
|
||||
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
|
||||
use super::form::FormState;
|
||||
|
||||
pub struct EventHandler {
|
||||
pub command_mode: bool,
|
||||
pub command_input: String,
|
||||
pub command_message: String,
|
||||
pub is_edit_mode: bool,
|
||||
pub edit_mode_cooldown: bool,
|
||||
pub ideal_cursor_column: usize,
|
||||
}
|
||||
|
||||
impl EventHandler {
|
||||
pub fn new() -> Self {
|
||||
EventHandler {
|
||||
command_mode: false,
|
||||
command_input: String::new(),
|
||||
command_message: String::new(),
|
||||
is_edit_mode: false,
|
||||
edit_mode_cooldown: false,
|
||||
ideal_cursor_column: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle_event(
|
||||
&mut self,
|
||||
event: Event,
|
||||
config: &Config,
|
||||
app_terminal: &mut AppTerminal,
|
||||
form_state: &mut FormState,
|
||||
is_saved: &mut bool,
|
||||
total_count: u64,
|
||||
current_position: &mut u64,
|
||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
if let Event::Key(key) = event {
|
||||
if !self.is_edit_mode && config.is_enter_edit_mode(key.code, key.modifiers) {
|
||||
self.is_edit_mode = true;
|
||||
self.edit_mode_cooldown = true;
|
||||
self.command_message = "Edit mode".to_string();
|
||||
app_terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
|
||||
return Ok((false, self.command_message.clone()));
|
||||
} else if self.is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) {
|
||||
if form_state.has_unsaved_changes {
|
||||
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
self.is_edit_mode = false;
|
||||
self.edit_mode_cooldown = true;
|
||||
self.command_message = "Read-only mode".to_string();
|
||||
app_terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
|
||||
if !self.is_edit_mode {
|
||||
// Handle navigation between entries
|
||||
if key.code == KeyCode::Left {
|
||||
let new_position = current_position.saturating_sub(1);
|
||||
if new_position >= 1 {
|
||||
*current_position = new_position;
|
||||
match app_terminal.get_adresar_by_position(*current_position).await {
|
||||
Ok(response) => {
|
||||
// Update the ID field - this is what was missing
|
||||
form_state.id = response.id;
|
||||
|
||||
// Update all form fields 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,
|
||||
];
|
||||
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
form_state.has_unsaved_changes = false;
|
||||
self.command_message = format!("Loaded entry {}", *current_position);
|
||||
}
|
||||
Err(e) => {
|
||||
self.command_message = format!("Error loading entry: {}", e);
|
||||
}
|
||||
}
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
} else if key.code == KeyCode::Right {
|
||||
if *current_position <= total_count {
|
||||
*current_position += 1;
|
||||
|
||||
if *current_position <= total_count {
|
||||
match app_terminal.get_adresar_by_position(*current_position).await {
|
||||
Ok(response) => {
|
||||
|
||||
// Update the ID field - this was missing
|
||||
form_state.id = response.id;
|
||||
|
||||
// Update all form fields 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,
|
||||
];
|
||||
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
form_state.has_unsaved_changes = false;
|
||||
self.command_message = format!("Loaded entry {}", *current_position);
|
||||
}
|
||||
Err(e) => {
|
||||
self.command_message = format!("Error loading entry: {}", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Clear form when entering new entry position
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
form_state.current_cursor_pos = 0;
|
||||
self.command_message = "New entry mode".to_string();
|
||||
}
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
} else {
|
||||
// Handle movement keybindings
|
||||
if let Some(action) = config.get_action_for_key(key.code, key.modifiers) {
|
||||
match action {
|
||||
"move_left" => {
|
||||
form_state.current_cursor_pos = form_state.current_cursor_pos.saturating_sub(1);
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
"move_right" => {
|
||||
let current_input = form_state.get_current_input();
|
||||
if form_state.current_cursor_pos < current_input.len() {
|
||||
form_state.current_cursor_pos += 1;
|
||||
}
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
"move_up" => {
|
||||
if form_state.current_field == 0 {
|
||||
// Wrap to the last field when at the top
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
} else {
|
||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
||||
}
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
"move_down" => {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// Handle other keys (e.g., command mode)
|
||||
match key.code {
|
||||
KeyCode::Char(':') => {
|
||||
self.command_mode = true;
|
||||
self.command_input.clear();
|
||||
self.command_message.clear();
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
self.command_mode = false;
|
||||
self.command_input.clear();
|
||||
self.command_message.clear();
|
||||
}
|
||||
_ => {
|
||||
if !self.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);
|
||||
self.command_message = format!("Read-only mode - press {} to edit", edit_key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Edit mode handling
|
||||
if self.command_mode {
|
||||
match key.code {
|
||||
KeyCode::Enter => {
|
||||
let command = self.command_input.trim();
|
||||
if command.is_empty() {
|
||||
self.command_message = "Empty command".to_string();
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
|
||||
let action = config.get_action_for_command(command)
|
||||
.unwrap_or("unknown");
|
||||
|
||||
if action == "save" {
|
||||
let is_new = *current_position == total_count + 1;
|
||||
|
||||
let message = if is_new {
|
||||
// POST new entry
|
||||
let post_request = PostAdresarRequest {
|
||||
firma: form_state.values[0].clone(),
|
||||
kz: form_state.values[1].clone(),
|
||||
drc: form_state.values[2].clone(),
|
||||
ulica: form_state.values[3].clone(),
|
||||
psc: form_state.values[4].clone(),
|
||||
mesto: form_state.values[5].clone(),
|
||||
stat: form_state.values[6].clone(),
|
||||
banka: form_state.values[7].clone(),
|
||||
ucet: form_state.values[8].clone(),
|
||||
skladm: form_state.values[9].clone(),
|
||||
ico: form_state.values[10].clone(),
|
||||
kontakt: form_state.values[11].clone(),
|
||||
telefon: form_state.values[12].clone(),
|
||||
skladu: form_state.values[13].clone(),
|
||||
fax: form_state.values[14].clone(),
|
||||
};
|
||||
let response = app_terminal.post_adresar(post_request).await?;
|
||||
// Update state
|
||||
let new_total = app_terminal.get_adresar_count().await?;
|
||||
*current_position = new_total;
|
||||
form_state.id = response.into_inner().id;
|
||||
"New entry created".to_string()
|
||||
} else {
|
||||
// PUT existing entry
|
||||
let put_request = PutAdresarRequest {
|
||||
id: form_state.id,
|
||||
firma: form_state.values[0].clone(),
|
||||
kz: form_state.values[1].clone(),
|
||||
drc: form_state.values[2].clone(),
|
||||
ulica: form_state.values[3].clone(),
|
||||
psc: form_state.values[4].clone(),
|
||||
mesto: form_state.values[5].clone(),
|
||||
stat: form_state.values[6].clone(),
|
||||
banka: form_state.values[7].clone(),
|
||||
ucet: form_state.values[8].clone(),
|
||||
skladm: form_state.values[9].clone(),
|
||||
ico: form_state.values[10].clone(),
|
||||
kontakt: form_state.values[11].clone(),
|
||||
telefon: form_state.values[12].clone(),
|
||||
skladu: form_state.values[13].clone(),
|
||||
fax: form_state.values[14].clone(),
|
||||
};
|
||||
let _ = app_terminal.put_adresar(put_request).await?;
|
||||
"Entry updated".to_string()
|
||||
};
|
||||
|
||||
*is_saved = true;
|
||||
form_state.has_unsaved_changes = false;
|
||||
self.command_input.clear(); // Clear the command input
|
||||
self.command_mode = false; // Reset command mode
|
||||
self.command_message.clear(); // Clear the command message
|
||||
return Ok((false, message));
|
||||
} else {
|
||||
let (should_exit, message) = app_terminal
|
||||
.handle_command(action, is_saved)
|
||||
.await?;
|
||||
self.command_message = message;
|
||||
self.command_input.clear(); // Clear the command input
|
||||
self.command_mode = false; // Reset command mode
|
||||
return Ok((should_exit, self.command_message.clone()));
|
||||
}
|
||||
}
|
||||
KeyCode::Char(c) => self.command_input.push(c),
|
||||
KeyCode::Backspace => {
|
||||
self.command_input.pop();
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
self.command_mode = false;
|
||||
self.command_input.clear();
|
||||
self.command_message.clear();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
// Handle arrow keys in edit mode
|
||||
match key.code {
|
||||
KeyCode::Left => {
|
||||
form_state.current_cursor_pos = form_state.current_cursor_pos.saturating_sub(1);
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
KeyCode::Right => {
|
||||
let current_input = form_state.get_current_input();
|
||||
if form_state.current_cursor_pos < current_input.len() {
|
||||
form_state.current_cursor_pos += 1;
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
}
|
||||
return Ok((false, "".to_string()));
|
||||
}
|
||||
KeyCode::Char(':') => {
|
||||
self.command_mode = true;
|
||||
self.command_input.clear();
|
||||
self.command_message.clear();
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
if config.is_exit_edit_mode(key.code, key.modifiers) {
|
||||
if form_state.has_unsaved_changes {
|
||||
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
self.is_edit_mode = false;
|
||||
self.edit_mode_cooldown = true;
|
||||
self.command_message = "Read-only mode".to_string();
|
||||
return Ok((false, self.command_message.clone()));
|
||||
}
|
||||
}
|
||||
KeyCode::Down => {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
}
|
||||
KeyCode::Up => {
|
||||
if form_state.current_field == 0 {
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
} else {
|
||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
||||
}
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
}
|
||||
KeyCode::Tab => {
|
||||
if key.modifiers.contains(KeyModifiers::SHIFT) {
|
||||
if form_state.current_field == 0 {
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
} else {
|
||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
||||
}
|
||||
} else {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
}
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
}
|
||||
KeyCode::BackTab => {
|
||||
if form_state.current_field == 0 {
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
} else {
|
||||
form_state.current_field = form_state.current_field.saturating_sub(1);
|
||||
}
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
let cursor_pos = form_state.current_cursor_pos;
|
||||
let field_value = form_state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos <= chars.len() {
|
||||
chars.insert(cursor_pos, c);
|
||||
*field_value = chars.into_iter().collect();
|
||||
form_state.current_cursor_pos = cursor_pos + 1;
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
form_state.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
if form_state.current_cursor_pos > 0 {
|
||||
let cursor_pos = form_state.current_cursor_pos;
|
||||
let field_value = form_state.get_current_input_mut();
|
||||
let mut chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos <= chars.len() && cursor_pos > 0 {
|
||||
chars.remove(cursor_pos - 1);
|
||||
*field_value = chars.into_iter().collect();
|
||||
form_state.current_cursor_pos = cursor_pos - 1;
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos;
|
||||
form_state.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Delete => {
|
||||
let cursor_pos = form_state.current_cursor_pos;
|
||||
let field_value = form_state.get_current_input_mut();
|
||||
let chars: Vec<char> = field_value.chars().collect();
|
||||
if cursor_pos < chars.len() {
|
||||
let mut new_chars = chars.clone();
|
||||
new_chars.remove(cursor_pos);
|
||||
*field_value = new_chars.into_iter().collect();
|
||||
form_state.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.edit_mode_cooldown = false;
|
||||
Ok((false, self.command_message.clone()))
|
||||
}
|
||||
}
|
||||
77
client/src/ui/handlers/form.rs
Normal file
77
client/src/ui/handlers/form.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
// src/client/ui/handlers/form.rs
|
||||
|
||||
use crate::client::components1::render_form;
|
||||
use crate::client::colors::Theme;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::Frame;
|
||||
|
||||
pub struct FormState {
|
||||
pub id: i64,
|
||||
pub fields: Vec<String>, // Use Vec<String> for dynamic field names
|
||||
pub values: Vec<String>, // Store field values dynamically
|
||||
pub current_field: usize,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub current_cursor_pos: usize,
|
||||
}
|
||||
|
||||
impl FormState {
|
||||
/// Create a new FormState with dynamic fields.
|
||||
pub fn new(fields: Vec<String>) -> Self {
|
||||
let values = vec![String::new(); fields.len()]; // Initialize values for each field
|
||||
FormState {
|
||||
id: 0,
|
||||
fields,
|
||||
values,
|
||||
current_field: 0,
|
||||
has_unsaved_changes: false,
|
||||
current_cursor_pos: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(
|
||||
&self,
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
) {
|
||||
// Convert Vec<String> to Vec<&str> for fields
|
||||
let fields: Vec<&str> = self.fields.iter().map(|s| s.as_str()).collect();
|
||||
// Convert Vec<String> to Vec<&String> for values
|
||||
let values: Vec<&String> = self.values.iter().collect();
|
||||
|
||||
render_form(
|
||||
f,
|
||||
area,
|
||||
self,
|
||||
&fields,
|
||||
&self.current_field,
|
||||
&values,
|
||||
&theme,
|
||||
is_edit_mode,
|
||||
total_count,
|
||||
current_position,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn reset_to_empty(&mut self) {
|
||||
self.id = 0; // Reset ID to 0 for new entries
|
||||
self.values.iter_mut().for_each(|v| v.clear()); // Clear all values
|
||||
self.has_unsaved_changes = false;
|
||||
}
|
||||
|
||||
pub fn get_current_input(&self) -> &str {
|
||||
self.values
|
||||
.get(self.current_field)
|
||||
.map(|s| s.as_str())
|
||||
.unwrap_or("")
|
||||
}
|
||||
|
||||
pub fn get_current_input_mut(&mut self) -> &mut String {
|
||||
self.values
|
||||
.get_mut(self.current_field)
|
||||
.expect("Invalid current_field index")
|
||||
}
|
||||
}
|
||||
53
client/src/ui/handlers/render.rs
Normal file
53
client/src/ui/handlers/render.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
// src/client/ui/handlers/render.rs
|
||||
|
||||
use crate::client::components1::{render_command_line, render_preview_card, render_status_line};
|
||||
use crate::client::colors::Theme;
|
||||
use ratatui::layout::{Constraint, Direction, Layout};
|
||||
use ratatui::Frame;
|
||||
use super::form::FormState;
|
||||
|
||||
pub fn render_ui(
|
||||
f: &mut Frame,
|
||||
form_state: &mut FormState,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
current_dir: &str,
|
||||
command_input: &str,
|
||||
command_mode: bool,
|
||||
command_message: &str,
|
||||
) {
|
||||
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
|
||||
form_state.render(f, main_chunks[0], theme, is_edit_mode, total_count, current_position);
|
||||
|
||||
// Right panel - Preview Card
|
||||
let preview_values: Vec<&String> = form_state.values.iter().collect();
|
||||
render_preview_card(
|
||||
f,
|
||||
main_chunks[1],
|
||||
&preview_values, // Pass dynamic values as &[&String]
|
||||
&theme,
|
||||
);
|
||||
|
||||
// Status line
|
||||
render_status_line(f, root[1], current_dir, theme, is_edit_mode);
|
||||
|
||||
// Command line
|
||||
render_command_line(f, root[2], command_input, command_mode, theme, command_message);
|
||||
}
|
||||
32
client/src/ui/handlers/state.rs
Normal file
32
client/src/ui/handlers/state.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
// src/client/ui/handlers/state.rs
|
||||
|
||||
use std::env;
|
||||
|
||||
pub struct AppState {
|
||||
pub is_saved: bool,
|
||||
pub current_dir: String,
|
||||
pub total_count: u64,
|
||||
pub current_position: u64,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let current_dir = env::current_dir()?
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
Ok(AppState {
|
||||
is_saved: false,
|
||||
current_dir,
|
||||
total_count: 0,
|
||||
current_position: 0,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn update_total_count(&mut self, total_count: u64) {
|
||||
self.total_count = total_count;
|
||||
}
|
||||
|
||||
pub fn update_current_position(&mut self, current_position: u64) {
|
||||
self.current_position = current_position;
|
||||
}
|
||||
}
|
||||
123
client/src/ui/handlers/ui.rs
Normal file
123
client/src/ui/handlers/ui.rs
Normal file
@@ -0,0 +1,123 @@
|
||||
// src/client/ui/handlers/ui.rs
|
||||
|
||||
use crate::client::terminal::AppTerminal;
|
||||
use crate::client::colors::Theme;
|
||||
use crate::client::config::Config;
|
||||
use crate::client::ui::handlers::{event::EventHandler, form::FormState, state::AppState, render::render_ui};
|
||||
|
||||
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = Config::load()?;
|
||||
let mut app_terminal = AppTerminal::new().await?;
|
||||
let theme = Theme::from_str(&config.colors.theme);
|
||||
|
||||
// Fetch table structure at startup (one-time)
|
||||
// TODO: Later, consider implementing a live update for table structure changes.
|
||||
let table_structure = app_terminal.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
|
||||
let mut form_state = FormState::new(column_names);
|
||||
|
||||
// The rest of your UI initialization remains the same
|
||||
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?;
|
||||
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();
|
||||
|
||||
loop {
|
||||
let total_count = app_terminal.get_adresar_count().await?;
|
||||
app_state.update_total_count(total_count);
|
||||
|
||||
app_terminal.draw(|f| {
|
||||
render_ui(
|
||||
f,
|
||||
&mut form_state,
|
||||
&theme,
|
||||
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,
|
||||
);
|
||||
})?;
|
||||
|
||||
let 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?;
|
||||
|
||||
// Handle position changes and update form state
|
||||
if !event_handler.is_edit_mode {
|
||||
// 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
|
||||
match app_terminal.get_adresar_by_position(app_state.current_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,
|
||||
];
|
||||
|
||||
let current_input = form_state.get_current_input();
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(current_input.len());
|
||||
form_state.has_unsaved_changes = false;
|
||||
event_handler.command_message = format!("Loaded entry {}", app_state.current_position);
|
||||
}
|
||||
Err(e) => {
|
||||
event_handler.command_message = format!("Error loading entry: {}", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Invalid position - reset to first entry
|
||||
app_state.current_position = 1;
|
||||
}
|
||||
}
|
||||
|
||||
event_handler.command_message = message;
|
||||
if should_exit {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user