Skip to content

Commit

Permalink
feat: #1232
Browse files Browse the repository at this point in the history
  • Loading branch information
alainm23 committed Jun 20, 2024
1 parent 3a9bf72 commit 25f6164
Show file tree
Hide file tree
Showing 22 changed files with 448 additions and 202 deletions.
102 changes: 60 additions & 42 deletions core/Enum.vala
Original file line number Diff line number Diff line change
Expand Up @@ -221,49 +221,49 @@ public enum RecurrencyType {

public string to_friendly_string (int? interval = null) {
switch (this) {
case NONE:
return _("Don't Repeat");
case MINUTELY:
if (interval == null || interval == 0) {
return _("Every minute");
} else {
return GLib.ngettext (_("Every minute"), _("Every %d minutes"), interval).printf (interval);
}
case HOURLY:
if (interval == null || interval == 0) {
return _("Every hour");
} else {
return GLib.ngettext (_("Every hour"), _("Every %d hours"), interval).printf (interval);
}
case EVERY_DAY:
if (interval == null || interval == 0) {
return _("Every day");
} else {
return GLib.ngettext (_("Every day"), _("Every %d days"), interval).printf (interval);
}
case EVERY_WEEK:
if (interval == null || interval == 0) {
return _("Every week");
} else {
return GLib.ngettext (_("Every week"), _("Every %d weeks"), interval).printf (interval);
}

case EVERY_MONTH:
if (interval == null || interval == 0) {
return _("Every month");
} else {
return GLib.ngettext (_("Every month"), _("Every %d months"), interval).printf (interval);
}

case EVERY_YEAR:
if (interval == null || interval == 0) {
return _("Every year");
} else {
return GLib.ngettext (_("Every year"), _("Every %d years"), interval).printf (interval);
}
case NONE:
return _("Don't Repeat");
case MINUTELY:
if (interval == null || interval == 0) {
return _("Every minute");
} else {
return GLib.ngettext (_("Every minute"), _("Every %d minutes"), interval).printf (interval);
}
case HOURLY:
if (interval == null || interval == 0) {
return _("Every hour");
} else {
return GLib.ngettext (_("Every hour"), _("Every %d hours"), interval).printf (interval);
}
case EVERY_DAY:
if (interval == null || interval == 0) {
return _("Every day");
} else {
return GLib.ngettext (_("Every day"), _("Every %d days"), interval).printf (interval);
}
case EVERY_WEEK:
if (interval == null || interval == 0) {
return _("Every week");
} else {
return GLib.ngettext (_("Every week"), _("Every %d weeks"), interval).printf (interval);
}

case EVERY_MONTH:
if (interval == null || interval == 0) {
return _("Every month");
} else {
return GLib.ngettext (_("Every month"), _("Every %d months"), interval).printf (interval);
}

case EVERY_YEAR:
if (interval == null || interval == 0) {
return _("Every year");
} else {
return GLib.ngettext (_("Every year"), _("Every %d years"), interval).printf (interval);
}

default:
assert_not_reached ();
default:
assert_not_reached ();
}
}
}
Expand Down Expand Up @@ -362,3 +362,21 @@ public enum FilterItemType {
}
}
}

public enum ReminderType {
ABSOLUTE,
RELATIVE;

public string to_string () {
switch (this) {
case ABSOLUTE:
return "absolute";

case RELATIVE:
return "relative";

default:
assert_not_reached ();
}
}
}
12 changes: 0 additions & 12 deletions core/Objects/DueDate.vala
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,6 @@ public class Objects.DueDate : GLib.Object {
is_recurring = object.get_boolean_member ("is_recurring");
Utils.Datetime.parse_todoist_recurrency (this, object);
}
// if (object.has_member ("recurrency_type")) {
// recurrency_type = (RecurrencyType) int.parse (object.get_string_member ("recurrency_type"));
// }

// if (object.has_member ("recurrency_interval")) {
// recurrency_interval = int.parse (object.get_string_member ("recurrency_interval"));
// }

// if (object.has_member ("recurrency_weeks")) {
// recurrency_weeks = object.get_string_member ("recurrency_weeks");
// }
}

public void update_from_json (Json.Object object) {
Expand Down Expand Up @@ -221,7 +210,6 @@ public class Objects.DueDate : GLib.Object {
new_due.recurrency_count = recurrency_count;
new_due.recurrency_end = recurrency_end;
new_due.recurrence_supported = recurrence_supported;

return new_due;
}
}
96 changes: 74 additions & 22 deletions core/Objects/Item.vala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ public class Objects.Item : Objects.BaseObject {
}
}

public bool has_time {
get {
if (due.datetime == null) {
return false;
}

return Utils.Datetime.has_time (due.datetime);
}
}

GLib.DateTime _completed_date;
public GLib.DateTime completed_date {
get {
Expand Down Expand Up @@ -773,7 +783,7 @@ public class Objects.Item : Objects.BaseObject {
reminder_added (reminder);
}

add_reminder (reminder);
_add_reminder (reminder);
}
return return_value;
}
Expand All @@ -783,7 +793,7 @@ public class Objects.Item : Objects.BaseObject {
Objects.Reminder? return_value = null;
lock (_reminders) {
foreach (var _reminder in _reminders) {
if (reminder.due.datetime.compare (_reminder.due.datetime) == 0) {
if (reminder.datetime.compare (_reminder.datetime) == 0) {
return_value = _reminder;
break;
}
Expand All @@ -792,7 +802,7 @@ public class Objects.Item : Objects.BaseObject {
return return_value;
}

private void add_reminder (Objects.Reminder reminder) {
private void _add_reminder (Objects.Reminder reminder) {
_reminders.add (reminder);
}

Expand Down Expand Up @@ -1303,24 +1313,6 @@ public class Objects.Item : Objects.BaseObject {
return new_item;
}

// public void duplicate () {
// var new_item = generate_copy ();
// new_item.content = "[%s] %s".printf (_("Duplicate"), content);

// if (project.backend_type == BackendType.TODOIST) {
// Services.Todoist.get_default ().add.begin (new_item, (obj, res) => {
// HttpResponse response = Services.Todoist.get_default ().add.end (res);
// if (response.status) {
// new_item.id = response.data;
// insert_duplicate (new_item);
// }
// });
// } else {
// new_item.id = Util.get_default ().generate_id (new_item);
// insert_duplicate (new_item);
// }
// }

public Objects.Item duplicate () {
var new_item = new Objects.Item ();
new_item.content = content;
Expand All @@ -1329,7 +1321,6 @@ public class Objects.Item : Objects.BaseObject {
new_item.pinned = pinned;
new_item.priority = priority;
new_item.labels = labels;

return new_item;
}

Expand Down Expand Up @@ -1553,4 +1544,65 @@ public class Objects.Item : Objects.BaseObject {

return text;
}

public void update_due (GLib.DateTime? datetime) {
due.date = datetime == null ? "" : Utils.Datetime.get_todoist_datetime_format (datetime);

if (Services.Settings.get_default ().get_boolean ("automatic-reminders-enabled") && has_time) {
remove_all_relative_reminders ();

var reminder = new Objects.Reminder ();
reminder.mm_offset = Util.get_reminders_mm_offset ();
reminder.reminder_type = ReminderType.RELATIVE;
add_reminder (reminder);
}

if (due.date == "") {
due.reset ();
remove_all_relative_reminders ();
}

if (!has_time) {
remove_all_relative_reminders ();
}

update_async ("");
}

public void add_reminder (Objects.Reminder reminder) {
reminder.item_id = id;

if (project.backend_type == BackendType.TODOIST) {
Services.Todoist.get_default ().add.begin (reminder, (obj, res) => {
HttpResponse response = Services.Todoist.get_default ().add.end (res);
loading = false;

if (response.status) {
reminder.id = response.data;
} else {
reminder.id = Util.get_default ().generate_id (reminder);
}

add_reminder_if_not_exists (reminder);
});
} else {
reminder.id = Util.get_default ().generate_id (reminder);
add_reminder_if_not_exists (reminder);
}
}

public void add_reminder_events (Objects.Reminder reminder) {
Services.Database.get_default ().reminder_added (reminder);
Services.Database.get_default ().reminders.add (reminder);
reminder.item.reminder_added (reminder);
_add_reminder (reminder);
}

private void remove_all_relative_reminders () {
foreach (Objects.Reminder reminder in reminders) {
if (reminder.reminder_type == ReminderType.RELATIVE) {
reminder.delete ();
}
}
}
}
61 changes: 47 additions & 14 deletions core/Objects/Reminder.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Objects.Reminder : Objects.BaseObject {
public Objects.DueDate due { get; set; default = new Objects.DueDate (); }
public int mm_offset { get; set; default = 0; }
public int is_deleted { get; set; default = 0; }
public ReminderType reminder_type { get; set; default = ReminderType.ABSOLUTE; }

Objects.Item? _item;
public Objects.Item item {
Expand All @@ -39,19 +40,45 @@ public class Objects.Reminder : Objects.BaseObject {
}
}

bool _loading = false;
public bool loading {
set {
_loading = value;
loading_changed (_loading);
GLib.DateTime _datetime;
public GLib.DateTime datetime {
get {
if (reminder_type == ReminderType.ABSOLUTE) {
_datetime = due.datetime;
} else {
_datetime = item.due.datetime.add_minutes (mm_offset * -1);
}

return _datetime;
}
}


string _relative_text;
public string relative_text {
get {
return _loading;
_relative_text = "";

if (reminder_type == ReminderType.ABSOLUTE) {
_relative_text = Utils.Datetime.get_relative_date_from_date (due.datetime);
} else {
_relative_text = Util.get_reminders_mm_offset_text (mm_offset);
}

return _relative_text;
}
}

public signal void loading_changed (bool value);
public Reminder.from_json (Json.Node node) {
id = node.get_object ().get_string_member ("id");
item_id = node.get_object ().get_string_member ("item_id");
reminder_type = node.get_object ().get_string_member ("type") == "absolute" ? ReminderType.ABSOLUTE : ReminderType.RELATIVE;
mm_offset = (int32) node.get_object ().get_int_member ("item_id");

if (reminder_type == ReminderType.ABSOLUTE) {
due.update_from_todoist_json (node.get_object ().get_object_member ("due"));
}
}

construct {
deleted.connect (() => {
Expand Down Expand Up @@ -94,13 +121,19 @@ public class Objects.Reminder : Objects.BaseObject {
builder.set_member_name ("item_id");
builder.add_string_value (item_id);

builder.set_member_name ("due");
builder.begin_object ();

builder.set_member_name ("date");
builder.add_string_value (due.date);

builder.end_object ();
builder.set_member_name ("type");
builder.add_string_value (reminder_type.to_string ());

builder.set_member_name ("minute_offset");
builder.add_int_value (mm_offset);

if (reminder_type == ReminderType.ABSOLUTE) {
builder.set_member_name ("due");
builder.begin_object ();
builder.set_member_name ("date");
builder.add_string_value (due.date);
builder.end_object ();
}

builder.end_object ();
builder.end_object ();
Expand Down
Loading

0 comments on commit 25f6164

Please sign in to comment.