From b610f3c14670370484ce5906e7f295014b9a5567 Mon Sep 17 00:00:00 2001 From: Shane Weaver Date: Tue, 1 Jun 2021 15:54:48 -0700 Subject: [PATCH 1/4] Added ProviderStateTrigger --- .../CommunityToolkit.Graph.Uwp.csproj | 4 + .../Triggers/ProviderStateTrigger.cs | 73 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs diff --git a/CommunityToolkit.Graph.Uwp/CommunityToolkit.Graph.Uwp.csproj b/CommunityToolkit.Graph.Uwp/CommunityToolkit.Graph.Uwp.csproj index c849b9b..5b061cc 100644 --- a/CommunityToolkit.Graph.Uwp/CommunityToolkit.Graph.Uwp.csproj +++ b/CommunityToolkit.Graph.Uwp/CommunityToolkit.Graph.Uwp.csproj @@ -49,6 +49,10 @@ MSBuild:Compile + + + + $(DefineConstants);WINRT diff --git a/CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs b/CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs new file mode 100644 index 0000000..1b95363 --- /dev/null +++ b/CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs @@ -0,0 +1,73 @@ +// 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 Microsoft.Toolkit.Uwp; +using Microsoft.Toolkit.Uwp.Helpers; +using Windows.System; +using Windows.UI.Xaml; + +namespace CommunityToolkit.Graph.Uwp +{ + /// + /// A StateTrigger for detecting when the global authentication provider has been signed in. + /// + public class ProviderStateTrigger : StateTriggerBase + { + /// + /// Identifies the DependencyProperty. + /// + public static readonly DependencyProperty StateProperty = + DependencyProperty.Register(nameof(State), typeof(ProviderState), typeof(ProviderStateTrigger), new PropertyMetadata(null, OnStateChanged)); + + private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is ProviderStateTrigger instance) + { + instance.UpdateState(); + } + } + + private DispatcherQueue _dispatcherQueue; + + /// + /// Gets or sets the expected ProviderState. + /// + public ProviderState? State + { + get => (ProviderState?)GetValue(StateProperty); + set => SetValue(StateProperty, value); + } + + /// + /// Initializes a new instance of the class. + /// + public ProviderStateTrigger() + { + _dispatcherQueue = DispatcherQueue.GetForCurrentThread(); + var weakEvent = + new WeakEventListener(this) + { + OnEventAction = (instance, source, args) => OnProviderUpdated(source, args), + OnDetachAction = (weakEventListener) => ProviderManager.Instance.ProviderUpdated -= weakEventListener.OnEvent, + }; + ProviderManager.Instance.ProviderUpdated += weakEvent.OnEvent; + UpdateState(); + } + + private void OnProviderUpdated(object sender, ProviderUpdatedEventArgs e) + { + _ = _dispatcherQueue.EnqueueAsync(UpdateState, DispatcherQueuePriority.Normal); + } + + private void UpdateState() + { + var provider = ProviderManager.Instance.GlobalProvider; + if (State != null && provider?.State != null) + { + SetActive(provider.State == State); + } + } + } +} \ No newline at end of file From 5887a5075e8c89dcce5e2ef4c7beea820c53a5fa Mon Sep 17 00:00:00 2001 From: Shane Weaver Date: Tue, 1 Jun 2021 16:23:07 -0700 Subject: [PATCH 2/4] Integrated into SampleTest app --- .../Triggers/ProviderStateTrigger.cs | 8 +- SampleTest/MainPage.xaml | 614 +++++++++--------- 2 files changed, 325 insertions(+), 297 deletions(-) diff --git a/CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs b/CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs index 1b95363..07b9306 100644 --- a/CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs +++ b/CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs @@ -29,7 +29,7 @@ private static void OnStateChanged(DependencyObject d, DependencyPropertyChanged } } - private DispatcherQueue _dispatcherQueue; + private readonly DispatcherQueue _dispatcherQueue; /// /// Gets or sets the expected ProviderState. @@ -66,7 +66,11 @@ private void UpdateState() var provider = ProviderManager.Instance.GlobalProvider; if (State != null && provider?.State != null) { - SetActive(provider.State == State); + SetActive(provider?.State == State); + } + else + { + SetActive(false); } } } diff --git a/SampleTest/MainPage.xaml b/SampleTest/MainPage.xaml index 817d2bf..a762b91 100644 --- a/SampleTest/MainPage.xaml +++ b/SampleTest/MainPage.xaml @@ -7,302 +7,326 @@ xmlns:samples="using:SampleTest.Samples" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="using:CommunityToolkit.Graph.Uwp.Controls" - xmlns:toolkit="using:Microsoft.Toolkit" xmlns:global="using:System.Globalization" + xmlns:toolkit="using:Microsoft.Toolkit" + xmlns:global="using:System.Globalization" + xmlns:uwp="using:CommunityToolkit.Graph.Uwp" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" mc:Ignorable="d"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The `LoginButton` above allows your user and application to easily connect to the Microsoft Graph. They can then also easily logout from the drop-down menu. + - - - - - - - - - The `LoginButton` above allows your user and application to easily connect to the Microsoft Graph. They can then also easily logout from the drop-down menu. - - - - - - - - - - The `PeoplePicker` lets a logged in user easily search for familiar people they interact with or contacts. Great for emails or messages. - - - - Picked People: - - - - - - - - - - - - - - - - - - The `GraphPresenter` is a unique control in the library which makes it easier for a developer to make any graph call and configure a nice display template in XAML. This opens up a world of possibilities for many uses outside of any other control available within this library. You can see a few examples of what's possible below. - - - - - - - - - - - - - The following example shows the `Me.CalendarView` API. - - My Upcoming Calendar Events: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - The following example shows the `Me.Messages` API for getting a user's inbox mail messages. - - My Messages: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - The following example shows the `Me.Planner.Tasks` API for getting a user's tasks. - - My Tasks: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Due - - - - - - - - - - - - - - - - - - - - - - - The following example shows the beta `Teams/id/Channels/id/messages` API for getting a list of messages (without replies) from a Channel in Teams. - - My Chat Messages: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + The `PeoplePicker` lets a logged in user easily search for familiar people they interact with or contacts. Great for emails or messages. + + + + Picked People: + + + + + + + + + + + + + + + + + + The `GraphPresenter` is a unique control in the library which makes it easier for a developer to make any graph call and configure a nice display template in XAML. This opens up a world of possibilities for many uses outside of any other control available within this library. You can see a few examples of what's possible below. + + + + + + + + + + + + + The following example shows the `Me.CalendarView` API. + + My Upcoming Calendar Events: + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + The following example shows the `Me.Messages` API for getting a user's inbox mail messages. + + My Messages: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + The following example shows the `Me.Planner.Tasks` API for getting a user's tasks. + + My Tasks: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Due + + + + + + + + + + + + + + + + + + + + + + + The following example shows the beta `Teams/id/Channels/id/messages` API for getting a list of messages (without replies) from a Channel in Teams. + + My Chat Messages: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From e0d6865bc21fea946f88b2aebf99b084b47ef72a Mon Sep 17 00:00:00 2001 From: Shane Weaver Date: Tue, 1 Jun 2021 16:24:58 -0700 Subject: [PATCH 3/4] Updated State to ActiveState --- .../Triggers/ProviderStateTrigger.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs b/CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs index 07b9306..1a24f88 100644 --- a/CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs +++ b/CommunityToolkit.Graph.Uwp/Triggers/ProviderStateTrigger.cs @@ -16,10 +16,10 @@ namespace CommunityToolkit.Graph.Uwp public class ProviderStateTrigger : StateTriggerBase { /// - /// Identifies the DependencyProperty. + /// Identifies the DependencyProperty. /// - public static readonly DependencyProperty StateProperty = - DependencyProperty.Register(nameof(State), typeof(ProviderState), typeof(ProviderStateTrigger), new PropertyMetadata(null, OnStateChanged)); + public static readonly DependencyProperty ActiveStateProperty = + DependencyProperty.Register(nameof(ActiveState), typeof(ProviderState), typeof(ProviderStateTrigger), new PropertyMetadata(null, OnStateChanged)); private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { @@ -34,10 +34,10 @@ private static void OnStateChanged(DependencyObject d, DependencyPropertyChanged /// /// Gets or sets the expected ProviderState. /// - public ProviderState? State + public ProviderState? ActiveState { - get => (ProviderState?)GetValue(StateProperty); - set => SetValue(StateProperty, value); + get => (ProviderState?)GetValue(ActiveStateProperty); + set => SetValue(ActiveStateProperty, value); } /// @@ -64,9 +64,9 @@ private void OnProviderUpdated(object sender, ProviderUpdatedEventArgs e) private void UpdateState() { var provider = ProviderManager.Instance.GlobalProvider; - if (State != null && provider?.State != null) + if (ActiveState != null && provider?.State != null) { - SetActive(provider?.State == State); + SetActive(provider?.State == ActiveState); } else { From 0943d40f28f5b1094cfb9eef5f7d96476c118c31 Mon Sep 17 00:00:00 2001 From: Shane Weaver Date: Tue, 1 Jun 2021 16:26:41 -0700 Subject: [PATCH 4/4] Updated sample to match name change --- SampleTest/MainPage.xaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SampleTest/MainPage.xaml b/SampleTest/MainPage.xaml index a762b91..54e9f68 100644 --- a/SampleTest/MainPage.xaml +++ b/SampleTest/MainPage.xaml @@ -18,7 +18,7 @@ - +