From 2e6a2912b5e742fbafbc7102dc85be6340ea5021 Mon Sep 17 00:00:00 2001 From: Anran Zhang Date: Fri, 4 Jun 2021 15:31:18 +0800 Subject: [PATCH 1/3] Support display account management panel --- .../AccountsSettingsPaneConfig.cs | 28 +++++-- .../WebAccountCommandParameter.cs | 37 +++++++++ .../WindowsProvider.cs | 77 ++++++++++++++++++- .../AccountManagerButton.xaml | 17 ++++ .../AccountManagerButton.xaml.cs | 36 +++++++++ Samples/UwpWindowsProviderSample/App.xaml.cs | 26 ++++++- .../UwpWindowsProviderSample/MainPage.xaml | 20 ++--- .../UwpWindowsProviderSample/MainPage.xaml.cs | 2 + .../UwpWindowsProviderSample.csproj | 7 ++ 9 files changed, 232 insertions(+), 18 deletions(-) create mode 100644 CommunityToolkit.Authentication.Uwp/WebAccountCommandParameter.cs create mode 100644 Samples/UwpWindowsProviderSample/AccountManagerButton.xaml create mode 100644 Samples/UwpWindowsProviderSample/AccountManagerButton.xaml.cs diff --git a/CommunityToolkit.Authentication.Uwp/AccountsSettingsPaneConfig.cs b/CommunityToolkit.Authentication.Uwp/AccountsSettingsPaneConfig.cs index ebee0a7..0a257fd 100644 --- a/CommunityToolkit.Authentication.Uwp/AccountsSettingsPaneConfig.cs +++ b/CommunityToolkit.Authentication.Uwp/AccountsSettingsPaneConfig.cs @@ -13,24 +13,42 @@ namespace CommunityToolkit.Authentication public struct AccountsSettingsPaneConfig { /// - /// Gets or sets the header text for the accounts settings pane. + /// Gets or sets the header text for the add accounts settings pane. /// - public string HeaderText { get; set; } + public string AddAccountHeaderText { get; set; } + + /// + /// Gets or sets the header text for the manage accounts settings pane. + /// + public string ManageAccountHeaderText { get; set; } /// /// Gets or sets the SettingsCommand collection for the account settings pane. /// public IList Commands { get; set; } + /// + /// Gets or sets the WebAccountCommandParameter collection for the account settings pane. + /// + public WebAccountCommandParameter AccountCommandParameter { get; set; } + /// /// Initializes a new instance of the struct. /// - /// The header text for the accounts settings pane. + /// The header text for the add accounts settings pane. + /// The header text for the manage accounts settings pane. /// The SettingsCommand collection for the account settings pane. - public AccountsSettingsPaneConfig(string headerText = null, IList commands = null) + /// The WebAccountCommandParameter for the account settings pane. + public AccountsSettingsPaneConfig( + string addAccountHeaderText = null, + string manageAccountHeaderText = null, + IList commands = null, + WebAccountCommandParameter accountCommandParameter = null) { - HeaderText = headerText; + AddAccountHeaderText = addAccountHeaderText; + ManageAccountHeaderText = manageAccountHeaderText; Commands = commands; + AccountCommandParameter = accountCommandParameter; } } } diff --git a/CommunityToolkit.Authentication.Uwp/WebAccountCommandParameter.cs b/CommunityToolkit.Authentication.Uwp/WebAccountCommandParameter.cs new file mode 100644 index 0000000..76733b5 --- /dev/null +++ b/CommunityToolkit.Authentication.Uwp/WebAccountCommandParameter.cs @@ -0,0 +1,37 @@ +// 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 Windows.UI.ApplicationSettings; + +namespace CommunityToolkit.Authentication +{ + /// + /// can be produced through this parameter. + /// + public class WebAccountCommandParameter + { + /// + /// Gets the delegate that's invoked when the user selects an account and a specific + /// action in the account settings pane. + /// + public WebAccountCommandInvokedHandler Invoked { get; } + + /// + /// Gets the actions that the command performs on the web account in the accounts pane. + /// + public SupportedWebAccountActions Actions { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The delegate that's invoked when the user selects an account and a specific + /// action in the account settings pane. + /// The actions that the command performs on the web account in the accounts pane. + public WebAccountCommandParameter(WebAccountCommandInvokedHandler invoked, SupportedWebAccountActions actions) + { + Invoked = invoked; + Actions = actions; + } + } +} diff --git a/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs b/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs index d21f319..3acd719 100644 --- a/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs +++ b/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs @@ -224,13 +224,84 @@ public override async Task GetTokenAsync(bool silentOnly = false) return null; } + /// + /// Display AccountSettingsPane for the management of logged-in users. + /// + /// . + public async Task ShowAccountManagementPaneAsync() + { + if (_webAccount == null) + { + throw new InvalidOperationException("Display account management pane requires at least one logged in account."); + } + + if (_accountsSettingsPaneConfig?.AccountCommandParameter == null) + { + throw new ArgumentNullException("At least one account command is required to display the account management pane."); + } + + // Build the AccountSettingsPane and configure it with available account commands. + void OnAccountCommandsRequested(AccountsSettingsPane sender, AccountsSettingsPaneCommandsRequestedEventArgs e) + { + AccountsSettingsPaneEventDeferral deferral = e.GetDeferral(); + + // Apply the configured header. + var headerText = _accountsSettingsPaneConfig?.ManageAccountHeaderText; + if (!string.IsNullOrWhiteSpace(headerText)) + { + e.HeaderText = headerText; + } + + // Generate account command. + var commandParameter = _accountsSettingsPaneConfig?.AccountCommandParameter; + var webAccountCommand = new WebAccountCommand( + _webAccount, + async (command, args) => + { + commandParameter.Invoked?.Invoke(command, args); + + // When the logout command is triggered, we also need to modify the state of the Provider. + if (args.Action == WebAccountAction.Remove) + { + await SignOutAsync(); + } + }, + commandParameter.Actions); + + e.WebAccountCommands.Add(webAccountCommand); + + deferral.Complete(); + } + + AccountsSettingsPane pane = null; + try + { + // GetForCurrentView may throw an exception if the current view isn't ready yet. + pane = AccountsSettingsPane.GetForCurrentView(); + pane.AccountCommandsRequested += OnAccountCommandsRequested; + + // Show the AccountSettingsPane and wait for the result. + await AccountsSettingsPane.ShowManageAccountsAsync(); + } + catch (Exception) + { + } + finally + { + if (pane != null) + { + pane.AccountCommandsRequested -= OnAccountCommandsRequested; + } + } + } + private async Task SetAccountAsync(WebAccount account) { if (account == null) { // Clear account - await SignOutAsync(); - return; + await SignOutAsync(); + return; } else if (account.Id == _webAccount?.Id) { @@ -353,7 +424,7 @@ async void OnAccountCommandsRequested(AccountsSettingsPane sender, AccountsSetti } // Apply the configured header. - var headerText = _accountsSettingsPaneConfig?.HeaderText; + var headerText = _accountsSettingsPaneConfig?.AddAccountHeaderText; if (!string.IsNullOrWhiteSpace(headerText)) { e.HeaderText = headerText; diff --git a/Samples/UwpWindowsProviderSample/AccountManagerButton.xaml b/Samples/UwpWindowsProviderSample/AccountManagerButton.xaml new file mode 100644 index 0000000..ddac190 --- /dev/null +++ b/Samples/UwpWindowsProviderSample/AccountManagerButton.xaml @@ -0,0 +1,17 @@ + + + +