Skip to content

Commit

Permalink
feat: add ability to toggle done tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-pitblado committed Oct 26, 2024
1 parent 8c56281 commit 2dacd1b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
33 changes: 27 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<dyn std::error::Error>> {
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;
}
Expand Down Expand Up @@ -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') => {
Expand Down
15 changes: 10 additions & 5 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -119,29 +121,32 @@ pub async fn run_app<B: Backend>(
)
.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<ListItem> = 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),
]
} 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)
Expand All @@ -150,7 +155,7 @@ pub async fn run_app<B: Backend>(
.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);
Expand Down

0 comments on commit 2dacd1b

Please sign in to comment.