From 39421057647a926cadfa05fdded9cec05dd57813 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Thu, 29 Aug 2024 23:28:40 +0300 Subject: [PATCH] fix(tasks): do not show bookmarks on childrenless tasks --- src/tasks.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/tasks.rs b/src/tasks.rs index 2ef8425..d12255a 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -379,10 +379,15 @@ impl Tasks { pub(crate) fn filtered_tasks<'a>(&'a self, position: Option<&'a EventId>) -> impl Iterator + 'a { let current: HashMap<&EventId, &Task> = self.resolve_tasks(self.children_of(position)).map(|t| (t.get_id(), t)).collect(); - let bookmarks = self.bookmarks.iter() - .filter(|id| !position.is_some_and(|p| &p == id) && !current.contains_key(id)) - .filter_map(|id| self.get_by_id(id)) - .collect_vec(); + let bookmarks = + if current.is_empty() { + vec![] + } else { + self.bookmarks.iter() + .filter(|id| !position.is_some_and(|p| &p == id) && !current.contains_key(id)) + .filter_map(|id| self.get_by_id(id)) + .collect_vec() + }; // TODO use ChildIterator current.into_values().chain( bookmarks @@ -528,7 +533,7 @@ impl Tasks { pub(crate) fn toggle_bookmark(&mut self, id: EventId) -> nostr_sdk::Result { match self.bookmarks.iter().position(|b| b == &id) { None => self.bookmarks.push(id), - Some(pos) => { self.bookmarks.remove(pos); }, + Some(pos) => { self.bookmarks.remove(pos); } } self.sender.submit( EventBuilder::new(Kind::Bookmarks, "mostr pins", @@ -1253,10 +1258,14 @@ mod tasks_test { assert_eq!(tasks.filtered_tasks(None).count(), 2); assert_eq!(tasks.filtered_tasks(Some(&zero)).count(), 0); + assert_eq!(tasks.visible_tasks().len(), 1); + assert_eq!(tasks.filtered_tasks(Some(&pin)).count(), 0); + assert_eq!(tasks.filtered_tasks(Some(&zero)).count(), 0); + tasks.submit(EventBuilder::new(Kind::Bookmarks, "", [Tag::event(pin), Tag::event(zero)])); - assert_eq!(tasks.filtered_tasks(Some(&zero)).collect_vec(), vec![tasks.get_by_id(&pin).unwrap()]); - tasks.move_to(Some(pin)); - assert!(tasks.visible_tasks().is_empty()); + assert_eq!(tasks.visible_tasks().len(), 1); + assert_eq!(tasks.filtered_tasks(Some(&pin)).count(), 0); + assert_eq!(tasks.filtered_tasks(Some(&zero)).count(), 0); tasks.move_to(None); assert_eq!(tasks.visible_tasks().len(), 3);