using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reactive.Linq; using System.Text; using System.Threading.Tasks; using Avalonia.Media.Imaging; using Avalonia.Media.TextFormatting; using AvaloniaApplicationTestNew.Models; using AvaloniaApplicationTestNew.Views; using Microsoft.EntityFrameworkCore; using ReactiveUI; using Tmds.DBus.Protocol; namespace AvaloniaApplicationTestNew.ViewModels { public class AdminPageViewModel : ViewModelBase { List users; List users0; List localusers; List genders; int idUser; string findText; Gender filtGendr; List imageUser; Dictionary ConvertImage = new Dictionary(); List> listDictionaries = new List>(); List localUser; public List Users { get => users; set => this.RaiseAndSetIfChanged(ref users, value); } public List Users0 { get => users0; set => this.RaiseAndSetIfChanged(ref users0, value); } public List LocalListusers { get => localusers; set => this.RaiseAndSetIfChanged(ref localusers, value); } public List Genders { get => genders; set => this.RaiseAndSetIfChanged(ref genders, value); } public string FindText { get => findText; set => this.RaiseAndSetIfChanged(ref findText, value); } public Gender FiltGendr { get => filtGendr; set => this.RaiseAndSetIfChanged(ref filtGendr, value); } public int IdUser { get => idUser; set => this.RaiseAndSetIfChanged(ref idUser, value); } public List ImageUser { get => imageUser; set => this.RaiseAndSetIfChanged(ref imageUser, value); } public List> ListDictionaries { get => listDictionaries; set => this.RaiseAndSetIfChanged(ref listDictionaries, value); } public AdminPageViewModel() { Users = myContext.Users.Include(x => x.IdUserNavigation.IdRoleNavigation).Include(x => x.IdGenderNavigation).Include(x => x.IdTraits).ToList(); int count = 0; LocalListusers = RemakeList(users); localUser = LocalListusers; genders = new List { new Gender() {Gender1 = "Сбросить фильтр", IdGender = 0} }.Concat(myContext.Genders).ToList(); FiltGendr = genders[0]; users0 = users; this.WhenAnyValue(x => x.FindText, x => x.FiltGendr).Subscribe(_ => AllFilters()); } public List RemakeList(List users) { List LocalListusers = new List(); foreach (var user in users) { Localuser temp = new Localuser { Name = user.Name, IdGender = user.IdGender, IdGenderNavigation = user.IdGenderNavigation, IdUser = user.IdUser, IdUserNavigation = user.IdUserNavigation, BirthDate = user.BirthDate, IdTraits = user.IdTraits, ConvertImage = user.Image != null ? new Bitmap(new MemoryStream(user.Image)) : new Bitmap("Assets\\ava.png") }; LocalListusers.Add(temp); } return LocalListusers; } public void OnTextFindChanged(string text) => AllFilters(); public void SortDate(int id) { switch (id) { case 1: LocalListusers = LocalListusers.OrderBy(x => x.BirthDate).ToList(); break; case 2: LocalListusers = LocalListusers.OrderByDescending(x => x.BirthDate).ToList(); break; } } public void OnFilterGenderChanged(Gender meaning) => AllFilters(); void AllFilters() { LocalListusers = localUser; if (!string.IsNullOrEmpty(findText)) LocalListusers = LocalListusers.Where(x => x.Name.Contains(findText)).ToList(); if (filtGendr != null && filtGendr.IdGender != 0) LocalListusers = LocalListusers.Where(x => x.IdGenderNavigation == filtGendr).ToList(); } public void InfoUsers(int id) { IdUser = id; MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(id); MainWindowViewModel.Self.Page = new UserPage(); } public void AddUser() { MainWindowViewModel.Self.AddUserPageVM = new AddUserPageViewModel(); MainWindowViewModel.Self.Page = new AddUserPage(); } public void Exit() { MainWindowViewModel.Self.AuthorizationVM = new AuthorizationViewModel(); MainWindowViewModel.Self.Page = new Authorization(); } } }