DateUserViewModel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Avalonia;
  2. using Avalonia.Collections;
  3. using Avalonia.Controls.ApplicationLifetimes;
  4. using Avalonia.Media.Imaging;
  5. using Avalonia.Platform.Storage;
  6. using CommunityToolkit.Mvvm.ComponentModel;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using TestRepeat.Models;
  12. using TestRepeat.Views;
  13. namespace TestRepeat.ViewModels
  14. {
  15. public partial class DateUserViewModel : ViewModelBase
  16. {
  17. [ObservableProperty] User userForPage;
  18. [ObservableProperty] string dateString;
  19. [ObservableProperty] List<Gender> genders;
  20. [ObservableProperty] bool isAdmin = false;
  21. [ObservableProperty] Bitmap imgUser;
  22. [ObservableProperty] List<Threat> idThreats;
  23. [ObservableProperty] bool isCanDeleteThreat = false;
  24. [ObservableProperty] Threat selectedItem;
  25. [ObservableProperty] List<Threat> idNotThreats;
  26. [ObservableProperty] Threat selectedThreat;
  27. public DateUserViewModel() { }
  28. public DateUserViewModel(User user)
  29. {
  30. idNotThreats = MainWindowViewModel.Db_context.Threats.ToList().Except(user.IdThreats).ToList();
  31. userForPage = user;
  32. genders = MainWindowViewModel.Db_context.Genders.ToList();
  33. dateString = userForPage.BirthDate.ToString();
  34. IdThreats = user.IdThreats.ToList();
  35. imgUser = new Bitmap(new MemoryStream(user.ImgUser));
  36. if (InfoUsersDate.CurrentUser != null)
  37. {
  38. isAdmin = true;
  39. }
  40. }
  41. public async void ChangeImg()
  42. {
  43. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
  44. desktop.MainWindow?.StorageProvider is not { } provider)
  45. throw new NullReferenceException("Missing StorageProvider instance.");
  46. var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
  47. {
  48. Title = "Âûáåðèòå ôàéë ñ èçîáðàæåíèåì ïîëüçîâàòåëÿ",
  49. AllowMultiple = false
  50. });
  51. if (files.Count == 0)
  52. {
  53. return;
  54. }
  55. await using var readStream = await files[0].OpenReadAsync();
  56. byte[] buffer = new byte[10000000];
  57. var bytes = readStream.ReadAtLeast(buffer, 1);
  58. Array.Resize(ref buffer, bytes);
  59. UserForPage.ImgUser = buffer;
  60. ImgUser = new Bitmap(new MemoryStream(buffer));
  61. }
  62. public void SaveChange()
  63. {
  64. MainWindowViewModel.Db_context.SaveChanges();
  65. }
  66. public DateTimeOffset NewDate
  67. {
  68. get => new DateTimeOffset((DateTime)UserForPage.BirthDate, TimeSpan.Zero);
  69. set
  70. {
  71. UserForPage.BirthDate = new DateTime(value.Year, value.Month, value.Day);
  72. DateString = value.ToString();
  73. }
  74. }
  75. public void BackToAuth()
  76. {
  77. InfoUsersDate.CurrentUser = null;
  78. MainWindowViewModel.Instance.Uc = new Authorization();
  79. }
  80. public void BackToListUser()
  81. {
  82. MainWindowViewModel.Instance.Uc = new InfoUsersDate(MainWindowViewModel.Db_context.Users.ToList());
  83. }
  84. public void DeleteThreatUser()
  85. {
  86. if (SelectedItem != null)
  87. {
  88. UserForPage.IdThreats.Remove(SelectedItem);
  89. SaveChange();
  90. MainWindowViewModel.Instance.Uc = new DateUser(UserForPage);
  91. }
  92. else
  93. {
  94. IsCanDeleteThreat = true;
  95. }
  96. }
  97. public void AddThreat()
  98. {
  99. if (SelectedThreat != null)
  100. {
  101. UserForPage.IdThreats.Add(SelectedThreat);
  102. SaveChange();
  103. MainWindowViewModel.Instance.Uc = new DateUser(UserForPage);
  104. }
  105. }
  106. }
  107. }