feat(task): more accessors and modifiers

This commit is contained in:
xeruf 2024-07-26 21:15:50 +03:00
parent c2f1f54170
commit 54e870a93a
1 changed files with 30 additions and 1 deletions

View File

@ -1,7 +1,9 @@
use std::collections::{BTreeSet, HashSet};
use std::fmt;
use nostr_sdk::{Event, EventId, Kind, Tag, Timestamp};
use nostr_sdk::{Event, EventBuilder, EventId, Kind, Tag, Timestamp};
use crate::EventSender;
pub(crate) struct Task {
pub(crate) event: Event,
@ -10,6 +12,7 @@ pub(crate) struct Task {
/// Cached sorted tags of the event
pub(crate) tags: Option<BTreeSet<Tag>>,
}
impl Task {
pub(crate) fn new(event: Event) -> Task {
Task {
@ -24,6 +27,10 @@ impl Task {
}
}
pub(crate) fn get_id(&self) -> &EventId {
&self.event.id
}
pub(crate) fn parent_id(&self) -> Option<EventId> {
for tag in self.event.tags.iter() {
match tag {
@ -66,6 +73,23 @@ impl Task {
self.state().map_or(State::Open, |s| s.state)
}
pub(crate) fn set_state(
&mut self,
sender: &EventSender,
state: State,
comment: &str,
) -> Option<Event> {
sender
.submit(EventBuilder::new(
state.kind(),
comment,
vec![Tag::event(self.event.id)],
))
.inspect(|e| {
self.props.insert(e.clone());
})
}
fn default_state(&self) -> TaskState {
TaskState {
name: None,
@ -136,6 +160,11 @@ impl TaskState {
pub(crate) fn get_label(&self) -> String {
self.name.clone().unwrap_or_else(|| self.state.to_string())
}
pub(crate) fn matches_label(&self, label: &str) -> bool {
self.state == State::Active
|| self.name.as_ref().is_some_and(|n| n == label)
|| self.state.to_string() == label
}
}
impl fmt::Display for TaskState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {