Ejemplo n.º 1
0
        /// <summary>
        /// Load the task lists and all associated data from the api
        /// </summary>
        /// <param name="alertWhenNoConnection">Alert the user that we could not connect to the api</param>
        /// <returns>Boolean result</returns>
        public static async Task <List <TaskListItem> > LoadTaskDataFromApi(bool alertWhenNoConnection = false, string specificList = null)
        {
            try
            {
                await LoginHelper.RefreshTokenCodeAwait(true);

                var settings = IsolatedStorageSettings.ApplicationSettings;

                string url = "https://www.googleapis.com/tasks/v1/users/@me/lists?access_token=" + GTaskSettings.AccessToken;
                if (!string.IsNullOrEmpty(specificList))
                {
                    url = "https://www.googleapis.com/tasks/v1/users/@me/lists/" + specificList + "?access_token=" + GTaskSettings.AccessToken;
                }

                // Get the Task Lists
                #region Get Task Lists
                var restRequest = new RestRequest(Method.GET)
                {
                    Resource = url,
                    Timeout  = GTaskSettings.RequestTimeout
                };

                //Make the call
                var restResponse = await GTaskSettings.ExecuteRestTask(restRequest, false);

                //Store Lists
                var TaskListObject = JObject.Parse(restResponse.Content);

                // If the limit is exceeded then pop up a message, otherwise lets continue
                if (restResponse.Content.Contains("Limit Exceeded"))
                {
                    MessageBox.Show("[1] Take a screenshot and send it to @MattLoflin or [email protected]\r\n \r\n" + restResponse.Content.ToString());
                    return(null);
                }

                List <TaskListItem> TaskListList = null;
                if (string.IsNullOrEmpty(specificList))
                {
                    TaskListList = TaskListObject["items"].Select(m => new TaskListItem((string)m.SelectToken("title"),
                                                                                        (string)m.SelectToken("id"),
                                                                                        (string)m.SelectToken("kind"),
                                                                                        (string)m.SelectToken("selfLink"),
                                                                                        (string)m.SelectToken("updated"))).ToList();
                }
                else
                {
                    TaskListList = new List <TaskListItem>();
                    TaskListList.Add(new TaskListItem((string)TaskListObject.SelectToken("title"),
                                                      (string)TaskListObject.SelectToken("id"),
                                                      (string)TaskListObject.SelectToken("kind"),
                                                      (string)TaskListObject.SelectToken("selfLink"),
                                                      (string)TaskListObject.SelectToken("updated")));
                }

                //Check to see if pageToken exists
                //If so - iterate until empty
                var pageToken = string.Empty;
                if (TaskListObject["nextPageToken"] != null)
                {
                    pageToken = TaskListObject["nextPageToken"].ToString();
                }

                while (pageToken != string.Empty)
                {
                    restRequest = new RestRequest(Method.GET)
                    {
                        Resource =
                            "https://www.googleapis.com/tasks/v1/users/@me/lists?pageToken=" + pageToken + "&access_token=" + GTaskSettings.AccessToken,
                        Timeout = GTaskSettings.RequestTimeout
                    };

                    restResponse = await GTaskSettings.ExecuteRestTask(restRequest, false);

                    TaskListObject = JObject.Parse(restResponse.Content);

                    //Add the new list to the current list
                    if (string.IsNullOrEmpty(specificList))
                    {
                        TaskListList.AddRange(TaskListObject["items"].Select(m => new TaskListItem((string)m.SelectToken("title"),
                                                                                                   (string)m.SelectToken("id"),
                                                                                                   (string)m.SelectToken("kind"),
                                                                                                   (string)m.SelectToken("selfLink"),
                                                                                                   (string)m.SelectToken("updated"))));
                    }
                    else
                    {
                        TaskListList.Add(new TaskListItem((string)TaskListObject.SelectToken("title"),
                                                          (string)TaskListObject.SelectToken("id"),
                                                          (string)TaskListObject.SelectToken("kind"),
                                                          (string)TaskListObject.SelectToken("selfLink"),
                                                          (string)TaskListObject.SelectToken("updated")));
                    }

                    //reset the pageToken
                    pageToken = string.Empty;
                    if (TaskListObject["nextPageToken"] != null)
                    {
                        pageToken = TaskListObject["nextPageToken"].ToString();
                    }
                }
                #endregion

                // Get the Task Items
                #region Get Task Items
                // Loop through the task list list and get the tasks for each list
                foreach (TaskListItem list in TaskListList)
                {
                    // Instantiate the task list
                    list.taskList = new List <TaskItem>();

                    //If user wants to see Hidden (Completed) tasks, then display them. Else return current list
                    restRequest.Resource = "https://www.googleapis.com/tasks/v1/lists/" + list.id + "/tasks?access_token=" + GTaskSettings.AccessToken;
                    if (GTaskSettings.HideCompleted == false)
                    {
                        restRequest.Resource = "https://www.googleapis.com/tasks/v1/lists/" + list.id + "/tasks?showHidden=True&access_token=" + GTaskSettings.AccessToken;
                    }

                    //Make the call
                    restResponse = await GTaskSettings.ExecuteRestTask(restRequest, false);

                    var TaskObject = JObject.Parse(restResponse.Content);
                    if (!restResponse.Content.Contains("Limit Exceeded"))
                    {
                        if (TaskObject != null && TaskObject["items"] != null)
                        {
                            list.taskList = TaskObject["items"].Select(m => new TaskItem((string)m.SelectToken("id"),
                                                                                         (string)m.SelectToken("kind"),
                                                                                         ((string)m.SelectToken("title")) ==
                                                                                         String.Empty
                                                            ? "Empty"
                                                            : (string)
                                                                                         m.SelectToken("title"),
                                                                                         (string)m.SelectToken("notes"),
                                                                                         list.id,
                                                                                         (string)
                                                                                         m.SelectToken("position"),
                                                                                         (string)m.SelectToken("update"),
                                                                                         (string)m.SelectToken("due"),
                                                                                         (string)m.SelectToken("deleted"),
                                                                                         (string)m.SelectToken("hidden"),
                                                                                         (string)m.SelectToken("status"),
                                                                                         (string)
                                                                                         m.SelectToken("selfLink"),
                                                                                         (string)
                                                                                         m.SelectToken("completed"),
                                                                                         (string)
                                                                                         m.SelectToken("updated")
                                                                                         )).ToList();
                        }

                        //Check to see if pageToken exists
                        //If so - iterate until empty
                        pageToken = string.Empty;
                        if (TaskObject["nextPageToken"] != null)
                        {
                            pageToken = TaskObject["nextPageToken"].ToString();
                        }

                        while (pageToken != string.Empty)
                        {
                            //If user wants to see Hidden (Completed) tasks, then display them. Else return current list
                            restRequest.Resource = "https://www.googleapis.com/tasks/v1/lists/" + list.id + "/tasks?pageToken=" + pageToken + "&access_token=" + GTaskSettings.AccessToken;
                            if (GTaskSettings.HideCompleted == false)
                            {
                                restRequest.Resource = "https://www.googleapis.com/tasks/v1/lists/" + list.id + "/tasks?pageToken=" + pageToken + "&showHidden=True&access_token=" + GTaskSettings.AccessToken;
                            }
                            restResponse = await GTaskSettings.ExecuteRestTask(restRequest, false);

                            TaskObject = JObject.Parse(restResponse.Content);

                            //Add the new list to the current list
                            list.taskList.AddRange(TaskObject["items"].Select(
                                                       m => new TaskItem((string)m.SelectToken("id"),
                                                                         (string)m.SelectToken("kind"),
                                                                         ((string)m.SelectToken("title")) ==
                                                                         String.Empty
                                                            ? "Empty"
                                                            : (string)
                                                                         m.SelectToken("title"),
                                                                         (string)m.SelectToken("notes"),
                                                                         (string)m.SelectToken("parent"),
                                                                         (string)
                                                                         m.SelectToken("position"),
                                                                         (string)m.SelectToken("update"),
                                                                         (string)m.SelectToken("due"),
                                                                         (string)m.SelectToken("deleted"),
                                                                         (string)m.SelectToken("hidden"),
                                                                         (string)m.SelectToken("status"),
                                                                         (string)
                                                                         m.SelectToken("selfLink"),
                                                                         (string)
                                                                         m.SelectToken("completed"),
                                                                         (string)
                                                                         m.SelectToken("updated")
                                                                         )).ToList());

                            //reset the pageToken
                            pageToken = string.Empty;
                            if (TaskObject["nextPageToken"] != null)
                            {
                                pageToken = TaskObject["nextPageToken"].ToString();
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("[2] Take a screenshot and send it to @MattLoflin or [email protected]\r\n \r\n" + restResponse.Content.ToString());
                    }
                }
                #endregion

                return(TaskListList);
            }
            catch (Exception e)
            {
                // Check if an error was returned indicated that we couldnt connect to the api
                if (alertWhenNoConnection && e.ToString().Contains("Error reading JObject from JsonReader"))
                {
                    var googError = MessageBox.Show("Google is having intermittent connectivity issues, please try again in a couple minutes.", "Google Connection Issues", MessageBoxButton.OK);
                }

                // Return false
                return(null);

                // There was an issue getting the data.
                // There might not have been an internet connection so ignore the error
                // This can be handled more delicately in the future
            }
        }