UpdateSmesharikViewModel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Threading.Tasks;
  9. using System.Windows.Input;
  10. using Avalonia;
  11. using Avalonia.Controls.ApplicationLifetimes;
  12. using Avalonia.Media.Imaging;
  13. using Avalonia.Platform.Storage;
  14. using Avalonia.Threading;
  15. using AvaloniaApplication1.Models;
  16. using Microsoft.EntityFrameworkCore;
  17. using ReactiveUI;
  18. namespace AvaloniaApplication1.ViewModels
  19. {
  20. public class UpdateSmesharikViewModel : ViewModelBase
  21. {
  22. Smeshariki smesh;
  23. List<Gender> genders;
  24. Bitmap imageSmesh;
  25. List<Property> property;
  26. List<Property> property2;
  27. Property selectedProperty;
  28. public ICommand DeleteItemCommand { get; }
  29. public ObservableCollection<Property> Properties { get; }
  30. public Smeshariki Smesh { get => smesh; set => this.RaiseAndSetIfChanged(ref smesh, value); }
  31. public List<Gender> Genders { get => genders; set => this.RaiseAndSetIfChanged(ref genders, value); }
  32. public Gender selectedGender;
  33. public Gender SelectedGender { get => selectedGender; set => this.RaiseAndSetIfChanged(ref selectedGender, value); }
  34. public Bitmap ImageSmesh { get => imageSmesh; set => this.RaiseAndSetIfChanged(ref imageSmesh, value); }
  35. public List<Property> Property { get => property; set => this.RaiseAndSetIfChanged(ref property, value); }
  36. public List<Property> Property2 { get => property2; set => this.RaiseAndSetIfChanged(ref property2, value); }
  37. public Property SelectedProperty { get => selectedProperty; set { selectedProperty = value; AddNewItem(); } }
  38. public UpdateSmesharikViewModel(int id)
  39. {
  40. Genders = myConnection.Genders.ToList();
  41. smesh = myConnection.Smesharikis.Include(x => x.GenderNavigation).Include(x=> x.IdProperties).FirstOrDefault(x=> x.Id ==id);
  42. imageSmesh = Smesh.Image != null ? new Bitmap(new MemoryStream(smesh.Image)) : new Bitmap("çàãëóøêà.png");
  43. Properties = new ObservableCollection<Property>(smesh.IdProperties);
  44. property2 = myConnection.Properties.ToList();
  45. property = property2.Except(Properties).ToList();
  46. }
  47. public void Save()
  48. {
  49. smesh.IdProperties = Properties.ToList();
  50. myConnection.SaveChanges();
  51. MainWindowViewModel.Self.Uc = new AdminPage();
  52. }
  53. public async Task Image()
  54. {
  55. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
  56. desktop.MainWindow?.StorageProvider is not { } provider)
  57. throw new NullReferenceException("Missing StorageProvider instance.");
  58. var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
  59. {
  60. Title = "Ôîòîð ñìåøàðèêà",
  61. AllowMultiple = false
  62. });
  63. await using var readStream = await files[0].OpenReadAsync();
  64. byte[] buffer = new byte[10000000];
  65. var bytes = readStream.ReadAtLeast(buffer, 1);
  66. Array.Resize(ref buffer, bytes);
  67. smesh.Image = buffer;
  68. ImageSmesh = new Bitmap(new MemoryStream(smesh.Image));
  69. myConnection.SaveChanges();
  70. }
  71. public void Back() => MainWindowViewModel.Self.Uc = new AdminPage();
  72. public async void AddNewItem()
  73. {
  74. var newItem = SelectedProperty;
  75. await Dispatcher.UIThread.InvokeAsync(() =>
  76. {
  77. if (newItem != null)
  78. {
  79. Properties.Add(newItem);
  80. Property = Property.Except(Properties).ToList();
  81. }
  82. });
  83. }
  84. public async void DeleteProperty(int id)
  85. {
  86. var newItem = await myConnection.Properties.FirstOrDefaultAsync(x=> x.Id == id);
  87. await Dispatcher.UIThread.InvokeAsync(() =>
  88. {
  89. if (newItem != null)
  90. {
  91. Properties.Remove(newItem);
  92. Property = Property2;
  93. Property = Property.Except(Properties).ToList();
  94. }
  95. });
  96. }
  97. public event PropertyChangedEventHandler PropertyChanged;
  98. protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
  99. {
  100. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  101. }
  102. }
  103. }