using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; using System.Linq; using ServiceApp.EnityModels; using System.Collections.Generic; using System.Collections.ObjectModel; using SkiaSharp; using Microsoft.EntityFrameworkCore.Storage.Json; using System; using Npgsql.TypeMapping; using Avalonia.Media.Imaging; using Avalonia.Platform; namespace ServiceApp; public partial class ServiceWindow : Window { public List sourceList { get; set; } public ObservableCollection displayList { get; } private string _searchWord = string.Empty; private string[] _sortingSource = new string[3] { "отсут.", "по возраст.", "по убыв." }; private Dictionary _dicountFilter = new Dictionary { {"все", (0, 0) }, { "0-5", (0,5)}, { "15-30", (5,15)}, { "30-70", (30,70)}, { "70-100", (70,100)}, }; public ServiceWindow() { InitializeComponent(); using (var dbContext = new IsajkinContext()) { sourceList = dbContext.Services.Select(service => new ServicePresenter { Title = service.Title, Discount = service.Discount, Durationinminutes = service.Durationinminutes, Id = service.Id, Cost = service.Cost, Mainimagepath = service.Mainimagepath, } ).ToList(); } displayList = new ObservableCollection(sourceList); SortingComboBox.ItemsSource = _sortingSource; ServiceListBox.ItemsSource = displayList; FilteringComboBox.ItemsSource = _dicountFilter.Keys; StatisticTextBlock.Text = String.Format("{0} из {1}", displayList.Count, sourceList.Count); } private void DisplayService() { var displayServiceList = sourceList; if (FilteringComboBox.SelectedIndex > 0) displayServiceList = displayServiceList.Where(service => FilterByDiscount(service, FilteringComboBox.SelectionBoxItem.ToString())).ToList(); if(!String.IsNullOrEmpty(_searchWord)) displayServiceList = displayServiceList.Where(service => SearchByWord(service, _searchWord)).ToList(); if(SortingComboBox.SelectedIndex > 0) displayServiceList = SortingComboBox.SelectedIndex == 1? displayServiceList.OrderBy(service => service.Cost).ToList() : displayServiceList.OrderByDescending(service => service.Cost).ToList(); displayList.Clear(); foreach (var service in displayServiceList) { displayList.Add(service); } StatisticTextBlock.Text = String.Format("{0} из {1}", displayServiceList.Count, sourceList.Count); } private bool SearchByWord(ServicePresenter service, string word) { if (service.Title.Contains(word, StringComparison.CurrentCultureIgnoreCase)) return true; if (String.IsNullOrEmpty(service.Description)) return false; if (service.Description.Contains(word, StringComparison.CurrentCultureIgnoreCase)) return true; return false; } private bool FilterByDiscount(ServicePresenter servicePresenter, string key) { (int left, int right) = _dicountFilter[key]; double discount = servicePresenter.Discount != null? Convert.ToDouble(servicePresenter.Discount) * 100 : 0; return left <= discount && discount < right; } private void SearchTextBox_TextChanged(object? sender, Avalonia.Controls.TextChangedEventArgs e) { _searchWord = SearchTextBox.Text.ToString(); DisplayService(); } private void FilteringComboBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e) { DisplayService(); } private void SortingComboBox_SelectionChanged(object? sender, Avalonia.Controls.SelectionChangedEventArgs e) { DisplayService(); } } public class ServicePresenter() : Service { public int ServiceID { get => this.Id; } public string ServiceName { get => this.Title; } public string SeviceCostPerSeconds { get => (this.Discount != 0 ? String.Format("{0:0.00} рублей за {1} минут", this.Cost - this.Cost * Convert.ToDecimal(this.Discount), this.Durationinminutes) : String.Format("{0:0.00} рублей за {1} минут", this.Cost, this.Durationinminutes) ); } public Bitmap ServiceImage { get => GetBitmap(this.Mainimagepath); } public decimal? OldCost { get => (this.Discount != 0 ? this.Cost : null); } public string? ServiceDiscount { get => (this.Discount != 0 ? String.Format("* скидка {0}%", Discount * 100) : String.Empty ); } private Bitmap GetBitmap(string fileName) { try { return new Bitmap($"Assets\\{fileName}"); } catch (Exception ex) { return new Bitmap("Assets\\download.png"); } } }