diff --git a/src/app.rs b/src/app.rs index c470bdb..efa4bc3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -11,6 +11,7 @@ pub struct App { pub input_mode: InputMode, pub new_task_title: String, pub page: usize, + pub show_done_tasks: bool, } pub enum InputMode { @@ -33,9 +34,25 @@ impl App { input_mode: InputMode::Normal, new_task_title: String::new(), page: 1, + show_done_tasks: false, } } + pub async fn refresh_tasks( + &mut self, + instance_url: &str, + api_key: &str, + ) -> Result<(), Box> { + let new_tasks = fetch_tasks(instance_url, api_key, self.page).await?; + if self.show_done_tasks { + self.tasks = new_tasks; + } else { + self.tasks = new_tasks.into_iter().filter(|task| !task.done).collect(); + } + self.state.select(Some(0)); + Ok(()) + } + pub fn next_page(&mut self) { self.page += 1; } @@ -101,17 +118,21 @@ impl App { KeyCode::Char('n') => { // Next page self.next_page(); - if let Ok(new_tasks) = fetch_tasks(instance_url, api_key, self.page).await { - self.tasks = new_tasks.into_iter().filter(|task| !task.done).collect(); - self.state.select(Some(0)); + if let Err(err) = self.refresh_tasks(instance_url, api_key).await { + eprintln!("Error fetching tasks: {}", err); } } KeyCode::Char('p') => { // Previous page self.previous_page(); - if let Ok(new_tasks) = fetch_tasks(instance_url, api_key, self.page).await { - self.tasks = new_tasks.into_iter().filter(|task| !task.done).collect(); - self.state.select(Some(0)); + if let Err(err) = self.refresh_tasks(instance_url, api_key).await { + eprintln!("Error fetching tasks: {}", err); + } + } + KeyCode::Char('t') => { + self.show_done_tasks = !self.show_done_tasks; + if let Err(err) = self.refresh_tasks(instance_url, api_key).await { + eprintln!("Error fetching tasks: {}", err); } } KeyCode::Char('a') => { diff --git a/src/ui.rs b/src/ui.rs index 479ccbf..496f025 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -70,6 +70,8 @@ fn get_legend(input_mode: &InputMode) -> Text<'static> { Span::raw(": Next Page "), Span::styled(" p ", Style::default().fg(Color::Red)), Span::raw(": Previous Page "), + Span::styled(" t ", Style::default().fg(Color::Red)), + Span::raw(": Toggle Done "), Span::styled(" Enter ", Style::default().fg(Color::Red)), Span::raw(": View Details "), Span::styled(" a ", Style::default().fg(Color::Red)), @@ -119,15 +121,19 @@ pub async fn run_app( ) .split(body_chunk); + let task_title = if app.show_done_tasks { + "Tasks (All)" + } else { + "Tasks (Undone)" + }; + // Left panel: Task list let tasks_widget = if !app.tasks.is_empty() { let tasks: Vec = app .tasks .iter() .map(|task| { - // Check if the task is done let content = if task.done { - // Prepend "DONE" for completed tasks vec![ Span::styled("DONE ", Style::default().fg(Color::Green)), Span::raw(&task.title), @@ -135,13 +141,12 @@ pub async fn run_app( } else { vec![Span::raw(&task.title)] }; - ListItem::new(Line::from(content)) }) .collect(); List::new(tasks) - .block(Block::default().borders(Borders::ALL).title("Tasks")) + .block(Block::default().borders(Borders::ALL).title(task_title)) .highlight_style( Style::default() .fg(Color::Green) @@ -150,7 +155,7 @@ pub async fn run_app( .highlight_symbol(">> ") } else { List::new(vec![ListItem::new("No tasks available")]) - .block(Block::default().borders(Borders::ALL).title("Tasks")) + .block(Block::default().borders(Borders::ALL).title(task_title)) }; f.render_stateful_widget(tasks_widget, chunks[0], &mut app.state);