Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrvladus committed Apr 18, 2024
1 parent b7670a1 commit 6022d5c
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 123 deletions.
1 change: 1 addition & 0 deletions build-aux/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ flatpak build \
--env=PKG_CONFIG_PATH=/app/lib/pkgconfig:/app/share/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig \
--filesystem=$cwd/_build \ $cwd/.flatpak/repo meson install -C _build

echo "Run"
flatpak build \
--with-appdir \
--allow=devel \
Expand Down
4 changes: 4 additions & 0 deletions data/resources/style-dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
background: #512e12;
}

.tag {
background-color: alpha(white, 0.25);
}

link {
color: white;
}
2 changes: 1 addition & 1 deletion data/resources/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
padding-bottom: 3px;
padding-left: 5px;
padding-right: 5px;
background-color: @shade_color;
background-color: alpha(black, 0.07);
}

.tag>button {
Expand Down
25 changes: 14 additions & 11 deletions errands/lib/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,27 +246,30 @@ def add_tag(self, tag: str) -> None:
self.tags = new_tags

def update_tags(self) -> None:
tags_list: list[str] = [t.tags for t in self.tasks if t.tags]
tasks_tags_texts: list[str] = []
for task in self.tasks:
tasks_tags_texts.extend(task.tags)

current_tags = self.tags
current_tags_texts = [t.text for t in current_tags]

tags: list[str] = []
for item in tags_list:
tags.extend(item.split(","))

for tag in tags:
added_tag: bool = False
for tag in tasks_tags_texts:
if tag not in current_tags_texts:
current_tags.append(TagsData(text=tag))
added_tag = True

self.tags = current_tags
if added_tag:
self.tags = current_tags

def remove_tags(self, tags: list[str]) -> None:
self.tags = [t for t in self.tags if t.text not in tags]
def remove_tag(self, tag: str) -> None:
self.tags = [t for t in self.tags if t.text != tag]
changed = False
tasks = self.tasks
for task in tasks:
if task.tags != []:
task.tags = [t for t in task.tags if t not in tags]
if task.tags != [] and tag in task.tags:
task.tags = [t for t in task.tags if t != tag]
task.synced = False
changed = True
if changed:
self.tasks = tasks
Expand Down
136 changes: 64 additions & 72 deletions errands/lib/sync/providers/caldav.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import datetime
from dataclasses import asdict, dataclass, field
import time
from typing import Any

import urllib3
from caldav import Calendar, DAVClient, Principal, Todo
Expand Down Expand Up @@ -89,6 +91,7 @@ def _connect(self) -> bool:
if "VTODO" in cal.get_supported_components()
]
except Exception as e:
time.sleep(2)
Log.error(
f"Sync: Can't connect to {self.name} server at '{self.url}'. {e}"
)
Expand All @@ -111,19 +114,13 @@ def __get_tasks(self, calendar: Calendar) -> list[TaskData]:
color=str(todo.icalendar_component.get("x-errands-color", "")),
completed=str(todo.icalendar_component.get("status", ""))
== "COMPLETED",
expanded=bool(
todo.icalendar_component.get("x-errands-expanded", False)
),
notes=str(todo.icalendar_component.get("description", "")),
parent=str(todo.icalendar_component.get("related-to", "")),
percent_complete=int(
todo.icalendar_component.get("percent-complete", 0)
),
priority=int(todo.icalendar_component.get("priority", 0)),
text=str(todo.icalendar_component.get("summary", "")),
toolbar_shown=bool(
todo.icalendar_component.get("x-errands-toolbar-shown", False)
),
uid=str(todo.icalendar_component.get("uid", "")),
list_uid=calendar.id,
)
Expand Down Expand Up @@ -236,6 +233,7 @@ def __finish_sync(self, args: UpdateUIArgs) -> None:
State.get_task(task.list_uid, task.uid).update_ui(False)
except Exception:
pass

# Update tags
if args.update_tags:
State.tags_sidebar_row.update_ui()
Expand All @@ -253,6 +251,17 @@ def sync(self) -> None:
update_ui_args: UpdateUIArgs = UpdateUIArgs()

remote_lists_uids = [c.id for c in self.calendars]
user_lists_uids = [lst.uid for lst in UserData.get_lists_as_dicts()]

# Add new local list
for calendar in self.calendars:
if calendar.id not in user_lists_uids:
Log.debug(f"Sync: Copy list from remote '{calendar.id}'")
new_list: TaskListData = UserData.add_list(
name=calendar.name, uuid=calendar.id, synced=True
)
update_ui_args.lists_to_add.append(new_list)

for list in UserData.get_lists_as_dicts():
# Rename list
for cal in self.calendars:
Expand Down Expand Up @@ -305,9 +314,6 @@ def sync(self) -> None:
if not self.__update_calendars():
return

user_lists_uids = [lst.uid for lst in UserData.get_lists_as_dicts()]
remote_lists_uids = [c.id for c in self.calendars]

for calendar in self.calendars:
# Get tasks
local_tasks = UserData.get_tasks_as_dicts(calendar.id)
Expand All @@ -316,74 +322,34 @@ def sync(self) -> None:
remote_ids = [task.uid for task in remote_tasks]
deleted_uids = [t.uid for t in UserData.get_tasks_as_dicts() if t.deleted]

# Add new local list
if calendar.id not in user_lists_uids:
Log.debug(f"Sync: Copy list from remote '{calendar.id}'")
new_list: TaskListData = UserData.add_list(
name=calendar.name, uuid=calendar.id, synced=True
)
update_ui_args.lists_to_add.append(new_list)

for task in local_tasks:
# Update local task that was changed on remote
if task.uid in remote_ids and task.synced:
for remote_task in remote_tasks:
if remote_task.uid == task.uid:
updated: bool = False
for key in asdict(task).keys():
remote_task_keys = asdict(remote_task).keys()
updated_props = []
updated_values = []
for key in remote_task_keys:
if (
key
not in "deleted list_uid synced expanded trash toolbar_shown"
and getattr(task, key) != getattr(remote_task, key)
not in "synced trash expanded toolbar_shown deleted"
and getattr(remote_task, key) != getattr(task, key)
):
UserData.update_props(
calendar.id,
task.uid,
[key],
[getattr(remote_task, key)],
)
updated = True
if updated:
updated_props.append(key)
updated_values.append(getattr(remote_task, key))
if updated_props:
Log.debug(
f"Sync: Update local task from remote '{task.uid}'"
f"Sync: Update local task from remote '{task.uid}'. Updated: {updated_props}"
)
update_ui_args.tasks_to_update.append(task)
update_ui_args.update_tags = True
update_ui_args.update_trash = True
UserData.update_props(
calendar.id, task.uid, updated_props, updated_values
)
update_ui_args.tasks_to_update.append(task)
update_ui_args.update_tags = True
update_ui_args.update_trash = True
break

# Create new task on remote that was created offline
if task.uid not in remote_ids and not task.synced:
try:
Log.debug(f"Sync: Create new task on remote: {task.uid}")
new_todo = calendar.save_todo(
categories=",".join(task.tags) if task.tags else None,
description=task.notes,
dtstart=(
datetime.datetime.fromisoformat(task.start_date)
if task.start_date
else None
),
due=(
datetime.datetime.fromisoformat(task.due_date)
if task.due_date
else None
),
priority=task.priority,
percent_complete=task.percent_complete,
related_to=task.parent,
summary=task.text,
uid=task.uid,
x_errands_color=task.color,
)
if task.completed:
new_todo.complete()
UserData.update_props(calendar.id, task.uid, ["synced"], [True])
except Exception as e:
Log.error(
f"Sync: Can't create new task on remote: {task.uid}. {e}"
)

# Update task on remote that was changed locally
elif task.uid in remote_ids and not task.synced:
try:
Expand Down Expand Up @@ -422,19 +388,45 @@ def sync(self) -> None:
)
todo.icalendar_component["related-to"] = task.parent
todo.icalendar_component["x-errands-color"] = task.color
todo.icalendar_component["x-errands-expanded"] = int(
task.expanded
)
todo.icalendar_component["x-errands-toolbar-shown"] = int(
task.toolbar_shown
)
todo.save()
if task.completed:
todo.complete()
UserData.update_props(calendar.id, task.uid, ["synced"], [True])
except Exception as e:
Log.error(f"Sync: Can't update task on remote: {task.uid}. {e}")

# Create new task on remote that was created offline
if task.uid not in remote_ids and not task.synced:
try:
Log.debug(f"Sync: Create new task on remote: {task.uid}")
new_todo = calendar.save_todo(
categories=",".join(task.tags) if task.tags else None,
description=task.notes,
dtstart=(
datetime.datetime.fromisoformat(task.start_date)
if task.start_date
else None
),
due=(
datetime.datetime.fromisoformat(task.due_date)
if task.due_date
else None
),
priority=task.priority,
percent_complete=task.percent_complete,
related_to=task.parent,
summary=task.text,
uid=task.uid,
x_errands_color=task.color,
)
if task.completed:
new_todo.complete()
UserData.update_props(calendar.id, task.uid, ["synced"], [True])
except Exception as e:
Log.error(
f"Sync: Can't create new task on remote: {task.uid}. {e}"
)

# Delete local task that was deleted on remote
elif task.uid not in remote_ids and task.synced:
Log.debug(f"Sync: Delete local task deleted on remote: {task.uid}")
Expand All @@ -454,7 +446,7 @@ def sync(self) -> None:
f"Sync: Can't delete task from remote: '{task.uid}'. {e}"
)

# Create new local task that was created on CalDAV
# Create new local task that was created on remote
for task in remote_tasks:
if task.uid not in local_ids and task.uid not in deleted_uids:
Log.debug(
Expand Down
Loading

0 comments on commit 6022d5c

Please sign in to comment.