From dc4e3dfa337cff6a80b173287809f8bdfcdc511f Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sun, 13 Aug 2023 20:20:10 -0700 Subject: [PATCH] added navigation and selection to new transaction manual tab --- src/uis/new_transaction.rs | 172 ++++++++++++++++++++++++++----------- 1 file changed, 120 insertions(+), 52 deletions(-) diff --git a/src/uis/new_transaction.rs b/src/uis/new_transaction.rs index 7ac7697..74d5b26 100644 --- a/src/uis/new_transaction.rs +++ b/src/uis/new_transaction.rs @@ -5,7 +5,7 @@ use crate::{ use crossterm::event::{KeyCode, KeyEvent, KeyEventKind}; use ratatui::{ backend::Backend, - layout::{Constraint, Direction, Layout, Rect, Alignment}, + layout::{Alignment, Constraint, Direction, Layout, Rect}, style::{Color, Style}, text::Text, widgets::{Block, Borders, Paragraph}, @@ -52,6 +52,21 @@ impl ManualData { breakdowns: Vec::new(), } } + pub fn send_tab(&mut self) { + let next = if self.focus.is_some() { + match self.focus.unwrap() { + ManualDataFocus::Account => ManualDataFocus::Amount, + ManualDataFocus::Amount => ManualDataFocus::Date, + ManualDataFocus::Date => ManualDataFocus::Bucket, + ManualDataFocus::Bucket => ManualDataFocus::Description, + ManualDataFocus::Description => ManualDataFocus::Breakdowns, + ManualDataFocus::Breakdowns => ManualDataFocus::Account, + } + } else { + ManualDataFocus::Account + }; + self.focus = Some(next); + } } pub struct NewTransactionTabState<'a> { @@ -75,10 +90,16 @@ impl<'a> NewTransactionTabState<'a> { } pub fn handle_event(event: KeyEvent, app: &mut App) { + let transact_state = &app.states.transactions; + if event.kind == KeyEventKind::Press { - match event.code { - KeyCode::Tab => app.states.transactions.next_tab(), - _ => {} + if transact_state.cur_tab_index == 1 { + handle_manual_event(event, app); + } else { + match event.code { + KeyCode::Tab => app.states.transactions.next_tab(), + _ => {} + } } } } @@ -125,15 +146,48 @@ pub fn render_new_transaction_tab(f: &mut Frame, body_rect: Rect, }; } -pub fn render_manual_entry(f: &mut Frame, area: Rect, app: &App) { - let num_can_show = (area.height / 3) as usize; - let mut constraints: Vec = Vec::new(); - for _ in 0..num_can_show { - constraints.push(Constraint::Length(1)); +fn handle_manual_event(event: KeyEvent, app: &mut App) { + if app.states.transactions.manual_data.focus.is_some() { + match event.code { + KeyCode::Tab => { + app.states.transactions.manual_data.send_tab(); + } + KeyCode::Char(value) => { + match app.states.transactions.manual_data.focus.unwrap() { + _ => {} + } + } + _ => {} + } + } else { + match event.code { + KeyCode::Tab => { + app.states.transactions.next_tab(); + } + KeyCode::Enter => { + app.states.transactions.manual_data.focus = Some(ManualDataFocus::Account); + } + _ => {} + }; } +} - debug!("Can show: {}", num_can_show); - debug!("saved {} constraints", constraints.len()); +pub fn render_manual_entry(f: &mut Frame, area: Rect, app: &App) { + let constraints: Vec = vec![ + Constraint::Length(1), + Constraint::Length(1), // account + Constraint::Length(1), + Constraint::Length(1), // amount + Constraint::Length(1), + Constraint::Length(1), // date + Constraint::Length(1), + Constraint::Length(1), // bucket + Constraint::Length(1), + Constraint::Max(5), // description + Constraint::Length(1), + Constraint::Length(1), // Breakdown + Constraint::Length(1), + ]; let split_body = Layout::default() .direction(Direction::Vertical) @@ -141,51 +195,61 @@ pub fn render_manual_entry(f: &mut Frame, area: Rect, app: &App) .split(area); { - let mut first_found = false; - let mut num_rendered = 0; let manual_state = &app.states.transactions.manual_data; - if manual_state.focus.is_none() - || matches!(manual_state.focus.unwrap(), ManualDataFocus::Account) - { - let right_text = match manual_state.account.clone() { - Some(val) => val.acnt_dsply_name, - None => "".to_string(), - }; + render_manual_row( + f, + split_body[1], + "Account: ", + manual_state.account.as_ref().map(|val| val.acnt_dsply_name.clone()).unwrap_or_default(), + manual_state.focus, + ManualDataFocus::Account, + ); - render_manual_row( - f, - split_body[num_rendered * 2], - "Account: ", - right_text, - manual_state.focus, - ManualDataFocus::Account - ); + render_manual_row( + f, + split_body[3], + "Amount: ", + manual_state.amount.map(|val| val.to_string()).unwrap_or_default(), + manual_state.focus, + ManualDataFocus::Amount, + ); - first_found = true; - num_rendered = 1; - } + render_manual_row( + f, + split_body[5], + "Date: ", + manual_state.date.as_ref().map(|val| val.to_string()).unwrap_or_default(), + manual_state.focus, + ManualDataFocus::Date, + ); - if (first_found && num_rendered < num_can_show) - || matches!(manual_state.focus.unwrap(), ManualDataFocus::Amount) - { - let right_text = match manual_state.amount { - Some(val) => val.to_string(), - None => "".to_string(), - }; + render_manual_row( + f, + split_body[7], + "Bucket: ", + manual_state.bucket.as_ref().map(|val| val.bkt_dsply_code.clone()).unwrap_or_default(), + manual_state.focus, + ManualDataFocus::Bucket, + ); - render_manual_row( - f, - split_body[num_rendered * 2], - "Amount: ", - right_text, - manual_state.focus, - ManualDataFocus::Amount - ); + render_manual_row( + f, + split_body[split_body.len() - 4], + "Description: ", + manual_state.description.clone().unwrap_or("".to_string()), + manual_state.focus, + ManualDataFocus::Description, + ); - first_found = true; - num_rendered += 1; - } + render_manual_row( + f, + split_body[split_body.len() - 2], + "Transaction Breakdown: ", + "[ + ]".to_string(), + manual_state.focus, + ManualDataFocus::Breakdowns, + ); } } @@ -201,12 +265,16 @@ pub fn render_manual_row( let horizontal_pieces = Layout::default() .direction(Direction::Horizontal) - .constraints([Constraint::Percentage(35), Constraint::Percentage(45), Constraint::Percentage(20)]) + .constraints([ + Constraint::Percentage(35), + Constraint::Percentage(45), + Constraint::Percentage(20), + ]) .split(row_area); f.render_widget( Paragraph::new(Text::styled(left_text, Style::default().fg(Color::Yellow))) - .alignment(Alignment::Right), + .alignment(Alignment::Right), horizontal_pieces[0], ); @@ -223,7 +291,7 @@ pub fn render_manual_row( Block::default() .borders(Borders::NONE) .style(Style::default().bg(right_bg_color)), - ), + ), horizontal_pieces[1], ); }