Files
Pf2eDataScrapper/Pf2e/WPF/EntityViewer/ViewModel/EntityListViewModel.cs
T

243 lines
6.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Pf2e.Database;
using Pf2e.Database.Model.Base;
using Pf2e.Database.Model.Enum;
using Pf2e.Scrapper.WPF.EntityViewer.Stores;
namespace Pf2e.WPF.EntityViewer.ViewModel;
internal class EntityListViewModel : ViewModelBase
{
public Pf2eDbContext Context { get; }
public SelectedEntityTypeStore SelectedEntityTypeStore { get; }
public SelectedEntityStore SelectedEntityStore { get; }
private EntityType? SelectedEntityType => SelectedEntityTypeStore.SelectedEntityType;
public Entity? SelectedEntity
{
get => SelectedEntityStore.SelectedEntity;
set => SelectedEntityStore.SelectedEntity = value;
}
public int Page
{
get => _page;
set
{
_page = Math.Clamp(value, 1, PageCount);
OnPropertyChanged();
OnPropertyChanged(nameof(HasPrevious));
OnPropertyChanged(nameof(HasNext));
Reload();
}
}
private int _page = 1;
public int EntitiesCount
{
get => _entitiesCount;
set
{
_entitiesCount = value;
OnPropertyChanged();
OnPropertyChanged(nameof(PageCount));
OnPropertyChanged(nameof(HasPrevious));
OnPropertyChanged(nameof(HasNext));
}
}
private int _entitiesCount = 1;
public int PerPage
{
get => _perPage;
set
{
_perPage = value;
OnPropertyChanged();
OnPropertyChanged(nameof(PageCount));
Reload();
}
}
private int _perPage = 30;
public string Search
{
get => _search;
set
{
_search = value;
OnPropertyChanged();
Reload();
}
}
private string _search = "";
public int PageCount => Convert.ToInt32(Math.Ceiling(1.0 * EntitiesCount / PerPage));
public IEnumerable<Entity> Entities
{
get => _entities;
set
{
_entities = [.. value];
SelectedEntity = Entities.FirstOrDefault();
OnPropertyChanged();
}
}
private ObservableCollection<Entity> _entities = [];
public EntityListViewModel(
Pf2eDbContext context,
SelectedEntityTypeStore selectedEntityTypeStore,
SelectedEntityStore selectedEntityStore
)
{
Context = context;
SelectedEntityTypeStore = selectedEntityTypeStore;
SelectedEntityStore = selectedEntityStore;
SelectedEntityTypeStore.SelectedEntityTypeChanged +=
SelectedEntityTypeStore_SelectedEntityTypeChanged;
}
~EntityListViewModel()
{
SelectedEntityTypeStore.SelectedEntityTypeChanged -=
SelectedEntityTypeStore_SelectedEntityTypeChanged;
}
private void SelectedEntityTypeStore_SelectedEntityTypeChanged(EntityType? entityType)
{
if (entityType is null)
{
Page = 1;
EntitiesCount = 1;
Entities = [];
return;
}
Page = 1;
Reload();
}
#region "Методы кнопок"
private PageNavigationCommand? _firstPageCommand;
public PageNavigationCommand FirstPageCommand
{
get
{
return _firstPageCommand ??= new PageNavigationCommand(() =>
{
Page = 1;
});
}
}
private PageNavigationCommand? _previousPageCommand;
public PageNavigationCommand PreviousPageCommand
{
get
{
return _previousPageCommand ??= new PageNavigationCommand(() =>
{
Page--;
});
}
}
private PageNavigationCommand? _nextPageCommand;
public PageNavigationCommand NextPageCommand
{
get
{
return _nextPageCommand ??= new PageNavigationCommand(() =>
{
Page++;
});
}
}
private PageNavigationCommand? _lastPageCommand;
public PageNavigationCommand LastPageCommand
{
get
{
return _lastPageCommand ??= new PageNavigationCommand(() =>
{
Page = PageCount;
});
}
}
public bool HasPrevious => Page != 1;
public bool HasNext => Page != PageCount;
#endregion
#region "Запросы"
/// <summary>
/// Выполняет запрос к базе данных и обрабатывает результаты.
/// </summary>
public void Reload()
{
Pf2eDbContext dbContext = new();
IQueryable<Entity> query =
Search == ""
? dbContext.Entities.Where(e => e.Type == SelectedEntityType)
:
// ReSharper disable once EntityFramework.UnsupportedServerSideFunctionCall
dbContext.Entities.Where(e =>
e.Type == SelectedEntityType && Pf2eDbContext.LocaleSearch(e.Name, Search)
);
EntitiesCount = query.Count();
Entities = [.. query.OrderBy(e => e.Id).Skip((Page - 1) * PerPage).Take(PerPage).ToList()];
}
#endregion
}
/// <summary>
/// Команда для навигации по страницам.
/// </summary>
/// <param name="action">Действие, выполняемое при выполнении команды.</param>
public class PageNavigationCommand(Action action) : System.Windows.Input.ICommand
{
/// <summary>
/// Действие, выполняемое при выполнении команды.
/// </summary>
public Action Action { get; } = action;
/// <summary>
/// Событие, вызываемое при изменении возможности выполнения команды.
/// </summary>
public event EventHandler? CanExecuteChanged;
/// <summary>
/// Проверяет, может ли команда быть выполнена с указанным параметром.
/// </summary>
/// <param name="parameter">Параметр команды.</param>
/// <returns>Флаг, указывающий, может ли команда быть выполнена.</returns>
public bool CanExecute(object? parameter)
{
return true;
}
/// <summary>
/// Выполняет команду с указанным параметром.
/// </summary>
/// <param name="parameter">Параметр команды.</param>
public void Execute(object? parameter)
{
Action.Invoke();
}
}