Take care of filters when resolving the selected exercise

This commit is contained in:
mo8it 2024-04-11 14:58:56 +02:00
parent f53a0e8700
commit 2e1a87d7d3

View file

@ -1,4 +1,4 @@
use anyhow::Result; use anyhow::{Context, Result};
use ratatui::{ use ratatui::{
layout::{Constraint, Rect}, layout::{Constraint, Rect},
style::{Style, Stylize}, style::{Style, Stylize},
@ -217,21 +217,44 @@ impl<'a> UiState<'a> {
return Ok(None); return Ok(None);
}; };
self.app_state.set_pending(selected)?; let (ind, exercise) = self
// TODO: Take care of filters! .app_state
let exercise = &self.app_state.exercises()[selected]; .exercises()
.iter()
.zip(self.app_state.progress())
.enumerate()
.filter_map(|(ind, (exercise, done))| match self.filter {
Filter::Done => done.then_some((ind, exercise)),
Filter::Pending => (!done).then_some((ind, exercise)),
Filter::None => Some((ind, exercise)),
})
.nth(selected)
.context("Invalid selection index")?;
self.app_state.set_pending(ind)?;
exercise.reset()?; exercise.reset()?;
Ok(Some(exercise)) Ok(Some(exercise))
} }
#[inline]
pub fn selected_to_current_exercise(&mut self) -> Result<()> { pub fn selected_to_current_exercise(&mut self) -> Result<()> {
let Some(selected) = self.table_state.selected() else { let Some(selected) = self.table_state.selected() else {
return Ok(()); return Ok(());
}; };
// TODO: Take care of filters! let ind = self
self.app_state.set_current_exercise_ind(selected) .app_state
.progress()
.iter()
.enumerate()
.filter_map(|(ind, done)| match self.filter {
Filter::Done => done.then_some(ind),
Filter::Pending => (!done).then_some(ind),
Filter::None => Some(ind),
})
.nth(selected)
.context("Invalid selection index")?;
self.app_state.set_current_exercise_ind(ind)
} }
} }