-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathMainPage.xaml.cs
105 lines (90 loc) · 3.72 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using CommunityToolkit.Authentication;
using ManualGraphRequestSample.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Http;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace ManualGraphRequestSample
{
public sealed partial class MainPage : Page
{
public ObservableCollection<TodoTask> TaskCollection = new ObservableCollection<TodoTask>();
public MainPage()
{
InitializeComponent();
ProviderManager.Instance.ProviderStateChanged += OnProviderStateChanged;
ProviderManager.Instance.GlobalProvider = new WindowsProvider(new string[] { "User.Read", "Tasks.ReadWrite" });
}
private async void OnProviderStateChanged(object sender, ProviderStateChangedEventArgs e)
{
switch (e.NewState)
{
case ProviderState.Loading:
SignInButton.Content = "Loading...";
SignInLoadingRing.IsActive = true;
SignInButton.IsEnabled = false;
break;
case ProviderState.SignedOut:
SignInButton.Content = "Sign In";
SignInLoadingRing.IsActive = false;
SignInButton.IsEnabled = true;
TaskCollection.Clear();
break;
case ProviderState.SignedIn:
SignInButton.Content = "Sign Out";
SignInLoadingRing.IsActive = false;
SignInButton.IsEnabled = true;
IList<TodoTask> tasks = await GetDefaultTaskListAsync();
if (tasks != null)
{
foreach (var task in tasks)
{
TaskCollection.Add(task);
}
}
break;
}
}
private async void OnSignInButtonClick(object sender, RoutedEventArgs e)
{
IProvider provider = ProviderManager.Instance.GlobalProvider;
if (provider?.State == ProviderState.SignedOut)
{
await ProviderManager.Instance.GlobalProvider.SignInAsync();
}
else if (provider?.State == ProviderState.SignedIn)
{
await ProviderManager.Instance.GlobalProvider.SignOutAsync();
}
}
private async Task<IList<TodoTask>> GetDefaultTaskListAsync()
{
var httpClient = new HttpClient();
var requestUri = "https://graph.microsoft.com/v1.0/me/todo/lists/tasks/tasks";
var getRequest = new HttpRequestMessage(HttpMethod.Get, requestUri);
await ProviderManager.Instance.GlobalProvider.AuthenticateRequestAsync(getRequest);
using (httpClient)
{
var response = await httpClient.SendAsync(getRequest);
if (response.IsSuccessStatusCode)
{
var jsonResponse = await response.Content.ReadAsStringAsync();
var jObject = JObject.Parse(jsonResponse);
if (jObject.ContainsKey("value"))
{
var tasks = JsonConvert.DeserializeObject<List<TodoTask>>(jObject["value"].ToString());
return tasks;
}
}
}
return null;
}
}
}