<< Click to Display Table of Contents >> Google Tasks |
![]() ![]() ![]() |
NG-ConnectionPack provide access to Google Tasks service, which allows the user to manage todo lists. TNGGTasks component provide access to Google Tasks service. To use Google Tasks service, please ensure that Tasks API is enabled for your application, registered as described in Google Registration topic:
Google Tasks service generally maintains several tasks lists for each user. The user can create and delete tasks lists as required. However, One of the list is considered to be a primary task list, which is created by default and can't be deleted.
Each task list contains a separate list of tasks. Speaking more precisely, the task list is not a list of tasks, but a tree of tasks, since the tasks can be organized using parent-child relationship.
In NG-ConnectionPack a task list is represented by TNGGTaskList record. As has been stated above there is a single primary task list which is available by default for every user account and cannot be deleted. This list is accessible using TNGGTasks.Default function.
Since Google Tasks service allows several task lists to be present in the user's account, TNGGTasks component provides TaskLists overloaded methods, which allow to retrieve all users tasks lists:
var
lsts: TArray<TNGGTaskList>;
i: Integer;
begin
lsts := MyGoogleTasks.TaskLists;
for i := 0 to High(lsts) do
Memo1.Lines.Add(lsts[i].Title);
end;
As can be seen from above example, TaskLists function return an array of TNGGTaskList objects. The second overload version allows to retrieve task lists via inline callback procedure, which in some cases can simplify application's code:
MyGoogleTasks.TaskLists(procedure(L: TNGGTaskList)
begin
Memo1.Lines.Add(L.Title);
end);
Except Id property, which represents the unique string identifier of the task list, TNGTaskList contains only one Title property, which is a task list title, intended to be shown to the user in the application user interface. Title property is writable, so a value can be assigned to it to rename the task list.
TNGGTasks component provides Add method, which allows to create new secondary task list:
lst := MyGoogleTasks.Add('My New Task List');
Existing task list can be deleted using its Delete method:
lst.Delete;
However, as noted before, the primary task list cannot be deleted.
In NG-ConnectionPack a task is represented by TNGGTask record. Since tasks are always owned by task lists, the can be retrieved or created only in the context of some task list. For retrieving task list tasks the Tasks overloaded methods can be used:
var
tsks: TArray<TNGGTask>;
i: Integer;
begin
tsks := MyTasks.Default.Tasks;
for i := 0 to High(tsks) do
Memo1.Lines.Add(tsks[i].Title);
end;
The second overload version allows to retrieve tasks via inline callback procedure, which in some cases can simplify application's code:
MyTasks.Default.Tasks(procedure(T: TNGGTask)
begin
Memo1.Lines.Add(T.Title);
end);
As about tasks parent-child relationship, NG-ConnectionPack has simplified support, and all tasks are reported as a plain array. However, since TNGGTask have Parent and HasParent properties along with TryGetParent method, its always possible to re-create the tasks tree. But, its up the the customer's application code.
TNGGTask record provides the following useful properties:
• | Id property represents the unique string task identifier, which is read-only and provided by the service. |
• | Parent and HasParent properties provide access to the task's parent task, if any. |
• | Title property provides access to end-user specified task title. Usually this is the main task text, as opposed to Notes, which are secondary. |
• | Notes property provides access to end-user specified task notes. Usually this is a secondary task text. |
• | Status property provides access to the current task's status. Possible values are: tsCompleted, which means that the task is already completed; completed tasks has meaningful Completed date-time. And tsNeedsAction, which simply means that the task is not yet completed. |
• | Updated property provides access to the task's lat modification date-time. |
• | Due and HasDue properties provide access to the task's due date, if it was specified by the user. The task can have no due date. |
• | Completed property provides access to the task's completion date-time, if the task was already completed. |
Its important to understand, that all these properties are read-only, but the task is still can be changed, using TNGGTask.TBuilder object, which is returned by the task's Update method. The use of builder object is very simple and its allows to update several task's properties in a single Http request. Usually, all the method calls chain can be written in code in a single statement like this:
var
tsk: TNGGTask;
begin
tsk := GetSomeTask;
tsk.Update.Title('My Updated Title')
.Notes('My Updated Notes')
.Status(tsCompleted)
.Execute;
end;
To create new task various overload of the Add method can be used. These methods are generally declared in TNGGTaskList record, but also similar methods are declared in TNGGTask record for simple child tasks creation. Please note, that all these methods returns builder object, and so, should be used as described above:
tsk := MyTasks.Default.Add.Title('My Title')
.Notes('My Notes')
.Execute;
Since the ordering of the tasks in the list are considered important, some Add method overloads include APrev parameter, which can be used to specify the previous task, that is the task, after which new task will be added. All other Add method overloads inserts new task at the top of the list (or parent task).
A task can be moved to new position in the task list using it MoveInto overloaded methods. These methods allows to specify new parent and previous tasks. Both AParent and APrev parameters values can be nil. A task can be deleted using its Delete method:
tsk.Delete;
TNGGTaskList record also supports Clear method, which can be used to remove all tasks from the list.