UserPageViewModel.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Avalonia.Controls.ApplicationLifetimes;
  6. using System.Threading.Tasks;
  7. using Avalonia.Media.Imaging;
  8. using Avalonia.Platform.Storage;
  9. using AvaloniaHomeTry.Models;
  10. using Microsoft.EntityFrameworkCore;
  11. using ReactiveUI;
  12. using Avalonia;
  13. namespace AvaloniaHomeTry.ViewModels
  14. {
  15. public class UserPageViewModel : ViewModelBase
  16. {
  17. UsersTable? currentUser;
  18. Bitmap? userImage;
  19. List<UsersTable> mainUsers;
  20. List<UsersTable> secondUsers;
  21. public UsersTable? CurrentUser { get => currentUser; set => this.RaiseAndSetIfChanged(ref currentUser, value); }
  22. public Bitmap? UserImage { get => userImage; set => this.RaiseAndSetIfChanged(ref userImage, value); }
  23. public UserPageViewModel(int idUser)
  24. {
  25. currentUser = db.UsersTable.Include(x => x.GenderNavigation).Include(x => x.RoleNavigation).FirstOrDefault(x => x.Id == idUser);
  26. UserImage = CurrentUser.Image != null ? new Bitmap(new MemoryStream(CurrentUser.Image)) : new Bitmap("D:\\avaloniax\\AvaloniaHomeTry\\Assets\\background.jpg");
  27. }
  28. public List<string> Genders => db.Genders.Select(x => x.GenderName).ToList();
  29. public string GenderIndex { get => currentUser.GenderNavigation.GenderName; set => currentUser.IdGender = db.Genders.FirstOrDefault(x => x.GenderName == value).Id; }
  30. public string DR { get => currentUser.Dataofbirth.ToString(); set => currentUser.Dataofbirth = DateTime.Parse(value); }
  31. public List<UsersTable> MainUsers { get => mainUsers; set => this.RaiseAndSetIfChanged(ref mainUsers,value); }
  32. public List<UsersTable> SecondUsers { get => secondUsers; set => this.RaiseAndSetIfChanged(ref secondUsers, value); }
  33. public void SaveData() => db.SaveChanges();
  34. public void ExitProfile()
  35. {
  36. if (currentUser.IdRole == 1)
  37. {
  38. MainWindowViewModel.Self.UC = new AdminPage();
  39. }
  40. else
  41. {
  42. MainWindowViewModel.Self.UC = new AuthPage();
  43. }
  44. }
  45. public async Task Image(int id)
  46. {
  47. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
  48. desktop.MainWindow?.StorageProvider is not { } provider)
  49. throw new NullReferenceException("Missing StorageProvider instance.");
  50. {
  51. var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
  52. {
  53. Title = "Âûáèðèòå ôàéë ñ èçîáðàæåíèåì: ",
  54. AllowMultiple = false
  55. });
  56. await using var readStream = await files[0].OpenReadAsync();
  57. byte[] buffer = new byte[10000000];
  58. var bytes = readStream.ReadAtLeast(buffer, 1);
  59. Array.Resize(ref buffer, bytes);
  60. currentUser = db.UsersTable.FirstOrDefault(x => x.Id == id);
  61. currentUser.Image = buffer;
  62. UserImage = new Bitmap(new MemoryStream(currentUser.Image));
  63. db.SaveChanges();
  64. SecondUsers = db.UsersTable.ToList();
  65. MainUsers = SecondUsers;
  66. }
  67. }
  68. }
  69. }