Skip to content

Commit

Permalink
Add task_tree to db operations
Browse files Browse the repository at this point in the history
Use some clippy sugesstions
Remove refresh day indicators inside Calendar::go_today and expand
method in handle_calendar_today_clicked
  • Loading branch information
iman-salmani committed Jul 29, 2023
1 parent 1ca9bde commit 81c3122
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 72 deletions.
29 changes: 25 additions & 4 deletions src/db/operations/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 9,7 @@ pub fn create_task(task: Task) -> Result<Task> {
"INSERT INTO tasks(name, project, section, position, parent, description, date) VALUES (?1,?2,?3,?4,?5,?6,?7)",
(task.name(), task.project(), task.section(), task.position(), task.parent(), task.description(), task.date()),
)?;
task.set_id(&conn.last_insert_rowid());
task.set_id(conn.last_insert_rowid());
Ok(task)
}

Expand Down Expand Up @@ -38,7 38,7 @@ pub fn read_tasks(
filters.push(format!("date >= {start} AND date < {end}"));
}
if !suspended {
filters.push(format!("suspended = false"));
filters.push("suspended = false".to_string());
}
let filters_str = &mut String::new();
for filter in filters {
Expand All @@ -63,14 63,35 @@ pub fn read_tasks(

pub fn read_subtasks_summary(task_id: i64) -> Result<Vec<(String, bool)>> {
let conn = get_connection();
let mut stmt =
conn.prepare("SELECT name, done FROM tasks WHERE parent = ?1 ORDER BY position DESC")?;
let mut rows = stmt.query([task_id])?;
let mut subtasks = Vec::new();
while let Some(row) = rows.next()? {
subtasks.push((row.get::<usize, String>(0)?, row.get::<usize, bool>(1)?));
}
Ok(subtasks)
}

pub fn task_tree(task_id: i64, has_date: bool) -> Result<Vec<i64>> {
let conn = get_connection();
let filter = if has_date { "WHERE date != 0" } else { "" };
let mut stmt = conn.prepare(&format!(
"SELECT name, done FROM tasks WHERE parent = ?1 ORDER BY position DESC"
"WITH RECURSIVE task_tree(id, parent, date) AS (
SELECT id, parent, date FROM tasks WHERE id=?1
UNION ALL
SELECT tasks.id, tasks.parent, tasks.date
FROM tasks
JOIN task_tree ON tasks.parent=task_tree.id
)
SELECT id FROM task_tree {filter}",
))?;
let mut rows = stmt.query([task_id])?;
let mut subtasks = Vec::new();
while let Some(row) = rows.next()? {
subtasks.push((row.get::<usize, String>(0)?, row.get::<usize, bool>(1)?));
subtasks.push(row.get::<usize, i64>(0)?);
}

Ok(subtasks)
}

Expand Down
121 changes: 53 additions & 68 deletions src/views/calendar/calendar.rs
Original file line number Diff line number Diff line change
@@ -1,10 1,11 @@
use gtk::{gdk, glib, prelude::*, subclass::prelude::*};
use std::cell::{Cell, RefCell};
use std::cmp::Ordering;
use std::thread;
use std::time::Duration;

use crate::db::models::Task;
use crate::db::operations::{read_records, read_task, read_tasks};
use crate::db::operations::{read_records, read_task, task_tree};
use crate::views::calendar::{DayIndicator, DayView};
use crate::views::task::TaskRow;
use crate::views::ActionScope;
Expand Down Expand Up @@ -126,29 127,16 @@ glib::wrapper! {
@implements gtk::Buildable;
}

#[gtk::template_callbacks]
impl Calendar {
pub fn new() -> Self {
impl Default for Calendar {
fn default() -> Self {
glib::Object::new::<Self>()
}
}

pub fn go_today(&self) {
let imp = self.imp();
let today = self.today_datetime();

loop {
if let Some(indicator) = imp.navigation_bar.first_child() {
imp.navigation_bar.remove(&indicator);
} else {
break;
}
}

for day in -2..5 {
let datetime = today.add_days(day).unwrap();
imp.navigation_bar.append(&self.new_day_indicator(datetime));
}
self.foucs_on_date(today);
#[gtk::template_callbacks]
impl Calendar {
pub fn new() -> Self {
Self::default()
}

pub fn refresh(&self) {
Expand All @@ -173,7 161,7 @@ impl Calendar {
let day_view = self.day_view_by_date(datetime).unwrap();
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
glib::idle_add(move || {
if let Ok(_) = tx.send(()) {
if tx.send(()).is_ok() {
glib::Continue(true)
} else {
glib::Continue(false)
Expand Down Expand Up @@ -259,16 247,9 @@ impl Calendar {
}

pub fn set_subtasks_suspended(&self, task_id: i64, suspended: bool) {
let subtasks = read_tasks(None, None, None, Some(task_id), None, true).unwrap();
let subtasks = task_tree(task_id, true).unwrap();
for subtask in subtasks {
let subtask_id = subtask.id();
self.set_subtasks_suspended(subtask_id, suspended);

if subtask.date() == 0 {
continue;
}

if let Some((_, row)) = self.task_row(subtask_id) {
if let Some((_, row)) = self.task_row(subtask) {
row.task().set_suspended(suspended);
row.changed();
}
Expand Down Expand Up @@ -380,43 361,46 @@ impl Calendar {
.difference(&first_day_indicator.datetime())
.as_days();

if difference > 0 {
for _ in 0..difference {
let first_day_indicator = imp
.navigation_bar
.first_child()
.and_downcast::<DayIndicator>()
.unwrap();
let last_day_indicator = imp
.navigation_bar
.last_child()
.and_downcast::<DayIndicator>()
.unwrap();
imp.navigation_bar.remove(&first_day_indicator);

match difference.cmp(&0) {
Ordering::Greater => {
for _ in 0..difference {
let first_day_indicator = imp
.navigation_bar
.first_child()
.and_downcast::<DayIndicator>()
.unwrap();
let last_day_indicator = imp
.navigation_bar
.last_child()
.and_downcast::<DayIndicator>()
.unwrap();
imp.navigation_bar.remove(&first_day_indicator);

let date = last_day_indicator.datetime().add_days(1).unwrap();
let day_indicator = obj.new_day_indicator(date);
imp.navigation_bar.append(&day_indicator);
}
} else if difference < 0 {
for _ in 0..difference.abs() {
let first_day_indicator = imp
.navigation_bar
.first_child()
.and_downcast::<DayIndicator>()
.unwrap();
let last_day_indicator = imp
.navigation_bar
.last_child()
.and_downcast::<DayIndicator>()
.unwrap();
imp.navigation_bar.remove(&last_day_indicator);
let date = last_day_indicator.datetime().add_days(1).unwrap();
let day_indicator = obj.new_day_indicator(date);
imp.navigation_bar.append(&day_indicator);
}
},
Ordering::Less => {
for _ in 0..difference.abs() {
let first_day_indicator = imp
.navigation_bar
.first_child()
.and_downcast::<DayIndicator>()
.unwrap();
let last_day_indicator = imp
.navigation_bar
.last_child()
.and_downcast::<DayIndicator>()
.unwrap();
imp.navigation_bar.remove(&last_day_indicator);

let date = first_day_indicator.datetime().add_days(-1).unwrap();
let day_indicator = obj.new_day_indicator(date);
imp.navigation_bar.prepend(&day_indicator);
}
let date = first_day_indicator.datetime().add_days(-1).unwrap();
let day_indicator = obj.new_day_indicator(date);
imp.navigation_bar.prepend(&day_indicator);
}
},
Ordering::Equal => {}
}

let first_day_view = imp.days_box.first_child().and_downcast::<DayView>().unwrap();
Expand All @@ -427,7 411,7 @@ impl Calendar {
imp.days_box.prepend(&day_view);
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
glib::idle_add(move || {
if let Ok(_) = tx.send(()) {
if tx.send(()).is_ok() {
glib::Continue(true)
} else {
glib::Continue(false)
Expand Down Expand Up @@ -554,6 538,7 @@ impl Calendar {

#[template_callback]
fn handle_calendar_today_clicked(&self, _: gtk::Button) {
self.go_today();
let today = self.today_datetime();
self.foucs_on_date(today);
}
}

0 comments on commit 81c3122

Please sign in to comment.