UserPageViewModel.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
  23. public User Current_user { get => current_user; set => current_user = value; }
  24. public List<Gender> GendersList { get => gendersList; set => gendersList = value; }
  25. public Bitmap ImageUser { get => imageUser; set => this.RaiseAndSetIfChanged(ref imageUser, value); }
  26. public UserPageViewModel(int id)
  27. {
  28. Current_user = myContext.Users.Include(x => x.IdGenderNavigation).FirstOrDefault(x => x.IdUser == id);
  29. GendersList = myContext.Genders.ToList();
  30. imageUser = Current_user.Image != null ? new Bitmap(new MemoryStream(Current_user.Image)) : new Bitmap("Assets\\ava.png");
  31. }
  32. public void SaveAndChange()
  33. {
  34. myContext.SaveChanges();
  35. Message = "Данные сохранены";
  36. }
  37. public void Back()
  38. {
  39. MainWindowViewModel.Self.AuthorizationVM = new AuthorizationViewModel();
  40. MainWindowViewModel.Self.Page = new Authorization();
  41. }
  42. public async Task Image()
  43. {
  44. if(Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
  45. desktop.MainWindow?.StorageProvider is not { } provider)
  46. throw new NullReferenceException("Missing StorageProvider instance.");
  47. var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
  48. {
  49. Title = "Выберите фото для аватарки",
  50. AllowMultiple = false
  51. });
  52. await using var readStream = await files[0].OpenReadAsync();
  53. byte[] buffer = new byte[1000000];
  54. var bytes = readStream.ReadAtLeast(buffer, 1);
  55. Array.Resize(ref buffer, bytes);
  56. Current_user.Image = buffer;
  57. ImageUser = new Bitmap(new MemoryStream(current_user.Image));
  58. myContext.SaveChanges();
  59. MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser);
  60. MainWindowViewModel.Self.Page = new UserPage();
  61. }
  62. public void DeleteImage()
  63. {
  64. MemoryStream stream = new MemoryStream();
  65. byte[] byteArray;
  66. ImageUser = new Bitmap("Assets\\ava.png");
  67. ImageUser.Save(stream);
  68. Current_user.Image = stream.ToArray();
  69. myContext.SaveChanges();
  70. MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser);
  71. MainWindowViewModel.Self.Page = new UserPage();
  72. }
  73. }
  74. }