Skip to content

Commit

Permalink
Removed unnecessary path in dependency methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaaskarian committed Jan 26, 2024
1 parent 5ff4e13 commit d034fc3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 60 deletions.
5 changes: 2 additions & 3 deletions src/todo_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,11 @@ impl TodoList {
}

#[inline]
fn remove_dependencies(&mut self, path: &PathBuf) -> io::Result<()> {
fn remove_dependencies(&mut self) {
let mut todos = [&mut self.undone.todos, &mut self.done.todos];
for todo in todos.iter_mut().flat_map(|v| v.iter_mut()) {
todo.remove_dependency(path);
todo.remove_dependency();
}
Ok(())
}

#[inline]
Expand Down
80 changes: 34 additions & 46 deletions src/todo_list/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Todo {
dependency_name: String,
dependency_type: DependencyType,
done:bool,
removed_files: Vec<PathBuf>,
removed_names: Vec<String>,
daily: bool,
date_str: String,
}
Expand Down Expand Up @@ -137,7 +137,7 @@ impl TryFrom<&str> for Todo {
dependency_type,
date_str,
daily,
removed_files: Vec::new(),
removed_names: Vec::new(),
dependency_name,
dependencies,
message,
Expand All @@ -160,7 +160,7 @@ impl Todo {
dependency_type: DependencyType::None,
date_str: String::new(),
daily: false,
removed_files: Vec::new(),
removed_names: Vec::new(),
dependency_name: String::new(),
dependencies: TodoList::new(),
message,
Expand All @@ -183,20 +183,10 @@ impl Todo {
format!("{name}.todo")
}

// pub fn dependency_path(&self) -> Option<PathBuf> {
// note_path(&self.dependency_name, self.dependency_dir()).unwrap()
// }

pub fn remove_note(&mut self, path: &PathBuf) {
// if let Some(path) = self.dependency_path() {
// self.removed_files.push(path);
// }
// if !self.dependency_name.is_empty() {
pub fn remove_note(&mut self) {
if self.dependency_type == DependencyType::Note {
self.remove_dependency(path);
// self.removed_files.push(path.join(name));
self.remove_dependency();
}
self.dependency_type = DependencyType::None;
}

#[inline]
Expand All @@ -215,13 +205,13 @@ impl Todo {
}

#[inline]
pub fn add_dependency(&mut self, path: &PathBuf) -> Result<(), TodoError>{
if self.has_dependency() {
pub fn add_todo_dependency(&mut self) -> Result<(), TodoError>{
if self.has_todo_dependency() {
return Err(TodoError::AlreadyExists)
}
if let Some(path) = self.dependency_path(path) {
let _ = self.remove_note(&path);
}
// if let Some(path) = self.dependency_path(path) {
// let _ = self.remove_note(&path);
// }
self.dependency_name = Self::todolist_name(&self.hash());

self.dependency_type = DependencyType::TodoList;
Expand All @@ -245,14 +235,14 @@ impl Todo {

pub fn remove_dependent_files(&mut self, path: &PathBuf) -> io::Result<()>{
self.dependencies.handle_dependent_files(path);
for path in &self.removed_files {
let _ = remove_file(path);
for name in &self.removed_names {
let _ = remove_file(path.join(name));
}
self.removed_files = Vec::new();
self.removed_names = Vec::new();
Ok(())
}

pub fn has_dependency(&self) -> bool {
pub fn has_todo_dependency(&self) -> bool {
self.dependency_type == DependencyType::TodoList
}

Expand Down Expand Up @@ -309,30 +299,28 @@ impl Todo {
}
}

pub fn remove_dependency(&mut self, path: &PathBuf) {
if let Some(path) = self.dependency_path(path) {
self.removed_files.push(path);
pub fn remove_dependency(&mut self) {
if self.dependency_type != DependencyType::None {
self.removed_names.push(self.dependency_name.clone());
}

self.dependency_type = DependencyType::None;
self.dependencies.remove_dependencies(path);
self.dependencies.remove_dependencies();
self.dependency_name = String::new();
self.note = String::new();
// self.dependencies = TodoList::new();
}

pub fn set_note(&mut self, note:String, path: &PathBuf) -> io::Result<()>{
// let _ = self.remove_dependency(&path);
pub fn set_note(&mut self, note:String) -> io::Result<()>{
self.dependency_name = sha1(&note);
self.dependency_type = DependencyType::Note;
self.note = note;
Ok(())
}

pub fn edit_note(&mut self, path: &PathBuf)-> io::Result<()>{
pub fn edit_note(&mut self)-> io::Result<()>{
let note = open_temp_editor(self.note.clone())?;

self.set_note(note, path)?;
self.set_note(note)?;
Ok(())
}

Expand Down Expand Up @@ -433,7 +421,7 @@ mod tests {
fn test_todo_into_string() {
let mut todo = Todo::default("Test".to_string(), 1);
let path = PathBuf::new();
todo.set_note("Note".to_string(), &path);
todo.set_note("Note".to_string());

let expected = "[1]>2c924e3088204ee77ba681f72be3444357932fca Test";
let result: String = (&todo).into();
Expand All @@ -449,7 +437,7 @@ mod tests {
dependency_type: DependencyType::Note,
date_str: String::new(),
daily: false,
removed_files: Vec::new(),
removed_names: Vec::new(),
dependency_name: String::from("2c924e3088204ee77ba681f72be3444357932fca"),
message: "Test".to_string(),
priority: 1,
Expand Down Expand Up @@ -501,7 +489,7 @@ mod tests {
let mut todo = Todo::default("Test".to_string(), 1);
let expected = "900a80c94f076b4ee7006a9747667ccf6878a72b.todo";
let pathbuf = PathBuf::from("tests/TODO_LIST");
todo.add_dependency(&pathbuf).expect("Error setting dependency");
todo.add_todo_dependency().expect("Error setting dependency");

let result = &todo.dependency_name;
assert_eq!(result, expected);
Expand All @@ -511,10 +499,10 @@ mod tests {
fn test_remove_note() {
let mut todo = Todo::default("Test".to_string(), 1);
let path = PathBuf::new();
todo.set_note("Note".to_string(), &path).expect("Error setting note");
todo.set_note("Note".to_string()).expect("Error setting note");
let pathbuf = PathBuf::from("test/TODO_LIST");

todo.remove_note(&pathbuf);
todo.remove_note();

assert_eq!(todo.dependency_type, DependencyType::None);
assert!(todo.note_empty());
Expand All @@ -526,20 +514,20 @@ mod tests {

let pathbuf = PathBuf::from("test/TODO_LIST");

todo.add_dependency(&pathbuf);
todo.add_todo_dependency();

assert!(todo.has_dependency());
assert!(todo.has_todo_dependency());
}

#[test]
fn test_remove_dependency() {
let mut todo = Todo::default("Test".to_string(), 1);
let pathbuf = PathBuf::from("test/TODO_LIST");
todo.add_dependency(&pathbuf);
todo.add_todo_dependency();

todo.remove_dependency(&pathbuf);
todo.remove_dependency();

assert!(!todo.has_dependency());
assert!(!todo.has_todo_dependency());
}

#[test]
Expand All @@ -563,7 +551,7 @@ mod tests {
dependency_type: DependencyType::TodoList,
date_str: String::new(),
daily: false,
removed_files: Vec::new(),
removed_names: Vec::new(),
dependency_name: "1BE348656D84993A6DF0DB0DECF2E95EF2CF461c.todo".to_string(),
message: "Read for exams".to_string(),
priority: 1,
Expand All @@ -582,7 +570,7 @@ mod tests {
dependency_type: DependencyType::None,
date_str: "2023-09-05".to_string(),
daily: true,
removed_files: Vec::new(),
removed_names: Vec::new(),
dependency_name: String::new(),
message: "this one should be daily".to_string(),
priority: 2,
Expand All @@ -599,7 +587,7 @@ mod tests {
dependency_type: DependencyType::None,
date_str: String::new(),
daily: true,
removed_files: Vec::new(),
removed_names: Vec::new(),
dependency_name: String::new(),
message: "this one should be daily".to_string(),
priority: 2,
Expand Down
19 changes: 8 additions & 11 deletions src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl<'a>App<'a>{
#[inline]
pub fn traverse_down(&mut self) {
match self.todo() {
Some(todo) if todo.has_dependency() => {
Some(todo) if todo.has_todo_dependency() => {
self.prior_indexes.push(self.index);
self.index = 0;
}
Expand Down Expand Up @@ -459,9 +459,9 @@ impl<'a>App<'a>{
#[inline]
pub fn add_dependency_traverse_down(&mut self) {
if let Some(todo) = self.todo() {
if !todo.has_dependency() && todo.note_empty() {
if !todo.has_todo_dependency() && todo.note_empty() {
let todo_path = self.todo_path.clone();
self.mut_todo().unwrap().add_dependency(&todo_path);
self.mut_todo().unwrap().add_todo_dependency();
}
}
self.traverse_down()
Expand Down Expand Up @@ -510,25 +510,22 @@ impl<'a>App<'a>{

#[inline]
pub fn edit_or_add_note(&mut self) {
let path = self.todo_path.clone();
if let Some(todo) = self.mut_todo() {
todo.edit_note(&path);
todo.edit_note();
}
}

#[inline]
pub fn add_dependency(&mut self) {
let todo_path = self.todo_path.clone();
if let Some(todo) = self.mut_todo() {
todo.add_dependency(&todo_path);
todo.add_todo_dependency();
}
}

#[inline]
pub fn remove_current_dependent(&mut self) {
let path = self.todo_path.clone();
if let Some(todo) = self.mut_todo() {
todo.remove_dependency(&path);
todo.remove_dependency();
}
}

Expand Down Expand Up @@ -765,7 +762,7 @@ impl<'a>App<'a>{
// };

let dependency_width = if let Some(todo) = todo {
let should_show_right = (todo.has_dependency() || !todo.get_note_content().is_empty()) && self.show_right;
let should_show_right = (todo.has_todo_dependency() || !todo.get_note_content().is_empty()) && self.show_right;
40 * (should_show_right as u16)
} else {
0
Expand Down Expand Up @@ -820,7 +817,7 @@ impl<'a>App<'a>{
let note_widget = Paragraph::new(Text::styled(todo.get_note_content(), Style::default())).wrap(Wrap { trim: true }).block(default_block("Todo note"));
frame.render_widget(note_widget, todos_layout[1]);
} else
if todo.has_dependency() {
if todo.has_todo_dependency() {
match create_todo_widget(&todo.dependencies.display(self.show_done), String::from("Todo dependencies")) {
TodoWidget::List(widget) =>frame.render_widget(widget, todos_layout[1]),
TodoWidget::Paragraph(widget) =>frame.render_widget(widget, todos_layout[1]),
Expand Down

0 comments on commit d034fc3

Please sign in to comment.