UserPageViewModel.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Avalonia;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Media.Imaging;
  4. using Avalonia.Platform.Storage;
  5. using AvaloniaApplicationTestNew.Models;
  6. using AvaloniaApplicationTestNew.Views;
  7. using Microsoft.EntityFrameworkCore;
  8. using ReactiveUI;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. namespace AvaloniaApplicationTestNew.ViewModels
  15. {
  16. public class UserPageViewModel : ViewModelBase
  17. {
  18. string message;
  19. User current_user;
  20. List<Gender> gendersList;
  21. Bitmap imageUser;
  22. bool isReadyToDelete = false;
  23. Trait selectTrait;
  24. Trait selectItem;
  25. List<Trait> withoutTraits;
  26. List<Trait> withTraits;
  27. public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
  28. public User Current_user { get => current_user; set => current_user = value; }
  29. public List<Gender> GendersList { get => gendersList; set => gendersList = value; }
  30. public Bitmap ImageUser { get => imageUser; set => this.RaiseAndSetIfChanged(ref imageUser, value); }
  31. public Trait SelectTrait { get => selectTrait; set => this.RaiseAndSetIfChanged(ref selectTrait, value); }
  32. public Trait SelectItem { get => selectItem; set => this.RaiseAndSetIfChanged(ref selectItem, value); }
  33. public List<Trait> WithoutTraits { get => withoutTraits; set => this.RaiseAndSetIfChanged(ref withoutTraits, value); }
  34. public List<Trait> WithTraits { get => withTraits; set => this.RaiseAndSetIfChanged(ref withTraits, value); }
  35. public bool IsReadyToDelete { get => isReadyToDelete; set => this.RaiseAndSetIfChanged(ref isReadyToDelete, value); }
  36. public UserPageViewModel(int id)
  37. {
  38. Current_user = myContext.Users.Include(x => x.IdTraits).Include(x => x.IdGenderNavigation).FirstOrDefault(x => x.IdUser == id);
  39. GendersList = myContext.Genders.ToList();
  40. imageUser = Current_user.Image != null ? new Bitmap(new MemoryStream(Current_user.Image)) : new Bitmap("Assets\\ava.png");
  41. WithoutTraits = myContext.Traits.ToList().Except(Current_user.IdTraits).ToList();
  42. WithTraits = Current_user.IdTraits.ToList();
  43. }
  44. public void SaveAndChange()
  45. {
  46. if (string.IsNullOrWhiteSpace(Current_user.Name) || Current_user.IdGenderNavigation == null || Current_user.BirthDate == null)
  47. {
  48. Message = "Заполните все поля";
  49. }
  50. else if (DateTime.Today <= Current_user.BirthDate)
  51. {
  52. Message = "Пользователь родился в будущем? Исправьте дату";
  53. Current_user.BirthDate = null;
  54. }
  55. else
  56. {
  57. myContext.SaveChanges();
  58. Message = "Данные сохранены";
  59. }
  60. }
  61. public void Back()
  62. {
  63. MainWindowViewModel.Self.AuthorizationVM = new AuthorizationViewModel();
  64. MainWindowViewModel.Self.Page = new Authorization();
  65. }
  66. public async Task Image()
  67. {
  68. if(Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
  69. desktop.MainWindow?.StorageProvider is not { } provider)
  70. throw new NullReferenceException("Missing StorageProvider instance.");
  71. var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
  72. {
  73. Title = "Выберите фото для аватарки",
  74. AllowMultiple = false
  75. });
  76. await using var readStream = await files[0].OpenReadAsync();
  77. byte[] buffer = new byte[1000000];
  78. var bytes = readStream.ReadAtLeast(buffer, 1);
  79. Array.Resize(ref buffer, bytes);
  80. Current_user.Image = buffer;
  81. ImageUser = new Bitmap(new MemoryStream(current_user.Image));
  82. myContext.SaveChanges();
  83. MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser);
  84. MainWindowViewModel.Self.Page = new UserPage();
  85. }
  86. public void DeleteImage()
  87. {
  88. MemoryStream stream = new MemoryStream();
  89. byte[] byteArray;
  90. ImageUser = new Bitmap("Assets\\ava.png");
  91. ImageUser.Save(stream);
  92. Current_user.Image = stream.ToArray();
  93. myContext.SaveChanges();
  94. MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser);
  95. MainWindowViewModel.Self.Page = new UserPage();
  96. }
  97. public void AddTrait()
  98. {
  99. if(SelectTrait != null)
  100. {
  101. Current_user.IdTraits.Add(SelectTrait);
  102. SaveAndChange();
  103. MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser);
  104. MainWindowViewModel.Self.Page = new UserPage();
  105. }
  106. }
  107. public void DeleteTrait()
  108. {
  109. if(SelectItem != null)
  110. {
  111. Current_user.IdTraits.Remove(SelectItem);
  112. SaveAndChange();
  113. MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser);
  114. MainWindowViewModel.Self.Page = new UserPage();
  115. }
  116. else
  117. {
  118. IsReadyToDelete = true;
  119. }
  120. }
  121. }
  122. }