added navigation and selection to new transaction manual tab

This commit is contained in:
Nickiel12 2023-08-13 20:20:10 -07:00
parent 3e247b98c4
commit dc4e3dfa33

View file

@ -5,7 +5,7 @@ use crate::{
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind}; use crossterm::event::{KeyCode, KeyEvent, KeyEventKind};
use ratatui::{ use ratatui::{
backend::Backend, backend::Backend,
layout::{Constraint, Direction, Layout, Rect, Alignment}, layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Style}, style::{Color, Style},
text::Text, text::Text,
widgets::{Block, Borders, Paragraph}, widgets::{Block, Borders, Paragraph},
@ -52,6 +52,21 @@ impl ManualData {
breakdowns: Vec::new(), 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> { pub struct NewTransactionTabState<'a> {
@ -75,10 +90,16 @@ impl<'a> NewTransactionTabState<'a> {
} }
pub fn handle_event(event: KeyEvent, app: &mut App) { pub fn handle_event(event: KeyEvent, app: &mut App) {
let transact_state = &app.states.transactions;
if event.kind == KeyEventKind::Press { if event.kind == KeyEventKind::Press {
match event.code { if transact_state.cur_tab_index == 1 {
KeyCode::Tab => app.states.transactions.next_tab(), 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<B: Backend>(f: &mut Frame<B>, body_rect: Rect,
}; };
} }
pub fn render_manual_entry<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) { fn handle_manual_event(event: KeyEvent, app: &mut App) {
let num_can_show = (area.height / 3) as usize; if app.states.transactions.manual_data.focus.is_some() {
let mut constraints: Vec<Constraint> = Vec::new(); match event.code {
for _ in 0..num_can_show { KeyCode::Tab => {
constraints.push(Constraint::Length(1)); 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); pub fn render_manual_entry<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App) {
debug!("saved {} constraints", constraints.len()); let constraints: Vec<Constraint> = 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() let split_body = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
@ -141,51 +195,61 @@ pub fn render_manual_entry<B: Backend>(f: &mut Frame<B>, area: Rect, app: &App)
.split(area); .split(area);
{ {
let mut first_found = false;
let mut num_rendered = 0;
let manual_state = &app.states.transactions.manual_data; let manual_state = &app.states.transactions.manual_data;
if manual_state.focus.is_none() render_manual_row(
|| matches!(manual_state.focus.unwrap(), ManualDataFocus::Account) f,
{ split_body[1],
let right_text = match manual_state.account.clone() { "Account: ",
Some(val) => val.acnt_dsply_name, manual_state.account.as_ref().map(|val| val.acnt_dsply_name.clone()).unwrap_or_default(),
None => "".to_string(), manual_state.focus,
}; ManualDataFocus::Account,
);
render_manual_row( render_manual_row(
f, f,
split_body[num_rendered * 2], split_body[3],
"Account: ", "Amount: ",
right_text, manual_state.amount.map(|val| val.to_string()).unwrap_or_default(),
manual_state.focus, manual_state.focus,
ManualDataFocus::Account ManualDataFocus::Amount,
); );
first_found = true; render_manual_row(
num_rendered = 1; 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) render_manual_row(
|| matches!(manual_state.focus.unwrap(), ManualDataFocus::Amount) f,
{ split_body[7],
let right_text = match manual_state.amount { "Bucket: ",
Some(val) => val.to_string(), manual_state.bucket.as_ref().map(|val| val.bkt_dsply_code.clone()).unwrap_or_default(),
None => "".to_string(), manual_state.focus,
}; ManualDataFocus::Bucket,
);
render_manual_row( render_manual_row(
f, f,
split_body[num_rendered * 2], split_body[split_body.len() - 4],
"Amount: ", "Description: ",
right_text, manual_state.description.clone().unwrap_or("".to_string()),
manual_state.focus, manual_state.focus,
ManualDataFocus::Amount ManualDataFocus::Description,
); );
first_found = true; render_manual_row(
num_rendered += 1; 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<B: Backend>(
let horizontal_pieces = Layout::default() let horizontal_pieces = Layout::default()
.direction(Direction::Horizontal) .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); .split(row_area);
f.render_widget( f.render_widget(
Paragraph::new(Text::styled(left_text, Style::default().fg(Color::Yellow))) Paragraph::new(Text::styled(left_text, Style::default().fg(Color::Yellow)))
.alignment(Alignment::Right), .alignment(Alignment::Right),
horizontal_pieces[0], horizontal_pieces[0],
); );
@ -223,7 +291,7 @@ pub fn render_manual_row<B: Backend>(
Block::default() Block::default()
.borders(Borders::NONE) .borders(Borders::NONE)
.style(Style::default().bg(right_bg_color)), .style(Style::default().bg(right_bg_color)),
), ),
horizontal_pieces[1], horizontal_pieces[1],
); );
} }