From 43046ede95674980766dc8473c7733c885808a36 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Mon, 25 Sep 2023 19:48:23 -0700 Subject: [PATCH] added quick entry and quick-paycheck screens --- src/app.rs | 4 +- src/uis/manual_transaction.rs | 2 +- src/uis/quick_transaction/mod.rs | 112 +++++++++++++++++++++--- src/uis/quick_transaction/paycheck.rs | 118 +++++++++++++++++++++++++- 4 files changed, 219 insertions(+), 17 deletions(-) diff --git a/src/app.rs b/src/app.rs index 27aad9c..9cbc005 100644 --- a/src/app.rs +++ b/src/app.rs @@ -78,7 +78,7 @@ pub enum ActiveFrame { pub struct States<'a> { pub nav_state: NavigationState<'a>, - pub transactions: QuickTransactionState, + pub quick_transactions: QuickTransactionState, pub manual_transactions: ManualTransactionState, pub history: HistoryState, @@ -89,7 +89,7 @@ impl<'a> States<'a> { pub fn new() -> States<'a> { States { nav_state: NavigationState::new(), - transactions: QuickTransactionState::new(), + quick_transactions: QuickTransactionState::new(), manual_transactions: ManualTransactionState::new(), history: HistoryState::new(), diff --git a/src/uis/manual_transaction.rs b/src/uis/manual_transaction.rs index 18c7a4e..4ff8b2a 100644 --- a/src/uis/manual_transaction.rs +++ b/src/uis/manual_transaction.rs @@ -390,7 +390,7 @@ pub fn render_manual_transaction(f: &mut Frame, body_rect: Rect, } } -pub fn render_manual_row( +fn render_manual_row( f: &mut Frame, row_body_rect: Rect, left_text: &str, diff --git a/src/uis/quick_transaction/mod.rs b/src/uis/quick_transaction/mod.rs index d27d55c..cb1f862 100644 --- a/src/uis/quick_transaction/mod.rs +++ b/src/uis/quick_transaction/mod.rs @@ -1,37 +1,69 @@ - pub mod paycheck; use crate::app::App; use crossterm::event::{KeyCode, KeyEvent, KeyEventKind}; use ratatui::{ backend::Backend, - layout::{Constraint, Direction, Layout, Rect}, + layout::{Alignment, Constraint, Direction, Layout, Rect}, style::{Color, Style}, - text::Text, + text::{Line, Span}, widgets::{Block, Borders, Paragraph}, Frame, }; +use log::debug; + +use self::paycheck::QuickPaycheckState; + enum SelectedQuickEntry { Paycheck, + ChoiceMenu(usize), } pub struct QuickTransactionState { - + current_screen: SelectedQuickEntry, + paycheck_state: QuickPaycheckState, } impl QuickTransactionState { pub fn new() -> QuickTransactionState { - QuickTransactionState { + QuickTransactionState { + current_screen: SelectedQuickEntry::ChoiceMenu(0), + paycheck_state: QuickPaycheckState::new(), } } pub fn handle_event(event: KeyEvent, app: &mut App) { - // let transact_state = &app.states.transactions; - - if event.kind == KeyEventKind::Press { - match event.code { - _ => {} + match app.states.quick_transactions.current_screen { + SelectedQuickEntry::ChoiceMenu(selected) => { + if event.kind == KeyEventKind::Press { + match event.code { + KeyCode::Up => { + if selected > 0 { + app.states.quick_transactions.current_screen = + SelectedQuickEntry::ChoiceMenu(selected - 1); + } + } + KeyCode::Down => { + // Change this when new rows are added + if selected < 1 { + app.states.quick_transactions.current_screen = + SelectedQuickEntry::ChoiceMenu(selected + 1); + } + } + KeyCode::Enter => match selected { + 0 => { + app.states.quick_transactions.current_screen = + SelectedQuickEntry::Paycheck; + } + _ => {} + }, + _ => {} + } + } + } + SelectedQuickEntry::Paycheck => { + paycheck::QuickPaycheckState::handle_event(event, app); } } } @@ -44,5 +76,63 @@ impl Default for QuickTransactionState { } pub fn render_quick_transaction(f: &mut Frame, body_rect: Rect, app: &App) { - + match app.states.quick_transactions.current_screen { + SelectedQuickEntry::Paycheck => { + QuickPaycheckState::render(f, body_rect, &app.states.quick_transactions.paycheck_state); + } + SelectedQuickEntry::ChoiceMenu(selected) => { + render_choose_menu(f, body_rect, selected); + } + } +} + +fn render_choose_menu(f: &mut Frame, body_rect: Rect, cur_select_index: usize) { + let width_chunks = Layout::default() + .direction(Direction::Horizontal) + .constraints([ + Constraint::Percentage(30), + Constraint::Percentage(40), + Constraint::Percentage(30), + ]) + .split(body_rect); + + let constraints: Vec = vec![ + Constraint::Length(1), + Constraint::Length(1), // account + Constraint::Length(1), + Constraint::Length(1), // amount + Constraint::Length(1), + ]; + + let split_body = Layout::default() + .direction(Direction::Vertical) + .constraints(constraints) + .split(width_chunks[1]); + + // Selected Quick Entry :: Paycheck + render_row(f, split_body[1], "Paycheck", cur_select_index == 0); + + // Selected Quick Entry :: Paycheck + render_row(f, split_body[3], "Nothing", cur_select_index == 1); +} + +fn render_row(f: &mut Frame, render_to: Rect, text: &str, focus: bool) { + let block: Block; + if focus { + block = Block::default() + .borders(Borders::NONE) + .style(Style::default().bg(Color::Yellow)); + } else { + block = Block::default() + .borders(Borders::NONE) + .style(Style::default().bg(Color::White)); + } + let p_widget: Paragraph = Paragraph::new(Line::from(Span::styled( + text, + Style::default().fg(Color::Black), + ))) + .alignment(Alignment::Center) + .block(block); + + f.render_widget(p_widget, render_to); } diff --git a/src/uis/quick_transaction/paycheck.rs b/src/uis/quick_transaction/paycheck.rs index ac276d7..f88a41d 100644 --- a/src/uis/quick_transaction/paycheck.rs +++ b/src/uis/quick_transaction/paycheck.rs @@ -1,7 +1,7 @@ - +use crossterm::event::{KeyEvent, KeyEventKind, KeyCode}; use ratatui::{ backend::Backend, - layout::{Constraint, Direction, Layout, Rect}, + layout::{Alignment, Constraint, Direction, Layout, Rect}, style::{Color, Style}, text::{Line, Span, Text}, widgets::{Block, Borders, List, ListItem, Paragraph}, @@ -10,9 +10,17 @@ use ratatui::{ use crate::app::App; +#[derive(PartialEq)] +enum SelectedEntry { + Gross, + Net, + Execute, +} + pub struct QuickPaycheckState { pub gross_amount: String, pub net_amount: String, + selected: SelectedEntry, } impl QuickPaycheckState { @@ -20,11 +28,75 @@ impl QuickPaycheckState { QuickPaycheckState { net_amount: "".to_string(), gross_amount: "".to_string(), + selected: SelectedEntry::Net, } } - pub fn render(f: &mut Frame, body_rect: Rect, app: &mut App) { + pub fn render(f: &mut Frame, body_rect: Rect, state: &QuickPaycheckState) { + let constraints: Vec = vec![ + Constraint::Length(1), + Constraint::Length(1), + Constraint::Length(1), // Gross amount + Constraint::Length(1), + Constraint::Length(1), // net amount + Constraint::Length(1), + Constraint::Length(1), // execute button + ]; + let split_body = Layout::default() + .direction(Direction::Vertical) + .constraints(constraints) + .split(body_rect); + + render_labled_entry_row( + f, + split_body[2], + "Take home:", + &state.net_amount, + state.selected == SelectedEntry::Net, + ); + + render_labled_entry_row( + f, + split_body[4], + "Gross:", + &state.net_amount, + state.selected == SelectedEntry::Gross, + ); + } + + pub fn handle_event(event: KeyEvent, app: &mut App) { + if event.kind == KeyEventKind::Press { + match event.code { + KeyCode::Up => { + match app.states.quick_transactions.paycheck_state.selected { + SelectedEntry::Net => { + app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Execute; + } + SelectedEntry::Gross => { + app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Net; + } + SelectedEntry::Execute => { + app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Gross; + } + } + } + KeyCode::Down => { + match app.states.quick_transactions.paycheck_state.selected { + SelectedEntry::Net => { + app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Gross; + } + SelectedEntry::Gross => { + app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Execute; + } + SelectedEntry::Execute => { + app.states.quick_transactions.paycheck_state.selected = SelectedEntry::Net; + } + } + } + _ => {} + } + } } } @@ -33,3 +105,43 @@ impl Default for QuickPaycheckState { Self::new() } } + +fn render_labled_entry_row( + f: &mut Frame, + row_body_rect: Rect, + left_text: &str, + right_text: &String, + do_highlight: bool, +) { + let horizontal_pieces = Layout::default() + .direction(Direction::Horizontal) + .constraints([ + Constraint::Percentage(35), + Constraint::Percentage(45), + Constraint::Percentage(20), + ]) + .split(row_body_rect); + + f.render_widget( + Paragraph::new(Text::styled(left_text, Style::default().fg(Color::Yellow))) + .alignment(Alignment::Right), + horizontal_pieces[0], + ); + + let right_bg_color = if do_highlight { + Color::Yellow + } else { + Color::White + }; + + f.render_widget( + Paragraph::new(Text::styled(right_text, Style::default().fg(Color::Black))) + .alignment(Alignment::Center) + .block( + Block::default() + .borders(Borders::NONE) + .style(Style::default().bg(right_bg_color)), + ), + horizontal_pieces[1], + ); +}