1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-12 21:38:38 +00:00

Implemented pagination in profile list

This commit is contained in:
Robert 2023-07-21 23:10:50 +02:00
parent cfb39b986d
commit 7c19937bce
11 changed files with 210 additions and 134 deletions

View File

@ -51,18 +51,20 @@ public partial class Pagination : TemplatedControl
private void OnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e) private void OnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
{ {
if (e.Property == ValueProperty) if (e.Property == ValueProperty || e.Property == MaximumProperty)
Update(); Update();
} }
private void NextButtonOnClick(object? sender, RoutedEventArgs e) private void NextButtonOnClick(object? sender, RoutedEventArgs e)
{ {
Value++; if (Value < Maximum)
Value++;
} }
private void PreviousButtonOnClick(object? sender, RoutedEventArgs e) private void PreviousButtonOnClick(object? sender, RoutedEventArgs e)
{ {
Value--; if (Value > 1)
Value--;
} }
private void Update() private void Update()

View File

@ -1,6 +1,7 @@
using System; using System;
using Avalonia; using Avalonia;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Data;
namespace Artemis.UI.Shared.Pagination; namespace Artemis.UI.Shared.Pagination;
@ -10,7 +11,7 @@ public partial class Pagination : TemplatedControl
/// Defines the <see cref="Value" /> property /// Defines the <see cref="Value" /> property
/// </summary> /// </summary>
public static readonly StyledProperty<int> ValueProperty = public static readonly StyledProperty<int> ValueProperty =
AvaloniaProperty.Register<Pagination, int>(nameof(Value), 1, enableDataValidation: true, coerce: (p, v) => Math.Clamp(v, 1, ((Pagination) p).Maximum)); AvaloniaProperty.Register<Pagination, int>(nameof(Value), 1, defaultBindingMode: BindingMode.TwoWay);
/// <summary> /// <summary>
/// Defines the <see cref="Maximum" /> property /// Defines the <see cref="Maximum" /> property

View File

@ -78,7 +78,8 @@ internal class Navigation
catch (Exception e) catch (Exception e)
{ {
Cancel(); Cancel();
_logger.Error(e, "Failed to navigate to {Path}", resolution.Path); if (e is not TaskCanceledException)
_logger.Error(e, "Failed to navigate to {Path}", resolution.Path);
} }
} }
@ -96,7 +97,8 @@ internal class Navigation
catch (Exception e) catch (Exception e)
{ {
Cancel(); Cancel();
_logger.Error(e, "Failed to navigate to {Path}", resolution.Path); if (e is not TaskCanceledException)
_logger.Error(e, "Failed to navigate to {Path}", resolution.Path);
} }
if (CancelIfRequested(args, "OnNavigating", screen)) if (CancelIfRequested(args, "OnNavigating", screen))

View File

@ -1,56 +1,64 @@
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Threading; using System.Reactive.Linq;
using System.Threading.Tasks;
using Artemis.UI.Shared; using Artemis.UI.Shared;
using Artemis.WebClient.Workshop; using Artemis.WebClient.Workshop;
using Artemis.WebClient.Workshop.Extensions;
using DynamicData; using DynamicData;
using DynamicData.Binding;
using ReactiveUI; using ReactiveUI;
using Serilog;
using StrawberryShake; using StrawberryShake;
namespace Artemis.UI.Screens.Workshop.Categories; namespace Artemis.UI.Screens.Workshop.Categories;
public class CategoriesViewModel : ActivatableViewModelBase public class CategoriesViewModel : ActivatableViewModelBase
{ {
private readonly IWorkshopClient _client; private ObservableAsPropertyHelper<IReadOnlyList<EntryFilterInput>?>? _categoryFilters;
private readonly ILogger _logger;
public readonly SourceList<CategoryViewModel> _categories;
public CategoriesViewModel(ILogger logger, IWorkshopClient client) public CategoriesViewModel(IWorkshopClient client)
{ {
_logger = logger; client.GetCategories
_client = client; .Watch(ExecutionStrategy.CacheFirst)
_categories = new SourceList<CategoryViewModel>(); .SelectOperationResult(c => c.Categories)
_categories.Connect().Bind(out ReadOnlyObservableCollection<CategoryViewModel> categoryViewModels).Subscribe(); .ToObservableChangeSet(c => c.Id)
.Transform(c => new CategoryViewModel(c))
.Bind(out ReadOnlyObservableCollection<CategoryViewModel> categoryViewModels)
.Subscribe();
Categories = categoryViewModels; Categories = categoryViewModels;
this.WhenActivated(d => ReactiveCommand.CreateFromTask(GetCategories).Execute().Subscribe().DisposeWith(d));
this.WhenActivated(d =>
{
_categoryFilters = Categories.ToObservableChangeSet()
.AutoRefresh(c => c.IsSelected)
.Filter(e => e.IsSelected)
.Select(_ => CreateFilter())
.ToProperty(this, vm => vm.CategoryFilters)
.DisposeWith(d);
});
} }
public ReadOnlyObservableCollection<CategoryViewModel> Categories { get; } public ReadOnlyObservableCollection<CategoryViewModel> Categories { get; }
public IReadOnlyList<EntryFilterInput>? CategoryFilters => _categoryFilters?.Value;
private IReadOnlyList<EntryFilterInput>? CreateFilter()
private async Task GetCategories(CancellationToken cancellationToken)
{ {
try List<int?> categories = Categories.Where(c => c.IsSelected).Select(c => (int?) c.Id).ToList();
{ if (!categories.Any())
IOperationResult<IGetCategoriesResult> result = await _client.GetCategories.ExecuteAsync(cancellationToken); return null;
if (result.IsErrorResult())
_logger.Warning("Failed to retrieve categories {Error}", result.Errors);
_categories.Edit(l => List<EntryFilterInput> categoryFilters = new();
foreach (int? category in categories)
{
categoryFilters.Add(new EntryFilterInput
{ {
l.Clear(); Categories = new ListFilterInputTypeOfCategoryFilterInput {Some = new CategoryFilterInput {Id = new IntOperationFilterInput {Eq = category}}}
if (result.Data?.Categories != null)
l.AddRange(result.Data.Categories.Select(c => new CategoryViewModel(c)));
}); });
} }
catch (Exception e)
{ return categoryFilters;
_logger.Warning(e, "Failed to retrieve categories");
}
} }
} }

View File

@ -10,13 +10,13 @@ public class EntryListViewModel : ViewModelBase
{ {
private readonly IRouter _router; private readonly IRouter _router;
public EntryListViewModel(IGetEntries_Entries_Nodes entry, IRouter router) public EntryListViewModel(IGetEntries_Entries_Items entry, IRouter router)
{ {
_router = router; _router = router;
Entry = entry; Entry = entry;
} }
public IGetEntries_Entries_Nodes Entry { get; } public IGetEntries_Entries_Items Entry { get; }
public async Task NavigateToEntry() public async Task NavigateToEntry()
{ {

View File

@ -3,28 +3,48 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:profile="clr-namespace:Artemis.UI.Screens.Workshop.Profile" xmlns:profile="clr-namespace:Artemis.UI.Screens.Workshop.Profile"
xmlns:pagination="clr-namespace:Artemis.UI.Shared.Pagination;assembly=Artemis.UI.Shared"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Artemis.UI.Screens.Workshop.Profile.ProfileListView" x:Class="Artemis.UI.Screens.Workshop.Profile.ProfileListView"
x:DataType="profile:ProfileListViewModel"> x:DataType="profile:ProfileListViewModel">
<Border Classes="router-container"> <Border Classes="router-container">
<Grid ColumnDefinitions="300,*" Margin="10"> <Grid ColumnDefinitions="300,*" Margin="10" RowDefinitions="*,Auto">
<Border Classes="card-condensed" Grid.Column="0" Margin="0 0 10 0" VerticalAlignment="Top"> <StackPanel Grid.Column="0" Grid.RowSpan="2" Margin="0 0 10 0" VerticalAlignment="Top">
<StackPanel> <TextBlock Classes="card-title" Margin="0 0 0 5">
<TextBlock Classes="h3">Categories</TextBlock> Categories
<ContentControl Content="{CompiledBinding CategoriesViewModel}"></ContentControl> </TextBlock>
</StackPanel> <Border Classes="card" VerticalAlignment="Stretch" Margin="0,0,5,0">
<ContentControl Content="{CompiledBinding CategoriesViewModel}"></ContentControl>
</Border> </Border>
<Border Classes="card-condensed" Grid.Column="1"> <TextBlock Classes="card-title">
<ItemsRepeater ItemsSource="{CompiledBinding Entries}"> Filters
</TextBlock>
<Border Classes="card" VerticalAlignment="Stretch" Margin="0,0,5,0">
<StackPanel>
<Label>Author</Label>
<AutoCompleteBox Watermark="Search authors.." />
</StackPanel>
</Border>
</StackPanel>
<ScrollViewer Grid.Column="1" Grid.Row="0">
<ItemsRepeater ItemsSource="{CompiledBinding Entries}" Margin="0 0 20 0">
<ItemsRepeater.ItemTemplate> <ItemsRepeater.ItemTemplate>
<DataTemplate> <DataTemplate>
<ContentControl Content="{CompiledBinding}" Margin="0 0 0 5"></ContentControl> <ContentControl Content="{CompiledBinding}" Margin="0 0 0 5"></ContentControl>
</DataTemplate> </DataTemplate>
</ItemsRepeater.ItemTemplate> </ItemsRepeater.ItemTemplate>
</ItemsRepeater> </ItemsRepeater>
</Border> </ScrollViewer>
<pagination:Pagination Grid.Column="1"
Grid.Row="1"
IsVisible="{CompiledBinding ShowPagination}"
Value="{CompiledBinding Page}"
Maximum="{CompiledBinding TotalPages}"
HorizontalAlignment="Center" />
</Grid> </Grid>
</Border> </Border>
</UserControl> </UserControl>

View File

@ -1,8 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Artemis.UI.Screens.Workshop.Categories; using Artemis.UI.Screens.Workshop.Categories;
@ -11,8 +11,6 @@ using Artemis.UI.Screens.Workshop.Parameters;
using Artemis.UI.Shared; using Artemis.UI.Shared;
using Artemis.UI.Shared.Routing; using Artemis.UI.Shared.Routing;
using Artemis.WebClient.Workshop; using Artemis.WebClient.Workshop;
using DynamicData;
using DynamicData.Alias;
using ReactiveUI; using ReactiveUI;
using StrawberryShake; using StrawberryShake;
@ -20,35 +18,44 @@ namespace Artemis.UI.Screens.Workshop.Profile;
public class ProfileListViewModel : RoutableScreen<ActivatableViewModelBase, WorkshopListParameters>, IWorkshopViewModel public class ProfileListViewModel : RoutableScreen<ActivatableViewModelBase, WorkshopListParameters>, IWorkshopViewModel
{ {
private readonly SourceList<IGetEntries_Entries_Nodes> _entries; private readonly IRouter _router;
private readonly IWorkshopClient _workshopClient; private readonly IWorkshopClient _workshopClient;
private readonly ObservableAsPropertyHelper<bool> _showPagination;
private List<EntryListViewModel>? _entries;
private int _page; private int _page;
private int _totalPages = 1;
private int _entriesPerPage = 5;
public ProfileListViewModel(IWorkshopClient workshopClient, IRouter router, CategoriesViewModel categoriesViewModel) public ProfileListViewModel(IWorkshopClient workshopClient, IRouter router, CategoriesViewModel categoriesViewModel)
{ {
_workshopClient = workshopClient; _workshopClient = workshopClient;
_router = router;
_showPagination = this.WhenAnyValue(vm => vm.TotalPages).Select(t => t > 1).ToProperty(this, vm => vm.ShowPagination);
CategoriesViewModel = categoriesViewModel; CategoriesViewModel = categoriesViewModel;
_entries = new SourceList<IGetEntries_Entries_Nodes>(); // Respond to page changes
_entries.Connect() this.WhenAnyValue(vm => vm.Page).Skip(1).Subscribe(p => Task.Run(() => _router.Navigate($"workshop/profiles/{p}")));
.Transform(e => new EntryListViewModel(e, router)) // Respond to filter changes
.Bind(out ReadOnlyObservableCollection<EntryListViewModel> observableEntries) this.WhenActivated(d => CategoriesViewModel.WhenAnyValue(vm => vm.CategoryFilters).Skip(1).Subscribe(_ =>
.Subscribe();
this.WhenActivated(d =>
{ {
CategoriesViewModel._categories.Connect() // Reset to page one, will trigger a query
.AutoRefresh(c => c.IsSelected) if (Page != 1)
.Filter(e => e.IsSelected) Page = 1;
.Select(e => e.Id) // If already at page one, force a query
.Subscribe(_ => ReactiveCommand.CreateFromTask(GetEntries).Execute().Subscribe()) else
.DisposeWith(d); Task.Run(() => Query(CancellationToken.None));
}); }).DisposeWith(d));
Entries = observableEntries;
} }
public bool ShowPagination => _showPagination.Value;
public CategoriesViewModel CategoriesViewModel { get; } public CategoriesViewModel CategoriesViewModel { get; }
public ReadOnlyObservableCollection<EntryListViewModel> Entries { get; set; }
public List<EntryListViewModel>? Entries
{
get => _entries;
set => RaiseAndSetIfChanged(ref _entries, value);
}
public int Page public int Page
{ {
@ -56,32 +63,49 @@ public class ProfileListViewModel : RoutableScreen<ActivatableViewModelBase, Wor
set => RaiseAndSetIfChanged(ref _page, value); set => RaiseAndSetIfChanged(ref _page, value);
} }
public int TotalPages
{
get => _totalPages;
set => RaiseAndSetIfChanged(ref _totalPages, value);
}
public int EntriesPerPage
{
get => _entriesPerPage;
set => RaiseAndSetIfChanged(ref _entriesPerPage, value);
}
public override async Task OnNavigating(WorkshopListParameters parameters, NavigationArguments args, CancellationToken cancellationToken) public override async Task OnNavigating(WorkshopListParameters parameters, NavigationArguments args, CancellationToken cancellationToken)
{ {
Page = Math.Max(1, parameters.Page); Page = Math.Max(1, parameters.Page);
await GetEntries(cancellationToken);
// Throttle page changes
await Task.Delay(200, cancellationToken);
if (!cancellationToken.IsCancellationRequested)
await Query(cancellationToken);
} }
private async Task GetEntries(CancellationToken cancellationToken) private async Task Query(CancellationToken cancellationToken)
{ {
IOperationResult<IGetEntriesResult> result = await _workshopClient.GetEntries.ExecuteAsync(CreateFilter(), cancellationToken); EntryFilterInput filter = GetFilter();
if (result.IsErrorResult() || result.Data?.Entries?.Nodes == null) IOperationResult<IGetEntriesResult> entries = await _workshopClient.GetEntries.ExecuteAsync(filter, EntriesPerPage * (Page - 1), EntriesPerPage, cancellationToken);
return; if (!entries.IsErrorResult() && entries.Data?.Entries?.Items != null)
_entries.Edit(e =>
{ {
e.Clear(); Entries = entries.Data.Entries.Items.Select(n => new EntryListViewModel(n, _router)).ToList();
e.AddRange(result.Data.Entries.Nodes); TotalPages = (int) Math.Ceiling(entries.Data.Entries.TotalCount / (double) EntriesPerPage);
}); }
else
TotalPages = 1;
} }
private EntryFilterInput CreateFilter() private EntryFilterInput GetFilter()
{ {
EntryFilterInput filter = new() {EntryType = new EntryTypeOperationFilterInput {Eq = WebClient.Workshop.EntryType.Profile}}; EntryFilterInput filter = new()
{
List<int?> categories = CategoriesViewModel.Categories.Where(c => c.IsSelected).Select(c => (int?) c.Id).ToList(); EntryType = new EntryTypeOperationFilterInput {Eq = WebClient.Workshop.EntryType.Profile},
if (categories.Any()) And = CategoriesViewModel.CategoryFilters
filter.Categories = new ListFilterInputTypeOfCategoryFilterInput {All = new CategoryFilterInput {Id = new IntOperationFilterInput {In = categories}}}; };
return filter; return filter;
} }

View File

@ -13,8 +13,10 @@
<PackageReference Include="IdentityModel" Version="6.1.0" /> <PackageReference Include="IdentityModel" Version="6.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="ReactiveUI" Version="18.4.26" />
<PackageReference Include="StrawberryShake.Server" Version="13.0.5" /> <PackageReference Include="StrawberryShake.Server" Version="13.0.5" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.31.0" /> <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.31.0" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,30 @@
using System.Reactive.Linq;
using ReactiveUI;
using StrawberryShake;
namespace Artemis.WebClient.Workshop.Extensions;
public static class ReactiveExtensions
{
/// <summary>
/// Projects the data of the provided operation result into a new observable sequence if the result is successfull and
/// contains data.
/// </summary>
/// <param name="source">A sequence of operation results to invoke a transform function on.</param>
/// <param name="selector">A transform function to apply to the data of each source element.</param>
/// <typeparam name="TSource">The type of data contained in the operation result.</typeparam>
/// <typeparam name="TResult">The type of data to project from the result.</typeparam>
/// <returns>
/// An observable sequence whose elements are the result of invoking the transform function on each element of
/// source.
/// </returns>
public static IObservable<TResult> SelectOperationResult<TSource, TResult>(this IObservable<IOperationResult<TSource>> source, Func<TSource, TResult?> selector) where TSource : class
{
return source
.Where(s => !s.Errors.Any())
.Select(s => s.Data)
.WhereNotNull()
.Select(selector)
.WhereNotNull();
}
}

View File

@ -1,6 +1,7 @@
query GetEntries($filter: EntryFilterInput) { query GetEntries($filter: EntryFilterInput $skip: Int $take: Int) {
entries(where: $filter) { entries(where: $filter skip: $skip take: $take) {
nodes { totalCount
items {
id id
author author
name name

View File

@ -11,24 +11,21 @@ type Category {
name: String! name: String!
} }
"A connection to a list of items." "Information about the offset pagination."
type EntriesConnection { type CollectionSegmentInfo {
"A list of edges." "Indicates whether more items exist following the set defined by the clients arguments."
edges: [EntriesEdge!] hasNextPage: Boolean!
"A flattened list of the nodes." "Indicates whether more items exist prior the set defined by the clients arguments."
nodes: [Entry!] hasPreviousPage: Boolean!
"Information to aid in pagination."
pageInfo: PageInfo!
"Identifies the total count of items in the connection."
totalCount: Int!
} }
"An edge in a connection." "A segment of a collection."
type EntriesEdge { type EntriesCollectionSegment {
"A cursor for use in pagination." "A flattened list of the items."
cursor: String! items: [Entry!]
"The item at the end of the edge." "Information to aid in pagination."
node: Entry! pageInfo: CollectionSegmentInfo!
totalCount: Int!
} }
type Entry { type Entry {
@ -54,35 +51,13 @@ type Image {
} }
type Mutation { type Mutation {
addEntry(input: EntryInput!): Entry addEntry(input: CreateEntryInput!): Entry
} updateEntry(input: UpdateEntryInput!): Entry
"Information about pagination in a connection."
type PageInfo {
"When paginating forwards, the cursor to continue."
endCursor: String
"Indicates whether more edges exist following the set defined by the clients arguments."
hasNextPage: Boolean!
"Indicates whether more edges exist prior the set defined by the clients arguments."
hasPreviousPage: Boolean!
"When paginating backwards, the cursor to continue."
startCursor: String
} }
type Query { type Query {
categories(order: [CategorySortInput!], where: CategoryFilterInput): [Category!]! categories(order: [CategorySortInput!], where: CategoryFilterInput): [Category!]!
entries( entries(order: [EntrySortInput!], skip: Int, take: Int, where: EntryFilterInput): EntriesCollectionSegment
"Returns the elements in the list that come after the specified cursor."
after: String,
"Returns the elements in the list that come before the specified cursor."
before: String,
"Returns the first _n_ elements from the list."
first: Int,
"Returns the last _n_ elements from the list."
last: Int,
order: [EntrySortInput!],
where: EntryFilterInput
): EntriesConnection
entry(id: UUID!): Entry entry(id: UUID!): Entry
searchEntries(input: String!, order: [EntrySortInput!], type: EntryType, where: EntryFilterInput): [Entry!]! searchEntries(input: String!, order: [EntrySortInput!], type: EntryType, where: EntryFilterInput): [Entry!]!
} }
@ -142,6 +117,15 @@ input CategorySortInput {
name: SortEnumType name: SortEnumType
} }
input CreateEntryInput {
categories: [Int!]!
description: String!
entryType: EntryType!
name: String!
summary: String!
tags: [String!]!
}
input DateTimeOperationFilterInput { input DateTimeOperationFilterInput {
eq: DateTime eq: DateTime
gt: DateTime gt: DateTime
@ -176,13 +160,6 @@ input EntryFilterInput {
tags: ListFilterInputTypeOfTagFilterInput tags: ListFilterInputTypeOfTagFilterInput
} }
input EntryInput {
description: String!
entryType: EntryType!
name: String!
tags: [String!]!
}
input EntrySortInput { input EntrySortInput {
author: SortEnumType author: SortEnumType
authorId: SortEnumType authorId: SortEnumType
@ -308,6 +285,15 @@ input TagFilterInput {
or: [TagFilterInput!] or: [TagFilterInput!]
} }
input UpdateEntryInput {
categories: [Int!]!
description: String!
id: UUID!
name: String!
summary: String!
tags: [String!]!
}
input UuidOperationFilterInput { input UuidOperationFilterInput {
eq: UUID eq: UUID
gt: UUID gt: UUID