Workshop Library - Added library pages

UI - Tweaked design to more closely match WinUI 3 gallery examples
This commit is contained in:
Robert committed 2023-09-01 20:33:50 +02:00
1 parent e545d2f3da
commit 9c6d7329a6
25 files changed
+519 -288

No files matched your search

@@ -0,0 +1,148 @@
using System;
using System.Collections.ObjectModel;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Artemis.UI.Screens.Workshop.Categories;
using Artemis.UI.Screens.Workshop.Parameters;
using Artemis.UI.Shared;
using Artemis.UI.Shared.Routing;
using Artemis.UI.Shared.Services;
using Artemis.UI.Shared.Services.Builders;
using Artemis.WebClient.Workshop;
using Avalonia.Threading;
using DynamicData;
using ReactiveUI;
using StrawberryShake;
namespace Artemis.UI.Screens.Workshop.Entries;
public abstract class EntryListBaseViewModel : RoutableScreen<ActivatableViewModelBase, WorkshopListParameters>, IWorkshopViewModel
{
private readonly INotificationService _notificationService;
private readonly IWorkshopClient _workshopClient;
private readonly ObservableAsPropertyHelper<bool> _showPagination;
private readonly ObservableAsPropertyHelper<bool> _isLoading;
private readonly SourceList<IGetEntries_Entries_Items> _entries = new();
private int _page;
private int _loadedPage = -1;
private int _totalPages = 1;
private int _entriesPerPage = 10;
protected EntryListBaseViewModel(IWorkshopClient workshopClient, IRouter router, CategoriesViewModel categoriesViewModel, INotificationService notificationService,
Func<IGetEntries_Entries_Items, EntryListItemViewModel> getEntryListViewModel)
{
_workshopClient = workshopClient;
_notificationService = notificationService;
_showPagination = this.WhenAnyValue(vm => vm.TotalPages).Select(t => t > 1).ToProperty(this, vm => vm.ShowPagination);
_isLoading = this.WhenAnyValue(vm => vm.Page, vm => vm.LoadedPage, (p, c) => p != c).ToProperty(this, vm => vm.IsLoading);
CategoriesViewModel = categoriesViewModel;
_entries.Connect()
.ObserveOn(new AvaloniaSynchronizationContext(DispatcherPriority.SystemIdle))
.Transform(getEntryListViewModel)
.Bind(out ReadOnlyObservableCollection<EntryListItemViewModel> entries)
.Subscribe();
Entries = entries;
// Respond to page changes
this.WhenAnyValue(vm => vm.Page).Skip(1).Subscribe(p => Task.Run(() => router.Navigate(GetPagePath(p))));
// Respond to filter changes
this.WhenActivated(d => CategoriesViewModel.WhenAnyValue(vm => vm.CategoryFilters).Skip(1).Subscribe(_ =>
{
// 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));
}
protected abstract string GetPagePath(int page);
public bool ShowPagination => _showPagination.Value;
public bool IsLoading => _isLoading.Value;
public CategoriesViewModel CategoriesViewModel { get; }
public ReadOnlyObservableCollection<EntryListItemViewModel> Entries { get; }
public int Page
{
get => _page;
set => RaiseAndSetIfChanged(ref _page, value);
}
public int LoadedPage
{
get => _loadedPage;
set => RaiseAndSetIfChanged(ref _loadedPage, 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 Task.Delay(200, cancellationToken);
if (!cancellationToken.IsCancellationRequested)
await Query(cancellationToken);
}
private async Task Query(CancellationToken cancellationToken)
{
try
{
EntryFilterInput filter = GetFilter();
IOperationResult<IGetEntriesResult> entries = await _workshopClient.GetEntries.ExecuteAsync(filter, EntriesPerPage * (Page - 1), EntriesPerPage, cancellationToken);
entries.EnsureNoErrors();
if (entries.Data?.Entries?.Items != null)
{
TotalPages = (int) Math.Ceiling(entries.Data.Entries.TotalCount / (double) EntriesPerPage);
_entries.Edit(e =>
{
e.Clear();
e.AddRange(entries.Data.Entries.Items);
});
}
else
TotalPages = 1;
}
catch (Exception e)
{
_notificationService.CreateNotification()
.WithTitle("Failed to load entries")
.WithMessage(e.Message)
.WithSeverity(NotificationSeverity.Error)
.Show();
}
finally
{
LoadedPage = Page;
}
}
protected virtual EntryFilterInput GetFilter()
{
return new EntryFilterInput {And = CategoriesViewModel.CategoryFilters};
}
public EntryType? EntryType => null;
}
@@ -7,16 +7,15 @@
xmlns:il="clr-namespace:AsyncImageLoader;assembly=AsyncImageLoader.Avalonia"
xmlns:converters="clr-namespace:Artemis.UI.Converters"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="110"
x:Class="Artemis.UI.Screens.Workshop.Entries.EntryListView"
x:DataType="entries1:EntryListViewModel">
x:Class="Artemis.UI.Screens.Workshop.Entries.EntryListItemView"
x:DataType="entries1:EntryListItemViewModel">
<UserControl.Resources>
<converters:EntryIconUriConverter x:Key="EntryIconUriConverter" />
<converters:DateTimeConverter x:Key="DateTimeConverter" />
</UserControl.Resources>
<Button MinHeight="110"
MaxHeight="140"
Padding="16"
CornerRadius="8"
Padding="12"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
Command="{CompiledBinding NavigateToEntry}"
@@ -24,8 +23,7 @@
<Grid ColumnDefinitions="Auto,*,Auto">
<!-- Icon -->
<Border Grid.Column="0"
CornerRadius="12"
Background="{StaticResource ControlStrokeColorOnAccentDefault}"
CornerRadius="6"
VerticalAlignment="Center"
Margin="0 0 10 0"
Width="80"
@@ -2,9 +2,9 @@ using Avalonia.ReactiveUI;
namespace Artemis.UI.Screens.Workshop.Entries;
public partial class EntryListView : ReactiveUserControl<EntryListViewModel>
public partial class EntryListItemView : ReactiveUserControl<EntryListItemViewModel>
{
public EntryListView()
public EntryListItemView()
{
InitializeComponent();
}
@@ -13,11 +13,11 @@ using ReactiveUI;
namespace Artemis.UI.Screens.Workshop.Entries;
public class EntryListViewModel : ActivatableViewModelBase
public class EntryListItemViewModel : ActivatableViewModelBase
{
private readonly IRouter _router;
public EntryListViewModel(IGetEntries_Entries_Items entry, IRouter router)
public EntryListItemViewModel(IGetEntries_Entries_Items entry, IRouter router)
{
_router = router;