Skip to content

Commit

Permalink
Local Test for adding PeoplePicker
Browse files Browse the repository at this point in the history
Needs artifacts from unoplatform/Uno.WindowsCommunityToolkit#59
Don't forget to add ClientID :)
  • Loading branch information
michael-hawker committed Sep 19, 2019
1 parent dd0aeb4 commit c4e31cf
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 System.Collections.ObjectModel;
using Microsoft.Graph;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Windows.UI.Xaml;

namespace Microsoft.Toolkit.Graph.Controls
{
/// <summary>
/// Control which allows user to search for a person or contact within Microsoft Graph. Built on top of <see cref="TokenizingTextBox"/>.
/// </summary>
public partial class PeoplePicker
{
/// <summary>
/// Gets the set of <see cref="Person"/> objects chosen by the user.
/// </summary>
public ObservableCollection<Person> PickedPeople
{
get { return (ObservableCollection<Person>)GetValue(SelectedPeopleProperty); }
internal set { SetValue(SelectedPeopleProperty, value); }
}

/// <summary>
/// Identifies the <see cref="PickedPeople"/> dependency property.
/// </summary>
/// <returns>
/// The identifier for the <see cref="PickedPeople"/> dependency property.
/// </returns>
public static readonly DependencyProperty SelectedPeopleProperty =
DependencyProperty.Register(nameof(PickedPeople), typeof(ObservableCollection<Person>), typeof(PeoplePicker), new PropertyMetadata(new ObservableCollection<Person>()));

/// <summary>
/// Gets or sets collection of people suggested by the graph from the user's query.
/// </summary>
public ObservableCollection<Person> SuggestedPeople
{
get { return (ObservableCollection<Person>)GetValue(SuggestedPeopleProperty); }
set { SetValue(SuggestedPeopleProperty, value); }
}

/// <summary>
/// Identifies the <see cref="SuggestedPeople"/> dependency property.
/// </summary>
/// <returns>
/// The identifier for the <see cref="SuggestedPeople"/> dependency property.
/// </returns>
public static readonly DependencyProperty SuggestedPeopleProperty =
DependencyProperty.Register(nameof(SuggestedPeople), typeof(ObservableCollection<Person>), typeof(PeoplePicker), new PropertyMetadata(new ObservableCollection<Person>()));
}
}
114 changes: 114 additions & 0 deletions Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// 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 System;
using Microsoft.Graph;
using Microsoft.Toolkit.Graph.Extensions;
using Microsoft.Toolkit.Graph.Providers;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Microsoft.Toolkit.Graph.Controls
{
/// <summary>
/// Control which allows user to search for a person or contact within Microsoft Graph. Built on top of <see cref="TokenizingTextBox"/>.
/// </summary>
[TemplatePart(Name = PeoplePickerTokenizingTextBoxName, Type = typeof(TokenizingTextBox))]
public partial class PeoplePicker : Control
{
private const string PeoplePickerTokenizingTextBoxName = "PART_TokenizingTextBox";

private TokenizingTextBox _tokenBox;

private DispatcherTimer _typeTimer = new DispatcherTimer();

/// <summary>
/// Initializes a new instance of the <see cref="PeoplePicker"/> class.
/// </summary>
public PeoplePicker()
{
this.DefaultStyleKey = typeof(PeoplePicker);
}

/// <inheritdoc/>
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

if (_tokenBox != null)
{
_tokenBox.QueryTextChanged -= TokenBox_QueryTextChanged;
_tokenBox.TokenItemAdded -= TokenBox_TokenItemAdded;
_tokenBox.TokenItemRemoved -= TokenBox_TokenItemRemoved;
}

_tokenBox = GetTemplateChild(PeoplePickerTokenizingTextBoxName) as TokenizingTextBox;

if (_tokenBox != null)
{
_tokenBox.QueryTextChanged += TokenBox_QueryTextChanged;
_tokenBox.TokenItemAdded += TokenBox_TokenItemAdded;
_tokenBox.TokenItemRemoved += TokenBox_TokenItemRemoved;
}
}

private void TokenBox_TokenItemAdded(TokenizingTextBox sender, TokenizingTextBoxItem args)
{
if (args?.Content is Person person)
{
PickedPeople.Add(person);
}
}

private void TokenBox_TokenItemRemoved(TokenizingTextBox sender, TokenItemRemovedEventArgs args)
{
PickedPeople.Remove(args.Item as Person);
}

private string _previousQuery;

private void TokenBox_QueryTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
#if !HAS_UNO
if (!args.CheckCurrent())
{
return;
}
#endif

if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
var text = sender.Text;
_typeTimer.Debounce(
async () =>
{
var graph = ProviderManager.Instance.GlobalProvider.Graph;
if (graph != null)
{
// If empty, clear out
if (string.IsNullOrWhiteSpace(text))
{
SuggestedPeople.Clear();
}
else
{
SuggestedPeople.Clear();

foreach (var contact in (await graph.FindPersonAsync(text)).CurrentPage)
{
SuggestedPeople.Add(contact);
}
}

_previousQuery = text;
}

// TODO: If we don't have Graph connection and just list of Person should we auto-filter here?
}, TimeSpan.FromSeconds(0.3));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Microsoft.Toolkit.Graph.Controls"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls">

<Style TargetType="local:PeoplePicker">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:PeoplePicker">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<controls:TokenizingTextBox
x:Name="PART_TokenizingTextBox"
QueryIcon="Find"
PlaceholderText="Start typing a name"
TokenDelimiter=" "
SuggestedItemsSource="{TemplateBinding SuggestedPeople}">
<controls:TokenizingTextBox.SuggestedItemTemplate>
<DataTemplate>
<local:PersonView PersonDetails="{Binding}" ShowName="True" ShowEmail="True"/>
</DataTemplate>
</controls:TokenizingTextBox.SuggestedItemTemplate>
<controls:TokenizingTextBox.TokenItemTemplate>
<DataTemplate>
<local:PersonView PersonDetails="{Binding}" ShowName="True"/>
</DataTemplate>
</controls:TokenizingTextBox.TokenItemTemplate>
</controls:TokenizingTextBox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == 'MonoAndroid' or '$(TargetFrameworkIdentifier)' == 'Xamarin.iOS' or '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Uno.UI" Version="1.46.0-dev.1991" />
<PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI" Version="5.1.0-build.200.gf9c311b069" />
<PackageReference Include="System.Buffers" Version="4.5.0.0" />
<PackageReference Include="Uno.UI" Version="1.46.230-dev.2723" />
<PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI" Version="6.0.0-build.4.g02d22c3964" />
<PackageReference Include="Uno.Microsoft.Toolkit.Uwp.UI.Controls" Version="6.0.0-build.4.g02d22c3964" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'uap10.0' or '$(TargetFramework)' == 'uap10.0.16299' ">
<PackageReference Include="Microsoft.Toolkit.Uwp.UI" Version="6.0.0-build.12" />
<PackageReference Include="Microsoft.Toolkit.Uwp.UI" Version="6.0.0-build.26" />
<PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls" Version="6.0.0-build.26" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Microsoft.Toolkit.Graph.Controls/Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.xaml" />
<ResourceDictionary Source="ms-appx:///Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.xaml" />
<ResourceDictionary Source="ms-appx:///Microsoft.Toolkit.Graph.Controls/Controls/PersonView/PersonView.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<File Reference="Microsoft.Toolkit.Graph.Controls.dll">
<ToolboxItems VSCategory="Windows Community Toolkit Graph Controls" BlendCategory="Windows Community Toolkit Graph Controls">
<Item Type="Microsoft.Toolkit.Graph.Controls.LoginButton" />
<Item Type="Microsoft.Toolkit.Graph.Controls.PeoplePicker" />
<Item Type="Microsoft.Toolkit.Graph.Controls.PersonView" />
</ToolboxItems>
</File>
Expand Down
5 changes: 3 additions & 2 deletions Microsoft.Toolkit.Graph/Microsoft.Toolkit.Graph.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<TargetFrameworks>$(TargetFrameworksOverride)</TargetFrameworks>
<Title>Windows Community Toolkit .NET Standard Services</Title>
<Title>Windows Community Toolkit .NET Standard Graph Services</Title>
<PackageId>Uno.Microsoft.Toolkit.Graph</PackageId>
<Description>
This package includes .NET Standard code helpers such as:
- GraphExtensions: Helpers for common tasks related to the Microsoft Graph used by the Microsoft.Toolkit.Graph.Controls.
Expand All @@ -26,6 +27,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.Graph.Beta" Version="0.7.0-preview" />
<PackageReference Include="Microsoft.Graph.Auth" Version="1.0.0-preview.1" />
<PackageReference Include="Microsoft.Toolkit" Version="6.0.0-build.12" />
<PackageReference Include="Uno.Microsoft.Toolkit" Version="6.0.0-build.4.g02d22c3964" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Uno.UI" Version="1.46.0-dev.1991" />
<PackageReference Include="Uno.UI" Version="1.46.230-dev.2723" />
<PackageReference Include="Uno.UniversalImageLoader" Version="1.9.32" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
Expand Down
16 changes: 14 additions & 2 deletions SampleGraphApp/SampleGraphApp.Shared/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wgt="using:Microsoft.Toolkit.Graph.Controls"
xmlns:graph="using:Microsoft.Graph"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<wgt:LoginButton VerticalAlignment="Top" HorizontalAlignment="Right"/>
<wgt:PersonView PersonQuery="Me" ShowName="True" ShowEmail="True"/>
<wgt:LoginButton VerticalAlignment="Top" HorizontalAlignment="Right"/>
<StackPanel Margin="16,48">
<wgt:PeoplePicker x:Name="PeopleChooser"/>
<TextBlock Margin="0,8,0,0" FontWeight="Bold">Picked People:</TextBlock>
<ItemsControl ItemsSource="{x:Bind PeopleChooser.PickedPeople}"
Margin="8,0,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="graph:Person">
<TextBlock Text="{x:Bind DisplayName}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<BundleResource Include="Resources\Fonts\winjs-symbols.ttf" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Uno.UI" Version="1.45.0" />
<PackageReference Include="Uno.UI" Version="1.46.230-dev.2723" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
</ItemGroup>
Expand Down

0 comments on commit c4e31cf

Please sign in to comment.