enhance(tasks): make state update movement more intuitive

This commit is contained in:
xeruf 2025-05-11 16:55:14 +02:00
parent 32fbf6db89
commit c836b0e57b
3 changed files with 19 additions and 10 deletions

View file

@ -460,13 +460,11 @@ async fn main() -> Result<()> {
} }
Some('>') => { Some('>') => {
tasks.update_state(arg_default, State::Done); tasks.update_state_and_up(arg_default, State::Done);
if tasks.custom_time.is_none() { tasks.move_up(); }
} }
Some('<') => { Some('<') => {
tasks.update_state(arg_default, State::Closed); tasks.update_state_and_up(arg_default, State::Closed);
if tasks.custom_time.is_none() { tasks.move_up(); }
} }
Some('&') => Some('&') =>

View file

@ -243,11 +243,15 @@ impl TasksRelay {
pub(crate) fn get_position(&self) -> Option<EventId> { pub(crate) fn get_position(&self) -> Option<EventId> {
self.position self.position
} }
pub(crate) fn calculate_position(&self, time: Option<Timestamp>) -> Option<EventId> {
self.get_position_at(time.unwrap_or(now())).1
}
// Update the current position // Update the current position
// Returns whether the position changed // Returns whether the position changed
pub(super) fn update_position(&mut self) -> bool { pub(super) fn update_position(&mut self) -> bool {
let new_position = self.get_position_at(now()).1; let new_position = self.calculate_position(None);
if new_position != self.position { if new_position != self.position {
self.view.clear(); self.view.clear();
self.position = new_position; self.position = new_position;
@ -1412,9 +1416,14 @@ impl TasksRelay {
self.submit(prop) self.submit(prop)
} }
pub(crate) fn update_state(&mut self, comment: &str, state: State) -> Option<EventId> { /// Update state of current task (if one is selected) and move out of it
let id = self.get_position()?; pub(crate) fn update_state_and_up(&mut self, comment: &str, state: State) {
Some(self.set_state_for(id, comment, state)) if let Some(id) = self.get_position() {
self.set_state_for(id, comment, state);
if self.calculate_position(self.custom_time) == Some(id) {
self.move_up();
}
}
} }
/// Creates a note or activity, depending on whether the parent is a task. /// Creates a note or activity, depending on whether the parent is a task.

View file

@ -68,17 +68,19 @@ fn test_recursive_closing() {
tasks.custom_time = Some(Timestamp::zero()); tasks.custom_time = Some(Timestamp::zero());
let parent = tasks.make_task_unwrapped("parent #tag1"); let parent = tasks.make_task_unwrapped("parent #tag1");
tasks.move_to(Some(parent)); tasks.move_to(Some(parent));
tasks.update_position();
let sub = tasks.make_task_unwrapped("sub #oi # tag2"); let sub = tasks.make_task_unwrapped("sub #oi # tag2");
assert_eq!( assert_eq!(
tasks.all_hashtags(), tasks.all_hashtags(),
["oi", "tag1", "tag2"].into_iter().map(Hashtag::from).collect() ["oi", "tag1", "tag2"].into_iter().map(Hashtag::from).collect()
); );
tasks.update_position();
tasks.make_note("note with #tag3 # yeah"); tasks.make_note("note with #tag3 # yeah");
let all_tags = ["oi", "tag1", "tag2", "tag3", "yeah"].into_iter().map(Hashtag::from).collect(); let all_tags = ["oi", "tag1", "tag2", "tag3", "yeah"].into_iter().map(Hashtag::from).collect();
assert_eq!(tasks.all_hashtags(), all_tags); assert_eq!(tasks.all_hashtags(), all_tags);
tasks.custom_time = Some(Timestamp::now()); tasks.custom_time = Some(Timestamp::now());
tasks.update_state("Finished #YeaH # oi", State::Done); tasks.update_state_and_up("Finished #YeaH # oi", State::Done);
assert_eq!( assert_eq!(
tasks.get_by_id(&parent).unwrap().list_hashtags().collect_vec(), tasks.get_by_id(&parent).unwrap().list_hashtags().collect_vec(),
["YeaH", "oi", "tag3", "yeah", "tag1"].map(Hashtag::from) ["YeaH", "oi", "tag3", "yeah", "tag1"].map(Hashtag::from)
@ -86,7 +88,7 @@ fn test_recursive_closing() {
assert_eq!(tasks.all_hashtags(), all_tags); assert_eq!(tasks.all_hashtags(), all_tags);
tasks.custom_time = Some(now()); tasks.custom_time = Some(now());
tasks.update_state("Closing Down", State::Closed); tasks.update_state_and_up("Closing Down", State::Closed);
assert_eq!(tasks.get_by_id(&sub).unwrap().pure_state(), State::Closed); assert_eq!(tasks.get_by_id(&sub).unwrap().pure_state(), State::Closed);
assert_eq!(tasks.get_by_id(&parent).unwrap().pure_state(), State::Closed); assert_eq!(tasks.get_by_id(&parent).unwrap().pure_state(), State::Closed);
assert_eq!(tasks.nonclosed_tasks().next(), None); assert_eq!(tasks.nonclosed_tasks().next(), None);