private async Task LoadAllTasksAsync() { try { GraphServiceClient graphClient = await GraphServiceHelper.GetGraphServiceClientAsync(); if (graphClient != null) { IPlannerPlanTasksCollectionPage tasks = await graphClient.Planner.Plans[PlanId].Tasks.Request().GetAsync(); Dictionary <string, string> buckets = Buckets.ToDictionary(s => s.Id, s => s.Name); List <PlannerTaskViewModel> taskList = new List <PlannerTaskViewModel>(); PlannerPlan plan = Plans.FirstOrDefault(s => s.Id == InternalPlanId); while (true) { foreach (PlannerTask task in tasks) { PlannerTaskViewModel taskViewModel = new PlannerTaskViewModel(task); if (plan != null) { taskViewModel.GroupId = plan.Owner; } taskViewModel.PropertyChanged += TaskViewModel_PropertyChanged; await GetAssignmentsAsync(taskViewModel, graphClient); if (!string.IsNullOrEmpty(taskViewModel.BucketId) && buckets.ContainsKey(taskViewModel.BucketId)) { taskViewModel.BucketName = buckets[taskViewModel.BucketId]; } taskList.Add(taskViewModel); } if (tasks.NextPageRequest == null) { break; } tasks = await tasks.NextPageRequest.GetAsync(); } _allTasks.Clear(); _allTasks.AddRange(taskList); LoadTasks(); } } catch (Exception exception) { MessageDialog messageDialog = new MessageDialog(exception.Message); await messageDialog.ShowAsync(); } }
private async void Add_Click(object sender, RoutedEventArgs e) { try { GraphServiceClient graphClient = await GraphServiceHelper.GetGraphServiceClientAsync(); if (graphClient != null && !string.IsNullOrWhiteSpace(_input?.Text)) { PlannerTask task = new PlannerTask { Title = _input.Text }; if (TaskType != TaskTypeAllTasksId && TaskType != TaskTypeClosedTasksId) { task.BucketId = TaskType; } task.PlanId = PlanId; _input.Text = string.Empty; CanAddTask = false; PlannerTask taskCreated = await graphClient.Planner.Tasks.Request().AddAsync(task); PlannerPlan plan = Plans.FirstOrDefault(s => s.Id == InternalPlanId); PlannerTaskViewModel taskViewModel = new PlannerTaskViewModel(taskCreated); Dictionary <string, string> buckets = Buckets.ToDictionary(s => s.Id, s => s.Name); if (plan != null) { taskViewModel.GroupId = plan.Owner; } if (!string.IsNullOrEmpty(taskViewModel.BucketId) && buckets.ContainsKey(taskViewModel.BucketId)) { taskViewModel.BucketName = buckets[taskViewModel.BucketId]; } if (taskCreated.PlanId == InternalPlanId) { taskViewModel.PropertyChanged += TaskViewModel_PropertyChanged; _allTasks.Add(taskViewModel); Tasks.Insert(0, taskViewModel); } } } catch (Exception exception) { MessageDialog messageDialog = new MessageDialog(exception.Message); await messageDialog.ShowAsync(); } CanAddTask = true; }
private bool IsTaskVisible(PlannerTaskViewModel task) { if (TaskType == TaskTypeAllTasksId) { return(task.PercentComplete != 100); } else if (TaskType == TaskTypeClosedTasksId) { return(task.PercentComplete == 100); } else { return(task.BucketId == TaskType); } }
private async Task GetAssignmentsAsync(PlannerTaskViewModel taskViewModel, GraphServiceClient graphClient = null) { try { if (graphClient == null) { graphClient = await GraphServiceHelper.GetGraphServiceClientAsync(); } string assignments = string.Empty; foreach (string userId in taskViewModel.AssignmentIds) { if (!string.IsNullOrEmpty(userId)) { if (!_userCache.ContainsKey(userId)) { User user = await graphClient.Users[userId].Request().GetAsync(); if (user != null) { _userCache.Add(user.Id, user.DisplayName); assignments += AssigneeSeperator + user.DisplayName; } } else { assignments += AssigneeSeperator + _userCache[userId]; } } } if (assignments.Length > AssigneeSeperator.Length) { assignments = assignments.Substring(2); } taskViewModel.AssignmentNames = assignments; } catch (Exception exception) { MessageDialog messageDialog = new MessageDialog(exception.Message); await messageDialog.ShowAsync(); } }
private async Task DeleteTaskAsync(PlannerTaskViewModel task) { MessageDialog confirmDialog = new MessageDialog(DeleteConfirmDialogMessage); confirmDialog.Commands.Add(new UICommand { Id = DeleteConfirmDialogYes, Label = DeleteConfirmDialogYesLabel }); confirmDialog.Commands.Add(new UICommand { Id = DeleteConfirmDialogNo, Label = DeleteConfirmDialogNoLabel }); confirmDialog.DefaultCommandIndex = 0; confirmDialog.CancelCommandIndex = 1; IUICommand result = await confirmDialog.ShowAsync(); if (result.Id.ToString() == DeleteConfirmDialogYes) { try { GraphServiceClient graphClient = await GraphServiceHelper.GetGraphServiceClientAsync(); if (graphClient != null) { PlannerTask taskToUpdate = await graphClient.Planner.Tasks[task.Id].Request().GetAsync(); await graphClient.Planner.Tasks[task.Id].Request().Header(HttpHeaderIfMatch, taskToUpdate.GetEtag()).DeleteAsync(); task.PropertyChanged -= TaskViewModel_PropertyChanged; Tasks.Remove(task); _allTasks.Remove(task); } } catch (Exception exception) { MessageDialog messageDialog = new MessageDialog(exception.Message); await messageDialog.ShowAsync(); } } }
private async void TaskViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { PlannerTaskViewModel plannerTaskViewModel = sender as PlannerTaskViewModel; if (Tasks.Contains(plannerTaskViewModel)) { Dictionary <string, object> task = new Dictionary <string, object>(); bool skipUpdate = false; switch (e.PropertyName) { case nameof(plannerTaskViewModel.AssignmentIds): await GetAssignmentsAsync(plannerTaskViewModel); PlannerAssignments assignments = new PlannerAssignments(); foreach (string assignee in plannerTaskViewModel.AssignmentIds) { assignments.AddAssignee(assignee); } task.Add(TaskAssignmentsJsonName, assignments); break; case nameof(plannerTaskViewModel.Title): task.Add(TaskTitleJsonName, plannerTaskViewModel.Title); break; case nameof(plannerTaskViewModel.BucketId): string bucketName = string.Empty; foreach (PlannerBucket bucket in Buckets) { if (bucket.Id == plannerTaskViewModel.BucketId) { bucketName = bucket.Name; break; } } plannerTaskViewModel.BucketName = bucketName; task.Add(TaskBucketIdJsonName, plannerTaskViewModel.BucketId); if (string.IsNullOrEmpty(plannerTaskViewModel.BucketId)) { skipUpdate = true; } break; case nameof(plannerTaskViewModel.DueDateTime): task.Add(TaskDueDateTimeJsonName, plannerTaskViewModel.DueDateTime); if (_list.ContainerFromItem(plannerTaskViewModel) is ListViewItem taskContainer) { var flyout = taskContainer.ContentTemplateRoot.FindDescendants <Button>().FirstOrDefault(s => s.Flyout != null)?.Flyout; if (flyout != null) { flyout.Hide(); } } break; default: skipUpdate = true; break; } if (!skipUpdate) { plannerTaskViewModel.IsUpdating = true; try { GraphServiceClient graphClient = await GraphServiceHelper.GetGraphServiceClientAsync(); if (graphClient != null) { PlannerTask taskToUpdate = await graphClient.Planner.Tasks[plannerTaskViewModel.Id].Request().GetAsync(); if (task.ContainsKey(TaskAssignmentsJsonName)) { PlannerAssignments assignments = task[TaskAssignmentsJsonName] as PlannerAssignments; string[] oldAssignees = taskToUpdate.Assignments.Assignees.ToArray(); string[] newAssignees = assignments.Assignees.ToArray(); foreach (string userId in oldAssignees) { if (!newAssignees.Contains(userId)) { assignments.AddAssignee(userId); assignments[userId] = null; } } } plannerTaskViewModel.ETag = taskToUpdate.GetEtag(); using (HttpRequestMessage request = graphClient.Planner.Tasks[plannerTaskViewModel.Id].Request().GetHttpRequestMessage()) { request.Method = new HttpMethod(HttpMethodPatch); string json = JsonConvert.SerializeObject(task); request.Content = new StringContent(json, System.Text.Encoding.UTF8, MediaTypeJson); request.Headers.Add(HttpHeaderIfMatch, plannerTaskViewModel.ETag); await graphClient.AuthenticationProvider.AuthenticateRequestAsync(request); HttpResponseMessage response = await graphClient.HttpProvider.SendAsync(request); response.Dispose(); } } } catch (Exception exception) { MessageDialog messageDialog = new MessageDialog(exception.Message); await messageDialog.ShowAsync(); } plannerTaskViewModel.IsUpdating = false; } } }