143 lines
5.4 KiB
C#
143 lines
5.4 KiB
C#
using Avalonia.Markup.Xaml.Templates;
|
|
using demo_trade.Data.Repository;
|
|
using demo_trade.Domain;
|
|
using demo_trade.Models;
|
|
using demo_trade.UI.Presenters;
|
|
using ReactiveUI;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reactive;
|
|
using System.Reactive.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace demo_trade.ViewModels
|
|
{
|
|
public class GuestProductViewModel : ViewModelBase, IRoutableViewModel
|
|
{
|
|
private GuestProductUseCase _guestProductUseCase;
|
|
|
|
private Dictionary<string, (ProductPresenter, int)> _productOrder = new();
|
|
public Dictionary<string, (ProductPresenter, int)> ProductOrder
|
|
{
|
|
get => _productOrder;
|
|
set => this.RaiseAndSetIfChanged(ref _productOrder, value);
|
|
}
|
|
|
|
private bool _thereItemsInOrder = false;
|
|
public bool ThereItemsInOrder {
|
|
get => _thereItemsInOrder;
|
|
set => this.RaiseAndSetIfChanged(ref _thereItemsInOrder, value);
|
|
}
|
|
|
|
public string? UrlPathSegment => Guid.NewGuid().ToString();
|
|
|
|
private Dictionary<int, (int, int?)> _filterValues = new Dictionary<int, (int, int?)>() {
|
|
{0, (0, null)},
|
|
{1, (0, 10)},
|
|
{2, (10, 15)},
|
|
{3, (15, null)}
|
|
};
|
|
public ReactiveCommand<ProductPresenter, Unit> AddToOrderCommand { get; }
|
|
public ReactiveCommand<Unit, Unit> AttachToOrderCommand { get; }
|
|
|
|
|
|
private int _selectedFilterValue = 0;
|
|
private int _selectedSortValue = 0;
|
|
public int SelectedFilterValue {
|
|
get => _selectedFilterValue;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedFilterValue, value);
|
|
}
|
|
public int SelectedSortValue
|
|
{
|
|
get => _selectedSortValue;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedSortValue, value);
|
|
}
|
|
|
|
private string _searchWord = string.Empty;
|
|
public string SearchWord {
|
|
|
|
get => _searchWord;
|
|
set => this.RaiseAndSetIfChanged(ref _searchWord, value);
|
|
}
|
|
|
|
private string _statisticText = string.Empty;
|
|
public string StatisticText
|
|
{
|
|
|
|
get => _statisticText;
|
|
set => this.RaiseAndSetIfChanged(ref _statisticText, value);
|
|
}
|
|
|
|
private List<ProductPresenter> _dataSource = new();
|
|
private List<ProductPresenter> _products = new();
|
|
public List<ProductPresenter> Products {
|
|
get => _products;
|
|
set => this.RaiseAndSetIfChanged(ref _products, value);
|
|
}
|
|
|
|
public IScreen HostScreen { get; }
|
|
|
|
|
|
|
|
public GuestProductViewModel(IScreen home, IProductRepository productRepository) {
|
|
HostScreen = home;
|
|
_guestProductUseCase = new GuestProductUseCase(productRepository);
|
|
_dataSource = _guestProductUseCase.GetProducts();
|
|
this.WhenAnyValue(search => search.SearchWord).Subscribe(_ => { DisplayList(); });
|
|
this.WhenAnyValue(selectedFilter => selectedFilter.SelectedFilterValue).Subscribe(_ => { DisplayList(); });
|
|
this.WhenAnyValue(selectedSort => selectedSort.SelectedSortValue).Subscribe(_ => { DisplayList(); });
|
|
DisplayList();
|
|
AddToOrderCommand = ReactiveCommand.Create<ProductPresenter>(product => {
|
|
if (ProductOrder.ContainsKey(product.ArticleNumber))
|
|
{
|
|
var values = ProductOrder[product.ArticleNumber];
|
|
ProductOrder[product.ArticleNumber] = (product, values.Item2 + 1);
|
|
}
|
|
else {
|
|
ProductOrder.Add(product.ArticleNumber, (product, 1));
|
|
}
|
|
ThereItemsInOrder = ProductOrder.Keys.Count > 0;
|
|
|
|
});
|
|
ShowDialog = new Interaction<OrderShowDialogViewModel, bool>();
|
|
AttachToOrderCommand = ReactiveCommand.CreateFromTask(async() =>
|
|
{
|
|
var userOrderCase = new OrderClientUseCase(new SQLOrderClientRepository(), new SQLPickupRepository());
|
|
var order = new OrderShowDialogViewModel(ProductOrder, userOrderCase);
|
|
var result = await ShowDialog.Handle(order);
|
|
}
|
|
);
|
|
|
|
}
|
|
private bool FilterByDiscount(short? discount) {
|
|
if (discount == null) return false;
|
|
(int leftBound, int? rightBound) = _filterValues[SelectedFilterValue];
|
|
if (rightBound == null) return leftBound <= discount;
|
|
return leftBound <= discount && discount < rightBound;
|
|
}
|
|
private void DisplayList() {
|
|
var temp = _dataSource;
|
|
if (SelectedFilterValue > 0) {
|
|
temp = temp.Where(it => FilterByDiscount(it.DiscountAmount)).ToList();
|
|
}
|
|
if (!String.IsNullOrEmpty(SearchWord)) {
|
|
temp = temp.Where(it => it.Name.Contains(SearchWord, StringComparison.CurrentCultureIgnoreCase)).ToList();
|
|
}
|
|
if (SelectedSortValue == 1)
|
|
{
|
|
temp = temp.OrderByDescending(it => it.Cost).ToList();
|
|
}
|
|
else if (SelectedSortValue == 2) {
|
|
temp = temp.OrderBy(it => it.Cost).ToList();
|
|
}
|
|
this.Products = temp;
|
|
StatisticText = String.Format("{0} из {1}", temp.Count, _dataSource.Count);
|
|
}
|
|
|
|
public Interaction<OrderShowDialogViewModel, bool> ShowDialog { get; }
|
|
|
|
}
|
|
}
|