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,13 +90,19 @@ 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 {
if transact_state.cur_tab_index == 1 {
handle_manual_event(event, app);
} else {
match event.code { match event.code {
KeyCode::Tab => app.states.transactions.next_tab(), KeyCode::Tab => app.states.transactions.next_tab(),
_ => {} _ => {}
} }
} }
} }
}
} }
pub fn render_new_transaction_tab<B: Backend>(f: &mut Frame<B>, body_rect: Rect, app: &App) { pub fn render_new_transaction_tab<B: Backend>(f: &mut Frame<B>, body_rect: Rect, app: &App) {
@ -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()
|| 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( render_manual_row(
f, f,
split_body[num_rendered * 2], split_body[1],
"Account: ", "Account: ",
right_text, manual_state.account.as_ref().map(|val| val.acnt_dsply_name.clone()).unwrap_or_default(),
manual_state.focus, manual_state.focus,
ManualDataFocus::Account ManualDataFocus::Account,
); );
first_found = true;
num_rendered = 1;
}
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( render_manual_row(
f, f,
split_body[num_rendered * 2], split_body[3],
"Amount: ", "Amount: ",
right_text, manual_state.amount.map(|val| val.to_string()).unwrap_or_default(),
manual_state.focus, manual_state.focus,
ManualDataFocus::Amount 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,
);
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[split_body.len() - 4],
"Description: ",
manual_state.description.clone().unwrap_or("".to_string()),
manual_state.focus,
ManualDataFocus::Description,
);
render_manual_row(
f,
split_body[split_body.len() - 2],
"Transaction Breakdown: ",
"[ + ]".to_string(),
manual_state.focus,
ManualDataFocus::Breakdowns,
);
} }
} }
@ -201,7 +265,11 @@ 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(