using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text.RegularExpressions; using Microsoft.EntityFrameworkCore; using ReactiveUI; namespace prakt1314.ViewModels { public class CourcesListViewModel : ReactiveObject { public CourcesListViewModel() { using (AvaloniaPrakt13DbContext _db = new AvaloniaPrakt13DbContext()) { _courcesBeforeFFS = _db.Cources.Include(a => a.TeacherCources).ToList(); } FFS(); } public ObservableCollection Cources { get => _courcesAfterFFS; set => this.RaiseAndSetIfChanged(ref _courcesAfterFFS, value); } public int SelectedFilterIndex { get => _selectedFilterIndex; set { this.RaiseAndSetIfChanged(ref _selectedFilterIndex, value); FFS(); } } public int SelectedSortIndex { get => _selectedSortIndex; set { this.RaiseAndSetIfChanged(ref _selectedSortIndex, value); FFS(); } } public string SearchQuarry { get => _searchQuarry; set { this.RaiseAndSetIfChanged(ref _searchQuarry, value); FFS(); } } public bool IsSearchByName { get => _isSearchByName; set { this.RaiseAndSetIfChanged(ref _isSearchByName, value); FFS(); } } public bool IsSortingABS { get => _isSortingABS; set { this.RaiseAndSetIfChanged(ref _isSortingABS, value); FFS(); } } private ObservableCollection _courcesAfterFFS = new ObservableCollection(); private List _courcesBeforeFFS = new List(); private int _selectedFilterIndex = 0; private int _selectedSortIndex = 0; private string _searchQuarry = ""; private bool _isSearchByName = true; private bool _isSortingABS = true; private void FFS() { Cources.Clear(); List FilteredCource = new List(); switch (SelectedFilterIndex) { case 0: { foreach (Cource c in _courcesBeforeFFS) { FilteredCource.Add(c); } break; } case 1: { foreach (Cource c in _courcesBeforeFFS) { if (c.HoursNumber >= 0 && c.HoursNumber < 30) { FilteredCource.Add(c); } } break; } case 2: { foreach (Cource c in _courcesBeforeFFS) { if (c.HoursNumber >= 30 && c.HoursNumber < 60) { FilteredCource.Add(c); } } break; } case 3: { foreach (Cource c in _courcesBeforeFFS) { if (c.HoursNumber >= 60 && c.HoursNumber < 120) { FilteredCource.Add(c); } } break; } case 4: { foreach (Cource c in _courcesBeforeFFS) { if (c.HoursNumber >= 120) { FilteredCource.Add(c); } } break; } } List SearchedCources = FilteredCource; if (SearchQuarry is not null && SearchQuarry.Length != 0) { SearchedCources = new List(); switch (_isSearchByName) { case true: { foreach (Cource c in FilteredCource) { if (Regex.IsMatch(c.Cource1, SearchQuarry)) SearchedCources.Add(c); } break; } case false: { foreach (Cource c in FilteredCource) { if (Regex.IsMatch(c.HoursNumber.ToString(), SearchQuarry)) SearchedCources.Add(c); } break; } } } else; switch (IsSortingABS) { case true: switch (SelectedSortIndex) { case 0: Cources = new ObservableCollection(SearchedCources.OrderBy((a) => a.Cource1)); return; case 1: Cources = new ObservableCollection(SearchedCources.OrderBy((a) => a.HoursNumber)); return; } return; case false: switch (SelectedSortIndex) { case 0: Cources = new ObservableCollection(SearchedCources.OrderByDescending((a) => a.Cource1)); return; case 1: Cources = new ObservableCollection(SearchedCources.OrderByDescending((a) => a.HoursNumber)); return; } return; } } } }