-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d88d444
commit 744e6ab
Showing
13 changed files
with
765 additions
and
267 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
components/IncrementalLoadingCollection/samples/IncrementalLoadingCollectionSample.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<!-- 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. --> | ||
<Page x:Class="IncrementalLoadingCollectionExperiment.Samples.IncrementalLoadingCollectionSample" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:controls="using:CommunityToolkit.WinUI" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:local="using:IncrementalLoadingCollectionExperiment.Samples" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d"> | ||
|
||
<Grid MaxWidth="460" | ||
HorizontalAlignment="Left"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="*" /> | ||
</Grid.RowDefinitions> | ||
|
||
<StackPanel HorizontalAlignment="Left"> | ||
<TextBlock Text="Items are loaded incrementally when the view needs to show them (i.e., when the user scrolls the ListView)" | ||
TextWrapping="Wrap" /> | ||
<Button Margin="0,12,0,12" | ||
Click="RefreshCollection" | ||
Content="Refresh collection" | ||
Style="{StaticResource AccentButtonStyle}" /> | ||
<TextBlock> | ||
<Run Text="Is loading:" /> | ||
<Run FontWeight="SemiBold" | ||
Text="{Binding IsLoading, Mode=OneWay}" /> | ||
</TextBlock> | ||
<TextBlock> | ||
<Run Text="Has more items:" /> | ||
<Run FontWeight="SemiBold" | ||
Text="{Binding HasMoreItems, Mode=OneWay}" /> | ||
</TextBlock> | ||
|
||
</StackPanel> | ||
|
||
<Grid Grid.Row="2" | ||
MaxHeight="420" | ||
Margin="0,24,0,0" | ||
VerticalAlignment="Top" | ||
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}" | ||
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}" | ||
BorderThickness="1" | ||
CornerRadius="4"> | ||
<ListView x:Name="PeopleListView"> | ||
<ListView.ItemTemplate> | ||
<DataTemplate> | ||
<Grid> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="Auto" /> | ||
<ColumnDefinition /> | ||
</Grid.ColumnDefinitions> | ||
<Image Width="24" | ||
Height="24" | ||
VerticalAlignment="Center" | ||
Source="ms-appx:///Assets/AppIcon.png" /> | ||
<TextBlock Grid.Column="1" | ||
Margin="12" | ||
VerticalAlignment="Center" | ||
Text="{Binding Name}" /> | ||
</Grid> | ||
</DataTemplate> | ||
</ListView.ItemTemplate> | ||
</ListView> | ||
</Grid> | ||
</Grid> | ||
</Page> |
32 changes: 32 additions & 0 deletions
32
components/IncrementalLoadingCollection/samples/IncrementalLoadingCollectionSample.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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.WinUI; | ||
|
||
namespace IncrementalLoadingCollectionExperiment.Samples; | ||
|
||
[ToolkitSample(id: nameof(IncrementalLoadingCollectionSample), "Incremental Loading Collection", description: $"A sample for showing how to create and use a IncrementalLoadingCollection.")] | ||
public sealed partial class IncrementalLoadingCollectionSample : Page | ||
{ | ||
public IncrementalLoadingCollectionSample() | ||
{ | ||
this.InitializeComponent(); | ||
Load(); | ||
} | ||
private void Load() | ||
{ | ||
// IncrementalLoadingCollection can be bound to a GridView or a ListView. In this case it is a ListView called PeopleListView. | ||
var collection = new IncrementalLoadingCollection<PeopleSource, Person>(); | ||
PeopleListView.ItemsSource = collection; | ||
|
||
// Binds the collection to the page DataContext in order to use its IsLoading and HasMoreItems properties. | ||
DataContext = collection; | ||
} | ||
|
||
private async void RefreshCollection(object sender, RoutedEventArgs e) | ||
{ | ||
var collection = (IncrementalLoadingCollection<PeopleSource, Person>)PeopleListView.ItemsSource; | ||
await collection.RefreshAsync(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
components/IncrementalLoadingCollection/samples/PeopleSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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.WinUI; | ||
|
||
namespace IncrementalLoadingCollectionExperiment.Samples; | ||
|
||
/// <summary> | ||
/// A sample implementation of the <see cref="Collections.IIncrementalSource{TSource}"/> interface. | ||
/// </summary> | ||
/// <seealso cref="Collections.IIncrementalSource{TSource}"/> | ||
public class PeopleSource : IIncrementalSource<Person> | ||
{ | ||
private readonly List<Person> _people; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PeopleSource"/> class. | ||
/// </summary> | ||
public PeopleSource() | ||
{ | ||
// Creates an example collection. | ||
_people = new List<Person>(); | ||
|
||
for (int i = 1; i <= 200; i++) | ||
{ | ||
var p = new Person { Name = "Person " + i }; | ||
_people.Add(p); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves items based on <paramref name="pageIndex"/> and <paramref name="pageSize"/> arguments. | ||
/// </summary> | ||
/// <param name="pageIndex"> | ||
/// The zero-based index of the page that corresponds to the items to retrieve. | ||
/// </param> | ||
/// <param name="pageSize"> | ||
/// The number of <see cref="Person"/> items to retrieve for the specified <paramref name="pageIndex"/>. | ||
/// </param> | ||
/// <param name="cancellationToken"> | ||
/// Used to propagate notification that operation should be canceled. | ||
/// </param> | ||
/// <returns> | ||
/// Returns a collection of <see cref="Person"/>. | ||
/// </returns> | ||
public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
// Gets items from the collection according to pageIndex and pageSize parameters. | ||
var result = (from p in _people | ||
select p).Skip(pageIndex * pageSize).Take(pageSize); | ||
|
||
// Simulates a longer request... | ||
// Make sure the list is still in order after a refresh, | ||
// even if the first page takes longer to load | ||
if (pageIndex == 0) | ||
{ | ||
await Task.Delay(2000); | ||
} | ||
else | ||
{ | ||
await Task.Delay(1000); | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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. | ||
|
||
namespace IncrementalLoadingCollectionExperiment.Samples; | ||
|
||
/// <summary> | ||
/// A sample class used to show how to use the <see cref="Collections.IIncrementalSource{TSource}"/> interface. | ||
/// </summary> | ||
public class Person | ||
{ | ||
/// <summary> | ||
/// Gets or sets the name of the person. | ||
/// </summary> | ||
public string? Name { get; set; } | ||
} |
13 changes: 13 additions & 0 deletions
13
...crementalLoadingCollection/src/CommunityToolkit.WinUI.IncrementalLoadingCollection.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="MSBuild.Sdk.Extras/3.0.23"> | ||
<PropertyGroup> | ||
<ToolkitComponentName>IncrementalLoadingCollection</ToolkitComponentName> | ||
<Description>This package contains IncrementalLoadingCollection.</Description> | ||
<Version>0.0.1</Version> | ||
|
||
<!-- Rns suffix is required for namespaces shared across projects. See https://github.com/CommunityToolkit/Labs-Windows/issues/152 --> | ||
<RootNamespace>CommunityToolkit.WinUI.IncrementalLoadingCollectionRns</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<!-- Sets this up as a toolkit component's source project --> | ||
<Import Project="$(ToolingDirectory)\ToolkitComponent.SourceProject.props" /> | ||
</Project> |
29 changes: 29 additions & 0 deletions
29
components/IncrementalLoadingCollection/src/IIncrementalSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// 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. | ||
|
||
namespace CommunityToolkit.WinUI; | ||
|
||
/// <summary> | ||
/// This interface represents a data source whose items can be loaded incrementally. | ||
/// </summary> | ||
/// <typeparam name="TSource">Type of collection element.</typeparam> | ||
public interface IIncrementalSource<TSource> | ||
{ | ||
/// <summary> | ||
/// This method is invoked every time the view need to show more items. Retrieves items based on <paramref name="pageIndex"/> and <paramref name="pageSize"/> arguments. | ||
/// </summary> | ||
/// <param name="pageIndex"> | ||
/// The zero-based index of the page that corresponds to the items to retrieve. | ||
/// </param> | ||
/// <param name="pageSize"> | ||
/// The number of <typeparamref name="TSource"/> items to retrieve for the specified <paramref name="pageIndex"/>. | ||
/// </param> | ||
/// <param name="cancellationToken"> | ||
/// Used to propagate notification that operation should be canceled. | ||
/// </param> | ||
/// <returns> | ||
/// Returns a collection of <typeparamref name="TSource"/>. | ||
/// </returns> | ||
Task<IEnumerable<TSource>> GetPagedItemsAsync(int pageIndex, int pageSize, CancellationToken cancellationToken = default); | ||
} |
Oops, something went wrong.