diff --git a/src/app.rs b/src/app.rs index e5ee782..098cd01 100644 --- a/src/app.rs +++ b/src/app.rs @@ -60,6 +60,11 @@ pub struct State<'a> { } impl State<'_> { + /// Creates a new [`State`]. + /// + /// # Panics + /// + /// Panics if we can't get all the columns from the database #[must_use] pub fn new(conn: Connection) -> Self { let db_conn = db::DBConn::new(conn); @@ -116,6 +121,11 @@ impl State<'_> { } } + /// Returns the move task up of this [`State`]. + /// + /// # Panics + /// + /// We have conditions to ensure this doesn't panic but we still unwrap() pub fn move_task_up(&mut self) -> bool { let column = self.get_selected_column_mut(); if column.selected_task_idx > 0 { @@ -133,6 +143,11 @@ impl State<'_> { } } + /// Returns the move task down of this [`State`]. + /// + /// # Panics + /// + /// We have conditions to ensure this doesn't panic but we still unwrap() pub fn move_task_down(&mut self) -> bool { let column = self.get_selected_column_mut(); if column.selected_task_idx < column.tasks.len().saturating_sub(1) { @@ -150,6 +165,11 @@ impl State<'_> { } } + /// Returns the move task previous column of this [`State`]. + /// + /// # Panics + /// + /// We have conditions to ensure this doesn't panic but we still unwrap() pub fn move_task_previous_column(&mut self) { let first_col = self.get_selected_column_mut(); let task_idx = first_col.selected_task_idx.saturating_sub(1); @@ -163,6 +183,11 @@ impl State<'_> { self.db_conn.set_selected_column(self.selected_column_idx); } + /// Returns the move task previous column of this [`State`]. + /// + /// # Panics + /// + /// We have conditions to ensure this doesn't panic but we still unwrap() pub fn move_task_next_column(&mut self) { let first_col = self.get_selected_column_mut(); let task_idx = first_col.selected_task_idx.saturating_sub(1); @@ -176,6 +201,11 @@ impl State<'_> { self.db_conn.set_selected_column(self.selected_column_idx); } + /// Returns the delete task of this [`State`]. + /// + /// # Panics + /// + /// We have conditions to ensure this doesn't panic but we still unwrap() pub fn delete_task(&mut self) { let column = self.get_selected_column_mut(); let task_id = column.get_selected_task().unwrap().id; diff --git a/src/db.rs b/src/db.rs index f74898e..2c4f870 100644 --- a/src/db.rs +++ b/src/db.rs @@ -65,12 +65,12 @@ impl DBConn { /// # Panics /// /// Panics if something goes wrong with the SQL - pub fn insert_new_task(&self, title: String, description: String, column: &Column) -> Task { + pub fn insert_new_task(&self, title: String, description: String, column_id: i64) -> Task { let mut stmt = self .0 .prepare("insert into task(title, description, column_id) values (?1, ?2, ?3)") .unwrap(); - stmt.execute(params![title, description, column.id]) + stmt.execute(params![title, description, column_id]) .unwrap(); let id = self.0.last_insert_rowid(); Task { diff --git a/src/input.rs b/src/input.rs index 9a78dab..0011398 100644 --- a/src/input.rs +++ b/src/input.rs @@ -7,7 +7,8 @@ pub fn handle_task_edit( state: &mut State<'_>, key: event::KeyEvent, ) { - if let Some(task) = &mut state.task_edit_state { + if let Some(mut task) = state.task_edit_state.take() { + let mut clear_task = false; match task.focus { // TODO: Handle wrapping around the enum rather than doing it manually TaskEditFocus::Title => match key.code { @@ -31,19 +32,22 @@ pub fn handle_task_edit( KeyCode::Enter => { let title = task.title.clone().into_lines().join("\n"); let description = task.description.clone().into_lines().join("\n"); - let column = state.get_selected_column_mut(); if task.is_edit { + let column = state.get_selected_column_mut(); if let Some(selected_task) = column.get_selected_task_mut() { selected_task.title = title; selected_task.description = description; - state.db_conn.update_task_text(selected_task); + let cloned = selected_task.clone(); + state.db_conn.update_task_text(&cloned); } } else { - let task = state.db_conn.insert_new_task(title, description, column); - column.add_task(task); - state.db_conn.set_selected_task_for_column(column.selected_task_idx, column.id); + let col_id = state.get_selected_column().id; + let selected_task_idx = state.get_selected_column().selected_task_idx; + let task = state.db_conn.insert_new_task(title, description, col_id); + state.get_selected_column_mut().add_task(task); + state.db_conn.set_selected_task_for_column(selected_task_idx, col_id); } - state.task_edit_state = None; + clear_task = true; } _ => (), }, @@ -51,11 +55,14 @@ pub fn handle_task_edit( KeyCode::Tab => task.focus = TaskEditFocus::Title, KeyCode::BackTab => task.focus = TaskEditFocus::ConfirmBtn, KeyCode::Enter => { - state.task_edit_state = None; + clear_task = true; } _ => (), }, } + if !clear_task { + state.task_edit_state = Some(task); + } } } @@ -111,6 +118,7 @@ pub fn handle_main(state: &mut State<'_>, key: event::KeyEvent) { KeyCode::Char('e') => state.task_edit_state = column.get_task_state_from_current(), KeyCode::Char('D') => { if !column.tasks.is_empty() { + state.delete_task(); } } _ => {}