|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading; |
| 9 | +using Microsoft.Graph; |
| 10 | +using Microsoft.Toolkit.Uwp.Helpers; |
| 11 | +using Newtonsoft.Json.Linq; |
| 12 | +using Windows.UI.Xaml; |
| 13 | +using Windows.UI.Xaml.Controls; |
| 14 | + |
| 15 | +namespace Microsoft.Toolkit.Graph.Controls |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Specialized <see cref="ContentPresenter"/> to fetch and display data from the Microsoft Graph. |
| 19 | + /// </summary> |
| 20 | + public class GraphPresenter : ContentPresenter |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// Gets or sets a <see cref="IBaseRequestBuilder"/> to be used to make a request to the graph. The results will be automatically populated to the <see cref="ContentPresenter.Content"/> property. Use a <see cref="ContentPresenter.ContentTemplate"/> to change the presentation of the data. |
| 24 | + /// </summary> |
| 25 | + public IBaseRequestBuilder RequestBuilder |
| 26 | + { |
| 27 | + get { return (IBaseRequestBuilder)GetValue(RequestBuilderProperty); } |
| 28 | + set { SetValue(RequestBuilderProperty, value); } |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Identifies the <see cref="RequestBuilder"/> dependency property. |
| 33 | + /// </summary> |
| 34 | + /// <returns> |
| 35 | + /// The identifier for the <see cref="RequestBuilder"/> dependency property. |
| 36 | + /// </returns> |
| 37 | + public static readonly DependencyProperty RequestBuilderProperty = |
| 38 | + DependencyProperty.Register(nameof(RequestBuilder), typeof(IBaseRequestBuilder), typeof(GraphPresenter), new PropertyMetadata(null)); |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Gets or sets the <see cref="Type"/> of item returned by the <see cref="RequestBuilder"/>. |
| 42 | + /// Set to the base item type and use the <see cref="IsCollection"/> property to indicate if a collection is expected back. |
| 43 | + /// </summary> |
| 44 | + public Type ResponseType { get; set; } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Gets or sets a value indicating whether the returned data from the <see cref="RequestBuilder"/> is a collection. |
| 48 | + /// </summary> |
| 49 | + public bool IsCollection { get; set; } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets or sets list of <see cref="QueryOption"/> representing <see cref="Microsoft.Graph.QueryOption"/> values to pass into the request built by <see cref="RequestBuilder"/>. |
| 53 | + /// </summary> |
| 54 | + public List<QueryOption> QueryOptions { get; set; } = new List<QueryOption>(); |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Gets or sets a string to indicate a sorting order for the <see cref="RequestBuilder"/>. This is a helper to add this specific request option to the <see cref="QueryOptions"/>. |
| 58 | + /// </summary> |
| 59 | + public string OrderBy { get; set; } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Initializes a new instance of the <see cref="GraphPresenter"/> class. |
| 63 | + /// </summary> |
| 64 | + public GraphPresenter() |
| 65 | + { |
| 66 | + Loaded += GraphPresenter_Loaded; |
| 67 | + } |
| 68 | + |
| 69 | + private async void GraphPresenter_Loaded(object sender, RoutedEventArgs e) |
| 70 | + { |
| 71 | + // Note: some interfaces from the Graph SDK don't implement IBaseRequestBuilder properly, see https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/722 |
| 72 | + if (RequestBuilder != null) |
| 73 | + { |
| 74 | + var request = new BaseRequest( |
| 75 | + RequestBuilder.RequestUrl, |
| 76 | + RequestBuilder.Client); // TODO: Do we need separate Options here? |
| 77 | + request.Method = "GET"; |
| 78 | + request.QueryOptions = QueryOptions?.Select(option => (Microsoft.Graph.QueryOption)option)?.ToList() ?? new List<Microsoft.Graph.QueryOption>(); |
| 79 | + |
| 80 | + // Handle Special QueryOptions |
| 81 | + if (!string.IsNullOrWhiteSpace(OrderBy)) |
| 82 | + { |
| 83 | + request.QueryOptions.Add(new Microsoft.Graph.QueryOption("$orderby", OrderBy)); |
| 84 | + } |
| 85 | + |
| 86 | + try |
| 87 | + { |
| 88 | + var response = await request.SendAsync<object>(null, CancellationToken.None).ConfigureAwait(false) as JObject; |
| 89 | + |
| 90 | + //// TODO: Deal with paging? |
| 91 | + |
| 92 | + var values = response["value"]; |
| 93 | + object data = null; |
| 94 | + |
| 95 | + if (IsCollection) |
| 96 | + { |
| 97 | + data = values.ToObject(Array.CreateInstance(ResponseType, 0).GetType()); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + data = values.ToObject(ResponseType); |
| 102 | + } |
| 103 | + |
| 104 | + _ = DispatcherHelper.ExecuteOnUIThreadAsync(() => Content = data); |
| 105 | + } |
| 106 | + catch |
| 107 | + { |
| 108 | + // TODO: We should figure out what we want to do for Loading/Error states here. |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments