mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 03:48:56 +00:00
Implemented pagination in profile list
This commit is contained in:
1 parent
cfb39b986d
commit
7c19937bce
11 files changed
+210
-134
No files matched your search
@@ -1,56 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Reactive.Disposables;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Reactive.Linq;
|
||||
using Artemis.UI.Shared;
|
||||
using Artemis.WebClient.Workshop;
|
||||
using Artemis.WebClient.Workshop.Extensions;
|
||||
using DynamicData;
|
||||
using DynamicData.Binding;
|
||||
using ReactiveUI;
|
||||
using Serilog;
|
||||
using StrawberryShake;
|
||||
|
||||
namespace Artemis.UI.Screens.Workshop.Categories;
|
||||
|
||||
public class CategoriesViewModel : ActivatableViewModelBase
|
||||
{
|
||||
private readonly IWorkshopClient _client;
|
||||
private readonly ILogger _logger;
|
||||
public readonly SourceList<CategoryViewModel> _categories;
|
||||
private ObservableAsPropertyHelper<IReadOnlyList<EntryFilterInput>?>? _categoryFilters;
|
||||
|
||||
public CategoriesViewModel(ILogger logger, IWorkshopClient client)
|
||||
public CategoriesViewModel(IWorkshopClient client)
|
||||
{
|
||||
_logger = logger;
|
||||
_client = client;
|
||||
_categories = new SourceList<CategoryViewModel>();
|
||||
_categories.Connect().Bind(out ReadOnlyObservableCollection<CategoryViewModel> categoryViewModels).Subscribe();
|
||||
|
||||
client.GetCategories
|
||||
.Watch(ExecutionStrategy.CacheFirst)
|
||||
.SelectOperationResult(c => c.Categories)
|
||||
.ToObservableChangeSet(c => c.Id)
|
||||
.Transform(c => new CategoryViewModel(c))
|
||||
.Bind(out ReadOnlyObservableCollection<CategoryViewModel> categoryViewModels)
|
||||
.Subscribe();
|
||||
|
||||
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 IReadOnlyList<EntryFilterInput>? CategoryFilters => _categoryFilters?.Value;
|
||||
|
||||
|
||||
private async Task GetCategories(CancellationToken cancellationToken)
|
||||
private IReadOnlyList<EntryFilterInput>? CreateFilter()
|
||||
{
|
||||
try
|
||||
{
|
||||
IOperationResult<IGetCategoriesResult> result = await _client.GetCategories.ExecuteAsync(cancellationToken);
|
||||
if (result.IsErrorResult())
|
||||
_logger.Warning("Failed to retrieve categories {Error}", result.Errors);
|
||||
List<int?> categories = Categories.Where(c => c.IsSelected).Select(c => (int?) c.Id).ToList();
|
||||
if (!categories.Any())
|
||||
return null;
|
||||
|
||||
_categories.Edit(l =>
|
||||
List<EntryFilterInput> categoryFilters = new();
|
||||
foreach (int? category in categories)
|
||||
{
|
||||
categoryFilters.Add(new EntryFilterInput
|
||||
{
|
||||
l.Clear();
|
||||
if (result.Data?.Categories != null)
|
||||
l.AddRange(result.Data.Categories.Select(c => new CategoryViewModel(c)));
|
||||
Categories = new ListFilterInputTypeOfCategoryFilterInput {Some = new CategoryFilterInput {Id = new IntOperationFilterInput {Eq = category}}}
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Warning(e, "Failed to retrieve categories");
|
||||
}
|
||||
|
||||
return categoryFilters;
|
||||
}
|
||||
}
|
||||
@@ -10,13 +10,13 @@ public class EntryListViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IRouter _router;
|
||||
|
||||
public EntryListViewModel(IGetEntries_Entries_Nodes entry, IRouter router)
|
||||
public EntryListViewModel(IGetEntries_Entries_Items entry, IRouter router)
|
||||
{
|
||||
_router = router;
|
||||
Entry = entry;
|
||||
}
|
||||
|
||||
public IGetEntries_Entries_Nodes Entry { get; }
|
||||
public IGetEntries_Entries_Items Entry { get; }
|
||||
|
||||
public async Task NavigateToEntry()
|
||||
{
|
||||
|
||||
@@ -3,28 +3,48 @@
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
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"
|
||||
x:Class="Artemis.UI.Screens.Workshop.Profile.ProfileListView"
|
||||
x:DataType="profile:ProfileListViewModel">
|
||||
<Border Classes="router-container">
|
||||
<Grid ColumnDefinitions="300,*" Margin="10">
|
||||
<Border Classes="card-condensed" Grid.Column="0" Margin="0 0 10 0" VerticalAlignment="Top">
|
||||
<StackPanel>
|
||||
<TextBlock Classes="h3">Categories</TextBlock>
|
||||
<ContentControl Content="{CompiledBinding CategoriesViewModel}"></ContentControl>
|
||||
</StackPanel>
|
||||
|
||||
</Border>
|
||||
<Grid ColumnDefinitions="300,*" Margin="10" RowDefinitions="*,Auto">
|
||||
<StackPanel Grid.Column="0" Grid.RowSpan="2" Margin="0 0 10 0" VerticalAlignment="Top">
|
||||
<TextBlock Classes="card-title" Margin="0 0 0 5">
|
||||
Categories
|
||||
</TextBlock>
|
||||
<Border Classes="card" VerticalAlignment="Stretch" Margin="0,0,5,0">
|
||||
<ContentControl Content="{CompiledBinding CategoriesViewModel}"></ContentControl>
|
||||
</Border>
|
||||
|
||||
<Border Classes="card-condensed" Grid.Column="1">
|
||||
<ItemsRepeater ItemsSource="{CompiledBinding Entries}">
|
||||
<TextBlock Classes="card-title">
|
||||
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>
|
||||
<DataTemplate>
|
||||
<ContentControl Content="{CompiledBinding}" Margin="0 0 0 5"></ContentControl>
|
||||
</DataTemplate>
|
||||
</ItemsRepeater.ItemTemplate>
|
||||
</ItemsRepeater>
|
||||
</Border>
|
||||
</ScrollViewer>
|
||||
|
||||
<pagination:Pagination Grid.Column="1"
|
||||
Grid.Row="1"
|
||||
IsVisible="{CompiledBinding ShowPagination}"
|
||||
Value="{CompiledBinding Page}"
|
||||
Maximum="{CompiledBinding TotalPages}"
|
||||
HorizontalAlignment="Center" />
|
||||
</Grid>
|
||||
</Border>
|
||||
</UserControl>
|
||||
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Reactive.Disposables;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.UI.Screens.Workshop.Categories;
|
||||
@@ -11,8 +11,6 @@ using Artemis.UI.Screens.Workshop.Parameters;
|
||||
using Artemis.UI.Shared;
|
||||
using Artemis.UI.Shared.Routing;
|
||||
using Artemis.WebClient.Workshop;
|
||||
using DynamicData;
|
||||
using DynamicData.Alias;
|
||||
using ReactiveUI;
|
||||
using StrawberryShake;
|
||||
|
||||
@@ -20,35 +18,44 @@ namespace Artemis.UI.Screens.Workshop.Profile;
|
||||
|
||||
public class ProfileListViewModel : RoutableScreen<ActivatableViewModelBase, WorkshopListParameters>, IWorkshopViewModel
|
||||
{
|
||||
private readonly SourceList<IGetEntries_Entries_Nodes> _entries;
|
||||
private readonly IRouter _router;
|
||||
private readonly IWorkshopClient _workshopClient;
|
||||
private readonly ObservableAsPropertyHelper<bool> _showPagination;
|
||||
private List<EntryListViewModel>? _entries;
|
||||
private int _page;
|
||||
private int _totalPages = 1;
|
||||
private int _entriesPerPage = 5;
|
||||
|
||||
public ProfileListViewModel(IWorkshopClient workshopClient, IRouter router, CategoriesViewModel categoriesViewModel)
|
||||
{
|
||||
_workshopClient = workshopClient;
|
||||
_router = router;
|
||||
_showPagination = this.WhenAnyValue(vm => vm.TotalPages).Select(t => t > 1).ToProperty(this, vm => vm.ShowPagination);
|
||||
|
||||
CategoriesViewModel = categoriesViewModel;
|
||||
|
||||
_entries = new SourceList<IGetEntries_Entries_Nodes>();
|
||||
_entries.Connect()
|
||||
.Transform(e => new EntryListViewModel(e, router))
|
||||
.Bind(out ReadOnlyObservableCollection<EntryListViewModel> observableEntries)
|
||||
.Subscribe();
|
||||
|
||||
this.WhenActivated(d =>
|
||||
// Respond to page changes
|
||||
this.WhenAnyValue(vm => vm.Page).Skip(1).Subscribe(p => Task.Run(() => _router.Navigate($"workshop/profiles/{p}")));
|
||||
// Respond to filter changes
|
||||
this.WhenActivated(d => CategoriesViewModel.WhenAnyValue(vm => vm.CategoryFilters).Skip(1).Subscribe(_ =>
|
||||
{
|
||||
CategoriesViewModel._categories.Connect()
|
||||
.AutoRefresh(c => c.IsSelected)
|
||||
.Filter(e => e.IsSelected)
|
||||
.Select(e => e.Id)
|
||||
.Subscribe(_ => ReactiveCommand.CreateFromTask(GetEntries).Execute().Subscribe())
|
||||
.DisposeWith(d);
|
||||
});
|
||||
Entries = observableEntries;
|
||||
// Reset to page one, will trigger a query
|
||||
if (Page != 1)
|
||||
Page = 1;
|
||||
// If already at page one, force a query
|
||||
else
|
||||
Task.Run(() => Query(CancellationToken.None));
|
||||
}).DisposeWith(d));
|
||||
}
|
||||
|
||||
public bool ShowPagination => _showPagination.Value;
|
||||
public CategoriesViewModel CategoriesViewModel { get; }
|
||||
public ReadOnlyObservableCollection<EntryListViewModel> Entries { get; set; }
|
||||
|
||||
public List<EntryListViewModel>? Entries
|
||||
{
|
||||
get => _entries;
|
||||
set => RaiseAndSetIfChanged(ref _entries, value);
|
||||
}
|
||||
|
||||
public int Page
|
||||
{
|
||||
@@ -56,32 +63,49 @@ public class ProfileListViewModel : RoutableScreen<ActivatableViewModelBase, Wor
|
||||
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)
|
||||
{
|
||||
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);
|
||||
if (result.IsErrorResult() || result.Data?.Entries?.Nodes == null)
|
||||
return;
|
||||
|
||||
_entries.Edit(e =>
|
||||
EntryFilterInput filter = GetFilter();
|
||||
IOperationResult<IGetEntriesResult> entries = await _workshopClient.GetEntries.ExecuteAsync(filter, EntriesPerPage * (Page - 1), EntriesPerPage, cancellationToken);
|
||||
if (!entries.IsErrorResult() && entries.Data?.Entries?.Items != null)
|
||||
{
|
||||
e.Clear();
|
||||
e.AddRange(result.Data.Entries.Nodes);
|
||||
});
|
||||
Entries = entries.Data.Entries.Items.Select(n => new EntryListViewModel(n, _router)).ToList();
|
||||
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}};
|
||||
|
||||
List<int?> categories = CategoriesViewModel.Categories.Where(c => c.IsSelected).Select(c => (int?) c.Id).ToList();
|
||||
if (categories.Any())
|
||||
filter.Categories = new ListFilterInputTypeOfCategoryFilterInput {All = new CategoryFilterInput {Id = new IntOperationFilterInput {In = categories}}};
|
||||
EntryFilterInput filter = new()
|
||||
{
|
||||
EntryType = new EntryTypeOperationFilterInput {Eq = WebClient.Workshop.EntryType.Profile},
|
||||
And = CategoriesViewModel.CategoryFilters
|
||||
};
|
||||
|
||||
return filter;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user