MainPageViewModel.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Avalonia.Controls;
  2. using Avalonia.Media.Imaging;
  3. using AvaloniaApplication1.Models;
  4. using AvaloniaApplication1.Views;
  5. using Microsoft.EntityFrameworkCore;
  6. using ReactiveUI;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reactive;
  11. using System.Reactive.Linq;
  12. namespace AvaloniaApplication1.ViewModels
  13. {
  14. partial class MainPageViewModel : ViewModelBase
  15. {
  16. List<Smeshariki> smesharikiMain;
  17. List<Smeshariki> smeshariki;
  18. string search;
  19. List<Gender> genders;
  20. Gender selectedGender;
  21. int id;
  22. List<Bitmap> images;
  23. public MainPageViewModel()
  24. {
  25. this.smesharikiMain = myConnection.Smesharikis.Include(x => x.GenderNavigation).Include(x=>x.IdProperties).ToList();
  26. this.smeshariki = this.smesharikiMain;
  27. genders =
  28. [
  29. new Gender() { Title = "Выберете пол", Id = 0 },
  30. .. smeshariki.Select(x => x.GenderNavigation).Distinct().OrderBy(x=>x.Id).ToList()
  31. ];
  32. selectedGender = genders.Where(x => x.Id == 0).First();
  33. }
  34. public string Search { get => search; set { search = value; AllFilters(); } }
  35. public List<Gender> Genders { get => genders; set => this.RaiseAndSetIfChanged(ref genders,value); }
  36. public Gender SelectedGender { get => selectedGender; set { selectedGender = value; AllFilters(); } }
  37. public List<Smeshariki> Smeshariki { get => smeshariki; set => this.RaiseAndSetIfChanged(ref smeshariki, value); }
  38. public List<Bitmap> Images { get => images; set => this.RaiseAndSetIfChanged(ref images, value); }
  39. public List<Smeshariki> SmesharikiMain { get => smesharikiMain; set => this.RaiseAndSetIfChanged(ref smesharikiMain, value); }
  40. public void ProfileUser()
  41. {
  42. MainWindowViewModel.Self.Uc = new ProfileUser(MainWindowViewModel.Self.Id);
  43. }
  44. public void OrderByAge(int id)
  45. {
  46. switch (id)
  47. {
  48. case 1:
  49. Smeshariki = Smeshariki.OrderBy(x => x.Age).ToList();
  50. break;
  51. case 2:
  52. Smeshariki = Smeshariki.OrderByDescending(x => x.Age).ToList();
  53. break;
  54. }
  55. }
  56. public void Drop(int id)
  57. {
  58. Smeshariki smesh = myConnection.Smesharikis.FirstOrDefault(x => x.Id == id);
  59. smesh.IdProperties = null;
  60. myConnection.Smesharikis.Remove(smesh);
  61. myConnection.SaveChanges();
  62. SmesharikiMain = myConnection.Smesharikis.ToList();
  63. Smeshariki = SmesharikiMain;
  64. }
  65. public void Update(int id)
  66. {
  67. MainWindowViewModel.Self.Uc = new UpdateSmesharik(id);
  68. }
  69. private void AllFilters()
  70. {
  71. Smeshariki = SmesharikiMain;
  72. if(!string.IsNullOrEmpty(Search)) Smeshariki = Smeshariki.Where(x => x.Name.ToLower().Contains(Search.ToLower())).ToList();
  73. if (selectedGender != null && selectedGender.Id != 0) { Smeshariki = Smeshariki.Where(x => x.GenderNavigation == selectedGender).ToList(); }
  74. }
  75. }
  76. }