Skip to content

Commit

Permalink
Merge pull request #26
Browse files Browse the repository at this point in the history
Added deletion popup
  • Loading branch information
TheRustyPickle committed Jun 30, 2023
2 parents 8b4f64c + 2846494 commit 5296bb5
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/key_checker/home_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ pub fn home_keys(handler: &mut InputKeyHandler) -> Option<HandlingOutput> {
KeyCode::Char('z') => handler.go_summary(),
KeyCode::Char('w') => handler.go_search(),
KeyCode::Char('e') => handler.edit_tx(),
KeyCode::Char('d') => handler.delete_tx(),
KeyCode::Char('d') => handler.do_deletion_popup(),
KeyCode::Right => handler.handle_right_arrow(),
KeyCode::Left => handler.handle_left_arrow(),
KeyCode::Up => handler.handle_up_arrow(),
KeyCode::Down => handler.handle_down_arrow(),
_ => {}
},
PopupState::TxDeletion => match handler.key.code {
KeyCode::Left | KeyCode::Right | KeyCode::Enter => handler.handle_deletion_popup(),
_ => {}
},
_ => handler.do_empty_popup(),
}
None
Expand Down
41 changes: 36 additions & 5 deletions src/key_checker/key_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::home_page::TransactionData;
use crate::outputs::TxType;
use crate::outputs::{HandlingOutput, TxUpdateError, VerifyingOutput};
use crate::page_handler::{
ChartTab, CurrentUi, HomeTab, IndexedData, PopupState, SortingType, SummaryTab, TableData,
TxTab,
ChartTab, CurrentUi, DeletionStatus, HomeTab, IndexedData, PopupState, SortingType, SummaryTab,
TableData, TxTab,
};
use crate::summary_page::SummaryData;
use crate::tx_handler::TxData;
Expand Down Expand Up @@ -46,6 +46,7 @@ pub struct InputKeyHandler<'a> {
chart_index: &'a mut Option<f64>,
chart_hidden_mode: &'a mut bool,
summary_hidden_mode: &'a mut bool,
deletion_status: &'a mut DeletionStatus,
conn: &'a mut Connection,
}

Expand Down Expand Up @@ -81,6 +82,7 @@ impl<'a> InputKeyHandler<'a> {
chart_index: &'a mut Option<f64>,
chart_hidden_mode: &'a mut bool,
summary_hidden_mode: &'a mut bool,
deletion_status: &'a mut DeletionStatus,
conn: &'a mut Connection,
) -> InputKeyHandler<'a> {
let total_tags = summary_data
Expand Down Expand Up @@ -117,6 +119,7 @@ impl<'a> InputKeyHandler<'a> {
chart_index,
summary_hidden_mode,
chart_hidden_mode,
deletion_status,
conn,
}
}
Expand Down Expand Up @@ -195,6 +198,14 @@ impl<'a> InputKeyHandler<'a> {
}
}

/// Turns on deletion confirmation popup
#[cfg(not(tarpaulin_include))]
pub fn do_deletion_popup(&mut self) {
if let Some(_) = self.table.state.selected() {
*self.popup = PopupState::TxDeletion
}
}

/// Removes popup status
#[cfg(not(tarpaulin_include))]
pub fn do_empty_popup(&mut self) {
Expand Down Expand Up @@ -323,7 +334,7 @@ impl<'a> InputKeyHandler<'a> {
}
}

/// Deletes the selected transaction and reloads home and chart page
/// Deletes the selected transaction and reloads pages
#[cfg(not(tarpaulin_include))]
pub fn delete_tx(&mut self) {
if let Some(index) = self.table.state.selected() {
Expand All @@ -332,11 +343,16 @@ impl<'a> InputKeyHandler<'a> {
Ok(_) => {
// transaction deleted so reload the data again
self.reload_home_table();
self.table.state.select(None);
*self.home_tab = HomeTab::Months;
self.reload_chart_data();
self.reload_summary_data();
self.reload_search_data();

if index == 0 {
self.table.state.select(None);
*self.home_tab = HomeTab::Months;
} else {
self.table.state.select(Some(index - 1));
}
}
Err(err) => {
*self.popup =
Expand Down Expand Up @@ -668,6 +684,21 @@ impl<'a> InputKeyHandler<'a> {
}
}
}

#[cfg(not(tarpaulin_include))]
pub fn handle_deletion_popup(&mut self) {
match self.key.code {
KeyCode::Left | KeyCode::Right => *self.deletion_status = self.deletion_status.next(),
KeyCode::Enter => match self.deletion_status {
DeletionStatus::Yes => {
self.delete_tx();
*self.popup = PopupState::Nothing
}
DeletionStatus::No => *self.popup = PopupState::Nothing,
},
_ => {}
}
}
}

impl<'a> InputKeyHandler<'a> {
Expand Down
9 changes: 6 additions & 3 deletions src/page_handler/ui_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::key_checker::{
};
use crate::outputs::{HandlingOutput, UiHandlingError};
use crate::page_handler::{
ChartTab, CurrentUi, HomeTab, IndexedData, PopupState, SortingType, SummaryTab, TableData,
TxTab,
ChartTab, CurrentUi, DeletionStatus, HomeTab, IndexedData, PopupState, SortingType, SummaryTab,
TableData, TxTab,
};
use crate::popup_page::PopupData;
use crate::search_page::search_ui;
Expand Down Expand Up @@ -124,6 +124,8 @@ pub fn start_app<B: Backend>(

let mut summary_hidden_mode = false;

let mut deletion_status: DeletionStatus = DeletionStatus::Yes;

// how it work:
// Default value from above -> Goes to an interface page and render -> Wait for an event key press.
//
Expand Down Expand Up @@ -215,7 +217,7 @@ pub fn start_app<B: Backend>(
),
CurrentUi::Search => search_ui(f, &search_data, &search_tab, &mut search_table),
}
popup_data.create_popup(f, &popup_state)
popup_data.create_popup(f, &popup_state, &deletion_status)
})
.map_err(UiHandlingError::DrawingError)?;

Expand Down Expand Up @@ -270,6 +272,7 @@ pub fn start_app<B: Backend>(
&mut chart_index,
&mut chart_hidden_mode,
&mut summary_hidden_mode,
&mut deletion_status,
conn,
);

Expand Down
15 changes: 15 additions & 0 deletions src/page_handler/ui_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ pub enum PopupState {
SummaryHelp,
SearchHelp,
DeleteFailed(String),
TxDeletion,
Nothing,
}

Expand Down Expand Up @@ -350,3 +351,17 @@ impl SortingType {
}
}
}

pub enum DeletionStatus {
Yes,
No,
}

impl DeletionStatus {
pub fn next(&mut self) -> Self {
match self {
DeletionStatus::Yes => DeletionStatus::No,
DeletionStatus::No => DeletionStatus::Yes,
}
}
}
2 changes: 1 addition & 1 deletion src/popup_page/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod popup_data;
mod popup_ui;

pub use popup_data::PopupData;
pub use popup_ui::create_popup;
pub use popup_ui::{create_deletion_popup, create_popup};
21 changes: 15 additions & 6 deletions src/popup_page/popup_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::page_handler::PopupState;
use crate::popup_page::create_popup;
use crate::page_handler::{DeletionStatus, PopupState};
use crate::popup_page::{create_deletion_popup, create_popup};
use ratatui::backend::Backend;
use ratatui::Frame;

Expand Down Expand Up @@ -28,7 +28,12 @@ impl<'a> PopupData<'a> {
}

#[cfg(not(tarpaulin_include))]
pub fn create_popup<B: Backend>(&mut self, f: &mut Frame<B>, popup_type: &PopupState) {
pub fn create_popup<B: Backend>(
&mut self,
f: &mut Frame<B>,
popup_type: &PopupState,
deletion_status: &DeletionStatus,
) {
let status = match popup_type {
PopupState::NewUpdate(data) => self.get_new_update_text(data),
PopupState::HomeHelp => self.get_home_help_text(),
Expand All @@ -37,11 +42,15 @@ impl<'a> PopupData<'a> {
PopupState::SummaryHelp => self.get_summary_help_text(),
PopupState::DeleteFailed(err) => self.get_delete_failed_text(err),
PopupState::SearchHelp => self.get_search_help_text(),
PopupState::Nothing => String::new(),
PopupState::Nothing | PopupState::TxDeletion => String::new(),
};

if !status.is_empty() {
create_popup(f, self.x_value, self.y_value, self.title, status);
if let PopupState::TxDeletion = popup_type {
create_deletion_popup(f, deletion_status)
} else {
if !status.is_empty() {
create_popup(f, self.x_value, self.y_value, self.title, status);
}
}
}

Expand Down
81 changes: 80 additions & 1 deletion src/popup_page/popup_ui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::page_handler::{BACKGROUND, BOX, RED, TEXT};
use crate::page_handler::{DeletionStatus, BACKGROUND, BLUE, BOX, HIGHLIGHTED, RED, TEXT};
use crate::utility::create_bolded_text;
use ratatui::backend::Backend;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
Expand Down Expand Up @@ -57,6 +57,85 @@ pub fn create_popup<B: Backend>(
f.render_widget(dismiss_sec, new_chunks[1]);
}

#[cfg(not(tarpaulin_include))]
pub fn create_deletion_popup<B: Backend>(f: &mut Frame<B>, deletion_status: &DeletionStatus) {
let text = "Are you sure you want to delete this transaction?";
let title = "TX Deletion";
let x_value = 40;
let y_value = 25;
let size = f.size();

let title = Span::styled(title, Style::default().add_modifier(Modifier::BOLD));
let text = create_bolded_text(&text);

// determines the size of the popup window
let x_value = x_value;
let y_value = y_value;

let block = Block::default()
.title(title)
.borders(Borders::ALL)
.style(Style::default().bg(BACKGROUND).fg(BOX));

// returns an area where we can add anything like a normal window.
let area = centered_rect(x_value, y_value, size);

let new_chunks = Layout::default()
.direction(Direction::Vertical)
.margin(2)
.constraints([Constraint::Min(1), Constraint::Length(5)].as_ref())
.split(area);

let selection_chunk = Layout::default()
.direction(Direction::Horizontal)
.margin(2)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(new_chunks[1]);

f.render_widget(Clear, area);
f.render_widget(block, area);

let deletion_text = Paragraph::new(Text::from(text))
.style(Style::default().bg(BACKGROUND).fg(TEXT))
.alignment(Alignment::Center);

let yes_text = match deletion_status {
DeletionStatus::Yes => Span::styled(
" Yes ",
Style::default()
.fg(RED)
.add_modifier(Modifier::BOLD)
.bg(HIGHLIGHTED),
),
DeletionStatus::No => Span::styled(
" Yes ",
Style::default().fg(RED).add_modifier(Modifier::BOLD),
),
};

let no_text = match deletion_status {
DeletionStatus::No => Span::styled(
" No ",
Style::default()
.fg(BLUE)
.add_modifier(Modifier::BOLD)
.bg(HIGHLIGHTED),
),
DeletionStatus::Yes => Span::styled(
" No ",
Style::default().fg(BLUE).add_modifier(Modifier::BOLD),
),
};

let yes_sec = Paragraph::new(yes_text).alignment(Alignment::Center);

let no_sec = Paragraph::new(no_text).alignment(Alignment::Center);

f.render_widget(deletion_text, new_chunks[0]);
f.render_widget(yes_sec, selection_chunk[0]);
f.render_widget(no_sec, selection_chunk[1]);
}

/// The function takes certain parameters to create an empty space in the layout
/// and returns an area where we can place various widgets. Taken from tui-rs examples.
/// This is used as a popup for helpful information.
Expand Down

0 comments on commit 5296bb5

Please sign in to comment.