123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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<Cource> 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<Cource> _courcesAfterFFS = new ObservableCollection<Cource>();
- private List<Cource> _courcesBeforeFFS = new List<Cource>();
- 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<Cource> FilteredCource = new List<Cource>();
- 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<Cource> SearchedCources = FilteredCource;
- if (SearchQuarry is not null && SearchQuarry.Length != 0)
- {
- SearchedCources = new List<Cource>();
- 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<Cource>(SearchedCources.OrderBy((a) => a.Cource1));
- return;
- case 1:
- Cources = new ObservableCollection<Cource>(SearchedCources.OrderBy((a) => a.HoursNumber));
- return;
- }
- return;
- case false:
- switch (SelectedSortIndex)
- {
- case 0:
- Cources = new ObservableCollection<Cource>(SearchedCources.OrderByDescending((a) => a.Cource1));
- return;
- case 1:
- Cources = new ObservableCollection<Cource>(SearchedCources.OrderByDescending((a) => a.HoursNumber));
- return;
- }
- return;
- }
- }
- }
- }
|